-
Notifications
You must be signed in to change notification settings - Fork 921
/
scan-imports.ts
382 lines (355 loc) · 12.5 KB
/
scan-imports.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import {ImportSpecifier, init as initESModuleLexer, parse} from 'es-module-lexer';
import {InstallTarget} from 'esinstall';
import glob from 'glob';
import picomatch from 'picomatch';
import {fdir} from 'fdir';
import path from 'path';
import slash from 'slash';
import stripComments from 'strip-comments';
import {logger} from './logger';
import {ScannableExt, SnowpackConfig, SnowpackSourceFile} from './types';
import {
createInstallTarget,
CSS_REGEX,
findMatchingAliasEntry,
getExtension,
HTML_JS_REGEX,
HTML_STYLE_REGEX,
isImportOfPackage,
isTruthy,
readFile,
SVELTE_VUE_REGEX,
ASTRO_REGEX,
IS_DOTFILE_REGEX,
} from './util';
// [@\w] - Match a word-character or @ (valid package name)
// (?!.*(:\/\/)) - Ignore if previous match was a protocol (ex: http://)
const BARE_SPECIFIER_REGEX = /^[@\w](?!.*(:\/\/))/;
const ESM_IMPORT_REGEX =
/(?<![^;\n])[ ]*import(?:["'\s]*([\w*${}\n\r\t, ]+)\s*from\s*)?\s*["'](.*?)["']/gm;
const ESM_DYNAMIC_IMPORT_REGEX = /(?<!\.)\bimport\((?:['"].+['"]|`[^$]+`)\)/gm;
const HAS_NAMED_IMPORTS_REGEX = /^[\w\s\,]*\{(.*)\}/s;
const STRIP_AS = /\s+as\s+.*/; // for `import { foo as bar }`, strips “as bar”
const DEFAULT_IMPORT_REGEX = /import\s+(\w)+(,\s\{[\w\s,]*\})?\s+from/s;
export async function getInstallTargets(
config: SnowpackConfig,
knownEntrypoints: string[],
scannedFiles?: SnowpackSourceFile[],
) {
let installTargets: InstallTarget[] = [];
if (knownEntrypoints.length > 0) {
installTargets.push(...scanDepList(knownEntrypoints, config.root));
}
// TODO: remove this if block; move logic inside scanImports
if (scannedFiles) {
installTargets.push(...(await scanImportsFromFiles(scannedFiles, config)));
} else {
installTargets.push(...(await scanImports(config.mode === 'test', config)));
}
return installTargets.filter(
(dep) =>
!config.packageOptions.external.some((packageName) =>
isImportOfPackage(dep.specifier, packageName),
),
);
}
const scannableExts = new Set<ScannableExt>([
'.astro',
'.cjs',
'.css',
'.html',
'.interface',
'.js',
'.jsx',
'.less',
'.mjs',
'.sass',
'.scss',
'.svelte',
'.ts',
'.tsx',
'.vue',
]);
function isFileScannable(ext: string): boolean {
return scannableExts.has(ext as ScannableExt); // note: <ScannableExts> needed to keep Set() correct above, but this fn should test any string (hence "as").
}
export function matchDynamicImportValue(importStatement: string) {
const matched = stripComments(importStatement).match(/^\s*('([^']+)'|"([^"]+)")\s*$/m);
return matched?.[2] || matched?.[3] || null;
}
export function getWebModuleSpecifierFromCode(code: string, imp: ImportSpecifier): string | null {
// import.meta: we can ignore
if (imp.d === -2) {
return null;
}
// Static imports: easy to parse
if (imp.d === -1) {
return code.substring(imp.s, imp.e);
}
// Dynamic imports: a bit trickier to parse. Today, we only support string literals.
const importStatement = code.substring(imp.s, imp.e);
return matchDynamicImportValue(importStatement);
}
/**
* parses an import specifier, looking for a web modules to install. If a web module is not detected,
* null is returned.
*/
function parseWebModuleSpecifier(specifier: string | null): null | string {
if (!specifier) {
return null;
}
// If specifier is a "bare module specifier" (ie: package name) just return it directly
if (BARE_SPECIFIER_REGEX.test(specifier)) {
return specifier;
}
return null;
}
function parseImportStatement(code: string, imp: ImportSpecifier): null | InstallTarget {
const webModuleSpecifier = parseWebModuleSpecifier(getWebModuleSpecifierFromCode(code, imp));
if (!webModuleSpecifier) {
return null;
}
const importStatement = stripComments(code.substring(imp.ss, imp.se));
if (/^import\s+type/.test(importStatement)) {
return null;
}
const isDynamicImport = imp.d > -1;
const hasDefaultImport = !isDynamicImport && DEFAULT_IMPORT_REGEX.test(importStatement);
const hasNamespaceImport = !isDynamicImport && importStatement.includes('*');
const namedImports = (importStatement.match(HAS_NAMED_IMPORTS_REGEX)! || [, ''])[1]
.split(',') // split `import { a, b, c }` by comma
.map((name) => name.replace(STRIP_AS, '').trim()) // remove “ as …” and trim
.filter(isTruthy);
return {
specifier: webModuleSpecifier,
all: isDynamicImport || (!hasDefaultImport && !hasNamespaceImport && namedImports.length === 0),
default: hasDefaultImport,
namespace: hasNamespaceImport,
named: namedImports,
};
}
function cleanCodeForParsing(code: string): string {
code = stripComments(code);
const allMatches: string[] = [];
let match;
const importRegex = new RegExp(ESM_IMPORT_REGEX);
while ((match = importRegex.exec(code))) {
allMatches.push(match);
}
const dynamicImportRegex = new RegExp(ESM_DYNAMIC_IMPORT_REGEX);
while ((match = dynamicImportRegex.exec(code))) {
allMatches.push(match);
}
return allMatches.map(([full]) => full).join('\n');
}
function parseJsForInstallTargets(contents: string): InstallTarget[] {
let imports: ImportSpecifier[] = [];
// Attempt #1: Parse the file as JavaScript. JSX and some decorator
// syntax will break this.
try {
imports.push(...parse(contents)[0]);
} catch (err) {
// Attempt #2: Parse only the import statements themselves.
// This lets us guarentee we aren't sending any broken syntax to our parser,
// but at the expense of possible false +/- caused by our regex extractor.
contents = cleanCodeForParsing(contents);
imports.push(...parse(contents)[0]);
}
return (
imports
.map((imp) => parseImportStatement(contents, imp))
.filter(isTruthy)
// Babel macros are not install targets!
.filter((target) => !/[./]macro(\.js)?$/.test(target.specifier))
);
}
function parseCssForInstallTargets(code: string): InstallTarget[] {
const installTargets: InstallTarget[] = [];
let match;
const importRegex = new RegExp(CSS_REGEX);
while ((match = importRegex.exec(code))) {
const [, spec] = match;
const webModuleSpecifier = parseWebModuleSpecifier(spec);
if (webModuleSpecifier) {
installTargets.push(createInstallTarget(webModuleSpecifier));
}
}
return installTargets;
}
function parseFileForInstallTargets({
locOnDisk,
baseExt,
contents,
root,
}: SnowpackSourceFile<string>): InstallTarget[] {
const relativeLoc = path.relative(root, locOnDisk);
try {
switch (baseExt as ScannableExt) {
case '.css':
case '.less':
case '.sass':
case '.scss': {
logger.debug(`Scanning ${relativeLoc} for imports as CSS`);
return parseCssForInstallTargets(contents);
}
case '.html':
case '.svelte':
case '.interface':
case '.vue': {
logger.debug(`Scanning ${relativeLoc} for imports as HTML`);
return [
...parseCssForInstallTargets(extractCssFromHtml(contents)),
...parseJsForInstallTargets(extractJsFromHtml({contents, baseExt})),
];
}
case '.astro': {
logger.debug(`Scanning ${relativeLoc} for imports as Astro`);
return [
...parseCssForInstallTargets(extractCssFromHtml(contents)),
...parseJsForInstallTargets(extractJsFromAstro(contents)),
];
}
case '.cjs':
case '.js':
case '.jsx':
case '.mjs':
case '.ts':
case '.tsx': {
logger.debug(`Scanning ${relativeLoc} for imports as JS`);
return parseJsForInstallTargets(contents);
}
default: {
logger.debug(
`Skip scanning ${relativeLoc} for imports (unknown file extension ${baseExt})`,
);
return [];
}
}
} catch (err) {
// Another error! No hope left, just abort.
logger.error(`! ${locOnDisk}`);
throw err;
}
}
/** Extract only JS within <script type="module"> tags (works for .svelte and .vue files, too) */
function extractJsFromHtml({contents, baseExt}: {contents: string; baseExt: string}): string {
// TODO: Replace with matchAll once Node v10 is out of TLS.
// const allMatches = [...result.matchAll(new RegExp(HTML_JS_REGEX))];
const allMatches: string[][] = [];
let match;
let regex = new RegExp(HTML_JS_REGEX);
if (baseExt === '.svelte' || baseExt === '.interface' || baseExt === '.vue') {
regex = new RegExp(SVELTE_VUE_REGEX); // scan <script> tags, not <script type="module">
}
while ((match = regex.exec(contents))) {
allMatches.push(match);
}
return allMatches
.map((match) => match[2]) // match[2] is the code inside the <script></script> element
.filter((s) => s.trim())
.join('\n');
}
/** Extract only CSS within <style> tags (works for .svelte and .vue files, too) */
function extractCssFromHtml(contents: string): string {
// TODO: Replace with matchAll once Node v10 is out of TLS.
// const allMatches = [...result.matchAll(new RegExp(HTML_JS_REGEX))];
const allMatches: string[][] = [];
let match;
let regex = new RegExp(HTML_STYLE_REGEX);
while ((match = regex.exec(contents))) {
allMatches.push(match);
}
return allMatches
.map((match) => match[2]) // match[2] is the code inside the <style></style> element
.filter((s) => s.trim())
.join('\n');
}
function extractJsFromAstro(contents: string): string {
const allMatches: string[][] = [];
let match;
let regex = new RegExp(ASTRO_REGEX);
// No while loop because we only care about the top frontmatter
if ((match = regex.exec(contents))) {
allMatches.push(match);
}
return allMatches
.map((match) => match[1]) // match[1] is the code inside the frontmatter
.filter((s) => s.trim())
.join('\n');
}
export function scanDepList(depList: string[], cwd: string): InstallTarget[] {
return depList
.map((whitelistItem) => {
if (!glob.hasMagic(whitelistItem)) {
return [createInstallTarget(whitelistItem, true)];
} else {
const nodeModulesLoc = path.join(cwd, 'node_modules');
return scanDepList(glob.sync(whitelistItem, {cwd: nodeModulesLoc, nodir: true}), cwd);
}
})
.reduce((flat, item) => flat.concat(item), []);
}
export async function scanImports(
includeTests: boolean,
config: SnowpackConfig,
): Promise<InstallTarget[]> {
await initESModuleLexer;
const includeFileSets = await Promise.all(
Object.entries(config.mount).map(async ([fromDisk, mountEntry]) => {
const allMatchedFiles = (await new fdir()
.withFullPaths()
.crawl(fromDisk)
.withPromise()) as string[];
if (mountEntry.dot) {
return allMatchedFiles;
}
return allMatchedFiles.filter((f) => !IS_DOTFILE_REGEX.test(slash(f))); // TODO: use a file URL instead
}),
);
const includeFiles = Array.from(new Set(([] as string[]).concat.apply([], includeFileSets)));
if (includeFiles.length === 0) {
return [];
}
// Scan every matched JS file for web dependency imports
const excludeGlobs = includeTests
? config.exclude
: [...config.exclude, ...config.testOptions.files];
const mountedNodeModules = Object.keys(config.mount).filter((v) => v.includes('node_modules'));
const foundExcludeMatch = picomatch(excludeGlobs);
const loadedFiles: (SnowpackSourceFile | null)[] = await Promise.all(
includeFiles.map(async (filePath: string): Promise<SnowpackSourceFile | null> => {
// don’t waste time trying to scan files that aren’t scannable
if (!isFileScannable(path.extname(filePath))) {
return null;
}
if (foundExcludeMatch(filePath)) {
const isMounted = mountedNodeModules.find((mountKey) => filePath.startsWith(mountKey));
if (!isMounted || (isMounted && foundExcludeMatch(filePath.slice(isMounted.length)))) {
return null;
}
}
return {
baseExt: getExtension(filePath),
root: config.root,
locOnDisk: filePath,
contents: await readFile(filePath),
};
}),
);
return scanImportsFromFiles(loadedFiles.filter(isTruthy), config);
}
export async function scanImportsFromFiles(
loadedFiles: SnowpackSourceFile[],
config: SnowpackConfig,
): Promise<InstallTarget[]> {
await initESModuleLexer;
return loadedFiles
.filter((sourceFile) => !Buffer.isBuffer(sourceFile.contents)) // filter out binary files from import scanning
.map((sourceFile) => parseFileForInstallTargets(sourceFile as SnowpackSourceFile<string>))
.reduce((flat, item) => flat.concat(item), [])
.filter((target) => {
const aliasEntry = findMatchingAliasEntry(config, target.specifier);
return !aliasEntry || aliasEntry.type === 'package';
})
.sort((impA, impB) => impA.specifier.localeCompare(impB.specifier));
}