Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@import-meta-env/unplugin

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@import-meta-env/unplugin - npm Package Compare versions

Comparing version 0.1.5 to 0.1.6

dist/unplugin/src/loadProd.d.ts

246

dist/index.js

@@ -482,7 +482,5 @@ var __create = Object.create;

var import_picocolors2 = __toESM(require_picocolors());
var import_dotenv2 = require("dotenv");
var import_object_hash = __toESM(require_object_hash());
// package.json
var version = "0.1.5";
var version = "0.1.6";

@@ -581,11 +579,2 @@ // ../shared/constant.ts

// src/vite/withhold-built-in-env.ts
var builtInEnvKeys = ["BASE_URL", "MODE", "DEV", "PROD", "SSR", "LEGACY"];
function withholdViteBuiltInEnv(code) {
builtInEnvKeys.forEach((key) => {
code = code.replace(new RegExp(uniqueVariableName + `.${key}`, "g"), `import.meta.env.${key}`);
});
return code;
}
// src/rollup/merge-manual-chunks.ts

@@ -671,96 +660,127 @@ function mergeManualChunks(originalOutput) {

// src/index.ts
var createPlugin = (0, import_unplugin.createUnplugin)((options, meta) => {
var _a;
const debug = false;
debug && console.debug(options, meta);
const envFilePath2 = (_a = options == null ? void 0 : options.env) != null ? _a : envFilePath;
const envExampleFilePath = options == null ? void 0 : options.example;
if (envExampleFilePath === void 0) {
throw Error(`example option is required. Please specify it in the plugin options.`);
}
let env = {};
let shouldInlineEnv = options == null ? void 0 : options.shouldInlineEnv;
let viteConfig;
function loadProd(id) {
debug && console.debug("loadProd: ", id);
if (id === virtualFile) {
const parsedExample = (() => {
const { parsed, error } = (0, import_dotenv2.config)({ path: envExampleFilePath });
if (error) {
return {};
}
return parsed;
})();
const hashValue = import_object_hash.default.keys(parsedExample);
let envCode;
switch (meta.framework) {
case "vite":
envCode = `const e = Object.assign(${placeholder}, ${JSON.stringify(viteConfig.env)});`;
break;
default:
envCode = `const e = ${placeholder};`;
break;
// src/loadProd.ts
var import_dotenv2 = require("dotenv");
var import_object_hash = __toESM(require_object_hash());
function loadProd({
id,
envExampleFilePath,
meta,
viteConfig
}) {
if (id === virtualFile) {
const parsedExample = (() => {
const { parsed, error } = (0, import_dotenv2.config)({ path: envExampleFilePath });
if (error) {
return {};
}
return [
`console.assert("${hashValue}"); // Invalidate the cache when the ${envExampleFilePath} changes.`,
envCode,
`export default e;`
].join("\n");
return parsed;
})();
const hashValue = import_object_hash.default.keys(parsedExample);
let envCode;
switch (meta.framework) {
case "vite":
envCode = `const e = Object.assign(${placeholder}, ${JSON.stringify(viteConfig.env)});`;
break;
default:
envCode = `const e = ${placeholder};`;
break;
}
return [
`console.assert("${hashValue}"); // Invalidate the cache when the ${envExampleFilePath} changes.`,
envCode,
`export default e;`
].join("\n");
}
function transformDev(code, id) {
debug && console.debug("transformDev: ", id);
if (id !== virtualFile && id.includes("node_modules") === false) {
switch (meta.framework) {
case "vite":
code = code.replace(/import\.meta\.env/g, "(" + JSON.stringify(__spreadValues(__spreadValues({}, env), viteConfig.env)) + ")");
code = withholdViteBuiltInEnv(code);
break;
default:
code = code.replace(/import\.meta\.env/g, "(" + JSON.stringify(env) + ")");
break;
}
}
// src/vite/withhold-built-in-env.ts
var builtInEnvKeys = ["BASE_URL", "MODE", "DEV", "PROD", "SSR", "LEGACY"];
function withholdViteBuiltInEnv(code) {
builtInEnvKeys.forEach((key) => {
code = code.replace(new RegExp(uniqueVariableName + `.${key}`, "g"), `import.meta.env.${key}`);
});
return code;
}
// src/transformDev.ts
function transformDev({
code,
id,
meta,
env,
viteConfig
}) {
if (id !== virtualFile && id.includes("node_modules") === false) {
switch (meta.framework) {
case "vite":
code = code.replace(/import\.meta\.env/g, "(" + JSON.stringify(__spreadValues(__spreadValues({}, env), viteConfig.env)) + ")");
code = withholdViteBuiltInEnv(code);
break;
default:
code = code.replace(/import\.meta\.env/g, "(" + JSON.stringify(env) + ")");
break;
}
return code;
}
function transformProd(code, id) {
debug && console.debug("transformProd: ", id);
debug && console.debug("=== code before ===");
debug && console.debug(code);
debug && console.debug("==================");
if (id !== virtualFile && id.includes("node_modules") === false) {
switch (meta.framework) {
case "webpack":
return code;
}
// src/transformProd.ts
function transformProd({
code,
id,
meta
}) {
if (id !== virtualFile && id.includes("node_modules") === false) {
switch (meta.framework) {
case "webpack":
if (isTransformingTs(code, id)) {
code = code.replace(/import\.meta\.env/g, `((${placeholder}) as any)`);
} else {
code = code.replace(/import\.meta\.env/g, `(${placeholder})`);
break;
default:
if (isTransformingJs(code, id)) {
code = `import ${uniqueVariableName} from '${virtualFile}';
}
break;
default:
if (isTransformingJs(code, id) || isTransformingTs(code, id)) {
code = `import ${uniqueVariableName} from '${virtualFile}';
` + code.replace(`import ${uniqueVariableName} from '${virtualFile}';
`, "");
} else if (isTransformingVue(code, id)) {
} else if (isTransformingVue(code, id)) {
code = code.replace(/(\<script.*?\>)/, `$1
import ${uniqueVariableName} from '${virtualFile}';`);
} else if (isTransformingSvelte(code, id)) {
if (code.includes("<!-- This file is generated by @sveltejs/kit \u2014 do not edit it! -->")) {
} else if (code.includes("<script ")) {
code = code.replace(/(\<script.*?\>)/, `$1
import ${uniqueVariableName} from '${virtualFile}';`);
} else if (isTransformingSvelte(code, id)) {
if (code.includes("<!-- This file is generated by @sveltejs/kit \u2014 do not edit it! -->")) {
} else if (code.includes("<script ")) {
code = code.replace(/(\<script.*?\>)/, `$1
import ${uniqueVariableName} from '${virtualFile}';`);
} else {
code = `import ${uniqueVariableName} from '${virtualFile}';
} else {
code = `import ${uniqueVariableName} from '${virtualFile}';
` + code.replace(`import ${uniqueVariableName} from '${virtualFile}';
`, "");
}
}
code = code.replace(/import\.meta\.env/g, uniqueVariableName);
code = withholdViteBuiltInEnv(code);
break;
}
}
code = code.replace(/import\.meta\.env/g, uniqueVariableName);
code = withholdViteBuiltInEnv(code);
break;
}
debug && console.debug("=== code after ===");
debug && console.debug(code);
debug && console.debug("==================");
return code;
}
return code;
}
var isTransformingJs = (code, id) => [".js", ".jsx"].some((ext) => id.endsWith(ext)) && id.includes("?vue&type=template") === false;
var isTransformingTs = (code, id) => [".ts", ".tsx"].some((ext) => id.endsWith(ext)) && id.includes("?vue&type=template") === false;
var isTransformingVue = (code, id) => id.endsWith(".vue") || id.includes("?vue&type=template") || id.includes("?vue&type=script");
var isTransformingSvelte = (code, id) => id.endsWith(".svelte");
// src/index.ts
var createPlugin = (0, import_unplugin.createUnplugin)((options, meta) => {
var _a;
const debug = false;
debug && console.debug("factory::", options, meta);
const envFilePath2 = (_a = options == null ? void 0 : options.env) != null ? _a : envFilePath;
const envExampleFilePath = options == null ? void 0 : options.example;
if (envExampleFilePath === void 0) {
throw Error(`example option is required. Please specify it in the plugin options.`);
}
let env = {};
let shouldInlineEnv = options == null ? void 0 : options.shouldInlineEnv;
let viteConfig;
return {

@@ -771,3 +791,3 @@ name: "import-meta-env",

apply(_, env2) {
debug && console.debug("apply");
debug && console.debug("apply::");
shouldInlineEnv = shouldInlineEnv != null ? shouldInlineEnv : env2.command === "serve";

@@ -777,3 +797,3 @@ return true;

config(config2) {
debug && console.debug("config:", config2);
debug && console.debug("config::", config2);
if (shouldInlineEnv) {

@@ -785,3 +805,3 @@ } else {

configResolved(_config) {
debug && console.debug("configResolved");
debug && console.debug("configResolved::");
if (_config.isProduction) {

@@ -797,3 +817,3 @@ } else {

transformIndexHtml(html) {
debug && console.debug("transformIndexHtml");
debug && console.debug("transformIndexHtml::");
html = html.replace(new RegExp(uniqueVariableName, "g"), "import.meta.env");

@@ -805,3 +825,3 @@ return html;

outputOptions(options2) {
debug && console.debug("rollup::outputOptions");
debug && console.debug("rollup::outputOptions::");
if (shouldInlineEnv) {

@@ -813,3 +833,3 @@ } else {

buildStart() {
debug && console.debug("rollup::buildStart");
debug && console.debug("rollup::buildStart::");
shouldInlineEnv = shouldInlineEnv != null ? shouldInlineEnv : process.env.ROLLUP_WATCH === "true";

@@ -842,7 +862,7 @@ if (shouldInlineEnv) {

buildStart() {
debug && console.debug("buildStart");
debug && console.debug("env:", env);
debug && console.debug("buildStart::");
debug && console.debug("env::", env);
},
resolveId(id, importer) {
debug && console.debug("resolveId: ", id, importer);
debug && console.debug("resolveId::", id, importer);
if (shouldInlineEnv) {

@@ -856,11 +876,12 @@ } else {

load(id) {
debug && console.debug("load: ", id);
debug && console.debug("load::", id);
if (shouldInlineEnv) {
return null;
} else {
return loadProd(id);
debug && console.debug("loadProd::", id);
return loadProd({ id, envExampleFilePath, meta, viteConfig });
}
},
transformInclude(id) {
debug && console.debug("transformIncludes: ", id);
debug && console.debug("transformIncludes::", id);
const allowExtensions = [

@@ -880,11 +901,19 @@ ".js",

transform(code, id) {
debug && console.debug("transform: ", id);
if (shouldInlineEnv) {
return transformDev(code, id);
debug && console.debug("transformDev::", id);
code = transformDev({ code, id, env, meta, viteConfig });
} else {
return transformProd(code, id);
debug && console.debug("transformProd::", id);
debug && console.debug("=== code before ===");
debug && console.debug(code);
debug && console.debug("==================");
code = transformProd({ code, id, meta });
debug && console.debug("=== code after ===");
debug && console.debug(code);
debug && console.debug("==================");
}
return code;
},
buildEnd() {
debug && console.debug("buildEnd");
debug && console.debug("buildEnd::");
const execCommand = getPackageManagerExecCommand();

@@ -905,5 +934,2 @@ if (shouldInlineEnv) {

var src_default = createPlugin;
var isTransformingJs = (code, id) => [".js", ".ts", ".jsx", ".tsx"].some((ext) => id.endsWith(ext)) && id.includes("?vue&type=template") === false;
var isTransformingVue = (code, id) => id.endsWith(".vue") || id.includes("?vue&type=template") || id.includes("?vue&type=script");
var isTransformingSvelte = (code, id) => id.endsWith(".svelte");
module.exports = createPlugin;

@@ -910,0 +936,0 @@ createPlugin['default'] = createPlugin;

@@ -0,1 +1,2 @@

export declare type Env = Record<string, string>;
export interface PluginOptions {

@@ -2,0 +3,0 @@ /**

{
"name": "@import-meta-env/unplugin",
"version": "0.1.5",
"version": "0.1.6",
"description": "Load environment variables into import.meta.env object",

@@ -5,0 +5,0 @@ "license": "MIT",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc