-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.dll.config.js
189 lines (174 loc) · 6.06 KB
/
webpack.dll.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
182
183
184
185
186
187
188
189
const path = require('path');
/**
* Plugin that simplifies creation of HTML files to serve your bundles
* npm i --save-dev html-webpack-plugin
* @type {HtmlWebpackPlugin}
*/
const HtmlWebpackPlugin = require('html-webpack-plugin');
/**
* A webpack plugin to remove/clean your build folder(s) before building
* npm i clean-webpack-plugin --save-dev
* @type {CleanWebpackPlugin}
*/
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
/**
* 提取css到单独文件
* npm install --save-dev mini-css-extract-plugin
*/
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
/**
* css 压缩 会清除css中注释
* npm install --save-dev optimize-css-assets-webpack-plugin
*/
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
/**
* js 压缩
* npm install uglifyjs-webpack-plugin --save-dev
*/
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const port = 8889;
module.exports = {
entry: {
index: path.join(__dirname, './src/index.js'),
vendor: ['react', 'react-dom', 'echarts']
},
output: {
path: path.join(__dirname, 'dist'),
filename: "[name].[hash].js",
chunkFilename: "[name].[hash].js"
},
mode: 'production',
// devServer: {
// contentBase: path.resolve(__dirname, 'dist'),//开发服务运行时的文件根目录
// historyApiFallback: true,//spa不跳转,history模式的路由需要true
// host: 'localhost',
// port: port,
// // hot:true,
// inline: true,//实时刷新
// compress: true,//Enable gzip compression for everything served
// overlay: true, //Shows a full-screen overlay in the browser
// stats: "errors-only",//To show only errors in your bundle
// open: true, //When open is enabled, the dev server will open the browser.
// },
resolve: {
alias: {
common: path.join(__dirname, './src/common'),
components: path.join(__dirname, './src/components')
},
extensions: [".js", ".jsx", '.ts', '.tsx', ".json"],
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader'
}
}, {
test: /\.(png|jpg|jpeg|gif|svg|svgz)(\?.+)?$/,
exclude: /(node_modules)/,
loader: ['url-loader']
}, {
test: /\.(woff|woff2|eot|ttf|otf)$/,
// include: origin,
exclude: /(node_modules)/,
loader: ['url-loader']
},
{
test: /\.(le|c)ss$/,
// include: [...origin, 'node_modules'],
exclude: /(node_modules)/,
loaders: ['style-loader', 'css-loader', 'less-loader']
}
]
},
externals: {
"react": "React",
"react-dom": "ReactDOM",
"echarts": 'echarts'
},
optimization: {
splitChunks: {
cacheGroups: {
// 注意: priority属性
// 其次: 打包业务中公共代码
common: {
name: "common",
chunks: "all",
minSize: 1,
priority: 0
},
// 首先: 打包node_modules中的文件
vendor: {
name: "vendor",
test: /[\\/]node_modules[\\/]/,
chunks: "all",
priority: 10
}
}
},
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
/**
* sourceMap 和 devtool: 'inline-source-map', 冲突
*/
sourceMap: false, // set to true if you want JS source maps,
/**
* extractComments 导出备注
*/
extractComments: 'all'
}),
// new OptimizeCSSAssetsPlugin({})
]
},
plugins: [
new HtmlWebpackPlugin({//预览时使用path.join(__dirname, './example/index.js')
template: path.resolve(__dirname, 'src', 'index.html'),//模板
// filename: 'index.html',
// // chunks: ['app'],
// inject: false, //允许插件修改哪些内容,包括head与body
// hash: true, //是否添加hash值
// minify: { //压缩HTML文件
// removeComments: true,//移除HTML中的注释
// collapseWhitespace: true //删除空白符与换行符
// },
// chunksSortMode: 'none' //如果使用webpack4将该配置项设置为'none'
}),
// /**
// * 打包HTML
// */
// new HtmlWebpackPlugin({
// title: 'Output Management',
// /**
// * template 指定要打包的原文件
// */
// template: './online.html'
// }),
/**
* 由 webpack 生成的文件或目录才能被 CleanWebpackPlugin 删除
* 下面配置的是 打包前 需要被删除的目录
*/
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ['dist']
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
}),
// new webpack.optimize.DedupePlugin(),
/**
* 提取SourceMap到独立文件
*/
// new webpack.SourceMapDevToolPlugin({
// filename: '[name].js.map',
// // exclude: ['vendor.js']
// })
],
};