Skip to content

Commit

Permalink
add support for "browser" object entrypoints (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott authored Feb 21, 2020
1 parent 46c39b9 commit f88cd8e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,22 @@ function resolveWebDependency(dep: string, isExplicit: boolean): DependencyLoc {
);
}
let foundEntrypoint: string =
depManifest['browser:module'] || depManifest.module || depManifest.browser;
depManifest['browser:module'] ||
depManifest.module ||
depManifest['main:esnext'] ||
depManifest.browser;
// Some packages define "browser" as an object. We'll do our best to find the
// right entrypoint in an entrypoint object, or fail otherwise.
// See: https://github.com/defunctzombie/package-browser-field-spec
if (typeof foundEntrypoint === 'object') {
foundEntrypoint =
foundEntrypoint[dep] ||
foundEntrypoint['./index.js'] ||
foundEntrypoint['./index'] ||
foundEntrypoint['./'] ||
foundEntrypoint['.'] ||
foundEntrypoint;
}
// If the package was a part of the explicit whitelist, fallback to it's main CJS entrypoint.
if (!foundEntrypoint && isExplicit) {
foundEntrypoint = depManifest.main || 'index.js';
Expand All @@ -233,6 +248,9 @@ function resolveWebDependency(dep: string, isExplicit: boolean): DependencyLoc {
),
);
}
if (typeof foundEntrypoint !== 'string') {
throw new Error(`"${dep}" has unexpected entrypoint: ${JSON.stringify(foundEntrypoint)}.`);
}
return {
type: 'JS',
loc: path.join(depManifestLoc, '..', foundEntrypoint),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"mock-test-package": "./mock-test-package.js"
}
}

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

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

2 changes: 2 additions & 0 deletions test/integration/dep-entrypoint-object/expected-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- snowpack installing...
✔ snowpack installed: mock-test-package.

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

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

9 changes: 9 additions & 0 deletions test/integration/dep-entrypoint-object/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"description": "Test that the --optimize flag overrides the config",
"scripts": {
"TEST": "node ../../../pkg/dist-node/index.bin.js --optimize"
},
"dependencies": {
"mock-test-package": "^1.2.3"
}
}

0 comments on commit f88cd8e

Please sign in to comment.