mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
Find dynamic imports with a comment before their parenthesis
The on-demand import scan tested each file's text for `import(` before
reading its calls, so `import /* note */ ("pkg")` was never followed:
the workspace package it loads and that package's npm dependencies were
left out of the module graph, and the dependency was refused with
SC2013. A file is now skipped only when it does not contain the keyword,
and its literal import() calls are read from the syntax. The
workspace-dynamic-import case loads its package with such a comment.
This commit is contained in:
@@ -127,9 +127,10 @@ function addDynamicImportGraph(load: LoadResult, scanned: Set<SourceFile>): void
|
||||
const sf = queue.pop()!;
|
||||
if (sf.fileName.includes("/node_modules/")) continue;
|
||||
const specs: string[] = [];
|
||||
const calls = /\bimport\s*\(/.test(sf.text)
|
||||
? moduleSourceCandidates(load.program, sf).calls
|
||||
: [];
|
||||
// Every literal import() in the file, read from the syntax: a comment
|
||||
// can stand between the keyword and its parenthesis. A file without
|
||||
// the keyword has none.
|
||||
const calls = sf.text.includes("import") ? moduleSourceCandidates(load.program, sf).calls : [];
|
||||
for (const call of calls) {
|
||||
const arg = call.arguments.nodes[0];
|
||||
if (
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
// A workspace package the program loads on demand through import(): its
|
||||
// A workspace package the program loads on demand with a dynamic import: its
|
||||
// own npm dependency (installed only in the workspace package's
|
||||
// node_modules) compiles with it, although neither is part of the startup
|
||||
// module graph.
|
||||
// module graph. A comment can stand between `import` and its parenthesis.
|
||||
async function main(): Promise<void> {
|
||||
console.log("before");
|
||||
const m = await import("wsdynamic");
|
||||
const m = await import /* loaded on demand */ ("wsdynamic");
|
||||
console.log(m.greet("world"));
|
||||
}
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user