Skip to content

Commit 00c4ebe

Browse files
committed
fix #3546: don't transform require glob imports
1 parent e1b7050 commit 00c4ebe

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
This release fixes a problem where bundling a TypeScript file containing a glob import could emit a call to a helper function that doesn't exist. The problem happened because esbuild's TypeScript transformation removes unused imports (which is required for correctness, as they may be type-only imports) and esbuild's glob import transformation wasn't correctly marking the imported helper function as used. This wasn't caught earlier because most of esbuild's glob import tests were written in JavaScript, not in TypeScript.
88

9+
* Fix `require()` glob imports with bundling disabled ([#3546](https://github.com/evanw/esbuild/issues/3546))
10+
11+
Previously `require()` calls containing glob imports were incorrectly transformed when bundling was disabled. All glob imports should only be transformed when bundling is enabled. This bug has been fixed.
12+
913
* Fix a panic when transforming optional chaining with `define` ([#3551](https://github.com/evanw/esbuild/issues/3551), [#3554](https://github.com/evanw/esbuild/pull/3554))
1014

1115
This release fixes a case where esbuild could crash with a panic, which was triggered by using `define` to replace an expression containing an optional chain. Here is an example:

internal/bundler_tests/bundler_glob_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ var glob_suite = suite{
1010
name: "glob",
1111
}
1212

13+
func TestGlobBasicNoBundle(t *testing.T) {
14+
glob_suite.expectBundled(t, bundled{
15+
files: map[string]string{
16+
"/entry.js": `
17+
const ab = Math.random() < 0.5 ? 'a.js' : 'b.js'
18+
console.log({
19+
concat: {
20+
require: require('./src/' + ab),
21+
import: import('./src/' + ab),
22+
},
23+
template: {
24+
require: require(` + "`./src/${ab}`" + `),
25+
import: import(` + "`./src/${ab}`" + `),
26+
},
27+
})
28+
`,
29+
},
30+
entryPaths: []string{"/entry.js"},
31+
options: config.Options{
32+
Mode: config.ModeConvertFormat,
33+
OutputFormat: config.FormatCommonJS,
34+
AbsOutputFile: "/out.js",
35+
},
36+
})
37+
}
38+
1339
func TestGlobBasicNoSplitting(t *testing.T) {
1440
glob_suite.expectBundled(t, bundled{
1541
files: map[string]string{

internal/bundler_tests/snapshots/snapshots_glob.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
TestGlobBasicNoBundle
2+
---------- /out.js ----------
3+
const ab = Math.random() < 0.5 ? "a.js" : "b.js";
4+
console.log({
5+
concat: {
6+
require: require("./src/" + ab),
7+
import: import("./src/" + ab)
8+
},
9+
template: {
10+
require: require(`./src/${ab}`),
11+
import: import(`./src/${ab}`)
12+
}
13+
});
14+
15+
================================================================================
116
TestGlobBasicNoSplitting
217
---------- /out.js ----------
318
// src/a.js

internal/js_parser/js_parser.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14790,8 +14790,10 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1479014790
}
1479114791

1479214792
// Handle glob patterns
14793-
if value := p.handleGlobPattern(arg, ast.ImportRequire, "globRequire", nil); value.Data != nil {
14794-
return value
14793+
if p.options.mode == config.ModeBundle {
14794+
if value := p.handleGlobPattern(arg, ast.ImportRequire, "globRequire", nil); value.Data != nil {
14795+
return value
14796+
}
1479514797
}
1479614798

1479714799
// Use a debug log so people can see this if they want to

0 commit comments

Comments
 (0)