This repository was archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.babel.js
252 lines (244 loc) · 7.49 KB
/
webpack.config.babel.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// 引入依赖
import webpack from 'webpack'
import path from 'path'
import autoprefixer from 'autoprefixer'
import cssImport from 'postcss-import'
import cssMixins from 'postcss-mixins'
import cssExtend from 'postcss-extend'
import conditionals from 'postcss-conditionals'
import cssEach from 'postcss-each'
import cssFor from 'postcss-for'
import nested from 'postcss-nested'
import cssSimpleVars from 'postcss-simple-vars'
import customMedia from 'postcss-custom-media'
import cssAtroot from 'postcss-atroot'
import sprites from 'postcss-sprites'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import OpenBrowserPlugin from 'open-browser-webpack-plugin'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import CleanWebpackPlugin from 'clean-webpack-plugin'
import CompressionWebpackPlugin from 'compression-webpack-plugin'
import HappyPack from 'happypack'
import os from 'os'
import lodash from 'lodash'
import pkg from './package.json'
import manifest from './manifest.json'
// 配置环境
const ENV = process.env.NODE_ENV
const isDev = ENV === 'development' || ENV === 'dev'
const isProd = ENV === 'production' || ENV === 'prod'
// 公共模块
const deps = lodash.uniq(Object.keys(pkg.dependencies))
const vendor = lodash.pullAll(deps, [])
const jsSourcePath = path.join(__dirname, 'example')
const buildPath = path.join(__dirname, 'build/demo')
const sourcePath = path.join(__dirname, 'example')
// 开启 happypack 多进程的模式加速编译
const happyPackHandle = {
threadPool: HappyPack.ThreadPool({
size: os.cpus().length,
}),
cacheLoaders: {},
cachePlugins: [],
createPlugin(id, loaders) {
this.cacheLoaders[id] = {
loaders,
happypack: `happypack/loader?id=${id}`,
}
this.cachePlugins.push(new HappyPack({
id,
loaders,
threadPool: this.threadPool,
cache: true,
verbose: true,
}))
return this
},
getLoaders(id) {
const { loaders, happypack } = this.cacheLoaders[id]
return isProd ? loaders[0] : happypack
},
}
// 创建 happypack 实例对象
happyPackHandle
.createPlugin('js', ['babel'])
.createPlugin('css', ['css!postcss'])
// 配置参数
const config = {
context: jsSourcePath,
entry: {
app: [
'./app.js',
],
vendor,
},
output: {
path: buildPath,
publicPath: '',
filename: 'assets/js/app.js',
chunkFilename: 'chunk/[name].chunk.js',
},
module: {
loaders: [
{
test: /\.js$/,
loader: happyPackHandle.getLoaders('js'),
exclude: [path.resolve(__dirname, 'node_modules')],
},
{
test: /\.css/,
loader: ExtractTextPlugin.extract('style', happyPackHandle.getLoaders('css'), {
publicPath: '../../',
}),
},
{
test: /\.(gif|jpg|png)\??.*$/,
loader: 'url-loader?limit=8192&name=assets/images/[hash].[ext]',
},
{
test: /\.(woff|svg|eot|ttf)\??.*$/,
loader: 'url-loader?limit=8192&name=assets/fonts/[hash].[ext]',
},
],
},
postcss: webpack => {
const dependent = {
addDependencyTo: webpack,
}
const processors = [
cssImport(dependent),
cssMixins,
cssExtend,
conditionals,
cssEach,
cssFor,
nested,
cssSimpleVars,
customMedia,
cssAtroot,
autoprefixer,
]
return processors
},
plugins: [
// 分析和优先考虑使用最多的模块,并为它们分配最小的 ID
new webpack.optimize.OccurenceOrderPlugin(),
// 删除重复的依赖
new webpack.optimize.DedupePlugin(),
// 跳过编译时出错的代码并记录,使编译后运行时的包不会发生错误
new webpack.NoErrorsPlugin(),
// 复制静态文件
new CopyWebpackPlugin([{
from: './assets/',
to: './assets/',
}]),
// 合并所有的 CSS 文件
new ExtractTextPlugin('assets/css/app.css', {
allChunks: true,
}),
// 自动生成页面
new HtmlWebpackPlugin({
template: path.join(sourcePath, isDev ? 'index.dev.html' : 'index.html'),
path: buildPath,
filename: 'index.html',
}),
],
resolve: {
extensions: [
'',
'.js',
'.json',
'.css',
],
root: [
path.resolve(__dirname),
jsSourcePath,
],
},
devServer: {
contentBase: isProd ? buildPath : sourcePath,
// historyApiFallback: true,
port: 3000,
// compress: isProd,
hot: !isProd,
inline: !isProd,
// disableHostCheck: true,
host: '0.0.0.0',
},
}
// 开发环境
if (isDev) {
Object.assign(config, {
plugins: [
...config.plugins,
// 抽离并打包变动不频繁的模块
new webpack.DllReferencePlugin({
context: __dirname,
manifest,
}),
// 配置全局变量
new webpack.DefinePlugin({
__DEBUG__: true,
}),
// happypack 插件
...happyPackHandle.cachePlugins,
// 开启全局的模块热替换(HMR)
new webpack.HotModuleReplacementPlugin(),
// 当模块热替换(HMR)时在浏览器控制台输出对用户更友好的模块名字信息
new webpack.NamedModulesPlugin()
// 打开浏览器
// new OpenBrowserPlugin({
// url: 'http://localhost:3000',
// }),
],
entry: Object.assign({}, config.entry, {
app: [
// 开发环境全局变量配置
// path.resolve(__dirname, 'dev.config.js'),
...config.entry.app,
],
}),
// 产生.map文件,方便调试
devtool: 'source-map',
})
}
// 生产环境
if (isProd) {
Object.assign(config, {
plugins: [
...config.plugins,
// 清除文件
new CleanWebpackPlugin(['build'], {
root: '',
verbose: true,
dry: false,
}),
// 提取公共模块单独打包,进而减小 rebuild 时的性能消耗
new webpack.optimize.CommonsChunkPlugin('vendor', 'assets/js/vendor.js'),
// 打包压缩 JS 文件
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console: true,
}
}),
// 压缩成 gzip 格式
new CompressionWebpackPlugin({
asset: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0,
}),
],
entry: Object.assign({}, config.entry, {
app: [
// 生成环境全局变量配置
// path.resolve(__dirname, 'prod.config.js'),
...config.entry.app,
],
}),
})
}
export default config