@import-meta-env/unplugin
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -486,8 +486,7 @@ var __create = Object.create; | ||
// package.json | ||
var version = "0.1.1"; | ||
var version = "0.1.2"; | ||
// ../shared/constant.ts | ||
var virtualFile = "import-meta-env"; | ||
var virtualId = "\0" + virtualFile; | ||
var placeholder = "__import_meta_env_placeholder__"; | ||
var placeholder = "'__import_meta_env_placeholder__'"; | ||
var uniqueVariableName = (() => { | ||
@@ -649,3 +648,3 @@ const uniqueVariableName2 = "import_meta_env_unique_id_"; | ||
// src/webpack/import-meta-env-plugin.ts | ||
// src/webpack/import-meta-plugin.ts | ||
var import_webpack = require("webpack"); | ||
@@ -714,7 +713,7 @@ var ImportMetaPlugin = class { | ||
case "vite": | ||
code = code.replace(/import\.meta\.env/g, JSON.stringify(__spreadValues(__spreadValues({}, env), viteConfig.env))); | ||
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)); | ||
code = code.replace(/import\.meta\.env/g, "(" + JSON.stringify(env) + ")"); | ||
break; | ||
@@ -731,12 +730,19 @@ } | ||
if (id !== virtualFile && id.includes("node_modules") === false) { | ||
if (isTransformingJs(code, id) || isTransformingSvelte(code, id)) { | ||
code = `import ${uniqueVariableName} from '${virtualFile}'; | ||
switch (meta.framework) { | ||
case "webpack": | ||
code = code.replace(/import\.meta\.env/g, `(${placeholder})`); | ||
break; | ||
default: | ||
if (isTransformingJs(code, id) || isTransformingSvelte(code, id)) { | ||
code = `import ${uniqueVariableName} from '${virtualFile}'; | ||
` + code.replace(`import ${uniqueVariableName} from '${virtualFile}'; | ||
`, ""); | ||
} else if (isTransformingVue(code, id)) { | ||
code = code.replace(/(\<script.*?\>)/, `$1 | ||
} else if (isTransformingVue(code, id)) { | ||
code = code.replace(/(\<script.*?\>)/, `$1 | ||
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); | ||
} | ||
@@ -791,3 +797,3 @@ debug && console.debug("=== code after ==="); | ||
debug && console.debug("rollup::buildStart"); | ||
shouldInlineEnv = shouldInlineEnv != null ? shouldInlineEnv : process.env.NODE_ENV !== "production"; | ||
shouldInlineEnv = shouldInlineEnv != null ? shouldInlineEnv : process.env.ROLLUP_WATCH === "true"; | ||
if (shouldInlineEnv) { | ||
@@ -806,4 +812,7 @@ env = resolveEnv({ | ||
} | ||
const mode = (_a2 = compiler.options.mode) != null ? _a2 : "production"; | ||
shouldInlineEnv = shouldInlineEnv != null ? shouldInlineEnv : mode !== "production"; | ||
const developmentModes = [ | ||
"development", | ||
"none" | ||
]; | ||
shouldInlineEnv = (_a2 = shouldInlineEnv != null ? shouldInlineEnv : compiler.options.watch) != null ? _a2 : developmentModes.includes(compiler.options.mode); | ||
if (shouldInlineEnv) { | ||
@@ -878,3 +887,3 @@ env = resolveEnv({ | ||
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"); | ||
var isTransformingVue = (code, id) => id.endsWith(".vue") || id.includes("?vue&type=template") || id.includes("?vue&type=script"); | ||
var isTransformingSvelte = (code, id) => id.endsWith(".svelte"); | ||
@@ -881,0 +890,0 @@ module.exports = createPlugin; |
export declare const virtualFile = "import-meta-env"; | ||
export declare const virtualId: string; | ||
export declare const placeholder = "__import_meta_env_placeholder__"; | ||
export declare const placeholder = "'__import_meta_env_placeholder__'"; | ||
export declare const uniqueVariableName: string; | ||
export declare const envFilePath = ".env"; | ||
export declare const envExampleFilePath = ".env.example"; |
@@ -21,9 +21,12 @@ export interface PluginOptions { | ||
* @default | ||
* `true` if following conditions are met, otherwise `false`: | ||
* | ||
* true, "if running with `vite` or `vite dev`" | ||
* true, "if webpack mode is NOT `production` or `undefined`" | ||
* true, "if `NODE_ENV` is NOT `production`" | ||
* false, "otherwise" | ||
* ```text | ||
* vite: if running with dev server, i.e. `vite`, `vite dev`, or `vite serve` | ||
* webpack: if running with watch mode or `mode` is `development` or `none` | ||
* rollup: if running with watch mode | ||
* others: if `NODE_ENV` is NOT `production` | ||
* ``` | ||
*/ | ||
shouldInlineEnv?: boolean; | ||
} |
{ | ||
"name": "@import-meta-env/unplugin", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Load environment variables into import.meta.env object", | ||
@@ -58,3 +58,3 @@ "license": "MIT", | ||
}, | ||
"readme": "# @import-meta-env/unplugin\n\n[![CI](https://github.com/iendeavor/import-meta-env/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/iendeavor/import-meta-env/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@import-meta-env/unplugin.svg)](https://www.npmjs.com/package/@import-meta-env/unplugin)\n[![PRs Welcome](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n\nThis plugin helps us inject environment variables into the `import.meta.env` object **_after_** building the application instead of statically replacing it during production.\n\n<br>\n\nThis project use [SemVer](https://semver.org/) for versioning. For the versions available, see the tags on this repository.\n\n## Motivation\n\nEnvironment variables should be easy to change between deployments **_without_** rebuilding the application or even changing any code, so we should set environment variables on the system instead of checking them into a repository with `.env` files.\n\nDuring production, this plugin generates chunks with placeholders, which allow us to statically replace environment variables **_after_** building the application (don't worry, we provide an executable for this, you don't need to write them yourself) .\n\n## 🚀 Quick Start\n\nInstall and register the plugin:\n\n```sh\n$ npm i dotenv @import-meta-env/unplugin @import-meta-env/cli\n```\n\n<details>\n<summary>Vite</summary>\n\n```ts\n// vite.config.ts\nimport ImportMetaEnvPlugin from \"./@import-meta-env/unplugin\";\n\nexport default {\n plugins: [\n ImportMetaEnvPlugin.vite({\n /* options */\n }),\n ],\n};\n```\n\n</details>\n\n<details>\n<summary>Rollup</summary>\n\n```js\n// rollup.config.js\nimport ImportMetaEnvPlugin from \"./@import-meta-env/unplugin\";\n\nexport default {\n plugins: [\n ImportMetaEnvPlugin.rollup({\n /* options */\n }),\n ],\n};\n```\n\n</details>\n\n<details>\n<summary>Webpack</summary>\n\n```js\n// webpack.config.js\nmodule.exports = {\n plugins: [\n require(\"./@import-meta-env/unplugin\").webpack({\n /* options */\n }),\n ],\n};\n```\n\n</details>\n\n<details>\n<summary>Esbuild</summary>\n\n```js\n// esbuild.config.js\nimport { build } from \"esbuild\";\n\nbuild({\n plugins: [\n require(\"./@import-meta-env/unplugin\").esbuild({\n /* options */\n }),\n ],\n});\n```\n\n</details>\n\nCreate a `.env.example` file in the root of your project:\n\n```sh\n# .env.example\n# To prevent exposure of sensitive credentials to clients,\n# only the keys defined in this file can be accessed.\nS3_BUCKET=\n```\n\nAdd `.env` file to .gitignore, and create a `.env` file in the project's root directory:\n\n(⚠ This step is completely optional, you should set environment variables directly on your system if you can.)\n\n```sh\n# .env\nS3_BUCKET=\"YOURS3BUCKET\"\nSECRET_KEY=\"YOURSECRETKEYGOESHERE\"\n```\n\n`import.meta.env` now has the keys and values you defined on your system:\n\n```ts\nconsole.log(import.meta.env.S3_BUCKET); // \"YOURS3BUCKET\"\nconsole.log(import.meta.env[\"S3_BUCKET\"]); // \"YOURS3BUCKET\", dynamic key also works\nconsole.log(import.meta.env.SECRET_KEY); // undefined\n```\n\nFinally, before serving your application, remember to execute [`import-meta-env`](https://github.com/iendeavor/import-meta-env/tree/main/packages/cli#readme) binary to inject environment variables, for example:\n\n```sh\n$ cross-env S3_BUCKET=YOURS3BUCKET import-meta-env && your-serve-script\n```\n\nSee also:\n\n- [examples](./examples)\n- [@import-meta-env/babel](https://github.com/iendeavor/import-meta-env/tree/main/packages/babel) - Provide an approximation of this plugin's specific transformations when running the code in other environments, for example, running tests with a NodeJS based test runner.\n- [@import-meta-env/cli](https://github.com/iendeavor/import-meta-env/tree/main/packages/cli) - A binary package is used to inject environment variables into those placeholders.\n\n## 📖 API\n\nPlease see [types.ts](./src/types.ts)\n\n## 📝 License\n\nThis project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details\n" | ||
"readme": "# @import-meta-env/unplugin\n\n[![CI](https://github.com/iendeavor/import-meta-env/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/iendeavor/import-meta-env/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@import-meta-env/unplugin.svg)](https://www.npmjs.com/package/@import-meta-env/unplugin)\n[![PRs Welcome](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n\nThis plugin helps us inject environment variables into the `import.meta.env` object **_after_** building the application instead of statically replacing it during production.\n\n<br>\n\nThis project use [SemVer](https://semver.org/) for versioning. For the versions available, see the tags on this repository.\n\n## Motivation\n\nEnvironment variables should be easy to change between deployments **_without_** rebuilding the application or even changing any code, so we should set environment variables on the system instead of checking them into a repository with `.env` files.\n\nDuring production, this plugin generates chunks with placeholders, which allow us to statically replace environment variables **_after_** building the application (don't worry, we provide an executable for this, you don't need to write them yourself) .\n\n## 🚀 Quick Start\n\nInstall and register the plugin:\n\n```sh\n$ npm i dotenv @import-meta-env/unplugin @import-meta-env/cli\n```\n\n<details>\n<summary>Vite</summary>\n\n```ts\n// vite.config.ts\nimport ImportMetaEnvPlugin from \"./@import-meta-env/unplugin\";\n\nexport default {\n plugins: [\n ImportMetaEnvPlugin.vite({\n /* options */\n }),\n ],\n};\n```\n\n</details>\n\n<details>\n<summary>Rollup</summary>\n\n```js\n// rollup.config.js\nimport ImportMetaEnvPlugin from \"./@import-meta-env/unplugin\";\n\nexport default {\n plugins: [\n ImportMetaEnvPlugin.rollup({\n /* options */\n }),\n ],\n};\n```\n\n</details>\n\n<details>\n<summary>Webpack</summary>\n\n```js\n// webpack.config.js\nmodule.exports = {\n plugins: [\n require(\"./@import-meta-env/unplugin\").webpack({\n /* options */\n }),\n ],\n};\n```\n\n</details>\n\n<details>\n<summary>Esbuild</summary>\n\n```js\n// esbuild.config.js\nimport { build } from \"esbuild\";\n\nbuild({\n plugins: [\n require(\"./@import-meta-env/unplugin\").esbuild({\n /* options */\n }),\n ],\n});\n```\n\n</details>\n\nCreate a `.env.example` file in the root of your project:\n\n```sh\n# .env.example\n# To prevent exposure of sensitive credentials to clients,\n# only the keys defined in this file can be accessed.\nS3_BUCKET=\n```\n\nAdd `.env` file to .gitignore, and create a `.env` file in the project's root directory:\n\n(⚠ This step is completely optional, you should set environment variables directly on your system if you can.)\n\n```sh\n# .env\nS3_BUCKET=\"YOURS3BUCKET\"\nSECRET_KEY=\"YOURSECRETKEYGOESHERE\"\n```\n\n`import.meta.env` now has the keys and values you defined on your system:\n\n```ts\nconsole.log(import.meta.env.S3_BUCKET); // \"YOURS3BUCKET\"\nconsole.log(import.meta.env[\"S3_BUCKET\"]); // \"YOURS3BUCKET\", dynamic key also works\nconsole.log(import.meta.env.SECRET_KEY); // undefined\n```\n\nFinally, before serving your application, remember to execute [`import-meta-env`](https://github.com/iendeavor/import-meta-env/tree/main/packages/cli#readme) binary to inject environment variables, for example:\n\n```sh\n$ cross-env S3_BUCKET=YOURS3BUCKET import-meta-env && your-serve-script\n```\n\nSee also:\n\n- [examples](https://github.com/iendeavor/import-meta-env/tree/main/packages/examples)\n- [@import-meta-env/babel](https://github.com/iendeavor/import-meta-env/tree/main/packages/babel) - Provide an approximation of this plugin's specific transformations when running the code in other environments, for example, running tests with a NodeJS based test runner.\n- [@import-meta-env/cli](https://github.com/iendeavor/import-meta-env/tree/main/packages/cli) - A binary package is used to inject environment variables into those placeholders.\n\n## 📖 API\n\nPlease see [types.ts](./src/types.ts)\n\n## 📝 License\n\nThis project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details\n" | ||
} |
@@ -132,3 +132,3 @@ # @import-meta-env/unplugin | ||
- [examples](./examples) | ||
- [examples](https://github.com/iendeavor/import-meta-env/tree/main/packages/examples) | ||
- [@import-meta-env/babel](https://github.com/iendeavor/import-meta-env/tree/main/packages/babel) - Provide an approximation of this plugin's specific transformations when running the code in other environments, for example, running tests with a NodeJS based test runner. | ||
@@ -135,0 +135,0 @@ - [@import-meta-env/cli](https://github.com/iendeavor/import-meta-env/tree/main/packages/cli) - A binary package is used to inject environment variables into those placeholders. |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
46777
937