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): incorrect raw expression value type in build #13003

Merged
merged 1 commit into from Apr 30, 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
13 changes: 5 additions & 8 deletions packages/vite/src/node/plugins/define.ts
Expand Up @@ -41,13 +41,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
if (isBuild) {
const match = key.match(metaEnvRe)
if (match) {
userDefineEnv[match[1]] =
// test if value is raw identifier to wrap with __vite__ so when
// stringified for `import.meta.env`, we can remove the quotes and
// retain being an identifier
typeof val === 'string' && /^[\p{L}_$]/u.test(val.trim())
? `__vite__define__${val}`
: val
userDefineEnv[match[1]] = `__vite__define__${userDefine[key]}`
}
}
}
Expand All @@ -67,7 +61,10 @@ export function definePlugin(config: ResolvedConfig): Plugin {
...config.env,
SSR: '__vite__ssr__',
...userDefineEnv,
}).replace(/"__vite__define__(.+?)"/g, (_, val) => val),
}).replace(
/"__vite__define__(.+?)"([,}])/g,
(_, val, suffix) => `${val.replace(/(^\\")|(\\"$)/g, '"')}${suffix}`,
),
})
}

Expand Down
6 changes: 5 additions & 1 deletion playground/env/__tests__/env.spec.ts
Expand Up @@ -37,8 +37,10 @@ test('inline variables', async () => {
)
})

test('bool', async () => {
test('define', async () => {
expect(await page.textContent('.bool')).toBe('boolean')
expect(await page.textContent('.number')).toBe('number')
expect(await page.textContent('.string')).toBe('string')
})

test('NODE_ENV', async () => {
Expand Down Expand Up @@ -79,6 +81,8 @@ test('env object', async () => {
MODE: mode,
DEV: !isBuild,
PROD: isBuild,
VITE_NUMBER: 123,
VITE_STRING: '123',
})
})

Expand Down
4 changes: 4 additions & 0 deletions playground/env/index.html
Expand Up @@ -13,6 +13,8 @@ <h1>Environment Variables</h1>
</p>
<p>import.meta.env.VITE_INLINE: <code class="inline"></code></p>
<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>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 @@ -34,6 +36,8 @@ <h1>Environment Variables</h1>
text('.mode-file', import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME)
text('.inline', import.meta.env.VITE_INLINE)
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('.ssr', import.meta.env.SSR)
text('.node-env', process.env.NODE_ENV)
text('.global-node-env', global.process.env.NODE_ENV)
Expand Down
2 changes: 2 additions & 0 deletions playground/env/vite.config.js
Expand Up @@ -10,5 +10,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'),
},
})