Skip to content

Commit

Permalink
fix #3887: omit dead export warning for default
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Sep 22, 2024
1 parent 6e049b8 commit 045a87f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
33 changes: 33 additions & 0 deletions internal/bundler_tests/bundler_packagejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3002,3 +3002,36 @@ node_modules/foo/package.json: NOTE: The "default" condition comes earlier and w
`,
})
}

// See: https://github.com/evanw/esbuild/issues/3887
func TestPackageJsonExportsDefaultWarningIssue3887(t *testing.T) {
packagejson_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import "foo"
`,
"/node_modules/foo/dist/index.js": `
success()
`,
"/node_modules/foo/package.json": `
{
"exports": {
".": {
"node": "./dist/index.js",
"require": "./dist/index.js",
"import": "./dist/index.esm.js",
"default": "./dist/index.esm.js"
}
}
}
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
Platform: config.PlatformNode,
AbsOutputFile: "/out.js",
},
debugLogs: true,
})
}
6 changes: 6 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_packagejson.txt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,12 @@ TestPackageJsonExportsDefaultOverImportAndRequire
// Users/user/project/node_modules/pkg/default.js
console.log("SUCCESS");

================================================================================
TestPackageJsonExportsDefaultWarningIssue3887
---------- /out.js ----------
// node_modules/foo/dist/index.js
success();

================================================================================
TestPackageJsonExportsEntryPointImportOverRequire
---------- /out.js ----------
Expand Down
3 changes: 2 additions & 1 deletion internal/resolver/package_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ func parseImportsExportsMap(source logger.Source, log logger.Log, json js_ast.Ex
// Track "dead" conditional branches that can never be reached
if foundDefault.Len != 0 || (foundImport.Len != 0 && foundRequire.Len != 0) {
deadCondition.ranges = append(deadCondition.ranges, keyRange)
if deadCondition.reason == "" {
// Note: Don't warn about the "default" condition as it's supposed to be a catch-all condition
if deadCondition.reason == "" && key != "default" {
if foundDefault.Len != 0 {
deadCondition.reason = "\"default\""
deadCondition.notes = []logger.MsgData{
Expand Down

0 comments on commit 045a87f

Please sign in to comment.