Skip to content

Commit 223ddc6

Browse files
committed
fix #4187: browser package.json regression
1 parent b2c8251 commit 223ddc6

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
* Fix a regression with `browser` in `package.json` ([#4187](https://github.com/evanw/esbuild/issues/4187))
6+
7+
The fix to [#4144](https://github.com/evanw/esbuild/issues/4144) in version 0.25.3 introduced a regression that caused `browser` overrides specified in `package.json` to fail to override relative path names that end in a trailing slash. That behavior change affected the `[email protected]` package. This regression has been fixed, and now has test coverage.
8+
59
* Add support for certain keywords as TypeScript tuple labels ([#4192](https://github.com/evanw/esbuild/issues/4192))
610

711
Previously esbuild could incorrectly fail to parse certain keywords as TypeScript tuple labels that are parsed by the official TypeScript compiler if they were followed by a `?` modifier. These labels included `function`, `import`, `infer`, `new`, `readonly`, and `typeof`. With this release, these keywords will now be parsed correctly. Here's an example of some affected code:

internal/bundler_tests/bundler_packagejson_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,3 +3069,35 @@ func TestConfusingNameCollisionsIssue4144(t *testing.T) {
30693069
},
30703070
})
30713071
}
3072+
3073+
// https://github.com/evanw/esbuild/issues/4187
3074+
func TestPackageJsonBrowserMatchingTrailingSlashIssue4187(t *testing.T) {
3075+
packagejson_suite.expectBundled(t, bundled{
3076+
files: map[string]string{
3077+
"/entry.js": `
3078+
import axios from "axios"
3079+
`,
3080+
"/node_modules/axios/package.json": `
3081+
{
3082+
"browser": {
3083+
"./node/index.js": "./browser/index.js"
3084+
}
3085+
}
3086+
`,
3087+
"/node_modules/axios/index.js": `
3088+
module.exports = require('./node/');
3089+
`,
3090+
"/node_modules/axios/node/index.js": `
3091+
module.exports = { get: () => new Promise('Node') }
3092+
`,
3093+
"/node_modules/axios/browser/index.js": `
3094+
module.exports = { get: () => new Promise('Browser') }
3095+
`,
3096+
},
3097+
entryPaths: []string{"/entry.js"},
3098+
options: config.Options{
3099+
Mode: config.ModeBundle,
3100+
AbsOutputFile: "/out.js",
3101+
},
3102+
})
3103+
}

internal/bundler_tests/snapshots/snapshots_packagejson.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,26 @@ var require_main_browser = __commonJS({
308308
var import_demo_pkg = __toESM(require_main_browser());
309309
console.log((0, import_demo_pkg.default)());
310310

311+
================================================================================
312+
TestPackageJsonBrowserMatchingTrailingSlashIssue4187
313+
---------- /out.js ----------
314+
// node_modules/axios/browser/index.js
315+
var require_browser = __commonJS({
316+
"node_modules/axios/browser/index.js"(exports, module) {
317+
module.exports = { get: () => new Promise("Browser") };
318+
}
319+
});
320+
321+
// node_modules/axios/index.js
322+
var require_axios = __commonJS({
323+
"node_modules/axios/index.js"(exports, module) {
324+
module.exports = require_browser();
325+
}
326+
});
327+
328+
// entry.js
329+
var import_axios = __toESM(require_axios());
330+
311331
================================================================================
312332
TestPackageJsonBrowserNoExt
313333
---------- /Users/user/project/out.js ----------

internal/resolver/resolver.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,21 @@ func (r resolverQuery) resolveWithoutSymlinks(sourceDir string, sourceDirInfo *d
10121012
strings.HasSuffix(importPath, "/.") ||
10131013
strings.HasSuffix(importPath, "/..")
10141014

1015+
// Check the "browser" map
1016+
if importDirInfo := r.dirInfoCached(r.fs.Dir(absPath)); importDirInfo != nil {
1017+
if remapped, ok := r.checkBrowserMap(importDirInfo, absPath, absolutePathKind); ok {
1018+
if remapped == nil {
1019+
return &ResolveResult{PathPair: PathPair{Primary: logger.Path{Text: absPath, Namespace: "file", Flags: logger.PathDisabled}}}
1020+
}
1021+
if remappedResult, ok, diffCase, sideEffects := r.resolveWithoutRemapping(importDirInfo.enclosingBrowserScope, *remapped); ok {
1022+
result = ResolveResult{PathPair: remappedResult, DifferentCase: diffCase, PrimarySideEffectsData: sideEffects}
1023+
hasTrailingSlash = false
1024+
checkRelative = false
1025+
checkPackage = false
1026+
}
1027+
}
1028+
}
1029+
10151030
if hasTrailingSlash {
10161031
if absolute, ok, diffCase := r.loadAsDirectory(absPath); ok {
10171032
checkPackage = false
@@ -1020,20 +1035,6 @@ func (r resolverQuery) resolveWithoutSymlinks(sourceDir string, sourceDirInfo *d
10201035
return nil
10211036
}
10221037
} else {
1023-
// Check the "browser" map
1024-
if importDirInfo := r.dirInfoCached(r.fs.Dir(absPath)); importDirInfo != nil {
1025-
if remapped, ok := r.checkBrowserMap(importDirInfo, absPath, absolutePathKind); ok {
1026-
if remapped == nil {
1027-
return &ResolveResult{PathPair: PathPair{Primary: logger.Path{Text: absPath, Namespace: "file", Flags: logger.PathDisabled}}}
1028-
}
1029-
if remappedResult, ok, diffCase, sideEffects := r.resolveWithoutRemapping(importDirInfo.enclosingBrowserScope, *remapped); ok {
1030-
result = ResolveResult{PathPair: remappedResult, DifferentCase: diffCase, PrimarySideEffectsData: sideEffects}
1031-
checkRelative = false
1032-
checkPackage = false
1033-
}
1034-
}
1035-
}
1036-
10371038
if checkRelative {
10381039
if absolute, ok, diffCase := r.loadAsFileOrDirectory(absPath); ok {
10391040
checkPackage = false

0 commit comments

Comments
 (0)