Skip to content

Commit

Permalink
fix(define): correctly replace same define values (#14786)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Oct 29, 2023
1 parent d5d96e7 commit f36fcd2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 10 additions & 1 deletion packages/vite/src/node/__tests__/plugins/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ describe('definePlugin', () => {
'import.meta.hot': 'import.meta.hot',
})
expect(await overrideTransform('const hot = import.meta.hot;')).toBe(
undefined,
'const hot = import.meta.hot;\n',
)
})

test('preserve import.meta.env.UNKNOWN with override', async () => {
const transform = await createDefinePluginTransform({
'import.meta.env.UNKNOWN': 'import.meta.env.UNKNOWN',
})
expect(await transform('const foo = import.meta.env.UNKNOWN;')).toBe(
'const foo = import.meta.env.UNKNOWN;\n',
)
})
})
10 changes: 0 additions & 10 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ export function definePlugin(config: ResolvedConfig): Plugin {
const userDefine: Record<string, string> = {}
const userDefineEnv: Record<string, any> = {}
for (const key in config.define) {
// user can define keys with the same values to declare that some keys
// should not be replaced. in this case, we delete references of the key
// so they aren't replaced in the first place.
const val = config.define[key]
if (key === val) {
delete processNodeEnv[key]
delete importMetaKeys[key]
continue
}

userDefine[key] = handleDefineValue(config.define[key])

// make sure `import.meta.env` object has user define properties
Expand Down

0 comments on commit f36fcd2

Please sign in to comment.