From 94268d4278f933c6185431e153ae91213b3dc745 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sat, 3 Oct 2020 10:26:36 -0700 Subject: [PATCH] fix #423: ignore invalid "main" in "package.json" --- CHANGELOG.md | 6 +++++ internal/bundler/bundler_packagejson_test.go | 26 +++++++++++++++++++ .../snapshots/snapshots_packagejson.txt | 14 ++++++++++ internal/resolver/resolver.go | 9 +++---- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd474961aba..52109ab0160 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/bundler/bundler_packagejson_test.go b/internal/bundler/bundler_packagejson_test.go index 996054970cd..a9964fbe7ed 100644 --- a/internal/bundler/bundler_packagejson_test.go +++ b/internal/bundler/bundler_packagejson_test.go @@ -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{ diff --git a/internal/bundler/snapshots/snapshots_packagejson.txt b/internal/bundler/snapshots/snapshots_packagejson.txt index bf6761b4b40..70781a7d361 100644 --- a/internal/bundler/snapshots/snapshots_packagejson.txt +++ b/internal/bundler/snapshots/snapshots_packagejson.txt @@ -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 diff --git a/internal/resolver/resolver.go b/internal/resolver/resolver.go index 944da2d0d2b..8bf7fd61f1b 100644 --- a/internal/resolver/resolver.go +++ b/internal/resolver/resolver.go @@ -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