Skip to content

Commit

Permalink
fix #423: ignore invalid "main" in "package.json"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Oct 3, 2020
1 parent 148684f commit 94268d4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Recover from bad `main` field in `package.json` ([#423](https://github.com/evanw/esbuild/issues/423))

Some packages are published with invalid information in the `main` field of `package.json`. In that case, path resolution should fall back to searching for a file named `index.js` before giving up. This matters for the `simple-exiftool` package, for example.

## 0.7.9

* Fixed panic when using a `url()` import in CSS with the `--metafile` option
Expand Down
26 changes: 26 additions & 0 deletions internal/bundler/bundler_packagejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ func TestPackageJsonMain(t *testing.T) {
})
}

func TestPackageJsonBadMain(t *testing.T) {
packagejson_suite.expectBundled(t, bundled{
files: map[string]string{
"/Users/user/project/src/entry.js": `
import fn from 'demo-pkg'
console.log(fn())
`,
"/Users/user/project/node_modules/demo-pkg/package.json": `
{
"main": "./does-not-exist.js"
}
`,
"/Users/user/project/node_modules/demo-pkg/index.js": `
module.exports = function() {
return 123
}
`,
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/Users/user/project/out.js",
},
})
}

func TestPackageJsonSyntaxErrorComment(t *testing.T) {
packagejson_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
14 changes: 14 additions & 0 deletions internal/bundler/snapshots/snapshots_packagejson.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
TestPackageJsonBadMain
---------- /Users/user/project/out.js ----------
// /Users/user/project/node_modules/demo-pkg/index.js
var require_demo_pkg = __commonJS((exports, module) => {
module.exports = function() {
return 123;
};
});

// /Users/user/project/src/entry.js
const demo_pkg = __toModule(require_demo_pkg());
console.log(demo_pkg.default());

================================================================================
TestPackageJsonBrowserMapAvoidMissing
---------- /Users/user/project/out.js ----------
// /Users/user/project/node_modules/component-indexof/index.js
Expand Down
9 changes: 3 additions & 6 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,12 +790,9 @@ func (r *resolver) dirInfoUncached(path string) *dirInfo {
info.tsConfigJson = parentInfo.tsConfigJson
}

// Are all main fields from "package.json" missing?
if info.packageJson == nil || info.packageJson.absMainFields == nil {
// Look for an "index" file with known extensions
if absolute, ok := r.loadAsIndex(path, entries); ok {
info.absPathIndex = &absolute
}
// Look for an "index" file with known extensions
if absolute, ok := r.loadAsIndex(path, entries); ok {
info.absPathIndex = &absolute
}

return info
Expand Down

0 comments on commit 94268d4

Please sign in to comment.