Skip to content

Commit

Permalink
fix #3546: don't transform require glob imports
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 18, 2023
1 parent e1b7050 commit 00c4ebe
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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.

* Fix `require()` glob imports with bundling disabled ([#3546](https://github.com/evanw/esbuild/issues/3546))

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.

* 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))

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:
Expand Down
26 changes: 26 additions & 0 deletions internal/bundler_tests/bundler_glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ var glob_suite = suite{
name: "glob",
}

func TestGlobBasicNoBundle(t *testing.T) {
glob_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
const ab = Math.random() < 0.5 ? 'a.js' : 'b.js'
console.log({
concat: {
require: require('./src/' + ab),
import: import('./src/' + ab),
},
template: {
require: require(` + "`./src/${ab}`" + `),
import: import(` + "`./src/${ab}`" + `),
},
})
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeConvertFormat,
OutputFormat: config.FormatCommonJS,
AbsOutputFile: "/out.js",
},
})
}

func TestGlobBasicNoSplitting(t *testing.T) {
glob_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
15 changes: 15 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_glob.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
TestGlobBasicNoBundle
---------- /out.js ----------
const ab = Math.random() < 0.5 ? "a.js" : "b.js";
console.log({
concat: {
require: require("./src/" + ab),
import: import("./src/" + ab)
},
template: {
require: require(`./src/${ab}`),
import: import(`./src/${ab}`)
}
});

================================================================================
TestGlobBasicNoSplitting
---------- /out.js ----------
// src/a.js
Expand Down
6 changes: 4 additions & 2 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14790,8 +14790,10 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
}

// Handle glob patterns
if value := p.handleGlobPattern(arg, ast.ImportRequire, "globRequire", nil); value.Data != nil {
return value
if p.options.mode == config.ModeBundle {
if value := p.handleGlobPattern(arg, ast.ImportRequire, "globRequire", nil); value.Data != nil {
return value
}
}

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

0 comments on commit 00c4ebe

Please sign in to comment.