Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sub-path module resolve wrong with pkg browser field #2001

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
bin
esbuild.exe
node_modules/
.idea/
.vscode/
42 changes: 42 additions & 0 deletions internal/bundler/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,48 @@ NOTE: The package "fs" wasn't found on the file system but is built into node. A
})
}

func TestSubImportModuleWithPkgBrowser(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import { v } from "pkg/sub";
console.log(v);
`,
"/node_modules/pkg/package.json": `{ "browser": { "./sub": "./sub/index.js" } }`,
"/node_modules/pkg/sub/index.js": `
export { version as v } from "sub";
`,
"/node_modules/sub/index.js": `export const version = 123`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
}

func TestSubImportModulePkgBrowser(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import { v } from "pkg/sub";
console.log(v);
`,
"/node_modules/pkg/package.json": `{ "browser": { "./sub": "./sub/index.js" } }`,
"/node_modules/pkg/sub/index.js": `
export { version as v } from "sub2";
`,
"/node_modules/sub2/index.js": `export const version = 123`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
}

func TestImportFSNodeCommonJS(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
18 changes: 18 additions & 0 deletions internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,24 @@ TestRequireFSNodeMinify
---------- /out.js ----------
return require("fs");

================================================================================
TestSubImportModuleWithPkgBrowser
---------- /out.js ----------
// node_modules/sub/index.js
var version = 123;

// entry.js
console.log(version);

================================================================================
TestSubImportModulePkgBrowser
---------- /out.js ----------
// node_modules/sub2/index.js
var version = 123;

// entry.js
console.log(version);

================================================================================
TestRequireJson
---------- /out.js ----------
Expand Down
2 changes: 1 addition & 1 deletion internal/resolver/package_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (r resolverQuery) checkBrowserMap(resolveDirInfo *dirInfo, inputPath string
}
}
if isInSamePackage {
checkPath("./" + inputPath)
checkPath(inputPath)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,17 @@
'node_modules/pkg/package.json': `{ "browser": { "./pkg2": "./file" } }`,
'node_modules/pkg/file.js': `var works = true`,
}),
test(['entry.js', '--bundle', '--outfile=node.js'], {
'entry.js': `
import { v } from "pkg/sub"
console.log(v)
`,
'node_modules/pkg/package.json': `{ "browser": { "./sub": "./sub/index.js" } }`,
'node_modules/pkg/sub/index.js': `
export { version as v } from "sub";
`,
'node_modules/sub/index.js': `export const version = 123`,
}),
)

// Test arbitrary module namespace identifier names
Expand Down