Skip to content

Commit

Permalink
fix(package-analyzer): ignore module field for 'aurelia-' packages
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Nov 6, 2018
1 parent ac889c0 commit 00de202
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/build/package-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function determineLoaderConfig(project, description) {
if (typeof metadata.browser === 'string') {
// use package.json browser field if possible.
metaMain = metadata.browser;
} else if (typeof metadata.module === 'string') {
} else if (typeof metadata.module === 'string' && !description.name.startsWith('aurelia-')) {
// prefer es module format over cjs, just like webpack.
// this improves compatibility with TypeScript.
metaMain = metadata.module;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 55 additions & 36 deletions spec/lib/build/package-analyzer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,44 +328,63 @@ describe('The PackageAnalyzer', () => {
.catch(e => done.fail(e));
});

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", "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] = {};
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('browser');
done();
})
.catch(e => done.fail(e));
describe('analyze() reads package.json as package metadata -- "browser/module/main" fields analysis', () => {
let fsConfig;
let mockFileNames = ['index.js', 'browser.js', 'module.js'];
beforeEach(function __setup__mock_package_json() {
fsConfig = {};
for (const fileName of mockFileNames) {
fsConfig[path.join('node_modules/my-package', fileName)] = 'some-content';
};
fsConfig[project.paths.root] = {};
});

it('respects browser field over module/main', done => {
fsConfig[path.join('node_modules/my-package', 'package.json')] = '{ "name": "my-package", "main": "index.js", "browser": "browser.js", "module": "module.js" }';
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('browser');
done();
})
.catch(e => done.fail(e));
});

it('respects module field over main', done => {
fsConfig[path.join('node_modules/my-package', 'package.json')] = '{ "name": "my-package", "main": "index.js", "module": "module.js"}';
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('respects "module" field over "main", but ignore module with "aurelia-"', done => {
fsConfig[path.join('node_modules/my-package', 'package.json')] = '{ "name": "my-package", "main": "index.js", "module": "module.js"}';
const aureliaPkg = 'aurelia-binding';
for (const fileName of mockFileNames) {
fsConfig[path.join(`node_modules/${aureliaPkg}`, fileName)] = 'export const content';
}
fsConfig[path.join(`node_modules/${aureliaPkg}`, 'package.json')] = '"name": "aurelia-binding", "main": "index.js", "module": "module.js"';

sut.analyze('aurelia-binding')
.then(description => {
expect(description.metadata.name).toBe('aurelia-binding');
expect(description.loaderConfig.path).toBe(`../node_modules/${aureliaPkg}/index.js`);
expect(description.loaderConfig.main).toBe('main');
done();
})
});
});

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
Expand Down

0 comments on commit 00de202

Please sign in to comment.