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: import.meta.env and process.env undefined variable replacement (fix #8663) #10958

Merged
merged 3 commits into from Nov 28, 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
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/define.ts
Expand Up @@ -85,7 +85,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
.join('|') +
// Mustn't be followed by a char that can be part of an identifier
// or an assignment (but allow equality operators)
')(?![\\p{L}\\p{N}_$]|\\s*?=[^=])',
')(?:(?<=\\.)|(?![\\p{L}\\p{N}_$]|\\s*?=[^=]))',
'gu'
)
: null
Expand Down
8 changes: 7 additions & 1 deletion playground/define/__tests__/define.spec.ts
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import viteConfig from '../vite.config'
import { page } from '~utils'
import { isBuild, page } from '~utils'

test('string', async () => {
const defines = viteConfig.define
Expand Down Expand Up @@ -44,4 +44,10 @@ test('string', async () => {
expect(await page.textContent('.define-in-dep')).toBe(
defines.__STRINGIFIED_OBJ__
)
expect(await page.textContent('.import-meta-env-undefined')).toBe(
isBuild ? '({}).UNDEFINED' : 'import.meta.env.UNDEFINED'
)
expect(await page.textContent('.process-env-undefined')).toBe(
isBuild ? '({}).UNDEFINED' : 'process.env.UNDEFINED'
)
})
6 changes: 5 additions & 1 deletion playground/define/commonjs-dep/index.js
@@ -1 +1,5 @@
module.exports = { defined: __STRINGIFIED_OBJ__ }
module.exports = {
defined: __STRINGIFIED_OBJ__,
importMetaEnvUndefined: 'import.meta.env.UNDEFINED',
processEnvUndefined: 'process.env.UNDEFINED'
}
8 changes: 7 additions & 1 deletion playground/define/index.html
Expand Up @@ -17,6 +17,10 @@ <h1>Define</h1>
<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>
<p>
import.meta.env.UNDEFINED: <code class="import-meta-env-undefined"></code>
</p>
<p>process.env.UNDEFINED: <code class="process-env-undefined"></code></p>

<script type="module">
const __VAR_NAME__ = true // ensure define doesn't replace var name
Expand Down Expand Up @@ -52,8 +56,10 @@ <h1>Define</h1>
document.querySelector(el).textContent = text
}

import { defined } from 'dep'
import { defined, importMetaEnvUndefined, processEnvUndefined } from 'dep'
text('.define-in-dep', JSON.stringify(defined))
text('.import-meta-env-undefined', importMetaEnvUndefined)
text('.process-env-undefined', processEnvUndefined)
</script>

<style>
Expand Down