Skip to content

Commit

Permalink
refactor: simplify build optimizer node_env handling (#14829)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Nov 1, 2023
1 parent 7950636 commit 275907b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 28 deletions.
11 changes: 5 additions & 6 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,13 +755,12 @@ async function prepareEsbuildOptimizerRun(
if (optimizerContext.cancelled) return { context: undefined, idToExports }

// esbuild automatically replaces process.env.NODE_ENV for platform 'browser'
// In lib mode, we need to keep process.env.NODE_ENV untouched, so to at build
// time we replace it by __vite_process_env_NODE_ENV. This placeholder will be
// later replaced by the define plugin
// But in lib mode, we need to keep process.env.NODE_ENV untouched
const define = {
'process.env.NODE_ENV': isBuild
? '__vite_process_env_NODE_ENV'
: JSON.stringify(process.env.NODE_ENV || config.mode),
'process.env.NODE_ENV':
isBuild && config.build.lib
? 'process.env.NODE_ENV'
: JSON.stringify(process.env.NODE_ENV || config.mode),
}

const platform =
Expand Down
6 changes: 1 addition & 5 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export function definePlugin(config: ResolvedConfig): Plugin {
'process.env.NODE_ENV': JSON.stringify(nodeEnv),
'global.process.env.NODE_ENV': JSON.stringify(nodeEnv),
'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv),
__vite_process_env_NODE_ENV: JSON.stringify(nodeEnv),
})
}

Expand Down Expand Up @@ -69,9 +68,6 @@ export function definePlugin(config: ResolvedConfig): Plugin {
}

// Additional define fixes based on `ssr` value
if (isBuild && !replaceProcessEnv) {
define['__vite_process_env_NODE_ENV'] = 'process.env.NODE_ENV'
}
if ('import.meta.env.SSR' in define) {
define['import.meta.env.SSR'] = ssr + ''
}
Expand All @@ -86,7 +82,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
// Create regex pattern as a fast check before running esbuild
const patternKeys = Object.keys(userDefine)
if (replaceProcessEnv && Object.keys(processEnv).length) {
patternKeys.push('process.env', '__vite_process_env_NODE_ENV')
patternKeys.push('process.env')
}
if (Object.keys(importMetaKeys).length) {
patternKeys.push('import.meta.env', 'import.meta.hot')
Expand Down
5 changes: 0 additions & 5 deletions playground/define/__tests__/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ test('ignores constants in string literals', async () => {
'.ignores-string-literals .globalThis-process-env-NODE_ENV',
),
).toBe('globalThis.process.env.NODE_ENV')
expect(
await page.textContent(
'.ignores-string-literals .__vite_process_env_NODE_ENV',
),
).toBe('__vite_process_env_NODE_ENV')
expect(
await page.textContent('.ignores-string-literals .import-meta-hot'),
).toBe('import' + '.meta.hot')
Expand Down
12 changes: 0 additions & 12 deletions playground/define/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ <h2>Define ignores string literals</h2>
globalThis.process.env.NODE_ENV
<code class="globalThis-process-env-NODE_ENV"></code>
</p>
<p>
__vite_process_env_NODE_ENV
<code class="__vite_process_env_NODE_ENV"></code>
</p>
<p>import.meta.hot <code class="import-meta-hot"></code></p>
</section>

Expand All @@ -60,10 +56,6 @@ <h2>Define replaces constants in template literal expressions</h2>
globalThis.process.env.NODE_ENV
<code class="globalThis-process-env-NODE_ENV"></code>
</p>
<p>
__vite_process_env_NODE_ENV
<code class="__vite_process_env_NODE_ENV"></code>
</p>
<p>import.meta.hot <code class="import-meta-hot"></code></p>
</section>

Expand Down Expand Up @@ -123,10 +115,6 @@ <h2>Define replaces constants in template literal expressions</h2>
'.ignores-string-literals .globalThis-process-env-NODE_ENV',
'globalThis.process.env.NODE_ENV',
)
text(
'.ignores-string-literals .__vite_process_env_NODE_ENV',
'__vite_process_env_NODE_ENV',
)
text('.ignores-string-literals .import-meta-hot', 'import.meta.hot')

text(
Expand Down

0 comments on commit 275907b

Please sign in to comment.