Skip to content

Commit

Permalink
fix(bundler): be resilient on malformed main/module/browser fields in…
Browse files Browse the repository at this point in the history
… package.json

closes #934
  • Loading branch information
3cp committed Oct 12, 2018
1 parent 23be17f commit 3a2143c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/build/package-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ function determineLoaderConfig(project, description) {
if (typeof metadata.browser === 'string') {
// use package.json browser field if possible.
metaMain = metadata.browser;
} else if (metadata.module) {
} else if (typeof metadata.module === 'string') {
// prefer es module format over cjs, just like webpack.
// this improves compatibility with TypeScript.
metaMain = metadata.module;
} else {
} else if (typeof metadata.main === 'string') {
metaMain = metadata.main;
}
}
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/build/package-analyzer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,25 @@ describe('The PackageAnalyzer', () => {
.catch(e => done.fail(e));
});

it('infers index.js as main file where package.json has malformed main property', done => {
// setup mock package.json
const fsConfig = {};
let json = '{ "name": "my-package", "main": ["some.js"] }';
fsConfig[path.join('node_modules/my-package', 'package.json')] = json;
fsConfig[path.join('node_modules/my-package', 'index.js')] = 'some-content';
fsConfig[project.paths.root] = {};
mockfs(fsConfig);

sut.analyze('my-package')
.then(description => {
expect(description.loaderConfig.name).toBe('my-package');
expect(description.loaderConfig.path).toBe(path.join('..', 'node_modules', 'my-package'));
expect(description.loaderConfig.main).toBe('index');
done();
})
.catch(e => done.fail(e));
});

it('analyze() rejects when there is no package.json.', done => {
// setup mock package.json
const fsConfig = {};
Expand Down

0 comments on commit 3a2143c

Please sign in to comment.