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

fix: avoid replacing defines and NODE_ENV in optimized deps (fix #8593) #8606

Merged
merged 6 commits into from Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 5 additions & 11 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -385,6 +385,7 @@ export async function runOptimizeDeps(
resolvedConfig: ResolvedConfig,
depsInfo: Record<string, OptimizedDepInfo>
): Promise<DepOptimizationResult> {
const isBuild = resolvedConfig.command === 'build'
const config: ResolvedConfig = {
...resolvedConfig,
command: 'build'
Expand Down Expand Up @@ -471,30 +472,24 @@ export async function runOptimizeDeps(
flatIdToExports[flatId] = exportsData
}

const define: Record<string, string> = {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || config.mode)
}
for (const key in config.define) {
const value = config.define[key]
define[key] = typeof value === 'string' ? value : JSON.stringify(value)
}

const start = performance.now()

const result = await build({
absWorkingDir: process.cwd(),
entryPoints: Object.keys(flatIdDeps),
bundle: true,
// Ensure resolution is handled by esbuildDepPlugin and
// avoid replacing `process.env.NODE_ENV` for 'browser'
platform: 'neutral',
format: 'esm',
target: config.build.target || undefined,
external: config.optimizeDeps?.exclude,
logLevel: 'error',
splitting: true,
sourcemap: true,
outdir: processingCacheDir,
ignoreAnnotations: resolvedConfig.command !== 'build',
ignoreAnnotations: !isBuild,
metafile: true,
define,
plugins: [
...plugins,
esbuildDepPlugin(flatIdDeps, flatIdToExports, config)
Expand Down Expand Up @@ -887,7 +882,6 @@ export function getDepHash(config: ResolvedConfig): string {
{
mode: process.env.NODE_ENV || config.mode,
root: config.root,
define: config.define,
resolve: config.resolve,
buildTarget: config.build.target,
assetsInclude: config.assetsInclude,
Expand Down
3 changes: 3 additions & 0 deletions playground/define/__tests__/define.spec.ts
Expand Up @@ -40,4 +40,7 @@ test('string', async () => {
// html would't need to define replacement
expect(await page.textContent('.exp-define')).toBe('__EXP__')
expect(await page.textContent('.import-json')).toBe('__EXP__')
expect(await page.textContent('.define-in-dep')).toBe(
defines.__STRINGIFIED_OBJ__
)
})
1 change: 1 addition & 0 deletions playground/define/commonjs-dep/index.js
@@ -0,0 +1 @@
module.exports = { defined: __STRINGIFIED_OBJ__ }
6 changes: 6 additions & 0 deletions playground/define/commonjs-dep/package.json
@@ -0,0 +1,6 @@
{
"name": "commonjs-dep",
"private": true,
"version": "1.0.0",
"type": "commonjs"
}
4 changes: 4 additions & 0 deletions playground/define/index.html
Expand Up @@ -16,6 +16,7 @@ <h1>Define</h1>
<p>no identifier substring: <span class="no-identifier-substring"></span></p>
<p>define variable in html: <code class="exp-define">__EXP__</code></p>
<p>import json: <code class="import-json"></code></p>
<p>define in dep: <code class="define-in-dep"></code></p>

<script type="module">
const __VAR_NAME__ = true // ensure define doesn't replace var name
Expand Down Expand Up @@ -50,6 +51,9 @@ <h1>Define</h1>
function text(el, text) {
document.querySelector(el).textContent = text
}

import { defined } from 'dep'
text('.define-in-dep', JSON.stringify(defined))
</script>

<style>
Expand Down
3 changes: 3 additions & 0 deletions playground/define/package.json
Expand Up @@ -7,5 +7,8 @@
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"dep": "file:./commonjs-dep"
}
}
3 changes: 2 additions & 1 deletion playground/define/vite.config.js
Expand Up @@ -19,6 +19,7 @@ module.exports = {
'process.env.SOMEVAR': '"SOMEVAR"',
$DOLLAR: 456,
ÖUNICODE_LETTERɵ: 789,
__VAR_NAME__: false
__VAR_NAME__: false,
__STRINGIFIED_OBJ__: JSON.stringify({ foo: true })
}
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.