cnpm webpack webpack-cli html-webpack-plugin webpack-dev-server cross-env -D
"scripts": {
"build": "webpack", // 直接打包用
"start": "webpack serve", // 启动服务器 可以开发用
},
module.exports = {
mode: 'development', // 配置的模式
devtool: 'source-map', // 调试工具
context: process.cwd(), // node命令运行的进程的当前目录 就是这个项目根目录
entry: {
main: './src/index.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
module: {
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
minify: {
// 压缩html
collapseWhitespace: true, // 压缩空白
removeComments: true // 去除注释
}
}),
]
};
cnpm i friendly-errors-webpack-plugin node-notifier -D
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const notifier = require('node-notifier');
const icon = path.join(__dirname, 'icon.jpg');
plugins: [
...
+ new FriendlyErrorsWebpackPlugin({
+ onErrors: (severity, errors) => {
+ let error = errors[0];
+ notifier.notify({
+ title: 'webpack编译失败了',
+ message: severity + ':' + error.name,
+ subtitle: error.file || '',
+ icon
+ });
+ }
+ }),
]
cnpm i speed-measure-webpack5-plugin -D
const SpeedMeasureWebpack5Plugin = require('speed-measure-webpack5-plugin');
const smw = new SpeedMeasureWebpack5Plugin();
module.exports = smw.wrap({
// 配置文件
mode: 'development', // 配置的模式
devtool: 'source-map', // 调试工具
context: process.cwd(), // node命令运行的进程的当前目录 就是这个项目根目录
entry: {
main: './src/index.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
module: {
},
plugins: [
]
})
cnpm i webpack-bundle-analyzer -D
const {
BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
plugins: [
...
+ new BundleAnalyzerPlugin({
+ analyzerMode: 'disabled', // 不启动展示打包报告的HTTP服务器 也可以启动 直接可以看效果
+ generateStatsFile: true // 要生成stats.json文件
+ }),
]
命令:
"build": "webpack", // 会生成stats.json文件
// 下次想看分析效果就执行analyzer 启动服务看
"analyzer": "webpack-bundle-analyzer --port 8888 ./dist/stats.json"
resolve:{
extensions: ['.js', '.jsx','.json']
}
安装 cnpm i bootstrap css-loader style-loader -S
const bootstrap = path.resolve(
__dirname,
'node_modules/bootstrap/dist/css/bootstrap.css'
);
alias: {
bootstrap // 查找别名
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
exclude: /node_modules/, // 不解析
use: [
// 开启多线程池 弊端开线程和线程通信需要时间的
{
loader: 'thread-loader',
options: { workers: 3 }
},
{
loader: 'babel-loader',
options: {
cacheDirectory: true // 自动babel缓存
}
}
]
},
]
}
cnpm i cache-loader -D
{
test: /\.css$/,
use: [
'cache-loader',
'css-loader'
]
}
cnpm i terser-webpack-plugin optimize-css-assets-webpack-plugin image-webpack-loader -D
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
const TerserWebpackPlugin = require('terser-webpack-plugin');
{
// js压缩
optimization: {
minimize: true, // 开始最小化
minimizer: [new TerserWebpackPlugin()]
},
module: {
rules: [
{
test: /\.(jpg|png|gif|bmp)$/,
use: [
{
// 图片压缩
+ loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: false
},
pngquant: {
quality: [0.65, 0.9],
speed: 4
},
gifsicle: {
interlaced: false
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
}
]
},
]
},
plugins: [
...
+ new OptimizeCssAssetsWebpackPlugin() // 压缩css
]
}
cnpm i purgecss-webpack-plugin mini-css-extract-plugin -D
const PurgecssWebpackPlugin = require('purgecss-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const glob = require('glob');
const PATHS = {
src: path.resolve(__dirname, 'src')
};
module: {
rules: [
{
test: /\.css$/,
use: [
'cache-loader',
'log-loader',
+ MiniCssExtractPlugin.loader, // 在css loader 前引入
'css-loader'
]
}
],
},
plugins: [
// 提取的css文件名
+ new MiniCssExtractPlugin({
+ filename: '[name].css'
+ }),
+ // /**/* **匹配任意字段 包括路径分隔符 *匹配任意字符 不包含路径分隔符 、、
// 去除无用css
new PurgecssWebpackPlugin({
paths: glob.sync(`${
PATHS.src}/**/*`, {
nodir: true })
}),
+
]
// 定义全局变量 修改process.env.NODE_ENV
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
ENV: JSON.stringify('ENV')
})
]
npm i cross-env -D
命令行
"build1": "cross-env NODE_ENV=development webpack",
可以在配置文件读到该值cross-env设置的值添加按到 process.env对象里面
根目录新建.env文件
// .env文件内容
NODE_ENV=development
ENV=TEST
cnpm i dotenv -D
// 可以读取.env文件, 获取里面的值 设置到process.env里面
require('dotenv').config();
webpack5和webpack4配置基本差不多,使用上不会有太大区别, dll 已经去除了,使用 hard-source-webpack-plugin代替等。
附:
试验过程的完整配置
/*
* @description:
* @author: steve.deng
* @Date: 2020-12-19 06:51:55
* @LastEditors: steve.deng
* @LastEditTime: 2020-12-22 14:38:50
*/
const path = require('path');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const notifier = require('node-notifier');
const icon = path.join(__dirname, 'icon.jpg');
const SpeedMeasureWebpack5Plugin = require('speed-measure-webpack5-plugin');
const {
BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const smw = new SpeedMeasureWebpack5Plugin();
const selfLoadersPath = path.resolve(__dirname, 'loader');
const webpack = require('webpack');
const TerserWebpackPlugin = require('terser-webpack-plugin');
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
const ImageWebpackLoader = require('image-webpack-loader');
const PurgecssWebpackPlugin = require('purgecss-webpack-plugin');
// 因为css和js加载可以并行 所以我们可以通过此插件提取css为单独的文件 然后去掉无用css 进行压缩
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const glob = require('glob');
const {
DefinePlugin } = require('webpack');
const PATHS = {
src: path.resolve(__dirname, 'src')
};
const bootstrap = path.resolve(
__dirname,
'node_modules/bootstrap/dist/css/bootstrap.css'
);
// 可以读取.env文件, 获取里面的值 设置到process.env.NODE_ENV里面
require('dotenv').config();
console.log('.env process.env.NODE_ENV', process.env.NODE_ENV);
console.log('.env ENV', process.env.ENV);
// 默认值undefined 无法在配置文件中获取 在模块代码内可以获取
console.log('process.env.NODE_ENV', process.env.NODE_ENV);
module.exports = smw.wrap({
// mode: 'development', // 配置的模式
devtool: 'source-map', // 调试工具
context: process.cwd(), // node命令运行的进程的当前目录 就是这个项目根目录
entry: {
main: './src/index.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
// js压缩
optimization: {
minimize: true, // 开始最小化
minimizer: [new TerserWebpackPlugin()]
},
resolve: {
extensions: ['.js', '.jsx', '.json'], // 指定扩展名
alias: {
bootstrap // 查找别名
},
modules: ['node_modules'], // 指定查找目录
mainFields: ['browser', 'module', 'main'], // 从package.json中的哪个字段查找入口文件
mainFiles: ['index'] // 如果找不到mainFields的话 会找索引文件 index.js
},
resolveLoader: {
modules: [selfLoadersPath, 'node_modules'] // selfLoadersPath 自定义loader解析
},
externals: {
jquery: 'jQuery'
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
exclude: /node_modules/, // 不解析
use: [
// 开启多线程池 弊端开线程和线程通信需要时间的
{
loader: 'thread-loader',
options: {
workers: 3 }
},
{
loader: 'babel-loader',
options: {
cacheDirectory: true // 自动babel缓存
}
}
]
},
{
test: /\.(jpg|png|gif|bmp)$/,
use: [
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: false
},
pngquant: {
quality: [0.65, 0.9],
speed: 4
},
gifsicle: {
interlaced: false
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
}
]
},
{
test: /\.css$/,
use: [
'cache-loader',
'log-loader',
MiniCssExtractPlugin.loader,
'css-loader'
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
minify: {
// 压缩html
collapseWhitespace: true, // 压缩空白
removeComments: true // 去除注释
}
}),
new MiniCssExtractPlugin({
filename: '[name].css'
}),
// /**/* **匹配任意字段 包括路径分隔符 *匹配任意字符 不包含路径分隔符 、、
// 去除无用css
new PurgecssWebpackPlugin({
paths: glob.sync(`${
PATHS.src}/**/*`, {
nodir: true })
}),
new FriendlyErrorsWebpackPlugin({
onErrors: (severity, errors) => {
let error = errors[0];
notifier.notify({
title: 'webpack编译失败了',
message: severity + ':' + error.name,
subtitle: error.file || '',
icon
});
}
}),
new BundleAnalyzerPlugin({
analyzerMode: 'disabled', // 不启动展示打包报告的HTTP服务器
generateStatsFile: true // 要生成stats.json文件
}),
// 忽略模块打包 比如语言包
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/, // 资源正则
contextRegExp: /moment$/ // 上下文, 目录正则
}),
// 定义全局变量
new webpack.DefinePlugin({
// 定义在编译使用的全局变量 在浏览器运行阶段时就是值了
// 'process.env.NODE_ENV': JSON.stringify('production'),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
ENV: JSON.stringify('ENV')
}),
new OptimizeCssAssetsWebpackPlugin() // 压缩css
]
});