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 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
11 changes: 9 additions & 2 deletions snowpack/src/build/import-resolver.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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';