-
Notifications
You must be signed in to change notification settings - Fork 25
feat: add vite plugin #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,27 @@ | ||
| --- | ||
| title: Vite | ||
| --- | ||
| --- | ||
|
|
||
| ## Install | ||
|
|
||
| ```console | ||
| npm add @dotenv-run/vite --save-dev | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Create a `vite.config.js` [configuration file](https://vite.dev/config) and import the plugin: | ||
|
|
||
| ```js | ||
| import env from "@dotenv-run/vite"; | ||
|
|
||
| export default { | ||
| envPrefix: 'MY_PREFIX_', | ||
| envDir: './my-env-directory', | ||
| plugins: [env()], | ||
| }; | ||
| ``` | ||
|
|
||
| Then call `vite` or `vite build` either via the [CLI](https://vite.dev/guide/cli.html). | ||
|
|
||
| The available options are similar to those supported by [`@dotenv-run/core`](https://www.npmjs.com/package/@dotenv-run/core), but this plugin seamlessly integrates with Vite by automatically deriving the root, prefix, and environment values from its standard configuration, ensuring a more cohesive experience. For more details, refer to the API section. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # @dotenv-run/vite | ||
|
|
||
| - ✅ Load environment variables from the command line `API_BASE=/v1/ vite` | ||
| - ✅ Load environment variables from `.env` files | ||
| - ✅ Expand environment variables `API_URL=$API_BASE/users` | ||
| - ✅ Define environment variables for a specific environment (e.g. `.env.production`) | ||
| - ✅ Load priorities of `.env.*` files (e.g. `.env.production` > `.env`) | ||
| - ✅ Hierarchical cascading configuration in monorepo projects ([Nx](https://nx.dev), [Turbo](https://turbo.build/), etc.) | ||
| `apps/next-app/.env` > `apps/.env` > `.env` | ||
|
|
||
| ## Install | ||
|
|
||
| ```sh | ||
| npm add @dotenv-run/vite --save-dev | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Create a `vite.config.js` [configuration file](https://vite.dev/config) and import the plugin: | ||
|
|
||
| ```js | ||
| import env from "@dotenv-run/vite"; | ||
|
|
||
| export default { | ||
| envPrefix: 'MY_PREFIX_', | ||
| envDir: './my-env-directory', | ||
| plugins: [env()], | ||
| }; | ||
| ``` | ||
|
|
||
| Then call `vite` or `vite build` either via the [CLI](https://vite.dev/guide/cli.html). | ||
|
|
||
| The available options are similar to those supported by [`@dotenv-run/core`](https://www.npmjs.com/package/@dotenv-run/core), but this plugin seamlessly integrates with Vite by automatically deriving the root, prefix, and environment values from its standard configuration, ensuring a more cohesive experience. For more details, refer to the API section. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "name": "@dotenv-run/vite", | ||
| "version": "1.0.0", | ||
| "description": "Run your scripts with dotenv variables", | ||
| "homepage": "https://github.com/chihab/dotenv-run", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "tsc -w", | ||
| "build": "tsc" | ||
| }, | ||
| "files": [ | ||
| "dist", | ||
| "README.md" | ||
| ], | ||
| "keywords": [ | ||
| "dotenv", | ||
| "run", | ||
| "cli", | ||
| "vite", | ||
| "vite-plugin" | ||
| ], | ||
| "author": "Iacopo Ciao <[email protected]>", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "@dotenv-run/core": "workspace:^1.3.6", | ||
| "@rollup/plugin-replace": "^5.0.7", | ||
| "lodash-es": "^4.17.21", | ||
| "vite": "^6.2.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/lodash-es": "^4.17.12", | ||
| "@types/node": "^16.18.112" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export const DEFAULT_PREFIX = 'VITE_'; | ||
|
|
||
| export const DEFAULT_ENV_FILES = [ | ||
| /** vault file */ `.env.vault`, | ||
| /** default file */ `.env`, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { env as loadEnv } from "@dotenv-run/core"; | ||
| import { Plugin } from "vite"; | ||
| import { viteEnvPrefixToPrefix } from "./mapper.js"; | ||
| import { sanitizeOptions, ViteDotenvRunOptions } from "./options.js"; | ||
| import replace from "@rollup/plugin-replace"; | ||
| import { DEFAULT_ENV_FILES } from "./constants.js"; | ||
|
|
||
| /** | ||
| * Vite plugin to load environment variables from .env files using `@dotenv-run/core`. | ||
| * | ||
| * This plugin seamlessly integrates with Vite by automatically deriving the root, | ||
| * prefix and environment options from Vite's `envDir`, `envPrefix` and `mode`, | ||
| * ensuring a more cohesive experience. | ||
| * | ||
| * @param {ViteDotenvRunOptions} [options] - Options for configuring the plugin. | ||
| * See {@link ViteDotenvRunOptions} for more details. | ||
| * | ||
| * @returns {Plugin} Vite plugin object that enhances the Vite configuration. | ||
| * | ||
| * @example | ||
| * // Usage in a Vite config file | ||
| * import dotenvRun from 'vite-plugin-dotenv-run'; | ||
| * | ||
| * export default { | ||
| * envDir: '../..', | ||
| * envPrefix: ['VITE_', 'CUSTOM_'], | ||
| * plugins: [ | ||
| * dotenvRun(), | ||
| * ], | ||
| * }; | ||
| */ | ||
| const dotenvRun = (options?: ViteDotenvRunOptions): Plugin => { | ||
| options = sanitizeOptions(options); | ||
| const files = options?.files ?? DEFAULT_ENV_FILES; | ||
|
|
||
| return { | ||
| name: "vite-plugin-dotenv-run", | ||
| config: (config, configEnv) => { | ||
| const prefix = viteEnvPrefixToPrefix(config.envPrefix); | ||
|
|
||
| const { full } = loadEnv({ | ||
| files, | ||
| prefix, | ||
| root: config.envDir, | ||
| environment: configEnv.mode, | ||
| ...options, | ||
| }); | ||
|
|
||
| return { | ||
| ...config, | ||
| ...replace({ | ||
| preventAssignment: true, | ||
| values: full, | ||
| }), | ||
| }; | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export { dotenvRun, ViteDotenvRunOptions }; | ||
| export default dotenvRun; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { | ||
| castArray, | ||
| escapeRegExp, | ||
| first, | ||
| isEmpty, | ||
| isNil, | ||
| negate, | ||
| } from "lodash-es"; | ||
| import { DEFAULT_PREFIX } from "./constants.js"; | ||
|
|
||
| /** | ||
| * Converts a Vite `envPrefix` configuration value into a usable prefix or RegExp for @dotenv-run/core. | ||
| * | ||
| * @param {string | string[] | undefined} prefixes - The prefix or list of prefixes to filter environment variables. | ||
| * @returns {string | RegExp} - A single prefix as a string if only one is provided, or a RegExp if multiple prefixes are given. | ||
| * | ||
| * @throws {Error} If an empty string (`''`) is included in the prefixes, as this could expose all environment variables. | ||
| * | ||
| * @example | ||
| * viteEnvPrefixToPrefix("VITE_") // Returns: "VITE_" | ||
| * viteEnvPrefixToPrefix(["VITE_", "CUSTOM_"]) // Returns: /^VITE_|CUSTOM_/ | ||
| * viteEnvPrefixToPrefix(undefined) // Returns: DEFAULT_PREFIX | ||
| * | ||
| * @see {@link https://vite.dev/config/shared-options.html#envprefix Vite Documentation on envPrefix} | ||
| * | ||
| * @security | ||
| * The `envPrefix` option should **never** be set to an empty string (`''`), | ||
| * as this will expose **all** environment variables, potentially leaking sensitive information. | ||
| * Vite has a built-in safeguard that throws an error when detecting `''` as a prefix. | ||
| * | ||
| * If you need to expose an unprefixed environment variable, use the `define` option instead: | ||
| * | ||
| * ``` | ||
| * define: { | ||
| * "process.env.MY_VAR": JSON.stringify(process.env.MY_VAR) | ||
| * } | ||
| * ``` | ||
| */ | ||
| export const viteEnvPrefixToPrefix = ( | ||
| prefixes: string | string[] | undefined | ||
| ): string | RegExp => { | ||
| prefixes = castArray(prefixes).filter(negate(isNil)); | ||
|
|
||
| if (isEmpty(prefixes)) { | ||
| return DEFAULT_PREFIX; | ||
| } | ||
|
|
||
| if (prefixes.includes("")) { | ||
| throw new Error( | ||
| `envPrefix option contains value '', which could lead unexpected exposure of sensitive information.` | ||
| ); | ||
| } | ||
|
|
||
| if (prefixes.length === 1) { | ||
| return first(prefixes); | ||
| } | ||
|
|
||
| return new RegExp(`^(${prefixes.map(escapeRegExp).join("|")})`); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { DotenvRunOptions } from "@dotenv-run/core"; | ||
| import { pick } from "lodash-es"; | ||
|
|
||
| /** | ||
| * Options for configuring the @dotenv-run/vite plugin. | ||
| * | ||
| * @interface ViteDotenvRunOptions | ||
| * @extends {Pick<DotenvRunOptions, 'verbose' | 'unsecure' | 'builtIn' | 'global' | 'nodeEnv' | 'runtime' | 'dotenv' | 'files'>} | ||
| * | ||
| * @property {DotenvConfigOptions} [dotenv] - Options for configuring dotenv. | ||
| * @property {string[]} [files] - Environment files to load. Defaults to `['.env.vault', '.env']`. | ||
| * @property {boolean} [unsecure] - Display environment variables in debug output. | ||
| * @property {boolean} [nodeEnv] - Node environment. | ||
| * @property {boolean} [verbose] - Print verbose output. | ||
| * @property {Dict} [builtIn] - Built-in environment variables. | ||
| * @property {boolean} [runtime] - Whether to use runtime variables. | ||
| * @property {string} [global] - Global variable name. | ||
| */ | ||
| export type ViteDotenvRunOptions = Pick<DotenvRunOptions, 'verbose' | 'unsecure' | 'builtIn' | 'global' | 'nodeEnv' | 'runtime' | 'dotenv' | 'files'>; | ||
|
|
||
| export const sanitizeOptions = <T extends ViteDotenvRunOptions>(options?: T): ViteDotenvRunOptions | undefined => { | ||
| return pick(options, 'verbose', 'unsecure', 'builtIn', 'global', 'nodeEnv', 'runtime', 'dotenv', 'files'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "include": ["src"], | ||
| "compilerOptions": { | ||
| "outDir": "dist", | ||
| "moduleResolution": "node", | ||
| "module": "ES2022", | ||
| "strict": true, | ||
| "allowSyntheticDefaultImports": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "strictPropertyInitialization": false, | ||
| "strictNullChecks": false, | ||
| "pretty": true, | ||
| "sourceMap": true, | ||
| "declaration": true, | ||
| "skipLibCheck": true | ||
| }, | ||
| "exclude": ["node_modules"], | ||
| "compileOnSave": false, | ||
| "buildOnSave": false | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.