Skip to content

Resolve wrong path when import sub-path module with browser field #2002

@ycjcl868

Description

@ycjcl868

Background:

// Browserify allows a browser map entry of "./pkg" to override a package
// path of "require('pkg')". This is weird, and arguably a bug. But we
// replicate this bug for compatibility. However, Browserify only allows
// this within the same package. It does not allow such an entry in a
// parent package to override this in a child package. So this behavior
// is disallowed if there is a "node_modules" folder in between the child
// package and the parent package.

Bad Test Cases:

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",
},
})
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions