-
Notifications
You must be signed in to change notification settings - Fork 124
/
bundle-utils.js
36 lines (30 loc) · 1.05 KB
/
bundle-utils.js
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
import path from "path";
export function getCodeAndDependencies(bundle, name) {
const chunk = findChunkWithName(bundle, name);
const output = [];
const crawlDependencies = new Set([chunk.fileName]);
for (const fileName of crawlDependencies) {
const chunk = bundle[fileName];
output.push(chunk.code);
for (const dep of chunk.imports) {
crawlDependencies.add(dep);
}
}
return output.join("\n");
}
export function findChunkWithName(bundle, name) {
return Object.values(bundle).find(desc =>
(desc.facadeModuleId || "").endsWith(name)
);
}
export function findAssetWithName(bundle, name) {
const parsedName = path.parse(name);
return Object.values(bundle).find(desc => {
if (!desc.isAsset) return false;
const parsedGraphName = path.parse(desc.fileName);
if (parsedGraphName.ext !== parsedName.ext) return false;
if (!parsedGraphName.name.startsWith(parsedName.name)) return false;
const expectedHash = parsedGraphName.name.slice(parsedName.name.length);
return /^-[0-9a-f]+$/.test(expectedHash);
});
}