Skip to content

Commit 5870809

Browse files
committed
fix #3367: ignore ESM flag for disabled modules
1 parent d5f397f commit 5870809

4 files changed

Lines changed: 54 additions & 0 deletions

File tree

CHANGELOG.md

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

3030
In the future, CSS `url()` tokens may contain additional stuff after the URL. This is irrelevant today as no CSS specification does this. But esbuild previously had a bug where using these tokens in an `@import` rule resulted in malformed output. This bug has been fixed.
3131

32+
* Fix `browser` + `false` + `type: module` in `package.json` ([#3367](https://github.com/evanw/esbuild/issues/3367))
33+
34+
The `browser` field in `package.json` allows you to map a file to `false` to have it be treated as an empty file when bundling for the browser. However, if `package.json` contains `"type": "module"` then all `.js` files will be considered ESM, not CommonJS. Importing a named import from an empty CommonJS file gives you undefined, but importing a named export from an empty ESM file is a build error. This release changes esbuild's interpretation of these files mapped to `false` in this situation from ESM to CommonJS to avoid generating build errors for named imports.
35+
3236
* Fix a bug in top-level await error reporting ([#3400](https://github.com/evanw/esbuild/issues/3400))
3337

3438
Using `require()` on a file that contains [top-level await](https://v8.dev/features/top-level-await) is not allowed because `require()` must return synchronously and top-level await makes that impossible. You will get a build error if you try to bundle code that does this with esbuild. This release fixes a bug in esbuild's error reporting code for complex cases of this situation involving multiple levels of imports to get to the module containing the top-level await.

internal/bundler_tests/bundler_packagejson_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,3 +2841,35 @@ NOTE: You can mark the path "msw/browser" as external to exclude it from the bun
28412841
`,
28422842
})
28432843
}
2844+
2845+
// See: https://github.com/evanw/esbuild/issues/3367
2846+
func TestPackageJsonDisabledTypeModuleIssue3367(t *testing.T) {
2847+
packagejson_suite.expectBundled(t, bundled{
2848+
files: map[string]string{
2849+
"/entry.js": `
2850+
import foo from 'foo'
2851+
foo()
2852+
`,
2853+
"/package.json": `
2854+
{
2855+
"browser": {
2856+
"foo": false
2857+
}
2858+
}
2859+
`,
2860+
"/node_modules/foo/package.json": `
2861+
{
2862+
"type": "module"
2863+
}
2864+
`,
2865+
"/node_modules/foo/index.js": `
2866+
export default function() {}
2867+
`,
2868+
},
2869+
entryPaths: []string{"/entry.js"},
2870+
options: config.Options{
2871+
Mode: config.ModeBundle,
2872+
AbsOutputFile: "/out.js",
2873+
},
2874+
})
2875+
}

internal/bundler_tests/snapshots/snapshots_packagejson.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,19 @@ function main_browser_esm_default() {
415415
// Users/user/project/src/entry.js
416416
console.log(main_browser_esm_default());
417417

418+
================================================================================
419+
TestPackageJsonDisabledTypeModuleIssue3367
420+
---------- /out.js ----------
421+
// (disabled):node_modules/foo/index.js
422+
var require_foo = __commonJS({
423+
"(disabled):node_modules/foo/index.js"() {
424+
}
425+
});
426+
427+
// entry.js
428+
var import_foo = __toESM(require_foo());
429+
(0, import_foo.default)();
430+
418431
================================================================================
419432
TestPackageJsonDualPackageHazardImportAndRequireBrowser
420433
---------- /Users/user/project/out.js ----------

internal/resolver/resolver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,11 @@ func (r resolverQuery) finalizeResolve(result *ResolveResult) {
889889
continue
890890
}
891891

892+
// Path attributes are not taken from disabled files
893+
if path.IsDisabled() {
894+
continue
895+
}
896+
892897
// Look up this file in the "sideEffects" map in the nearest enclosing
893898
// directory with a "package.json" file.
894899
//

0 commit comments

Comments
 (0)