Skip to content

Commit

Permalink
Fix browser entry-point resolution (#1284)
Browse files Browse the repository at this point in the history
Fixes #1283
  • Loading branch information
fathyb authored and devongovett committed May 6, 2018
1 parent 0b3d80f commit 98a293f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,16 @@ class Resolver {
}

getPackageMain(pkg) {
let {browser} = pkg;

if (typeof browser === 'object' && browser[pkg.name]) {
browser = browser[pkg.name];
}

// libraries like d3.js specifies node.js specific files in the "main" which breaks the build
// we use the "module" or "browser" field to get the full dependency tree if available.
// If this is a linked module with a `source` field, use that as the entry point.
let main = [pkg.source, pkg.module, pkg.browser, pkg.main].find(
let main = [pkg.source, pkg.module, browser, pkg.main].find(
entry => typeof entry === 'string'
);

Expand Down
12 changes: 9 additions & 3 deletions test/integration/resolve-entries/browser-multiple.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const required = require('./pkg-browser-multiple/projected')
const projected = require('./pkg-browser-multiple/projected')

if(required.test() !== 'pkg-browser-multiple') {
if(projected.test() !== 'pkg-browser-multiple') {
throw new Error('Invalid module')
}

export const test = required.test
const entry = require('./pkg-browser-multiple')

if(entry.test() !== 'pkg-browser-multiple browser-entry') {
throw new Error('Invalid module')
}

export const test = {projected, entry}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function test() {
return 'pkg-browser-multiple browser-entry'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "pkg-browser-multiple",
"main": "./does-not-exist.js",
"browser": {
"./projected.js": "./projected-module.js"
"./projected.js": "./projected-module.js",
"pkg-browser-multiple": "browser-entry.js"
}
}
14 changes: 10 additions & 4 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,18 +507,24 @@ describe('javascript', function() {

assertBundleTree(b, {
name: 'browser-multiple.js',
assets: ['browser-multiple.js', 'projected-module.js'],
assets: [
'browser-multiple.js',
'projected-module.js',
'browser-entry.js'
],
childBundles: [
{
type: 'map'
}
]
});

let output = run(b);
let {test: output} = run(b);

assert.equal(typeof output.test, 'function');
assert.equal(output.test(), 'pkg-browser-multiple');
assert.equal(typeof output.projected.test, 'function');
assert.equal(typeof output.entry.test, 'function');
assert.equal(output.projected.test(), 'pkg-browser-multiple');
assert.equal(output.entry.test(), 'pkg-browser-multiple browser-entry');
});

it('should resolve the module field before main', async function() {
Expand Down

0 comments on commit 98a293f

Please sign in to comment.