-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsplitChunks.ts
More file actions
134 lines (127 loc) · 3.52 KB
/
splitChunks.ts
File metadata and controls
134 lines (127 loc) · 3.52 KB
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
interface SplitChunkStrategyItem {
match: string | RegExp | (string | RegExp)[];
name: string | ((s: string) => string);
}
type SplitChunkStrategy = SplitChunkStrategyItem[];
export const strategy: SplitChunkStrategy = [
// Split Monaco into its own chunk to keep app chunk smaller
{
match: [/^monaco-editor$/],
name: "monaco",
},
// Heavy editors: keep Excalidraw / Zenmark in their own async chunks
{
match: [/^@excalidraw\/excalidraw$/],
name: "excalidraw",
},
{
match: [/^zenmark-editor$/],
name: "zenmark-editor",
},
{
match: [
/^react$/,
/^react-dom$/,
/^react-router-dom$/,
/^react-redux$/,
/^@reduxjs/,
],
name: "vendor",
},
{
match: [/^@chakra-ui/, /^@emotion/, /^framer-motion/],
name: "chakra-ui",
},
{
match: [/react-icons/, /usehooks-ts/],
name: "react-utils",
},
{
match: [/fuse.js/, /history/, /nanoid/],
name: "common-utils",
},
{
match: [
/remark-gfm/,
/remark-math/,
/remark/,
/react-syntax-highlighter/,
/rehype-katex/,
/rehype/,
/react-markdown/,
/device-detector-js/,
/crisp-sdk-web/,
/@cloudbase\/js-sdk/,
/recharts/,
],
name: (file) => {
return file;
},
},
];
/**
* Generate a Rollup/Vite `manualChunks` function from the current strategy.
* Rolldown (rolldown-vite) only accepts a function here, not an object,
* so we build a dep -> chunk name map and return a resolver function.
*/
export const renderChunksWithStrategy = (deps: Record<string, string>) => {
const depNames = Object.keys(deps).filter(
(dep) => !dep.startsWith("@types"),
);
const chunks: Record<string, string[]> = {};
const depToChunk: Record<string, string> = {};
const match = (ptns: string | RegExp | (string | RegExp)[], s: string) => {
const matchOne = (ptn: string | RegExp, s: string) => {
if (typeof ptn === "string") ptn = new RegExp("^" + ptn + "$");
return ptn.test(s);
};
if (!Array.isArray(ptns)) ptns = [ptns];
for (let i = 0; i < ptns.length; i++) {
if (matchOne(ptns[i], s)) return true;
}
return false;
};
for (let i = 0; i < depNames.length; i++) {
const dep = depNames[i];
for (let j = 0; j < strategy.length; j++) {
const rule = strategy[j];
if (match(rule.match, dep)) {
const name =
typeof rule.name === "function" ? rule.name(dep) : rule.name;
if (!(name in chunks)) {
chunks[name] = [];
}
chunks[name].push(dep);
depToChunk[dep] = name;
break; //stop matching
}
}
}
if (process.env.VITE_DEBUG_CHUNKS === "1") {
console.log("chunks:", chunks);
}
// manualChunks resolver for Rollup / Rolldown
return (id: string) => {
if (!id.includes("node_modules")) return;
let pkgName: string | undefined;
// pnpm layout: node_modules/.pnpm/<name>@<ver>/node_modules/<name>/...
const pnpmMatch = id.match(/node_modules\/\.pnpm\/([^@/]+)@/);
if (pnpmMatch) {
// Scoped packages are encoded as @scope+name
pkgName = pnpmMatch[1].replace(/\+/g, "/");
} else {
// Fallback: classic node_modules layout
const scopedMatch = id.match(/node_modules\/(@[^/]+\/[^/]+)/);
if (scopedMatch) {
pkgName = scopedMatch[1];
} else {
const plainMatch = id.match(/node_modules\/([^/]+)/);
if (plainMatch) {
pkgName = plainMatch[1];
}
}
}
if (!pkgName) return;
return depToChunk[pkgName];
};
};