一、什么是webpack的多进程打包
Webpack的多进程打包是通过利用多个子进程同时处理不同的模块来加速打包过程的技术。这种技术可以提高打包效率,减少等待时间,使得项目构建更快。
使用多进程打包的好处在于,它可以将一些比较耗时的操作分配到不同的进程中进行并行处理,从而提高打包效率。这使得打包过程更加高效,节省了时间和资源。另外,和单进程打包相比,多进程打包也有助于避免进程卡死或异常终止的问题。
对于大型项目或需要进行复杂操作的项目,采用多进程打包可以更快地完成项目构建,提高效率,减少等待时间,使开发人员可以更快地检验代码。
但是对于小型项目,比如我这个学习的demo其实使用多进程打包反而会比单进程慢,应为没多开启一个进程都要耗费一定时间和性能
二,为js和eslint配置多进程
我们启动进程的数量就是我们 CPU 的核数。
1. 获取 CPU 的核数,因为每个电脑都不一样。
const os = require("os");
const threads = os.cpus().length;
2. 下载包
npm i thread-loader -D
3. 核心配置
const os = require('os')
const threads = os.cpus().length
const path = require('path')
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, '../dist'),
filename: 'static/js/main.js'
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: "thread-loader",
options: {
workers: threads,
},
},
{
loader: "babel-loader",
options: {
cacheDirectory: true,
cacheCompression: false,
},
},
],
},
]
},
plugins: [
new ESLintWebpackPlugin({
context: path.resolve(__dirname, "../src"),
exclude: "node_modules",
cache: true,
cacheLocation: path.resolve(
__dirname,
"../node_modules/.cache/.eslintcache"
),
threads,
}),
],
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin(),
new TerserPlugin({
parallel: threads
})
],
},
}
4. 完整代码
const path = require("path");
const os = require("os");
const threads = os.cpus().length;
const ESLintWebpackPlugin = require("eslint-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const getStyleLoaders = (preProcessor) => {
return [
MiniCssExtractPlugin.loader,
"css-loader",
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
"postcss-preset-env",
],
},
},
},
preProcessor,
].filter(Boolean);
};
module.exports = {
entry: "./src/main.js",
output: {
path: path.resolve(__dirname, "../dist"),
filename: "js/main.js",
clean: true,
},
module: {
rules: [
{
oneOf: [
{
test: /\.css$/,
use: getStyleLoaders(),
},
{
test: /\.less$/,
use: getStyleLoaders("less-loader"),
},
{
test: /\.s[ac]ss$/,
use: getStyleLoaders("sass-loader"),
},
{
test: /\.(png|jpe?g|gif|webp)$/,
type: "asset",
parser: {
dataUrlCondition: {
maxSize: 10 * 1024,
},
},
generator: {
filename: "images/[hash:8][ext][query]",
},
},
{
test: /\.(ttf|woff2?|map4|map3|avi)$/,
type: "asset/resource",
generator: {
filename: "media/[hash:8][ext][query]",
},
},
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: "thread-loader",
options: {
workers: threads,
},
},
{
loader: "babel-loader",
options: {
cacheDirectory: true,
cacheCompression: false,
},
},
],
},
],
},
],
},
plugins: [
new ESLintWebpackPlugin({
context: path.resolve(__dirname, "../src"),
cache: true,
cacheLocation: path.resolve(
__dirname,
"../node_modules/.cache/.eslintcache"
),
threads,
}),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "../public/index.html"),
}),
new MiniCssExtractPlugin({
filename: "css/main.css",
}),
],
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin(),
new TerserPlugin({
parallel: threads
})
],
},
mode: "production",
devtool: "source-map",
};