Skip to content

Commit

Permalink
feat: allow import.meta.hot define override (#8944)
Browse files Browse the repository at this point in the history
  • Loading branch information
doman412 committed Dec 19, 2022
1 parent 2f5b9b3 commit 857d578
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/vite/src/node/__tests__/plugins/define.spec.ts
Expand Up @@ -39,4 +39,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;',
)
})
})
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/define.ts
Expand Up @@ -45,13 +45,14 @@ export function definePlugin(config: ResolvedConfig): Plugin {
...config.env,
SSR: !!config.build.ssr,
}
// 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])
}
Object.assign(importMetaFallbackKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(config.env),
'import.meta.hot': `false`,
})
}

Expand Down

0 comments on commit 857d578

Please sign in to comment.