-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
123 lines (113 loc) · 3.93 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
112
113
114
115
116
117
118
119
120
121
122
123
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin').CleanWebpackPlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const TerserWebpackPlugin = require("terser-webpack-plugin");
const FontSpiderPlugin = require('font-spider-webpack-plugin')
// const CopyWebpackPlugin = require('copy-webpack-plugin');
// const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
const StyleLoader = MiniCssExtractPlugin.loader || 'style-loader';
const MODE = (process.env.NODE_ENV || '').trim() === 'production' ? 'production' : 'development';
const IS_DEV = MODE === 'development';
module.exports = {
entry: {
index: path.join(__dirname, './src/scripts/index.ts')
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js'
},
mode: MODE,
devtool: IS_DEV ? "source-map" : undefined,
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
compress: true,
port: 5555,
},
module: {
rules: [{ // TypeScript
test: /\.tsx?$/i,
use: 'ts-loader',
exclude: /node_modules/
}, { // CSS
test: /\.css$/i,
use: [StyleLoader, 'css-loader']
}, { // SCSS
test: /\.s[ac]ss$/i,
use: [StyleLoader, 'css-loader', 'sass-loader', ]
}, { // Image File
test: /\.(png|jpe?g|svg|gif|webp)$/i,
type: 'asset',
generator: {
filename: 'images/[hash][ext][query]'
},
parser: {
dataUrlCondition: {
maxSize: 32 * 1024 // 限制大小(单位:字节)
}
}
}, { // Font File
test: /\.(ttf|eot|woff2?)$/i,
type: 'asset',
generator: {
filename: '[hash][ext][query]'
},
parser: {
dataUrlCondition: {
maxSize: 32 * 1024 // 限制大小(单位:字节)
}
}
}]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: path.join(__dirname, './src/index.html'),
filename: 'index.html',
inject: "head",
scriptLoading: 'blocking'
}),
new MiniCssExtractPlugin(), // 单独提取 CSS
// new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/\.(js)$/i]) // Inline JS
// new CopyWebpackPlugin({
// patterns: [{
// from: path.resolve(__dirname, './src/public'),
// to: path.resolve(__dirname, './dist/public')
// }],
// })
].concat(IS_DEV ? [ // 仅在开发环境
] : [ // 仅在生产环境
new FontSpiderPlugin()
]),
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin(), // 优化 Minify CSS
new TerserWebpackPlugin({
parallel: true,
extractComments: false,
terserOptions: {
ecma: undefined,
parse: {},
compress: {
dead_code: false, // 移除没被引用的代码
loops: true, // 当循环的判断条件可以确定时,对其进行优化
drop_debugger: false,
drop_console: false, // 移除全部 console
pure_funcs: IS_DEV ? [] : ['console.log'] // 移除特定函数
}
}
})
],
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
alias: {
'@': path.resolve(__dirname, './src')
}
}
}