Skip to content

Commit

Permalink
fix: import.meta.env and process.env undefined variable replacement (fix
Browse files Browse the repository at this point in the history
 #8663) (#10958)

Co-authored-by: bluwy <bjornlu.dev@gmail.com>
fix #8663
  • Loading branch information
julienv3 authored Nov 28, 2022
1 parent ac2cfd6 commit 3e0cd3d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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

0 comments on commit 3e0cd3d

Please sign in to comment.