Skip to content

Commit

Permalink
feat: allow import.meta.hot define override
Browse files Browse the repository at this point in the history
  • Loading branch information
doman412 committed Jul 6, 2022
1 parent df5688c commit ad6659d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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 @@ -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
Original file line number Diff line number Diff line change
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

0 comments on commit ad6659d

Please sign in to comment.