forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose env variables on
import.meta.env
BREAKING CHANGE: env variables are now exposed on `import.meta.env` instead of `process.env`. - For example, with a `.env` file containing `VITE_FOO=1`, you can access it as `import.meta.env.VITE_FOO`. - Only variables that start with `VITE_` are exposed to the client code. This is because sometimes users may use the same `.env` file for build scripts or other server-side code where it may contain sensitive information that should not be exposed in client-side code. - `import.meta.env.MODE` will be the mode the app is running in (default is `development` in dev and `production` in build). - `import.meta.env.BASE_URL` will be the base public URL as specified via the `base` config option. - `import.meta.env.DEV` will be `true` when mode is `development`. - `import.meta.env.PROD` will be `true` when mode is `production`. - `process.env` is still shimmed because some dependencies rely on it, but will only expose `process.env.NODE_ENV` and will not contain any user env variables.
- Loading branch information
Showing
12 changed files
with
104 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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,2 +1,2 @@ | ||
CUSTOM_ENV_VARIABLE=9527 | ||
EFFECTIVE_MODE_FILE_NAME=.env | ||
VITE_CUSTOM_ENV_VARIABLE=9527 | ||
VITE_EFFECTIVE_MODE_FILE_NAME=.env |
This file contains 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 +1 @@ | ||
EFFECTIVE_MODE_FILE_NAME=.env.development | ||
VITE_EFFECTIVE_MODE_FILE_NAME=.env.development |
This file contains 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 +1 @@ | ||
EFFECTIVE_MODE_FILE_NAME=.env.production | ||
VITE_EFFECTIVE_MODE_FILE_NAME=.env.production |
This file contains 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 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 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 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 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 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 { ServerPlugin } from '.' | ||
|
||
export const envPublicPath = '/vite/env' | ||
|
||
export const envPlugin: ServerPlugin = ({ app, config }) => { | ||
const mode = config.mode || 'development' | ||
const env = JSON.stringify({ | ||
...config.env, | ||
BASE_URL: '/', | ||
MODE: mode, | ||
DEV: mode === 'development', | ||
PROD: mode === 'production' | ||
}) | ||
|
||
app.use((ctx, next) => { | ||
if (ctx.path === envPublicPath) { | ||
ctx.type = 'js' | ||
ctx.body = `export default ${env}` | ||
return | ||
} | ||
return next() | ||
}) | ||
} |
This file contains 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 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 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