phase 5 part 2: vite 8, webpack 5, fe-testing-perf topics with verified examples

This commit is contained in:
Shikui Wang committed 2026-10-23 16:29:27 +08:00
1 parent 7b5cae7d24
commit 9eeb10aede
38 files changed
+1823 -37

No files matched your search

@@ -0,0 +1,7 @@
// 不自动运行:webpack loader,由 webpack 调用
// loader 是一个函数:输入文件内容,返回 JS 代码字符串
module.exports = function mdLoader(source) {
const { trim = false } = this.getOptions();
const text = trim ? source.trim() : source;
return `export default ${JSON.stringify(text)};`;
};
@@ -0,0 +1,14 @@
{
"name": "webpack-config-demo",
"private": true,
"type": "module",
"sideEffects": ["*.css"],
"scripts": {
"build": "webpack --mode production",
"analyze": "webpack --mode production --json > stats.json"
},
"devDependencies": {
"webpack": "^5.112.0",
"webpack-cli": "^7.0.0"
}
}
@@ -0,0 +1,4 @@
// 不自动运行:由 webpack 打包
import { formatPrice } from "./utils.js";
console.log("admin", formatPrice(500));
@@ -0,0 +1,7 @@
// 不自动运行:由 webpack 打包
import { formatPrice } from "./utils.js";
import notice from "./notice.md";
console.log(formatPrice(1999), notice.split("\n")[0]);
// 动态 import:单独的 chunk,用到时才加载
import("./report.js").then((m) => console.log(m.buildReport([1, 2, 3])));
@@ -0,0 +1,5 @@
# 公告
退货政策调整,详见帮助中心。
@@ -0,0 +1,4 @@
// 不自动运行:被动态 import
export function buildReport(nums) {
return { count: nums.length, sum: nums.reduce((a, b) => a + b, 0) };
}
@@ -0,0 +1,8 @@
// 不自动运行:由 webpack 打包
export function formatPrice(cents) {
return `¥${(cents / 100).toFixed(2)}`;
}
// 没有被任何地方使用:生产构建时会被 tree shaking 删除
export function unusedHelper() {
return "UNUSED_HELPER_MARKER";
}
@@ -0,0 +1,49 @@
// 不自动运行:需要先 npm install,然后 npm run build
// webpack 5 配置示例:loader、plugin、splitChunks、contenthash、tree shaking、持久化缓存。以官方文档为准:https://webpack.js.org/concepts/
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// 自定义 plugin:构建结束后打印每个产物的大小(plugin = 在编译生命周期的钩子上做事)
class SizeReportPlugin {
apply(compiler) {
compiler.hooks.done.tap("SizeReportPlugin", (stats) => {
const assets = stats.toJson({ all: false, assets: true }).assets ?? [];
for (const a of assets.sort((x, y) => y.size - x.size)) {
console.log(`[size] ${a.name.padEnd(36)} ${(a.size / 1024).toFixed(2)} KiB`);
}
});
}
}
export default (env, argv) => {
const isProd = argv.mode === "production";
return {
entry: { main: "./src/index.js", admin: "./src/admin.js" }, // 两个入口,共享 utils 和第三方代码
output: {
path: path.resolve(__dirname, "dist"),
filename: isProd ? "[name].[contenthash:8].js" : "[name].js", // 内容变了文件名才变,利于长期缓存
clean: true,
},
module: {
rules: [
// 自定义 loader:把 .md 文件转成导出字符串的 JS 模块(loader = 把某类文件转换成 JS 模块)
{ test: /\.md$/, use: [{ loader: path.resolve(__dirname, "loaders/md-loader.cjs"), options: { trim: true } }] },
],
},
optimization: {
usedExports: true, // 标记未使用的导出,压缩时删除(tree shaking)
runtimeChunk: "single", // webpack 运行时单独一个文件,避免业务代码变化影响 vendor 的哈希
splitChunks: {
chunks: "all",
cacheGroups: {
shared: { name: "shared", minChunks: 2, minSize: 0, priority: 10 }, // 两个入口都用到的代码
},
},
},
cache: { type: "filesystem" }, // 持久化缓存:第二次构建明显变快
plugins: [new SizeReportPlugin()],
devtool: isProd ? false : "eval-cheap-module-source-map",
};
};