Skip to content

Commit 7b55547

Browse files
cyfung1031CodFrm
andauthored
👷 rspack 打包优化 (#982)
* rspack 打包优化 * 修改eslint --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent 5df4899 commit 7b55547

3 files changed

Lines changed: 144 additions & 32 deletions

File tree

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@eslint/js": "9.38.0",
5858
"@rspack/cli": "^1.5.8",
5959
"@rspack/core": "^1.5.8",
60+
"@swc/helpers": "^0.5.17",
6061
"@testing-library/jest-dom": "^6.6.3",
6162
"@testing-library/react": "^16.3.0",
6263
"@types/chrome": "^0.1.27",
@@ -92,5 +93,10 @@
9293
"unocss": "66.5.4",
9394
"vitest": "^3.2.4"
9495
},
95-
"packageManager": "pnpm@10.12.4"
96+
"packageManager": "pnpm@10.12.4",
97+
"sideEffects": [
98+
"**/*.css",
99+
"**/*.scss",
100+
"**/*.less"
101+
]
96102
}

pnpm-lock.yaml

Lines changed: 18 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rspack.config.ts

Lines changed: 119 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as path from "path";
22
import { defineConfig } from "@rspack/cli";
33
import { rspack } from "@rspack/core";
44
import { readFileSync } from "fs";
5+
import { NormalModule } from "@rspack/core";
56

6-
const pkg = JSON.parse(readFileSync("./package.json") as unknown as string);
7+
const pkg = JSON.parse(readFileSync("./package.json", "utf-8"));
78

89
const version = pkg.version;
910
const dirname = path.resolve();
@@ -13,9 +14,12 @@ const isBeta = version.includes("-");
1314
// Target browsers, see: https://github.com/browserslist/browserslist
1415
const targets = ["chrome >= 87", "edge >= 88", "firefox >= 78", "safari >= 14"];
1516

16-
const src = `${dirname}/src`;
17-
const dist = `${dirname}/dist`;
18-
const assets = `${src}/assets`;
17+
const src = path.join(dirname, "src");
18+
const dist = path.join(dirname, "dist");
19+
const assets = path.join(src, "assets");
20+
21+
// 排除这些文件,不进行分离
22+
const chunkExcludeSet = new Set(["editor.worker", "ts.worker", "linter.worker", "service_worker", "content", "inject"]);
1923

2024
export default defineConfig({
2125
...(isDev
@@ -81,6 +85,7 @@ export default defineConfig({
8185
loader: "builtin:swc-loader",
8286
options: {
8387
jsc: {
88+
externalHelpers: true,
8489
parser: {
8590
syntax: "typescript",
8691
tsx: true,
@@ -201,25 +206,124 @@ export default defineConfig({
201206
chunks: ["sandbox"],
202207
}),
203208
].filter(Boolean),
209+
experiments: {
210+
css: true,
211+
parallelCodeSplitting: true,
212+
parallelLoader: true,
213+
},
204214
optimization: {
205215
minimizer: [
206-
new rspack.SwcJsMinimizerRspackPlugin({}),
216+
new rspack.SwcJsMinimizerRspackPlugin({
217+
minimizerOptions: {
218+
minify: !isDev,
219+
mangle: {
220+
keep_classnames: false,
221+
keep_fnames: false,
222+
keep_private_props: false,
223+
ie8: false,
224+
toplevel: true,
225+
},
226+
module: true,
227+
compress: {
228+
passes: 2,
229+
drop_console: false,
230+
drop_debugger: !isDev,
231+
ecma: 2020,
232+
arrows: true,
233+
dead_code: true,
234+
ie8: false,
235+
keep_classnames: false,
236+
keep_fargs: false,
237+
keep_fnames: false,
238+
toplevel: true,
239+
sequences: true,
240+
hoist_props: false,
241+
hoist_vars: false,
242+
reduce_funcs: true,
243+
reduce_vars: true,
244+
pure_getters: "strict",
245+
},
246+
format: {
247+
comments: false,
248+
beautify: false,
249+
ecma: 2020,
250+
},
251+
},
252+
}),
207253
new rspack.LightningCssMinimizerRspackPlugin({
208254
minimizerOptions: { targets },
209255
}),
210256
],
257+
removeAvailableModules: true,
258+
removeEmptyChunks: true,
259+
realContentHash: true,
260+
sideEffects: true,
261+
providedExports: true,
262+
concatenateModules: true,
263+
avoidEntryIife: true,
264+
mergeDuplicateChunks: true,
211265
splitChunks: {
212-
chunks: (chunk) => {
213-
// 排除这些文件,不进行分离
214-
return !["editor.worker", "ts.worker", "linter.worker", "service_worker", "content", "inject"].includes(
215-
chunk.name || ""
216-
);
266+
minChunks: 1,
267+
maxAsyncRequests: 30,
268+
maxInitialRequests: 30,
269+
minSize: {
270+
javascript: 40 * 1024, // 40 kB
271+
css: 10 * 1024, // 10 kB
272+
},
273+
maxSize: {
274+
javascript: 2 * 1024 * 1024, // 2 MB
275+
css: 2 * 1024 * 1024, // 2 MB
276+
},
277+
chunks: (chunk) => !chunkExcludeSet.has(chunk.name || ""),
278+
hidePathInfo: false,
279+
name: (module, _ctx) => {
280+
if (module instanceof NormalModule) {
281+
const p = `/${module.rawRequest}|/${module.resource}`.toLowerCase().replace(/[\\@/]+/g, "/");
282+
if (p.startsWith("/packages/message/")) return "lib_message";
283+
if (module.type === "json" && p.includes("translation.json")) return "translation_json";
284+
let tag = "";
285+
const idx = p.indexOf("/node_modules/");
286+
if (idx >= 0) {
287+
let q = p.replace(/\.pnpm\/?/g, "");
288+
q = q.substring(idx);
289+
q = q.replace(/\..*/, "");
290+
tag = q.split("/")[2] || "";
291+
}
292+
if (module.type !== "css" && tag === "monaco-editor") return "lib_monaco";
293+
switch (tag) {
294+
case "react-icons":
295+
if (p.includes("/react-icons/tb")) return undefined;
296+
// eslint-disable-next-line no-fallthrough
297+
case "react-dropzone":
298+
case "react-dom":
299+
case "react-i18next":
300+
case "react-router-dom":
301+
case "react-joyride":
302+
case "react":
303+
return `lib_${tag}`;
304+
}
305+
if (tag.startsWith("dnd-kit")) return "lib_dnd-kit";
306+
if (tag.startsWith("popper")) return "lib_react-joyride";
307+
if (tag.startsWith("react-")) return "lib_react";
308+
if (tag.startsWith("eslint")) return "lib_eslint";
309+
if (tag.startsWith("i18n")) return "lib_i18n";
310+
if (
311+
tag.startsWith("arco-design") ||
312+
tag === "resize-observer-polyfill" ||
313+
tag === "b-validate" ||
314+
tag === "lodash" ||
315+
tag === "focus-lock"
316+
) {
317+
return "lib_arco_design";
318+
}
319+
if (tag) {
320+
// cron, dayjs, yaml, jszip, prettier, ...
321+
if (tag === "luxon") return "lib_cron";
322+
return `lib_${tag}`;
323+
}
324+
return "chunk";
325+
}
217326
},
218-
minSize: 307200,
219-
maxSize: 4194304,
220327
},
221328
},
222-
experiments: {
223-
css: true,
224-
},
225329
});

0 commit comments

Comments
 (0)