fix #423: ignore invalid "main" in "package.json"

This commit is contained in:
Evan Wallace
2020-10-03 10:26:36 -07:00
parent 148684f949
commit 94268d4278
4 changed files with 49 additions and 6 deletions

View File

@@ -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

View File

@@ -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{

View File

@@ -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

View File

@@ -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