Skip to content

Commit

Permalink
fix(define): correctly replace SSR in dev (#12204)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Mar 8, 2023
1 parent 4433c30 commit 0f6de4d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
42 changes: 29 additions & 13 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
// stringified for `import.meta.env`, we can remove the quotes and
// retain being an identifier
typeof val === 'string' && /^[\p{L}_$]/u.test(val.trim())
? `__vite__${val}__vite__`
? `__vite__define__${val}`
: val
}
}
Expand All @@ -56,34 +56,50 @@ export function definePlugin(config: ResolvedConfig): Plugin {
const importMetaKeys: Record<string, string> = {}
const importMetaFallbackKeys: Record<string, string> = {}
if (isBuild) {
const env: Record<string, any> = {
...config.env,
SSR: !!config.build.ssr,
}
// set here to allow override with config.define
importMetaKeys['import.meta.hot'] = `undefined`
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
for (const key in config.env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(config.env[key])
}
Object.assign(importMetaFallbackKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify({ ...env, ...userDefineEnv }).replace(
/"__vite__(.+?)__vite__"/g,
(_, val) => val,
),
'import.meta.env': JSON.stringify({
...config.env,
SSR: '__vite__ssr__',
...userDefineEnv,
}).replace(/"__vite__define__(.+?)"/g, (_, val) => val),
})
}

function getImportMetaKeys(ssr: boolean): Record<string, string> {
if (!isBuild) return {}
return {
...importMetaKeys,
'import.meta.env.SSR': ssr + '',
}
}

function getImportMetaFallbackKeys(ssr: boolean): Record<string, string> {
if (!isBuild) return {}
return {
...importMetaFallbackKeys,
'import.meta.env': importMetaFallbackKeys['import.meta.env'].replace(
'"__vite__ssr__"',
ssr + '',
),
}
}

function generatePattern(
ssr: boolean,
): [Record<string, string | undefined>, RegExp | null] {
const replaceProcessEnv = !ssr || config.ssr?.target === 'webworker'

const replacements: Record<string, string> = {
...(replaceProcessEnv ? processNodeEnv : {}),
...importMetaKeys,
...getImportMetaKeys(ssr),
...userDefine,
...importMetaFallbackKeys,
...getImportMetaFallbackKeys(ssr),
...(replaceProcessEnv ? processEnv : {}),
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
if (!_env) {
_env = `import.meta.env = ${JSON.stringify({
...config.env,
SSR: '__vite__ssr__vite__',
SSR: '__vite__ssr__',
})};`
// account for user env defines
for (const key in config.define) {
Expand All @@ -183,7 +183,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}
}
}
return _env.replace('"__vite__ssr__vite__"', ssr + '')
return _env.replace('"__vite__ssr__"', ssr + '')
}

return {
Expand Down

0 comments on commit 0f6de4d

Please sign in to comment.