Skip to content

Commit

Permalink
fix: define in build optimized deps (fix #8593)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Jun 15, 2022
1 parent 6234bcd commit dd68152
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 7 deletions.
20 changes: 14 additions & 6 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,12 +472,19 @@ 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)
let define: Record<string, string> | undefined
if (!isBuild) {
// We only use define for dev optimized deps. During build we let the define keys
// unchanged as the define plugin will process them
define = {
'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()
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.

0 comments on commit dd68152

Please sign in to comment.