Skip to content

Commit

Permalink
add better external support (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Jun 23, 2020
1 parent 5fa5d4d commit f60ae3d
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ function formatInstallResults(): string {
.join(', ');
}

function isImportOfPackage(importUrl: string, packageName: string) {
return packageName === importUrl || importUrl.startsWith(packageName + '/');
}

/**
* Formats the snowpack dependency name from a "webDependencies" input value:
* 2. Remove any ".js"/".mjs" extension (will be added automatically by Rollup)
Expand Down Expand Up @@ -272,7 +276,10 @@ export async function install(
}
const allInstallSpecifiers = new Set(
installTargets
.filter((dep) => !externalPackages.includes(dep.specifier))
.filter(
(dep) =>
!externalPackages.some((packageName) => isImportOfPackage(dep.specifier, packageName)),
)
.map((dep) => dep.specifier)
.map((specifier) => installAlias[specifier] || specifier)
.sort(),
Expand Down Expand Up @@ -350,7 +357,7 @@ export async function install(
let isCircularImportFound = false;
const inputOptions: InputOptions = {
input: installEntrypoints,
external: externalPackages,
external: (id) => externalPackages.some((packageName) => isImportOfPackage(id, packageName)),
treeshake: {moduleSideEffects: 'no-external'},
plugins: [
rollupPluginReplace(getRollupReplaceKeys(env)),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"mock-test-package-a": "./mock-test-package-a.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { foo } from 'mock-test-package-b';
import { foo as foo$1 } from 'mock-test-package-b/entrypoint';

console.log(foo, foo$1);
3 changes: 3 additions & 0 deletions test/integration/config-external/expected-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
✔ snowpack install complete.
⦿ web_modules/ size gzip brotli
└─ mock-test-package-a.js XXXX KB XXXX KB XXXX KB
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
export1: 'foo',
export2: 'bar',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "mock-test-package",
"version": "1.2.3",
"main": "entrypoint.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.

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.

16 changes: 16 additions & 0 deletions test/integration/config-external/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"description": "Handle external packages",
"scripts": {
"TEST": "node ../../../pkg/dist-node/index.bin.js"
},
"snowpack": {
"installOptions": {
"externalPackage": ["mock-test-package-b"]
},
"install": ["mock-test-package-a"]
},
"dependencies": {
"mock-test-package-a": "^1.0.0",
"mock-test-package-b": "^1.0.0"
}
}

1 comment on commit f60ae3d

@vercel
Copy link

@vercel vercel bot commented on f60ae3d Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.