From ad6659d1f4fb9f379d17f8bdac2cca9180694be1 Mon Sep 17 00:00:00 2001 From: Derek Arner Date: Tue, 5 Jul 2022 21:46:45 -0700 Subject: [PATCH 1/2] feat: allow import.meta.hot define override --- .../src/node/__tests__/plugins/define.spec.ts | 15 +++++++++++++++ packages/vite/src/node/plugins/define.ts | 6 ++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/define.spec.ts b/packages/vite/src/node/__tests__/plugins/define.spec.ts index 932560a749f24d..7764af533c1165 100644 --- a/packages/vite/src/node/__tests__/plugins/define.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/define.spec.ts @@ -37,4 +37,19 @@ describe('definePlugin', () => { 'const isSSR = false;' ) }) + + test('preserve import.meta.hot with override', async () => { + // assert that the default behavior is to replace import.meta.hot with false + const transform = await createDefinePluginTransform() + expect(await transform('const isHot = import.meta.hot;')).toBe( + 'const isHot = false;' + ) + // assert that we can specify a user define to preserve import.meta.hot + const overrideTransform = await createDefinePluginTransform({ + 'import.meta.hot': 'import.meta.hot' + }) + expect(await overrideTransform('const isHot = import.meta.hot;')).toBe( + 'const isHot = import.meta.hot;' + ) + }) }) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index 0448327d660ed6..c42955ff59f47e 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -45,13 +45,15 @@ export function definePlugin(config: ResolvedConfig): Plugin { ...config.env, SSR: !!config.build.ssr } + // put import.meta.hot replacement in importMetaKeys to allow + // users to override with config.define. + importMetaKeys['import.meta.hot'] = `false` for (const key in env) { importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key]) } Object.assign(importMetaFallbackKeys, { 'import.meta.env.': `({}).`, - 'import.meta.env': JSON.stringify(config.env), - 'import.meta.hot': `false` + 'import.meta.env': JSON.stringify(config.env) }) } From 478c0b80576e23c850802908c68abcbc33d08b60 Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 19 Dec 2022 10:57:27 +0800 Subject: [PATCH 2/2] docs: update comment --- packages/vite/src/node/plugins/define.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index b427b28ad1cfb7..4a1b83e5390aa6 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -45,8 +45,7 @@ export function definePlugin(config: ResolvedConfig): Plugin { ...config.env, SSR: !!config.build.ssr, } - // put import.meta.hot replacement in importMetaKeys to allow - // users to override with config.define. + // set here to allow override with config.define importMetaKeys['import.meta.hot'] = `false` for (const key in env) { importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])