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(define): stringify object parse error in build mode #13600

Merged
merged 2 commits into from
Jul 5, 2023
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
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' }),
},
})