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
  • Loading branch information
julienv3 committed Nov 16, 2022
1 parent 02c334a commit 6424fe4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
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
7 changes: 7 additions & 0 deletions playground/define/__tests__/define.spec.ts
Expand Up @@ -3,6 +3,7 @@ import viteConfig from '../vite.config'
import { page } from '~utils'

test('string', async () => {
const isBuild = !!process.env.VITE_TEST_BUILD
const defines = viteConfig.define

expect(await page.textContent('.exp')).toBe(
Expand Down Expand Up @@ -44,4 +45,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

0 comments on commit 6424fe4

Please sign in to comment.