-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
181 lines (179 loc) · 5.09 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
// // 分离css
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const cssnano = require('cssnano');
// 引入favicon
const FaviconWebpackPlugin = require('favicons-webpack-plugin');
const bundleAnalyzer = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OfflinePlugin = require('offline-plugin');
console.log(process.env.NODE_ARUG);
module.exports = {
mode: 'production',
entry: {
// app入口文件
main: ['@babel/polyfill', './src/index'],
},
output: {
path: path.join(__dirname, process.env.NODE_ARUG === 'deploy' ? 'deploy/node-js-getting-started/public' : 'build'),
filename: '[name].[chunkhash].bundle.js',
// 公共路径, 根目录
publicPath: './',
},
// 绝对路径, 用于解析入口文件和loader的位置,而不依赖与CWD(current working directory)
context: path.resolve(__dirname),
// 需要解析的扩展名
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
modules: [path.resolve(__dirname, 'node_modules')],
},
devtool: "source-map",
module: {
rules: [{
use: ['babel-loader', 'ts-loader'],
test: /\.(tsx|ts)$/,
exclude: /node_modules/,
sideEffects: false,
},{
use: ['babel-loader'],
test: /\.(jsx|js)$/,
exclude: /node_modules/,
sideEffects: false,
},{
test: /\.scss$/,
// css-loader会遍历css文件,找到所有的url(...)并且处理。style-loader会把所有的样式插入到你页面的一个style tag中。
use: [
// devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
'style-loader',
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
importLoaders: 1,
},
},
'sass-loader',
]
},{
test: /\.(jpg|jpeg|gif|png)$/,
use: {loader: 'file-loader'}
},{
test: /\.css$/,
// css-loader会遍历css文件,找到所有的url(...)并且处理。style-loader会把所有的样式插入到你页面的一个style tag中。
use: ['style-loader',{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
},
}]
}]
},
plugins: [
new CopyWebpackPlugin([
{
from: 'src/img/icons',
to: 'icons/'
},
{
from: 'manifest.json',
to: 'manifest.json'
}, {
from: 'src/service-worker.js',
to: 'sw.js'
},{
from: 'src/component/current_bar/click.mp3',
to: 'click.mp3'
},
]),
// if not production env need add following one plugin
new webpack.optimize.ModuleConcatenationPlugin(),
new HtmlWebpackPlugin({
template: __dirname + '/index.html'
}),
// // 开启全局HMR
// devMode && (new webpack.HotModuleReplacementPlugin()),
// // console输出更友好的模块名称,可以知道热重载时是哪个模块作出了变动
// devMode && (new webpack.NamedModulesPlugin()),
new FaviconWebpackPlugin({
logo: './favicon.png',
background: '#fff',
icons: {
// condig no-need format
android: false,
appleStartup: false,
appleIcon: false,
}
}),
new OptimizeCSSAssetsPlugin({
cssProcessor: cssnano,
cssProcessorOptions: {
discardComments: {
removeAll: true,
},
safe: false,
},
canPrint: false,
}),
// 打包模块分析
// new bundleAnalyzer(),
// new OfflinePlugin(),
],
node: {
fs: "empty",
tls:"empty"
},
optimization: {
minimizer: [
new UglifyJsPlugin({
sourceMap: false,
uglifyOptions: {
sourceMap: true,
compress: {
// 去掉所有的console
drop_console: true,
// 对多次引用但没定义为变量的变量提取
reduce_vars: true
},
output: {
// 紧凑的输出
beautify: false,
// 去掉所有
comments: false,
}
}
})
],
splitChunks: {
// 对所有模块进行优化, 将业务代码和库代码区分, 设置最小20kb,没有最大限制, 可异步加载, 节省单个文件阻塞时间
chunks: 'all',
minSize: 20000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
// 单独抽离react
react: {
test: /[\\/]node_modules[\\/]react.*[\\/]/,
priority: -9,
},
venders: {
test: /[\\/]node_modules[\\/]/,
priority: -10
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
}
}
},
},
}