Skip to content

Commit

Permalink
close #4013: credit to @sapphi-red for the fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 20, 2024
1 parent 947f99f commit 199a0d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

## Unreleased

* Fix regression with `--define` and `import.meta` ([#4010](https://github.com/evanw/esbuild/issues/4010), [#4012](https://github.com/evanw/esbuild/issues/4012))
* 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))

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.

This fix was contributed by [@sapphi-red](https://github.com/sapphi-red).

## 0.24.1

* Allow `es2024` as a target in `tsconfig.json` ([#4004](https://github.com/evanw/esbuild/issues/4004))
Expand Down
23 changes: 22 additions & 1 deletion scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6188,7 +6188,7 @@ class Foo {
},

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

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

const { code: code5 } = await esbuild.transform(`foo(x['y'].z, x.y['z'], x['y']['z'])`, { define: { 'x["y"][\'z\']': 'true' } })
assert.strictEqual(code5, `foo(true, true, true);\n`)

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' } })
assert.strictEqual(code6, `foo(true, true, true);\n`)

const { code: code7 } = await esbuild.transform(`foo(import.meta['y!'].z, import.meta.y['z!'], import.meta['y!']['z!'])`, {
define: {
'import.meta["y!"].z': 'true',
'import.meta.y["z!"]': 'true',
'import.meta["y!"]["z!"]': 'true'
},
})
assert.strictEqual(code7, `foo(true, true, true);\n`)
},


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

async pureImportMeta({ esbuild }) {
const { code: code1 } = await esbuild.transform(`import.meta.foo(123, foo)`, { minifySyntax: true, pure: [] })
assert.strictEqual(code1, `import.meta.foo(123, foo);\n`)

const { code: code2 } = await esbuild.transform(`import.meta.foo(123, foo)`, { minifySyntax: true, pure: ['import.meta.foo'] })
assert.strictEqual(code2, `foo;\n`)
},

async nameCollisionEvalRename({ esbuild }) {
const { code } = await esbuild.transform(`
// "arg" must not be renamed to "arg2"
Expand Down

0 comments on commit 199a0d3

Please sign in to comment.