Skip to content

Commit

Permalink
Ensure that the import resolver respects installOptions.externalPacka…
Browse files Browse the repository at this point in the history
…ge. (#1070)

* Ensure that the import resolver respects installOptions.externalPackage.

* Clean up conditions and add comments.
  • Loading branch information
pkaminski committed Sep 20, 2020
1 parent 8acbb90 commit 21610bc
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
11 changes: 9 additions & 2 deletions snowpack/src/build/import-resolver.ts
@@ -1,12 +1,12 @@
import fs from 'fs';
import path from 'path';
import url from 'url';
import {ImportMap, SnowpackConfig} from '../types/snowpack';
import {
findMatchingAliasEntry,
getExt,
relativeURL,
replaceExt,
URL_HAS_PROTOCOL_REGEX,
} from '../util';
import srcFileExtensionMapping from './src-file-extension-mapping';

Expand Down Expand Up @@ -56,9 +56,16 @@ export function createImportResolver({
config,
}: ImportResolverOptions) {
return function importResolver(spec: string): string | false {
if (URL_HAS_PROTOCOL_REGEX.test(spec)) {
// Ignore "http://*" imports
if (url.parse(spec).protocol) {
return spec;
}

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

if (spec.startsWith('/') || spec.startsWith('./') || spec.startsWith('../')) {
const importStats = getImportStats(path.dirname(fileLoc), spec);
spec = resolveSourceSpecifier(spec, importStats, config);
Expand Down
4 changes: 4 additions & 0 deletions snowpack/src/commands/build.ts
Expand Up @@ -198,6 +198,10 @@ class FileBuilder {
if (url.parse(resolvedImportUrl).protocol) {
return spec;
}
// Ignore packages marked as external
if (this.config.installOptions.externalPackage?.includes(resolvedImportUrl)) {
return spec;
}
// Handle normal "./" & "../" import specifiers
const importExtName = path.extname(resolvedImportUrl);
const isProxyImport =
Expand Down
2 changes: 0 additions & 2 deletions snowpack/src/util.ts
Expand Up @@ -35,8 +35,6 @@ export const HTML_JS_REGEX = /(<script[^>]*?type="module".*?>)(.*?)<\/script>/gi
export const CSS_REGEX = /@import\s*['"](.*?)['"];/gs;
export const SVELTE_VUE_REGEX = /(<script[^>]*>)(.*?)<\/script>/gims;

export const URL_HAS_PROTOCOL_REGEX = /^(\w+:)?\/\//;

/** Read file from disk; return a string if it’s a code file */
export async function readFile(filepath: string): Promise<string | Buffer> {
const data = await fs.promises.readFile(filepath);
Expand Down
50 changes: 50 additions & 0 deletions test/build/__snapshots__/build.test.js.snap
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
@@ -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
@@ -0,0 +1,2 @@
import 'fs';
import 'array-flatten';

1 comment on commit 21610bc

@vercel
Copy link

@vercel vercel bot commented on 21610bc Sep 20, 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.