Skip to content

Commit

Permalink
Get only existing package main (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranfdev authored and devongovett committed Jul 21, 2018
1 parent 7514852 commit 7a77bca
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
39 changes: 21 additions & 18 deletions packages/core/parcel/src/Resolver.js
Expand Up @@ -228,14 +228,17 @@ class Resolver {
try {
pkg = await this.readPackage(dir);

// First try loading package.main as a file, then try as a directory.
let main = this.getPackageMain(pkg);
let res =
(await this.loadAsFile(main, extensions, pkg)) ||
(await this.loadDirectory(main, extensions, pkg));

if (res) {
return res;
// Get a list of possible package entry points.
let entries = this.getPackageEntries(pkg);

for (let file of entries) {
// First try loading package.main as a file, then try as a directory.
const res =
(await this.loadAsFile(file, extensions, pkg)) ||
(await this.loadDirectory(file, extensions, pkg));
if (res) {
return res;
}
}
} catch (err) {
// ignore
Expand Down Expand Up @@ -275,7 +278,7 @@ class Resolver {
return target === 'browser' ? pkg.browser : null;
}

getPackageMain(pkg) {
getPackageEntries(pkg) {
let browser = this.getBrowserField(pkg);
if (browser && typeof browser === 'object' && browser[pkg.name]) {
browser = browser[pkg.name];
Expand All @@ -284,16 +287,16 @@ class Resolver {
// libraries like d3.js specifies node.js specific files in the "main" which breaks the build
// we use the "browser" or "module" 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, browser, pkg.module, pkg.main].find(
entry => typeof entry === 'string'
);

// Default to index file if no main field find
if (!main || main === '.' || main === './') {
main = 'index';
}
return [pkg.source, browser, pkg.module, pkg.main]
.filter(entry => typeof entry === 'string')
.map(main => {
// Default to index file if no main field find
if (!main || main === '.' || main === './') {
main = 'index';
}

return path.resolve(pkg.pkgdir, main);
return path.resolve(pkg.pkgdir, main);
});
}

async loadAsFile(file, extensions, pkg) {
Expand Down
Empty file.

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

12 changes: 12 additions & 0 deletions packages/core/parcel/test/resolver.js
Expand Up @@ -176,6 +176,18 @@ describe('resolver', function() {
assert.equal(resolved.pkg.name, 'package-browser');
});

it('should fall back to package.main when package.module does not exist', async function() {
let resolved = await resolver.resolve(
'package-module-fallback',
path.join(rootDir, 'foo.js')
);
assert.equal(
resolved.path,
path.join(rootDir, 'node_modules', 'package-module-fallback', 'main.js')
);
assert.equal(resolved.pkg.name, 'package-module-fallback');
});

it('should not resolve a node_modules package.browser main field with --target=node', async function() {
let resolver = new Resolver({
rootDir,
Expand Down

0 comments on commit 7a77bca

Please sign in to comment.