@provino/loader 0.0.0-stage → 0.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/graph/index.d.ts +199 -0
- package/dist/graph/index.js +3 -0
- package/dist/graph.js +571 -0
- package/dist/index.d.ts +200 -0
- package/dist/index.js +3272 -0
- package/package.json +46 -3
package/dist/graph.js
ADDED
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { threadId } from "node:worker_threads";
|
|
6
|
+
import { init, parse } from "es-module-lexer";
|
|
7
|
+
|
|
8
|
+
// src/graph/cache.ts
|
|
9
|
+
/** The cache file's layout version: bumped when a node's shape or the scan's meaning changes. */
|
|
10
|
+
const GRAPH_FORMAT = 3;
|
|
11
|
+
const INSTALL_MARKERS = ["node_modules/.modules.yaml", "node_modules/.package-lock.json"];
|
|
12
|
+
let lexerVersion;
|
|
13
|
+
/** The installed lexer's version, which decides what a scan finds. */
|
|
14
|
+
function lexerVersionOf() {
|
|
15
|
+
if (lexerVersion !== void 0) return lexerVersion;
|
|
16
|
+
try {
|
|
17
|
+
const entry = fileURLToPath(import.meta.resolve("es-module-lexer"));
|
|
18
|
+
const manifest = JSON.parse(readFileSync(join(dirname(entry), "..", "package.json"), "utf8"));
|
|
19
|
+
const version = manifest !== null && typeof manifest === "object" ? Reflect.get(manifest, "version") : null;
|
|
20
|
+
lexerVersion = typeof version === "string" ? version : "unknown";
|
|
21
|
+
} catch {
|
|
22
|
+
lexerVersion = "unknown";
|
|
23
|
+
}
|
|
24
|
+
return lexerVersion;
|
|
25
|
+
}
|
|
26
|
+
/** The size and mtime of the install marker nearest at or above `root` (empty when none). */
|
|
27
|
+
function installStamp(root) {
|
|
28
|
+
let dir = resolve(root);
|
|
29
|
+
for (;;) {
|
|
30
|
+
for (const marker of INSTALL_MARKERS) try {
|
|
31
|
+
const stats = statSync(join(dir, marker));
|
|
32
|
+
return `${marker}:${stats.size}:${stats.mtimeMs}`;
|
|
33
|
+
} catch {}
|
|
34
|
+
const parent = dirname(dir);
|
|
35
|
+
if (parent === dir) return "";
|
|
36
|
+
dir = parent;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The cache key: a hash of the lockfile's hash (`lockfileHash`, which the loader takes from the
|
|
41
|
+
* transform cache's), the install marker's size and mtime, the graph format, the lexer's version
|
|
42
|
+
* and how edges resolve (`extra`: the resolver's fingerprint and the conditions). `null` when the
|
|
43
|
+
* project has no lockfile, and the graph then stays in memory.
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
function graphCacheKey(root, extra, lockfileHash) {
|
|
47
|
+
if (lockfileHash === null) return null;
|
|
48
|
+
const hash = createHash("sha256");
|
|
49
|
+
hash.update(String(3)).update("\0").update(lexerVersionOf()).update("\0");
|
|
50
|
+
hash.update(resolve(root)).update("\0").update(extra).update("\0");
|
|
51
|
+
hash.update(lockfileHash).update("\0").update(installStamp(root));
|
|
52
|
+
return hash.digest("hex").slice(0, 32);
|
|
53
|
+
}
|
|
54
|
+
/** The cache file for a key under a cache directory. */
|
|
55
|
+
function graphCacheFile(dir, key) {
|
|
56
|
+
return join(dir, "graph", `${key}.json`);
|
|
57
|
+
}
|
|
58
|
+
/** A node as the graph holds it, or `null` for a stored form that is not one (a damaged file). */
|
|
59
|
+
function fromStored(stored) {
|
|
60
|
+
try {
|
|
61
|
+
return {
|
|
62
|
+
dynamic: stored[0] === 1,
|
|
63
|
+
esm: stored[1] === 1,
|
|
64
|
+
edges: stored[2].map(([specifier, kind, target]) => ({
|
|
65
|
+
specifier,
|
|
66
|
+
kind: kind === 1 ? "require" : "import",
|
|
67
|
+
target
|
|
68
|
+
}))
|
|
69
|
+
};
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function toStored(node, stamp) {
|
|
75
|
+
return [
|
|
76
|
+
node.dynamic ? 1 : 0,
|
|
77
|
+
node.esm ? 1 : 0,
|
|
78
|
+
node.edges.map((edge) => [
|
|
79
|
+
edge.specifier,
|
|
80
|
+
edge.kind === "require" ? 1 : 0,
|
|
81
|
+
edge.target
|
|
82
|
+
]),
|
|
83
|
+
stamp.mtimeMs,
|
|
84
|
+
stamp.size
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Reads a cache file: its nodes by path, in the stored form (a node is checked when it is first
|
|
89
|
+
* visited), or `null` when it is missing, corrupt, or another format or key.
|
|
90
|
+
*/
|
|
91
|
+
function readGraphCache(file, key) {
|
|
92
|
+
let parsed;
|
|
93
|
+
try {
|
|
94
|
+
parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
if (parsed === null || typeof parsed !== "object") return null;
|
|
99
|
+
if (Reflect.get(parsed, "format") !== 3 || Reflect.get(parsed, "key") !== key) return null;
|
|
100
|
+
const stored = Reflect.get(parsed, "nodes");
|
|
101
|
+
if (stored === null || typeof stored !== "object") return null;
|
|
102
|
+
return new Map(Object.entries(stored));
|
|
103
|
+
}
|
|
104
|
+
/** Names a temp file apart from every other writer's: another process, or a worker thread of this one. */
|
|
105
|
+
const NONCE = `${process.pid}.${threadId}.${randomBytes(4).toString("hex")}`;
|
|
106
|
+
let counter = 0;
|
|
107
|
+
/**
|
|
108
|
+
* Writes `nodes` merged over the copy on disk (in the stored form, with no conversion), through a
|
|
109
|
+
* temp file and a rename, so a reader never sees half a file. A failure is swallowed, and its temp
|
|
110
|
+
* file removed: the cache is only a speed-up.
|
|
111
|
+
*/
|
|
112
|
+
function writeGraphCache(file, key, nodes) {
|
|
113
|
+
const temp = `${file}.${NONCE}.${counter++}.tmp`;
|
|
114
|
+
try {
|
|
115
|
+
const merged = readGraphCache(file, key) ?? /* @__PURE__ */ new Map();
|
|
116
|
+
for (const [path, node] of nodes) merged.set(path, node);
|
|
117
|
+
const body = {
|
|
118
|
+
format: 3,
|
|
119
|
+
key,
|
|
120
|
+
nodes: Object.fromEntries(merged)
|
|
121
|
+
};
|
|
122
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
123
|
+
writeFileSync(temp, JSON.stringify(body));
|
|
124
|
+
renameSync(temp, file);
|
|
125
|
+
} catch {
|
|
126
|
+
try {
|
|
127
|
+
unlinkSync(temp);
|
|
128
|
+
} catch {}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/graph/conditions.ts
|
|
133
|
+
/** `conditions` when the caller gave some, else Node's (`['node']`). */
|
|
134
|
+
function conditionsOf(options) {
|
|
135
|
+
return options.conditions ?? ["node"];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/graph/scan.ts
|
|
139
|
+
/**
|
|
140
|
+
* Starts the lexer's WebAssembly build. `scanSource` works without it (the first scan compiles the
|
|
141
|
+
* module synchronously); a worker awaits this once at start-up, where an `await` is free.
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
async function initLexer() {
|
|
145
|
+
await init();
|
|
146
|
+
}
|
|
147
|
+
/** `require`, or `module.require`, as a whole word and not a property or a `typeof` operand. */
|
|
148
|
+
const REQUIRE = /(?<![\w$.])(?<!typeof\s+)(?:module\s*\.\s*)?require(?![\w$])/g;
|
|
149
|
+
/** Whether a file has module syntax for the lexer: `import`/`export` as words (not `exports`). */
|
|
150
|
+
const MODULE_SYNTAX = /(?<![\w$])(?:import|export)(?![\w$])/;
|
|
151
|
+
/** The index of the first character at or after `at` that is not white space or a comment. */
|
|
152
|
+
function skipTrivia(source, at) {
|
|
153
|
+
let i = at;
|
|
154
|
+
for (;;) {
|
|
155
|
+
const code = source.charCodeAt(i);
|
|
156
|
+
if (code === 32 || code === 9 || code === 10 || code === 13 || code === 11 || code === 12) i++;
|
|
157
|
+
else if (code === 47) {
|
|
158
|
+
const next = source.charCodeAt(i + 1);
|
|
159
|
+
if (next === 42) {
|
|
160
|
+
const end = source.indexOf("*/", i + 2);
|
|
161
|
+
if (end === -1) return source.length;
|
|
162
|
+
i = end + 2;
|
|
163
|
+
} else if (next === 47) {
|
|
164
|
+
const end = source.indexOf("\n", i + 2);
|
|
165
|
+
if (end === -1) return source.length;
|
|
166
|
+
i = end + 1;
|
|
167
|
+
} else return i;
|
|
168
|
+
} else return i;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The literal specifier of a `require(` whose `(` is at `open`, with the index after its `)` or
|
|
173
|
+
* `,`; `null` when the argument is not a plain string.
|
|
174
|
+
*/
|
|
175
|
+
function literalAfter(source, open) {
|
|
176
|
+
const start = skipTrivia(source, open + 1);
|
|
177
|
+
const quote = source[start];
|
|
178
|
+
if (quote !== "'" && quote !== "\"" && quote !== "`") return null;
|
|
179
|
+
let end = start + 1;
|
|
180
|
+
for (; end < source.length; end++) {
|
|
181
|
+
const char = source[end];
|
|
182
|
+
if (char === quote) break;
|
|
183
|
+
if (char === "\\" || char === "\n" || quote === "`" && char === "$") return null;
|
|
184
|
+
}
|
|
185
|
+
if (end >= source.length) return null;
|
|
186
|
+
const after = skipTrivia(source, end + 1);
|
|
187
|
+
const closer = source[after];
|
|
188
|
+
if (closer !== "," && closer !== ")") return null;
|
|
189
|
+
return {
|
|
190
|
+
specifier: source.slice(start + 1, end),
|
|
191
|
+
end: after + 1
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Whether `source` has ES module syntax: an `import` or `export` declaration, or `import.meta`
|
|
196
|
+
* (`import()` alone is CommonJS too). `null` when the file has the words and the lexer can't parse
|
|
197
|
+
* it: a caller says what a file nothing can run counts as (Node reads it as ES module syntax,
|
|
198
|
+
* jest-runtime's `esmLexer.ts` `hasEsmSyntax` as none).
|
|
199
|
+
* @public
|
|
200
|
+
*/
|
|
201
|
+
function esmSyntaxOf(source) {
|
|
202
|
+
if (!MODULE_SYNTAX.test(source)) return false;
|
|
203
|
+
try {
|
|
204
|
+
return parse(source)[3];
|
|
205
|
+
} catch {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The edges of `source`. A file the lexer can't parse (it holds syntax no engine would run) is
|
|
211
|
+
* `dynamic`, and still gets its `require` edges.
|
|
212
|
+
* @public
|
|
213
|
+
*/
|
|
214
|
+
function scanSource(source) {
|
|
215
|
+
const edges = [];
|
|
216
|
+
const seen = /* @__PURE__ */ new Set();
|
|
217
|
+
const add = (specifier, kind) => {
|
|
218
|
+
const key = `${kind}\0${specifier}`;
|
|
219
|
+
if (seen.has(key)) return;
|
|
220
|
+
seen.add(key);
|
|
221
|
+
edges.push({
|
|
222
|
+
specifier,
|
|
223
|
+
kind
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
let dynamic = false;
|
|
227
|
+
let esm = false;
|
|
228
|
+
if (MODULE_SYNTAX.test(source)) try {
|
|
229
|
+
const [imports, , , hasModuleSyntax] = parse(source);
|
|
230
|
+
esm = hasModuleSyntax;
|
|
231
|
+
for (const found of imports) {
|
|
232
|
+
if (found.type === "import-meta") continue;
|
|
233
|
+
if (found.type === "dynamic") {
|
|
234
|
+
if (found.specifier === void 0 || found.glob) dynamic = true;
|
|
235
|
+
else add(found.specifier, "import");
|
|
236
|
+
} else if (!found.typeOnly) add(found.specifier, "import");
|
|
237
|
+
}
|
|
238
|
+
} catch {
|
|
239
|
+
dynamic = true;
|
|
240
|
+
esm = true;
|
|
241
|
+
}
|
|
242
|
+
if (source.includes("createRequire")) dynamic = true;
|
|
243
|
+
if (source.includes("require")) {
|
|
244
|
+
REQUIRE.lastIndex = 0;
|
|
245
|
+
for (let match = REQUIRE.exec(source); match !== null; match = REQUIRE.exec(source)) {
|
|
246
|
+
const at = skipTrivia(source, REQUIRE.lastIndex);
|
|
247
|
+
const next = source[at];
|
|
248
|
+
if (next === ".") continue;
|
|
249
|
+
if (next !== "(") {
|
|
250
|
+
dynamic = true;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const literal = literalAfter(source, at);
|
|
254
|
+
if (literal === null) {
|
|
255
|
+
dynamic = true;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
add(literal.specifier, "require");
|
|
259
|
+
REQUIRE.lastIndex = literal.end;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
edges,
|
|
264
|
+
dynamic,
|
|
265
|
+
esm
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// src/graph/build.ts
|
|
270
|
+
const NODE_MODULES = /[\\/]node_modules[\\/]/;
|
|
271
|
+
const SCANNED = /\.[cm]?[jt]sx?$/;
|
|
272
|
+
const NO_STAMP = {
|
|
273
|
+
mtimeMs: 0,
|
|
274
|
+
size: 0
|
|
275
|
+
};
|
|
276
|
+
const EMPTY = {
|
|
277
|
+
paths: [],
|
|
278
|
+
edges: [],
|
|
279
|
+
dynamic: false
|
|
280
|
+
};
|
|
281
|
+
/** Whether `path` is a real file under `node_modules` the graph covers. */
|
|
282
|
+
function inGraph(path) {
|
|
283
|
+
return NODE_MODULES.test(path);
|
|
284
|
+
}
|
|
285
|
+
function scannable(path) {
|
|
286
|
+
return SCANNED.test(path) && !path.endsWith(".d.ts");
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Builds a graph, empty but for the cache file's nodes. Nothing is scanned until an entry is
|
|
290
|
+
* added, and the lexer should be started first (`initLexer`).
|
|
291
|
+
* @public
|
|
292
|
+
*/
|
|
293
|
+
function buildThirdPartyGraph(options) {
|
|
294
|
+
const conditions = conditionsOf(options);
|
|
295
|
+
const key = options.cacheDir == null ? null : graphCacheKey(options.root, `${options.resolver.fingerprint}\0${conditions.join(",")}`, options.lockfileHash ?? null);
|
|
296
|
+
const file = key === null || options.cacheDir == null ? null : graphCacheFile(options.cacheDir, key);
|
|
297
|
+
const slots = /* @__PURE__ */ new Map();
|
|
298
|
+
const fresh = /* @__PURE__ */ new Map();
|
|
299
|
+
const closures = /* @__PURE__ */ new Map();
|
|
300
|
+
let edgeCount = 0;
|
|
301
|
+
let scannedCount = 0;
|
|
302
|
+
let fromCache = false;
|
|
303
|
+
let loaded = false;
|
|
304
|
+
function load() {
|
|
305
|
+
if (loaded) return;
|
|
306
|
+
loaded = true;
|
|
307
|
+
if (file === null || key === null) return;
|
|
308
|
+
const cached = readGraphCache(file, key);
|
|
309
|
+
if (cached === null) return;
|
|
310
|
+
fromCache = true;
|
|
311
|
+
for (const [path, stored] of cached) {
|
|
312
|
+
const slot = {
|
|
313
|
+
node: null,
|
|
314
|
+
stored,
|
|
315
|
+
stamp: {
|
|
316
|
+
mtimeMs: stored[3],
|
|
317
|
+
size: stored[4]
|
|
318
|
+
},
|
|
319
|
+
edgeCount: stored[2].length,
|
|
320
|
+
verified: false
|
|
321
|
+
};
|
|
322
|
+
slots.set(path, slot);
|
|
323
|
+
edgeCount += slot.edgeCount;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function scan(path) {
|
|
327
|
+
let source = "";
|
|
328
|
+
let stamp = NO_STAMP;
|
|
329
|
+
let transient = false;
|
|
330
|
+
if (scannable(path)) try {
|
|
331
|
+
const stats = statSync(path);
|
|
332
|
+
stamp = {
|
|
333
|
+
mtimeMs: stats.mtimeMs,
|
|
334
|
+
size: stats.size
|
|
335
|
+
};
|
|
336
|
+
source = readFileSync(path, "utf8");
|
|
337
|
+
} catch {
|
|
338
|
+
transient = true;
|
|
339
|
+
}
|
|
340
|
+
const scanned = scanSource(source);
|
|
341
|
+
return {
|
|
342
|
+
node: {
|
|
343
|
+
edges: scanned.edges.map(({ specifier, kind }) => {
|
|
344
|
+
let target = null;
|
|
345
|
+
try {
|
|
346
|
+
const result = options.resolver.resolve(specifier, {
|
|
347
|
+
importer: path,
|
|
348
|
+
kind,
|
|
349
|
+
conditions
|
|
350
|
+
});
|
|
351
|
+
if (result.type === "file") target = result.path;
|
|
352
|
+
else if (result.type === "builtin") target = result.id;
|
|
353
|
+
} catch {
|
|
354
|
+
transient = true;
|
|
355
|
+
}
|
|
356
|
+
return {
|
|
357
|
+
specifier,
|
|
358
|
+
kind,
|
|
359
|
+
target
|
|
360
|
+
};
|
|
361
|
+
}),
|
|
362
|
+
dynamic: scanned.dynamic || transient,
|
|
363
|
+
esm: scanned.esm
|
|
364
|
+
},
|
|
365
|
+
stamp,
|
|
366
|
+
transient
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
/** Puts a scanned file in the graph in place of what was there. */
|
|
370
|
+
function install(path, scanned) {
|
|
371
|
+
const old = slots.get(path);
|
|
372
|
+
if (old !== void 0) edgeCount -= old.edgeCount;
|
|
373
|
+
const slot = {
|
|
374
|
+
node: scanned.node,
|
|
375
|
+
stored: null,
|
|
376
|
+
stamp: scanned.stamp,
|
|
377
|
+
edgeCount: scanned.node.edges.length,
|
|
378
|
+
verified: true
|
|
379
|
+
};
|
|
380
|
+
slots.set(path, slot);
|
|
381
|
+
edgeCount += slot.edgeCount;
|
|
382
|
+
scannedCount++;
|
|
383
|
+
if (scanned.transient) fresh.delete(path);
|
|
384
|
+
else fresh.set(path, scanned);
|
|
385
|
+
if (old !== void 0) closures.clear();
|
|
386
|
+
return slot;
|
|
387
|
+
}
|
|
388
|
+
/** The node of a slot, built from its stored form on first use; a damaged one is rescanned. */
|
|
389
|
+
function nodeOf(path, slot) {
|
|
390
|
+
if (slot.node !== null) return slot.node;
|
|
391
|
+
const node = slot.stored === null ? null : fromStored(slot.stored);
|
|
392
|
+
if (node !== null) {
|
|
393
|
+
slot.node = node;
|
|
394
|
+
return node;
|
|
395
|
+
}
|
|
396
|
+
const scanned = scan(path);
|
|
397
|
+
install(path, scanned);
|
|
398
|
+
return scanned.node;
|
|
399
|
+
}
|
|
400
|
+
/** The slot of `path` as a walk should see it: stamped on its first visit in this worker. */
|
|
401
|
+
function visit(path, create) {
|
|
402
|
+
let slot = slots.get(path);
|
|
403
|
+
if (slot === void 0) {
|
|
404
|
+
if (!create || !inGraph(path)) return void 0;
|
|
405
|
+
return install(path, scan(path));
|
|
406
|
+
}
|
|
407
|
+
if (!slot.verified) {
|
|
408
|
+
slot.verified = true;
|
|
409
|
+
if (scannable(path)) {
|
|
410
|
+
let same = false;
|
|
411
|
+
try {
|
|
412
|
+
const stats = statSync(path);
|
|
413
|
+
same = stats.mtimeMs === slot.stamp.mtimeMs && stats.size === slot.stamp.size;
|
|
414
|
+
} catch {}
|
|
415
|
+
if (!same) slot = install(path, scan(path));
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return slot;
|
|
419
|
+
}
|
|
420
|
+
function closureOf(start, create) {
|
|
421
|
+
const known = closures.get(start);
|
|
422
|
+
if (known !== void 0) return known;
|
|
423
|
+
if (!create && !slots.has(start)) return void 0;
|
|
424
|
+
const seen = /* @__PURE__ */ new Set();
|
|
425
|
+
const entries = [];
|
|
426
|
+
const stack = [start];
|
|
427
|
+
while (stack.length > 0) {
|
|
428
|
+
const current = stack.pop();
|
|
429
|
+
if (current === void 0 || seen.has(current)) continue;
|
|
430
|
+
seen.add(current);
|
|
431
|
+
const slot = visit(current, true);
|
|
432
|
+
if (slot === void 0) continue;
|
|
433
|
+
const node = nodeOf(current, slot);
|
|
434
|
+
entries.push({
|
|
435
|
+
path: current,
|
|
436
|
+
node
|
|
437
|
+
});
|
|
438
|
+
for (const edge of node.edges) if (edge.target !== null && inGraph(edge.target)) stack.push(edge.target);
|
|
439
|
+
}
|
|
440
|
+
const closure = {
|
|
441
|
+
entries,
|
|
442
|
+
edges: null
|
|
443
|
+
};
|
|
444
|
+
closures.set(start, closure);
|
|
445
|
+
return closure;
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
add(entry) {
|
|
449
|
+
load();
|
|
450
|
+
const stack = [entry];
|
|
451
|
+
while (stack.length > 0) {
|
|
452
|
+
const path = stack.pop();
|
|
453
|
+
if (path === void 0 || !inGraph(path) || slots.has(path)) continue;
|
|
454
|
+
const scanned = scan(path);
|
|
455
|
+
install(path, scanned);
|
|
456
|
+
for (const edge of scanned.node.edges) if (edge.target !== null && inGraph(edge.target) && !slots.has(edge.target)) stack.push(edge.target);
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
has: (path) => slots.has(path),
|
|
460
|
+
nodeOf(path) {
|
|
461
|
+
const slot = slots.get(path);
|
|
462
|
+
return slot === void 0 ? void 0 : nodeOf(path, slot);
|
|
463
|
+
},
|
|
464
|
+
visitNode(path) {
|
|
465
|
+
load();
|
|
466
|
+
const slot = visit(path, true);
|
|
467
|
+
return slot === void 0 ? void 0 : nodeOf(path, slot);
|
|
468
|
+
},
|
|
469
|
+
reachable: (path) => closureOf(path, false)?.entries ?? [],
|
|
470
|
+
edgesOf(path) {
|
|
471
|
+
load();
|
|
472
|
+
if (!inGraph(path)) return EMPTY;
|
|
473
|
+
const closure = closureOf(path, true);
|
|
474
|
+
if (closure === void 0) return EMPTY;
|
|
475
|
+
if (closure.edges === null) {
|
|
476
|
+
const asked = /* @__PURE__ */ new Set();
|
|
477
|
+
const edges = [];
|
|
478
|
+
let dynamic = false;
|
|
479
|
+
for (const { path: importer, node } of closure.entries) {
|
|
480
|
+
if (node.dynamic) dynamic = true;
|
|
481
|
+
for (const edge of node.edges) {
|
|
482
|
+
const request = `${edge.kind}\0${edge.target ?? ""}\0${edge.specifier}`;
|
|
483
|
+
if (asked.has(request)) continue;
|
|
484
|
+
asked.add(request);
|
|
485
|
+
edges.push({
|
|
486
|
+
importer,
|
|
487
|
+
edge
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
closure.edges = {
|
|
492
|
+
paths: closure.entries.map((entry) => entry.path),
|
|
493
|
+
edges,
|
|
494
|
+
dynamic
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
return closure.edges;
|
|
498
|
+
},
|
|
499
|
+
paths: () => slots.keys(),
|
|
500
|
+
get size() {
|
|
501
|
+
return slots.size;
|
|
502
|
+
},
|
|
503
|
+
get edgeCount() {
|
|
504
|
+
return edgeCount;
|
|
505
|
+
},
|
|
506
|
+
get scanned() {
|
|
507
|
+
return scannedCount;
|
|
508
|
+
},
|
|
509
|
+
get fromCache() {
|
|
510
|
+
return fromCache;
|
|
511
|
+
},
|
|
512
|
+
save() {
|
|
513
|
+
if (file === null || key === null || fresh.size === 0) return;
|
|
514
|
+
const stored = /* @__PURE__ */ new Map();
|
|
515
|
+
for (const [path, scanned] of fresh) stored.set(path, toStored(scanned.node, scanned.stamp));
|
|
516
|
+
writeGraphCache(file, key, stored);
|
|
517
|
+
fresh.clear();
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// src/graph/package-dir.ts
|
|
523
|
+
/**
|
|
524
|
+
* The directory of the package a `node_modules` file belongs to (`…/node_modules/@scope/name`),
|
|
525
|
+
* with `/` for separators, or `null` for a path that has no package directory (it is not under
|
|
526
|
+
* `node_modules`, or it is a package's directory itself). The innermost `node_modules` counts, so a
|
|
527
|
+
* nested copy is its own package.
|
|
528
|
+
* @public
|
|
529
|
+
*/
|
|
530
|
+
function packageDirOf(path) {
|
|
531
|
+
const normal = path.replaceAll("\\", "/");
|
|
532
|
+
const at = normal.lastIndexOf("/node_modules/");
|
|
533
|
+
if (at === -1) return null;
|
|
534
|
+
const start = at + 14;
|
|
535
|
+
const first = normal.indexOf("/", start);
|
|
536
|
+
if (first === -1) return null;
|
|
537
|
+
if (normal[start] !== "@") return normal.slice(0, first);
|
|
538
|
+
const second = normal.indexOf("/", first + 1);
|
|
539
|
+
return second === -1 ? null : normal.slice(0, second);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// src/graph/query.ts
|
|
543
|
+
/**
|
|
544
|
+
* Every file in the graph that can reach any of `targets` through its edges (a target is
|
|
545
|
+
* included only if another target, or itself through a cycle, reaches it), in no particular order.
|
|
546
|
+
* @public
|
|
547
|
+
*/
|
|
548
|
+
function importersOf(graph, targets) {
|
|
549
|
+
const wanted = typeof targets === "string" ? [targets] : targets;
|
|
550
|
+
const importers = /* @__PURE__ */ new Map();
|
|
551
|
+
for (const path of graph.paths()) for (const edge of graph.nodeOf(path)?.edges ?? []) {
|
|
552
|
+
if (edge.target === null) continue;
|
|
553
|
+
const list = importers.get(edge.target);
|
|
554
|
+
if (list === void 0) importers.set(edge.target, [path]);
|
|
555
|
+
else list.push(path);
|
|
556
|
+
}
|
|
557
|
+
const found = /* @__PURE__ */ new Set();
|
|
558
|
+
const stack = [...wanted];
|
|
559
|
+
while (stack.length > 0) {
|
|
560
|
+
const target = stack.pop();
|
|
561
|
+
if (target === void 0) continue;
|
|
562
|
+
for (const importer of importers.get(target) ?? []) {
|
|
563
|
+
if (found.has(importer)) continue;
|
|
564
|
+
found.add(importer);
|
|
565
|
+
stack.push(importer);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return [...found];
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
export { esmSyntaxOf as a, conditionsOf as c, inGraph as i, graphCacheKey as l, packageDirOf as n, initLexer as o, buildThirdPartyGraph as r, scanSource as s, importersOf as t };
|