Skip to content

Commit

Permalink
fix: replace import.meta.hot with undefined in the production (#11317)
Browse files Browse the repository at this point in the history
  • Loading branch information
xunmi1 committed Jan 26, 2023
1 parent f1f050e commit 73afe6d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
10 changes: 5 additions & 5 deletions packages/vite/src/node/__tests__/plugins/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ describe('definePlugin', () => {
})

test('preserve import.meta.hot with override', async () => {
// assert that the default behavior is to replace import.meta.hot with false
// assert that the default behavior is to replace import.meta.hot with undefined
const transform = await createDefinePluginTransform()
expect(await transform('const isHot = import.meta.hot;')).toBe(
'const isHot = false;',
expect(await transform('const hot = import.meta.hot;')).toBe(
'const hot = undefined;',
)
// 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;',
expect(await overrideTransform('const hot = import.meta.hot;')).toBe(
'const hot = import.meta.hot;',
)
})
})
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
SSR: !!config.build.ssr,
}
// set here to allow override with config.define
importMetaKeys['import.meta.hot'] = `false`
importMetaKeys['import.meta.hot'] = `undefined`
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}
Expand Down
4 changes: 3 additions & 1 deletion playground/hmr/counter/dep.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// This file is never loaded
import.meta.hot.accept(() => {})
if (import.meta.hot) {
import.meta.hot.accept(() => {})
}

0 comments on commit 73afe6d

Please sign in to comment.