Skip to content

Commit

Permalink
fix(cli-bundler): correctly handle browser "." mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Sep 16, 2020
1 parent 16a5a2a commit f2fc025
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/build/dependency-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ exports.DependencyDescription = class {

for (let i = 0, keys = Object.keys(browser); i < keys.length; i++) {
let key = keys[i];
// leave {".": "dist/index.js"} for main replacement
if (key === '.') continue;
let target = browser[key];

let sourceModule = filePathToModuleId(key);
Expand Down
3 changes: 3 additions & 0 deletions lib/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ function nodejsLoadAsDirectory(resourcePath) {
if (typeof metadata.browser === 'string') {
// use package.json browser field if possible.
metaMain = metadata.browser;
} else if (typeof metadata.browser === 'object' && typeof metadata.browser['.'] === 'string') {
// use package.json browser mapping {".": "dist/index.js"} if possible.
metaMain = metadata.browser['.'];
} else if (typeof metadata.module === 'string' &&
!(metadata.name && metadata.name.startsWith('aurelia-'))) {
// prefer es module format over cjs, just like webpack.
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/build/dependency-description.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ describe('The DependencyDescription', () => {
'./server/only': './shims/server-only'
});
});

it('gets browser replacement but leave . for main replacement', () => {
sut.metadata = {
browser: {
"readable-stream": "./lib/readable-stream-browser.js",
".": "dist/jszip.min.js"
}
};
expect(sut.browserReplacement()).toEqual({
"readable-stream": "./lib/readable-stream-browser"
});
});
});
24 changes: 24 additions & 0 deletions spec/lib/build/util.spec.js → spec/lib/build/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,30 @@ describe('the Utils.nodejsLoad function', () => {
expect(Utils.nodejsLoad(path.resolve('foo', 'bar'))).toBe(path.resolve('foo', 'bar', 'lo.js'));
});

it('load directory with package.json browser field', () => {
const fsConfig = {};
fsConfig[path.join('foo', 'bar', 'package.json')] = '{"main": "lo2.js", "browser": "lo.js"}';
fsConfig[path.join('foo', 'bar', 'lo.js')] = 'bar/lo.js';
mockfs(fsConfig);
expect(Utils.nodejsLoad(path.resolve('foo', 'bar'))).toBe(path.resolve('foo', 'bar', 'lo.js'));
});

it('load directory with package.json browser "." mapping', () => {
const fsConfig = {};
fsConfig[path.join('foo', 'bar', 'package.json')] = '{"main": "lo2.js", "browser": {".": "lo.js"}}';
fsConfig[path.join('foo', 'bar', 'lo.js')] = 'bar/lo.js';
mockfs(fsConfig);
expect(Utils.nodejsLoad(path.resolve('foo', 'bar'))).toBe(path.resolve('foo', 'bar', 'lo.js'));
});

it('load directory with package.json browser field', () => {
const fsConfig = {};
fsConfig[path.join('foo', 'bar', 'package.json')] = '{"main": "lo2.js", "module": "lo.js"}';
fsConfig[path.join('foo', 'bar', 'lo.js')] = 'bar/lo.js';
mockfs(fsConfig);
expect(Utils.nodejsLoad(path.resolve('foo', 'bar'))).toBe(path.resolve('foo', 'bar', 'lo.js'));
});

it('load directory with package.json, case2', () => {
const fsConfig = {};
fsConfig[path.join('foo', 'bar', 'package.json')] = '{"main": "lo.js"}';
Expand Down

0 comments on commit f2fc025

Please sign in to comment.