From 199a0d38e4e4191e970f2a0a25e50e5c7ae36464 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Fri, 20 Dec 2024 11:43:45 -0500 Subject: [PATCH] close #4013: credit to @sapphi-red for the fix --- CHANGELOG.md | 4 +++- scripts/js-api-tests.js | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a111ed4be..436fc66204 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/scripts/js-api-tests.js b/scripts/js-api-tests.js index cdd8021612..283f717cb0 100644 --- a/scripts/js-api-tests.js +++ b/scripts/js-api-tests.js @@ -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' } }) @@ -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']` }, @@ -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"