Skip to content

Commit 8b829b1

Browse files
committed
fix #4407: incorrect error for inject edge case
1 parent 4384bad commit 8b829b1

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@
2929
}
3030
```
3131

32+
* Fix an edge case with the `inject` feature ([#4407](https://github.com/evanw/esbuild/issues/4407))
33+
34+
This release fixes an edge case where esbuild's `inject` feature could not be used with arbitrary module namespace names exported using an `export {} from` statement with bundling disabled and a target environment where arbitrary module namespace names is unsupported.
35+
36+
With the fix, the following `inject` file:
37+
38+
```js
39+
import jquery from 'jquery';
40+
export { jquery as 'window.jQuery' };
41+
```
42+
43+
Can now always be rewritten as this without esbuild sometimes incorrectly generating an error:
44+
45+
```js
46+
export { default as 'window.jQuery' } from 'jquery';
47+
```
48+
3249
## 0.27.3
3350

3451
* Preserve URL fragments in data URLs ([#4370](https://github.com/evanw/esbuild/issues/4370))

internal/bundler_tests/bundler_default_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9110,7 +9110,7 @@ func TestForbidStringExportNamesNoBundle(t *testing.T) {
91109110
UnsupportedJSFeatures: compat.ArbitraryModuleNamespaceNames,
91119111
},
91129112
expectedCompileLog: `entry.js: ERROR: Using the string "not ok" as an export name is not supported in the configured target environment
9113-
entry.js: ERROR: Using the string "same name" as an export name is not supported in the configured target environment
9113+
entry.js: ERROR: Using the string "same name" as an import name is not supported in the configured target environment
91149114
entry.js: ERROR: Using the string "name 1" as an import name is not supported in the configured target environment
91159115
entry.js: ERROR: Using the string "name 2" as an export name is not supported in the configured target environment
91169116
entry.js: ERROR: Using the string "name space" as an export name is not supported in the configured target environment
@@ -9232,6 +9232,26 @@ func TestInjectWithStringExportNameBundle(t *testing.T) {
92329232
})
92339233
}
92349234

9235+
func TestInjectWithStringReExportNameNoBundle(t *testing.T) {
9236+
default_suite.expectBundled(t, bundled{
9237+
files: map[string]string{
9238+
"/entry.js": `
9239+
console.log(test)
9240+
`,
9241+
"/inject.js": `
9242+
export { fn as "console.log" } from 'pkg'
9243+
`,
9244+
},
9245+
entryPaths: []string{"/entry.js"},
9246+
options: config.Options{
9247+
Mode: config.ModePassThrough,
9248+
AbsOutputFile: "/out.js",
9249+
InjectPaths: []string{"/inject.js"},
9250+
UnsupportedJSFeatures: compat.ArbitraryModuleNamespaceNames,
9251+
},
9252+
})
9253+
}
9254+
92359255
func TestStringExportNamesCommonJS(t *testing.T) {
92369256
default_suite.expectBundled(t, bundled{
92379257
files: map[string]string{

internal/bundler_tests/snapshots/snapshots_default.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,12 @@ var old = console.log;
22492249
var fn = (...args) => old.apply(console, ["log:"].concat(args));
22502250
fn(test);
22512251

2252+
================================================================================
2253+
TestInjectWithStringReExportNameNoBundle
2254+
---------- /out.js ----------
2255+
import { fn } from "pkg";
2256+
fn(test);
2257+
22522258
================================================================================
22532259
TestJSXAutomaticImportsCommonJS
22542260
---------- /out.js ----------

internal/linker/linker.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4467,10 +4467,7 @@ func (c *linkerContext) convertStmtsForChunk(sourceIndex uint32, stmtList *stmtL
44674467

44684468
if c.options.UnsupportedJSFeatures.Has(compat.ArbitraryModuleNamespaceNames) {
44694469
for _, item := range s.Items {
4470-
c.maybeForbidArbitraryModuleNamespaceIdentifier("export", sourceIndex, item.AliasLoc, item.Alias)
4471-
if item.AliasLoc != item.Name.Loc {
4472-
c.maybeForbidArbitraryModuleNamespaceIdentifier("import", sourceIndex, item.Name.Loc, item.OriginalName)
4473-
}
4470+
c.maybeForbidArbitraryModuleNamespaceIdentifier("import", sourceIndex, item.Name.Loc, item.OriginalName)
44744471
}
44754472
}
44764473

@@ -4485,6 +4482,12 @@ func (c *linkerContext) convertStmtsForChunk(sourceIndex uint32, stmtList *stmtL
44854482
ImportRecordIndex: s.ImportRecordIndex,
44864483
IsSingleLine: s.IsSingleLine,
44874484
}
4485+
} else if c.options.UnsupportedJSFeatures.Has(compat.ArbitraryModuleNamespaceNames) {
4486+
for _, item := range s.Items {
4487+
if item.AliasLoc != item.Name.Loc {
4488+
c.maybeForbidArbitraryModuleNamespaceIdentifier("export", sourceIndex, item.AliasLoc, item.Alias)
4489+
}
4490+
}
44884491
}
44894492

44904493
// Make sure these don't end up in the wrapper closure

0 commit comments

Comments
 (0)