Skip to content

Commit 199a0d3

Browse files
committed
close #4013: credit to @sapphi-red for the fix
1 parent 947f99f commit 199a0d3

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
## Unreleased
44

5-
* Fix regression with `--define` and `import.meta` ([#4010](https://github.com/evanw/esbuild/issues/4010), [#4012](https://github.com/evanw/esbuild/issues/4012))
5+
* Fix regression with `--define` and `import.meta` ([#4010](https://github.com/evanw/esbuild/issues/4010), [#4012](https://github.com/evanw/esbuild/issues/4012), [#4013](https://github.com/evanw/esbuild/pull/4013))
66

77
The previous change in version 0.24.1 to use a more expression-like parser for `define` values to allow quoted property names introduced a regression that removed the ability to use `--define:import.meta=...`. Even though `import` is normally a keyword that can't be used as an identifier, ES modules special-case the `import.meta` expression to behave like an identifier anyway. This change fixes the regression.
88

9+
This fix was contributed by [@sapphi-red](https://github.com/sapphi-red).
10+
911
## 0.24.1
1012

1113
* Allow `es2024` as a target in `tsconfig.json` ([#4004](https://github.com/evanw/esbuild/issues/4004))

scripts/js-api-tests.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -6188,7 +6188,7 @@ class Foo {
61886188
},
61896189

61906190
async defineQuotedPropertyNameTransform({ esbuild }) {
6191-
const { code: code1 } = await esbuild.transform(`return x.y['z']`, { define: { 'x.y["z"]': 'true' } })
6191+
const { code: code1 } = await esbuild.transform(`return x.y['z!']`, { define: { 'x.y["z!"]': 'true' } })
61926192
assert.strictEqual(code1, `return true;\n`)
61936193

61946194
const { code: code2 } = await esbuild.transform(`foo(x['y'].z, x.y['z'], x['y']['z'])`, { define: { 'x.y.z': 'true' } })
@@ -6202,8 +6202,21 @@ class Foo {
62026202

62036203
const { code: code5 } = await esbuild.transform(`foo(x['y'].z, x.y['z'], x['y']['z'])`, { define: { 'x["y"][\'z\']': 'true' } })
62046204
assert.strictEqual(code5, `foo(true, true, true);\n`)
6205+
6206+
const { code: code6 } = await esbuild.transform(`foo(import.meta['y'].z, import.meta.y['z'], import.meta['y']['z'])`, { define: { 'import.meta["y"].z': 'true' } })
6207+
assert.strictEqual(code6, `foo(true, true, true);\n`)
6208+
6209+
const { code: code7 } = await esbuild.transform(`foo(import.meta['y!'].z, import.meta.y['z!'], import.meta['y!']['z!'])`, {
6210+
define: {
6211+
'import.meta["y!"].z': 'true',
6212+
'import.meta.y["z!"]': 'true',
6213+
'import.meta["y!"]["z!"]': 'true'
6214+
},
6215+
})
6216+
assert.strictEqual(code7, `foo(true, true, true);\n`)
62056217
},
62066218

6219+
62076220
async defineQuotedPropertyNameBuild({ esbuild }) {
62086221
const { outputFiles } = await esbuild.build({
62096222
stdin: { contents: `return process.env['SOME-TEST-VAR']` },
@@ -6548,6 +6561,14 @@ class Foo {
65486561
assert.strictEqual(code2, `foo;\n`)
65496562
},
65506563

6564+
async pureImportMeta({ esbuild }) {
6565+
const { code: code1 } = await esbuild.transform(`import.meta.foo(123, foo)`, { minifySyntax: true, pure: [] })
6566+
assert.strictEqual(code1, `import.meta.foo(123, foo);\n`)
6567+
6568+
const { code: code2 } = await esbuild.transform(`import.meta.foo(123, foo)`, { minifySyntax: true, pure: ['import.meta.foo'] })
6569+
assert.strictEqual(code2, `foo;\n`)
6570+
},
6571+
65516572
async nameCollisionEvalRename({ esbuild }) {
65526573
const { code } = await esbuild.transform(`
65536574
// "arg" must not be renamed to "arg2"

0 commit comments

Comments
 (0)