Skip to content

Commit 857d578

Browse files
authoredDec 19, 2022
feat: allow import.meta.hot define override (#8944)
1 parent 2f5b9b3 commit 857d578

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

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

+15
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,19 @@ describe('definePlugin', () => {
3939
'const isSSR = false;',
4040
)
4141
})
42+
43+
test('preserve import.meta.hot with override', async () => {
44+
// assert that the default behavior is to replace import.meta.hot with false
45+
const transform = await createDefinePluginTransform()
46+
expect(await transform('const isHot = import.meta.hot;')).toBe(
47+
'const isHot = false;',
48+
)
49+
// assert that we can specify a user define to preserve import.meta.hot
50+
const overrideTransform = await createDefinePluginTransform({
51+
'import.meta.hot': 'import.meta.hot',
52+
})
53+
expect(await overrideTransform('const isHot = import.meta.hot;')).toBe(
54+
'const isHot = import.meta.hot;',
55+
)
56+
})
4257
})

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ export function definePlugin(config: ResolvedConfig): Plugin {
4545
...config.env,
4646
SSR: !!config.build.ssr,
4747
}
48+
// set here to allow override with config.define
49+
importMetaKeys['import.meta.hot'] = `false`
4850
for (const key in env) {
4951
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
5052
}
5153
Object.assign(importMetaFallbackKeys, {
5254
'import.meta.env.': `({}).`,
5355
'import.meta.env': JSON.stringify(config.env),
54-
'import.meta.hot': `false`,
5556
})
5657
}
5758

0 commit comments

Comments
 (0)