认识代码分离
多入口起点
Entry Dependencies(入口依赖)
SplitChunks
SplitChunks自定义配置
- 当然,我们可以自定义更多配置,我们来了解几个非常关键的属性:
SplitChunks自定义配置解析
SplitChunks自定义配置解析
动态导入(dynamic import)
动态导入的文件命名
代码的懒加载
optimization.chunkIds配置
optimization. runtimeChunk配置
Prefetch和Preload
目录结构
package.json
{
"name": "webpack_devserver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --config ./config/webpack.common.js --env production",
"serve": "webpack serve --config ./config/webpack.common.js --env development"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.12.17",
"@babel/preset-env": "^7.12.17",
"@babel/preset-react": "^7.12.13",
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
"babel-loader": "^8.2.2",
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^5.0.2",
"html-webpack-plugin": "^5.2.0",
"react-refresh": "^0.9.0",
"style-loader": "^2.0.0",
"vue-loader": "^15.9.6",
"vue-template-compiler": "^2.6.12",
"webpack": "^5.23.0",
"webpack-cli": "^4.5.0",
"webpack-dev-server": "^3.11.2",
"webpack-merge": "^5.7.3"
},
"dependencies": {
"axios": "^0.21.1",
"dayjs": "^1.10.4",
"express": "^4.17.1",
"lodash": "^4.17.21",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"vue": "^2.6.12",
"webpack-dev-middleware": "^4.1.0"
}
}
webpack.common.js
const resolveApp = require("./paths");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const VueLoaderPlugin = require("vue-loader/lib/plugin");
const TerserPlugin = require("terser-webpack-plugin");
const { merge } = require("webpack-merge");
const prodConfig = require("./webpack.prod");
const devConfig = require("./webpack.dev");
const commonConfig = {
entry: {
main: "./src/main.js",
index: "./src/index.js"
},
output: {
path: resolveApp("./build"),
filename: "[name].bundle.js",
chunkFilename: "[name].[hash:6].chunk.js"
},
resolve: {
extensions: [".wasm", ".mjs", ".js", ".json", ".jsx", ".ts", ".vue"],
alias: {
"@": resolveApp("./src"),
pages: resolveApp("./src/pages"),
},
},
optimization: {
minimizer: [
new TerserPlugin({
extractComments: false,
}),
],
splitChunks: {
chunks: "all",
minSize: 20000,
maxSize: 20000,
minChunks: 1,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
filename: "[id]_vendors.js",
priority: -10
},
default: {
minChunks: 2,
filename: "common_[id].js",
priority: -20
}
}
},
runtimeChunk: {
name: function(entrypoint) {
return `why-${entrypoint.name}`
}
}
},
module: {
rules: [
{
test: /\.jsx?$/i,
use: "babel-loader",
},
{
test: /\.vue$/i,
use: "vue-loader",
},
{
test: /\.css/i,
use: ["style-loader", "css-loader"],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./index.html",
}),
new VueLoaderPlugin(),
],
};
module.exports = function(env) {
const isProduction = env.production;
process.env.NODE_ENV = isProduction ? "production" : "development";
const config = isProduction ? prodConfig : devConfig;
const mergeConfig = merge(commonConfig, config);
return mergeConfig;
};
main.js
import _ from 'lodash';
import dayjs from 'dayjs';
import "./bar_01";
import "./bar_02";
console.log("Hello Main");
console.log(_.join(["Hello", "Main"]));
console.log(dayjs(), "Main");
import("./foo").then(res => {
console.log(res);
});
import("./foo_02").then(res => {
console.log(res);
});
index.js
import "./bar_01";
console.log("Hello Index");
const button = document.createElement("button");
button.innerHTML = "加载元素";
button.addEventListener("click", () => {
import(
"./element"
).then(({ default: element }) => {
document.body.appendChild(element);
})
});
document.body.appendChild(button);
babel.config.js
const presets = [
["@babel/preset-env"],
["@babel/preset-react"],
];
const plugins = [];
const isProduction = process.env.NODE_ENV === "production";
if (!isProduction) {
plugins.push(["react-refresh/babel"]);
} else {
}
module.exports = {
presets,
plugins
}