Skip to content

Commit 73afe6d

Browse files
authored
fix: replace import.meta.hot with undefined in the production (#11317)
1 parent f1f050e commit 73afe6d

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ describe('definePlugin', () => {
4141
})
4242

4343
test('preserve import.meta.hot with override', async () => {
44-
// assert that the default behavior is to replace import.meta.hot with false
44+
// assert that the default behavior is to replace import.meta.hot with undefined
4545
const transform = await createDefinePluginTransform()
46-
expect(await transform('const isHot = import.meta.hot;')).toBe(
47-
'const isHot = false;',
46+
expect(await transform('const hot = import.meta.hot;')).toBe(
47+
'const hot = undefined;',
4848
)
4949
// assert that we can specify a user define to preserve import.meta.hot
5050
const overrideTransform = await createDefinePluginTransform({
5151
'import.meta.hot': 'import.meta.hot',
5252
})
53-
expect(await overrideTransform('const isHot = import.meta.hot;')).toBe(
54-
'const isHot = import.meta.hot;',
53+
expect(await overrideTransform('const hot = import.meta.hot;')).toBe(
54+
'const hot = import.meta.hot;',
5555
)
5656
})
5757
})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
4646
SSR: !!config.build.ssr,
4747
}
4848
// set here to allow override with config.define
49-
importMetaKeys['import.meta.hot'] = `false`
49+
importMetaKeys['import.meta.hot'] = `undefined`
5050
for (const key in env) {
5151
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
5252
}

playground/hmr/counter/dep.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
// This file is never loaded
2-
import.meta.hot.accept(() => {})
2+
if (import.meta.hot) {
3+
import.meta.hot.accept(() => {})
4+
}

0 commit comments

Comments
 (0)