Skip to content

Commit 20f8a3f

Browse files
committedJul 1, 2022
fix(bundler-vite): handle process.env replacement in dev
1 parent 6c41407 commit 20f8a3f

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed
 

‎packages/@vuepress/bundler-vite/src/plugins/constantsReplacementPlugin.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const vueTemplateBreaker = '<wbr>'
1515
* constant strings in page data, and insert `<wbr>` tag into constant strings
1616
* in template of page component.
1717
*
18+
* Notice that `process.env.NODE_ENV` will be statically replaced even
19+
* in dev mode, while the vite docs does not mention it yet.
20+
*
1821
* @see https://vitejs.dev/guide/env-and-mode.html#production-replacement
1922
*/
2023
export const constantsReplacementPlugin = (app: App): Plugin => {
@@ -32,13 +35,16 @@ export const constantsReplacementPlugin = (app: App): Plugin => {
3235

3336
enforce: 'pre',
3437

35-
apply: 'build',
36-
3738
configResolved(resolvedConfig) {
38-
const constants = ['import.meta', 'process.env']
39+
// `process.env` need to be handled in both dev and build
40+
// @see https://github.com/vitejs/vite/blob/cad27ee8c00bbd5aeeb2be9bfb3eb164c1b77885/packages/vite/src/node/plugins/clientInjections.ts#L57-L64
41+
const constants = ['process.env']
3942

40-
if (resolvedConfig.define) {
41-
constants.push(...Object.keys(resolvedConfig.define))
43+
if (app.env.isBuild) {
44+
constants.push('import.meta')
45+
if (resolvedConfig.define) {
46+
constants.push(...Object.keys(resolvedConfig.define))
47+
}
4248
}
4349

4450
constantsRegexp = new RegExp(

0 commit comments

Comments
 (0)
Please sign in to comment.