1. html-webpack-externals-plugin widget
Installation html-webpack-externals-plugin
yarn add html-webpack-externals-plugin
Configuration webpack.config.js (page automatic introduction Once you've configured)
Demo CDN link
const HtmlWebpackExternalsPlugin = require("html-webpack-externals-plugin");
module.exports = {
plugins: [
new HtmlWebpackExternalsPlugin({
externals: [
{
module: "react",
entry: "https://unpkg.com/react@16/umd/react.production.min.js",
global: "React"
},
{
module: "react-dom",
entry:
"https://unpkg.com/react-dom@16/umd/react-dom.production.min.js",
global: "ReactDOM"
}
]
}),
],
};
After comparing pulled package
(Former detached)
After detached
2. Use webpack comes SplitChunksPlugin plug- split-chunks-plugin
module.exports = {
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /(react|react-dom)/,
name: "vendors",
chunks: "all"
}
}
}
}
};
Packaging npm run build
You can see have been constructed out
The need to introduce the use of words in html template, but also to change the multi-entry mode
module.exports = {
entry: {
index: "./src/index.js" // 这里改成多入口
},
output: {
path: path.join(__dirname, "dist"),
filename: "[name]_[hash:8].js"
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, "index.html"),
filename: "index.html",
chunks: ["vendors", "index"] // 这里改成 chunks 引入
}),
],
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /(react|react-dom)/,
name: "vendors",
chunks: "all"
}
}
}
},
};
Bale
3. Public method used repeatedly pulled out, still need to configure multiple entry
module.exports = {
entry: {
index: "./src/index.js"
},
output: {
path: path.join(__dirname, "dist"),
filename: "[name]_[hash:8].js"
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, "index.html"),
filename: "index.html",
chunks: ["commons", "index"]
}),
],
optimization: {
splitChunks: {
minSize: 0,
cacheGroups: {
commons: {
name: "commons",
chunks: "all",
minChunks: 1 // 某个方法至少引用了两次,才会单独抽离
}
}
}
},
};