Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow import.meta.hot define override #8944

Merged
merged 3 commits into from Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/vite/src/node/__tests__/plugins/define.spec.ts
Expand Up @@ -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;'
)
})
})
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/define.ts
Expand Up @@ -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)
})
}

Expand Down