文章首發於:github.com/USTB-musion…css
webpack能夠說是當下最流行的打包庫,當webpack處理應用程序時,它會遞歸地構建一個依賴關係圖,其中包含應用程序須要的每個模塊,而後將全部這些模塊打包成一個或多個bundle。這篇文章將介紹webpack很是重要的一部分——性能優化。文章的代碼的在此: github.com/USTB-musion…html
本文將從如下幾部分進行總結:前端
啓用noParse:vue
module: {
// 不去解析jquery的依賴關係
noParse: /jquery/
},
複製代碼
ignorePlugin啓用方法:node
// 用法:
new webpack.IgnorePlugin(requestRegExp, [contextRegExp]);
//eg.
plugins: [new webpack.IgnorePlugin(/\.\/local/, /moment/)];
複製代碼
let path = require("path");
let webpack = require("webpack");
module.exports = {
mode: "development",
entry: {
react: ["react", "react-dom"]
},
output: {
filename: "_dll_[name].js", // 產生的文件名
path: path.resolve(__dirname, "dist"),
library: "_dll_[name]"
},
plugins: [
// name要等於library裏的name
new webpack.DllPlugin({
name: "_dll_[name]",
path: path.resolve(__dirname, "dist", "manifest.json")
})
]
};
複製代碼
let path = require("path");
let HtmlWebpackPlugin = require("html-webpack-plugin");
let webpack = require("webpack");
module.exports = {
mode: "development",
// 多入口
entry: {
home: "./src/index.js"
},
devServer: {
port: 3000,
open: true,
contentBase: "./dist"
},
module: {
// 不去解析jquery的依賴關係
noParse: /jquery/,
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
{
test: /\.js$/,
exclude: /node_modules/,
include: path.resolve("src"),
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"]
}
}
}
]
},
output: {
// name -> home a
filename: "[name].js",
path: path.resolve(__dirname, "dist")
},
plugins: [
new webpack.DllReferencePlugin({
manifest: path.resolve(__dirname, "dist", "manifest.json")
}),
new webpack.IgnorePlugin(/\.\/local/, /moment/),
new HtmlWebpackPlugin({
template: "./src/index.html",
filename: "index.html"
}),
new webpack.DefinePlugin({
DEV: JSON.stringify("production")
})
]
};
複製代碼
將loader中的配置轉移到happypack中就好:react
let path = require("path");
let HtmlWebpackPlugin = require("html-webpack-plugin");
let webpack = require("webpack");
// 模塊 happypack 能夠實現多線程📦
let Happypack = require("happypack");
module.exports = {
mode: "development",
// 多入口
entry: {
home: "./src/index.js"
},
devServer: {
port: 3000,
open: true,
contentBase: "./dist"
},
module: {
// 不去解析jquery的依賴關係
noParse: /jquery/,
rules: [
{
test: /\.css$/,
use: "Happypack/loader?id=css"
},
{
test: /\.js$/,
exclude: /node_modules/,
include: path.resolve("src"),
use: "Happypack/loader?id=js"
}
]
},
output: {
// name -> home a
filename: "[name].js",
path: path.resolve(__dirname, "dist")
},
plugins: [
new Happypack({
id: "css",
use: ["style-loader", "css-loader"]
}),
new Happypack({
id: "js",
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"]
}
}
]
}),
new webpack.DllReferencePlugin({
manifest: path.resolve(__dirname, "dist", "manifest.json")
}),
new webpack.IgnorePlugin(/\.\/local/, /moment/),
new HtmlWebpackPlugin({
template: "./src/index.html",
filename: "index.html"
}),
new webpack.DefinePlugin({
DEV: JSON.stringify("production")
})
]
};
複製代碼
把公共代碼抽離出來的好處:jquery
// webpack 4.x版本以前的commonChunkPlugins
optimization: {
// 分割代碼塊
splitChunks: {
// 緩存組
cacheGroups: {
// 公共模塊
common: {
chunks: "initial",
minSize: 0,
// 最小公用模塊次數
minChunks: 2
},
vendor: {
priority: 1,
// 抽離出來
test: /node_modules/,
chunks: "initial",
minSize: 0,
minChunks: 2
}
}
}
}
複製代碼
按需加載的思想webpack
經過 es6 的 import 實現按需加載,在使用 import() 分割代碼後,你的瀏覽器而且要支持 Promise API 才能讓代碼正常運行, 由於 import() 返回一個 Promise,它依賴 Promise。對於不原生支持 Promise 的瀏覽器,你能夠注入 Promise polyfill。git
let button = document.createElement("button");
button.innerHTML = "musion";
// vue,react的懶加載原理也是如此
button.addEventListener("click", function() {
// es6草案中的語法, jsonp實現動態加載文件
import("./source.js").then(data => {
console.log(data.default);
});
console.log("click");
});
document.body.appendChild(button);
複製代碼
啓用HRMes6
let path = require("path");
let HtmlWebpackPlugin = require("html-webpack-plugin");
let webpack = require("webpack");
module.exports = {
mode: "production",
// 多入口
entry: {
index: "./src/index.js",
other: "./src/other.js"
},
devServer: {
// 啓用熱更新
hot: true,
port: 3000,
open: true,
contentBase: "./dist"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
include: path.resolve("src"),
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: ["@babel/plugin-syntax-dynamic-import"]
}
}
}
]
},
output: {
// name -> home a
filename: "[name].js",
path: path.resolve(__dirname, "dist")
},
plugins: [
new webpack.NamedModulesPlugin(), // 打印更新的模塊路徑
new webpack.HotModuleReplacementPlugin() // 熱更新插件
]
};
複製代碼