Permalink
Browse files

feat(bundler): use package.json "module" (es module format) over "main"

Improves compatibility with TypeScript generated code.
Try 1.browser > 2.module > 3.main.
The order is to target browser.
When aurelia-cli introduces multi-targets build in future, it probably should use different order for electron app:
For electron 1.module > 2.browser > 3.main
  • Loading branch information...
huochunpeng committed Oct 4, 2018
1 parent 15af83f commit a3bc63aed4f7e042af431e0e69dc58e2042b4926
Showing with 32 additions and 3 deletions.
  1. +10 −1 lib/build/package-analyzer.js
  2. +22 −2 spec/lib/build/package-analyzer.spec.js
@@ -130,9 +130,18 @@ function determineLoaderConfig(project, description) {
let metaMain;
if (metadata) {
// try 1.browser > 2.module > 3.main
// the order is to target browser.
// when aurelia-cli introduces multi-targets build,
// it probably should use different order for electron app
// for electron 1.module > 2.browser > 3.main
if (typeof metadata.browser === 'string') {
// use package.json browser field if possible
// use package.json browser field if possible.
metaMain = metadata.browser;
} else if (metadata.module) {
// prefer es module format over cjs, just like webpack.
// this improves compatibility with TypeScript.
metaMain = metadata.module;
} else {
metaMain = metadata.main;
}
@@ -328,10 +328,10 @@ describe('The PackageAnalyzer', () => {
.catch(e => done.fail(e));
});
it('analyze() reads package.json as package metadata, respect browser field', done => {
it('analyze() reads package.json as package metadata, respect browser field over module/main', done => {
// setup mock package.json
const fsConfig = {};
fsConfig[path.join('node_modules/my-package', 'package.json')] = '{ "name": "my-package", "main": "index.js", "browser": "browser.js" }';
fsConfig[path.join('node_modules/my-package', 'package.json')] = '{ "name": "my-package", "main": "index.js", "browser": "browser.js", "module": "module.js" }';
fsConfig[path.join('node_modules/my-package', 'index.js')] = 'some-content';
fsConfig[path.join('node_modules/my-package', 'browser.js')] = 'some-content';
fsConfig[project.paths.root] = {};
@@ -347,6 +347,26 @@ describe('The PackageAnalyzer', () => {
.catch(e => done.fail(e));
});
it('analyze() reads package.json as package metadata, respect module field over main', done => {
// setup mock package.json
const fsConfig = {};
fsConfig[path.join('node_modules/my-package', 'package.json')] = '{ "name": "my-package", "main": "index.js", "module": "module.js"}';
fsConfig[path.join('node_modules/my-package', 'index.js')] = 'some-content';
fsConfig[path.join('node_modules/my-package', 'browser.js')] = 'some-content';
fsConfig[path.join('node_modules/my-package', 'module.js')] = 'some-content';
fsConfig[project.paths.root] = {};
mockfs(fsConfig);
sut.analyze('my-package')
.then(description => {
expect(description.metadata.name).toBe('my-package');
expect(description.loaderConfig.path).toBe('../node_modules/my-package');
expect(description.loaderConfig.main).toBe('module');
done();
})
.catch(e => done.fail(e));
});
it('analyze() supports parent node_modules folder', done => {
// setup mock package.json
const fsConfig = {};

0 comments on commit a3bc63a

Please sign in to comment.