From f36fcd234d61b4b133667bda8f25c85d9687384d Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Sun, 29 Oct 2023 14:06:16 +0800 Subject: [PATCH] fix(define): correctly replace same define values (#14786) --- .../vite/src/node/__tests__/plugins/define.spec.ts | 11 ++++++++++- packages/vite/src/node/plugins/define.ts | 10 ---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/define.spec.ts b/packages/vite/src/node/__tests__/plugins/define.spec.ts index de9d4b36b742c6..2ac85712b2a072 100644 --- a/packages/vite/src/node/__tests__/plugins/define.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/define.spec.ts @@ -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', ) }) }) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index 36b0df05a9b55a..ccd6a5571a6e39 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -49,16 +49,6 @@ export function definePlugin(config: ResolvedConfig): Plugin { const userDefine: Record = {} const userDefineEnv: Record = {} 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