forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-tree-shaking.js
79 lines (65 loc) · 1.86 KB
/
check-tree-shaking.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
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
import fs from "fs/promises";
import { rollup } from "rollup";
const packageJson = JSON.parse(await fs.readFile("package.json", "utf-8"));
export function listEntrypoints() {
const exports = packageJson.exports;
const entrypoints = [];
for (const [key, value] of Object.entries(exports)) {
if (typeof value === "string") {
entrypoints.push(value);
} else if (typeof value === "object") {
entrypoints.push(value.import);
}
}
return entrypoints;
}
export function listExternals() {
return [
...Object.keys(packageJson.dependencies),
...Object.keys(packageJson.peerDependencies),
/node\:/,
"axios", // axios is a dependency of openai
"@fortaine/fetch-event-source/parse",
"pdf-parse/lib/pdf-parse.js",
];
}
export async function checkTreeShaking() {
const externals = listExternals();
const entrypoints = listEntrypoints();
const consoleLog = console.log;
const reportMap = new Map();
for (const entrypoint of entrypoints) {
let sideEffects = "";
console.log = function (...args) {
const line = args.length ? args.join(" ") : "";
if (line.trim().startsWith("First side effect in")) {
sideEffects += line + "\n";
}
};
await rollup({
external: externals,
input: entrypoint,
experimentalLogSideEffects: true,
});
reportMap.set(entrypoint, {
log: sideEffects,
hasSideEffects: sideEffects.length > 0,
});
}
console.log = consoleLog;
let failed = false;
for (const [entrypoint, report] of reportMap) {
if (report.hasSideEffects) {
failed = true;
console.log("---------------------------------");
console.log(`Tree shaking failed for ${entrypoint}`);
console.log(report.log);
}
}
if (failed) {
process.exit(1);
} else {
console.log("Tree shaking checks passed!");
}
}
checkTreeShaking();