Skip to content

Commit 975c91e

Browse files
committed
fix: make js define plugin works at dev build
1 parent 5865b62 commit 975c91e

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

packages/vite/src/node/plugins/define.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const importMetaEnvMarker = '__vite_import_meta_env__'
1212
const importMetaEnvKeyReCache = new Map<string, RegExp>()
1313

1414
export function definePlugin(config: ResolvedConfig): Plugin {
15-
const isBuild = config.command === 'build'
16-
const isBuildLib = isBuild && config.build.lib
15+
// const isBuild = config.command === 'build'
16+
const isBuildLib = config.build.lib
1717

1818
// ignore replace process.env in lib build
1919
const processEnv: Record<string, string> = {}
@@ -33,17 +33,17 @@ export function definePlugin(config: ResolvedConfig): Plugin {
3333
const importMetaKeys: Record<string, string> = {}
3434
const importMetaEnvKeys: Record<string, string> = {}
3535
const importMetaFallbackKeys: Record<string, string> = {}
36-
if (isBuild) {
37-
importMetaKeys['import.meta.hot'] = `undefined`
38-
for (const key in config.env) {
39-
const val = JSON.stringify(config.env[key])
40-
importMetaKeys[`import.meta.env.${key}`] = val
41-
importMetaEnvKeys[key] = val
42-
}
43-
// these will be set to a proper value in `generatePattern`
44-
importMetaKeys['import.meta.env.SSR'] = `undefined`
45-
importMetaFallbackKeys['import.meta.env'] = `undefined`
36+
// if (isBuild) {
37+
importMetaKeys['import.meta.hot'] = `undefined`
38+
for (const key in config.env) {
39+
const val = JSON.stringify(config.env[key])
40+
importMetaKeys[`import.meta.env.${key}`] = val
41+
importMetaEnvKeys[key] = val
4642
}
43+
// these will be set to a proper value in `generatePattern`
44+
importMetaKeys['import.meta.env.SSR'] = `undefined`
45+
importMetaFallbackKeys['import.meta.env'] = `undefined`
46+
// }
4747

4848
function generatePattern(environment: Environment) {
4949
const keepProcessEnv = environment.config.keepProcessEnv
@@ -54,7 +54,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
5454
userDefine[key] = handleDefineValue(environment.config.define[key])
5555

5656
// make sure `import.meta.env` object has user define properties
57-
if (isBuild && key.startsWith('import.meta.env.')) {
57+
if (key.startsWith('import.meta.env.')) {
5858
userDefineEnv[key.slice(16)] = environment.config.define[key]
5959
}
6060
}
@@ -128,12 +128,12 @@ export function definePlugin(config: ResolvedConfig): Plugin {
128128

129129
transform: {
130130
async handler(code, id) {
131-
if (this.environment.config.consumer === 'client' && !isBuild) {
132-
// for dev we inject actual global defines in the vite client to
133-
// avoid the transform cost. see the `clientInjection` and
134-
// `importAnalysis` plugin.
135-
return
136-
}
131+
// if (this.environment.config.consumer === 'client' && !isBuild) {
132+
// for dev we inject actual global defines in the vite client to
133+
// avoid the transform cost. see the `clientInjection` and
134+
// `importAnalysis` plugin.
135+
// return
136+
// }
137137

138138
if (
139139
// exclude html, css and static assets for performance

0 commit comments

Comments
 (0)