Skip to content

Commit

Permalink
fix(define): stringify object parse error in build mode (#13600)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day authored Jul 5, 2023
1 parent be4b0c0 commit 71516db
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
if (isBuild) {
const match = key.match(metaEnvRe)
if (match) {
userDefineEnv[match[1]] = `__vite__define__${userDefine[key]}`
userDefineEnv[match[1]] = `__vite__define__${key}__define__vite__`
}
}
}
Expand All @@ -62,8 +62,8 @@ export function definePlugin(config: ResolvedConfig): Plugin {
SSR: '__vite__ssr__',
...userDefineEnv,
}).replace(
/"__vite__define__(.+?)"([,}])/g,
(_, val, suffix) => `${val.replace(/(^\\")|(\\"$)/g, '"')}${suffix}`,
/"__vite__define__(.+?)__define__vite__"/g,
(_, key) => userDefine[key],
),
})
}
Expand Down
7 changes: 6 additions & 1 deletion playground/env/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ test('define', async () => {
expect(await page.textContent('.bool')).toBe('boolean')
expect(await page.textContent('.number')).toBe('number')
expect(await page.textContent('.string')).toBe('string')
expect(await page.textContent('.stringify-object')).toBe('object')
})

test('NODE_ENV', async () => {
Expand Down Expand Up @@ -82,7 +83,11 @@ test('env object', async () => {
DEV: !isBuild,
PROD: isBuild,
VITE_NUMBER: 123,
VITE_STRING: '123',
VITE_STRING: '{"123",}',
VITE_STRINGIFY_OBJECT: {
a: '1',
b: '2',
},
})
})

Expand Down
5 changes: 5 additions & 0 deletions playground/env/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ <h1>Environment Variables</h1>
<p>typeof import.meta.env.VITE_BOOL: <code class="bool"></code></p>
<p>typeof import.meta.env.VITE_NUMBER: <code class="number"></code></p>
<p>typeof import.meta.env.VITE_STRING: <code class="string"></code></p>
<p>
typeof import.meta.env.VITE_STRINGIFY_OBJECT:
<code class="stringify-object"></code>
</p>
<p>process.env.NODE_ENV: <code class="node-env"></code></p>
<p>global.process.env.NODE_ENV: <code class="global-node-env"></code></p>
<p>
Expand All @@ -38,6 +42,7 @@ <h1>Environment Variables</h1>
text('.bool', typeof import.meta.env.VITE_BOOL)
text('.number', typeof import.meta.env.VITE_NUMBER)
text('.string', typeof import.meta.env.VITE_STRING)
text('.stringify-object', typeof import.meta.env.VITE_STRINGIFY_OBJECT)
text('.ssr', import.meta.env.SSR)
text('.node-env', process.env.NODE_ENV)
text('.global-node-env', global.process.env.NODE_ENV)
Expand Down
3 changes: 2 additions & 1 deletion playground/env/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default defineConfig({
define: {
'import.meta.env.VITE_BOOL': true,
'import.meta.env.VITE_NUMBER': '123',
'import.meta.env.VITE_STRING': JSON.stringify('123'),
'import.meta.env.VITE_STRING': JSON.stringify('{"123",}'),
'import.meta.env.VITE_STRINGIFY_OBJECT': JSON.stringify({ a: '1', b: '2' }),
},
})

0 comments on commit 71516db

Please sign in to comment.