Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get only existing package main #1577

Merged
merged 7 commits into from Jul 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 21 additions & 18 deletions 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 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