-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
111 lines (109 loc) · 3.02 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const BundleAnalyzerPlugin =
require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
/**@type {import('webpack').Configuration} */
module.exports = {
// mode: 'production',
// cache: false, // 强制全局更新
cache: {
type: 'filesystem',
buildDependencies: {
config: [__filename],
},
},
optimization: {
usedExports: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
unused: true,
drop_console: true,
drop_debugger: true,
dead_code: true,
},
},
}),
],
},
entry: path.resolve(__dirname, 'src/index.js'),
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
module: {
rules: [
{
test: /\.jsx?/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
babelrc: false,
presets: [
require.resolve('@babel/preset-react'),
[require.resolve('@babel/preset-env', { module: false })],
],
},
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.less$/,
exclude: /node_modules/,
use: ['style-loader', 'css-loader', 'less-loader'],
},
{
// svg组件化
test: /\.svg$/,
use: ['@svgr/webpack'],
},
{
// 图片格式处理
test: /\.(jpeg|png|gif)$/,
use: {
loader: 'file-loader',
options: {
// 定义打包后文件的名称;
// [name]:原文件名,[hash]:hash字符串(如果不定义名称,默认就以hash命名,[ext]:原文件的后缀名)
name: '[name]_[hash].[ext]',
outputPath: 'images/', // 定义图片输出的文件夹名(在output.path目录下)
},
},
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src/index.html'),
favicon: path.join(__dirname, 'src/assets/favicon.png'),
filename: 'index.html',
}),
new webpack.HotModuleReplacementPlugin(),
// new BundleAnalyzerPlugin(), // 开启则分析数据包大小
new CompressionPlugin({
algorithm: 'gzip', // 使用gzip压缩
test: /\.js$|\.html$|\.css$/, // 匹配文件名
filename: '[path][base].gz', // 压缩后的文件名(保持原文件名,后缀加.gz)
minRatio: 0.8, // 压缩率小于1才会压缩
threshold: 10240, // 对超过10k的数据压缩
}),
],
devServer: {
port: 3001,
historyApiFallback: true,
},
// devtool: 'source-map',
devtool: false,
resolve: {
extensions: ['.wasm', '.mjs', '.js', '.jsx', '.json'],
},
};