Skip to content

Commit

Permalink
feat: allow import.meta.hot define override (vitejs#8944)
Browse files Browse the repository at this point in the history
  • Loading branch information
doman412 authored and futurGH committed Feb 26, 2023
1 parent a567c21 commit 3cf9853
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 3cf9853

Please sign in to comment.