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

Ensure that the import resolver respects installOptions.externalPackage. #1070

Merged
merged 2 commits into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion snowpack/src/build/import-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function createImportResolver({
config,
}: ImportResolverOptions) {
return function importResolver(spec: string): string | false {
if (URL_HAS_PROTOCOL_REGEX.test(spec)) {
if (URL_HAS_PROTOCOL_REGEX.test(spec) || config.installOptions.externalPackage?.includes(spec)) {
Copy link
Owner

Choose a reason for hiding this comment

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

ha, this looks like an outdated version of what we use in build.ts. Can you replace both here and build.ts with:

// Ignore "http://*" imports
if (url.parse(spec).protocol) {
  return spec;
}

// Ignore packages marked as external
if (this.config.installOptions.externalPackage?.includes(spec)) {
    return spec;
}

It's definitely a little more verbose, but these functions are notoriously buggy so good documentation is important.

Copy link
Owner

Choose a reason for hiding this comment

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

Otherwise, LGTM!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing, done!

return spec;
}
if (spec.startsWith('/') || spec.startsWith('./') || spec.startsWith('../')) {
Expand Down
3 changes: 2 additions & 1 deletion snowpack/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ class FileBuilder {
return spec;
}
// Ignore "http://*" imports
if (url.parse(resolvedImportUrl).protocol) {
if (url.parse(resolvedImportUrl).protocol ||
this.config.installOptions.externalPackage?.includes(resolvedImportUrl)) {
return spec;
}
// Handle normal "./" & "../" import specifiers
Expand Down
50 changes: 50 additions & 0 deletions test/build/__snapshots__/build.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,56 @@ exports[`snowpack build config-extends-plugins: index.html 1`] = `
</html>"
`;

exports[`snowpack build config-external-package: __snowpack__/env.js 1`] = `"export default {\\"MODE\\":\\"production\\",\\"NODE_ENV\\":\\"production\\"};"`;

exports[`snowpack build config-external-package: _dist_/index.js 1`] = `
"import 'fs';
import '../web_modules/array-flatten.js';"
`;

exports[`snowpack build config-external-package: allFiles 1`] = `
Array [
"__snowpack__/env.js",
"_dist_/index.js",
"web_modules/array-flatten.js",
"web_modules/import-map.json",
]
`;

exports[`snowpack build config-external-package: web_modules/array-flatten.js 1`] = `
"/**
* Flatten an array indefinitely.
*/
function flatten(array) {
var result = [];
$flatten(array, result);
return result;
}
/**
* Internal flatten function recursively passes \`result\`.
*/
function $flatten(array, result) {
for (var i = 0; i < array.length; i++) {
var value = array[i];
if (Array.isArray(value)) {
$flatten(value, result);
}
else {
result.push(value);
}
}
}
export { flatten };"
`;

exports[`snowpack build config-external-package: web_modules/import-map.json 1`] = `
"{
\\"imports\\": {
\\"array-flatten\\": \\"./array-flatten.js\\"
}
}"
`;

exports[`snowpack build config-out: __snowpack__/env.js 1`] = `"export default {\\"MODE\\":\\"production\\",\\"NODE_ENV\\":\\"production\\"};"`;

exports[`snowpack build config-out: allFiles 1`] = `
Expand Down
28 changes: 28 additions & 0 deletions test/build/config-external-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"private": true,
"version": "1.0.0",
"name": "@snowpack/test-config-external-package",
"description": "Test for installOptions.externalPackage as it applies to building.",
"scripts": {
"testbuild": "snowpack build"
},
"snowpack": {
"buildOptions": {
"minify": false
},
"mount": {
"./src": "/_dist_"
},
"installOptions": {
"externalPackage": [
"fs"
]
}
},
"devDependencies": {
"snowpack": "^2.7.0"
},
"dependencies": {
"array-flatten": "^3.0.0"
}
}
2 changes: 2 additions & 0 deletions test/build/config-external-package/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'fs';
import 'array-flatten';