forked from chemdemo/webpack-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-webpack.config.js
More file actions
224 lines (195 loc) · 7.76 KB
/
make-webpack.config.js
File metadata and controls
224 lines (195 loc) · 7.76 KB
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
/*
* @Author: dmyang
* @Date: 2015-08-02 14:16:41
* @Last Modified by: Ian Hu
* @Last Modified time: 2016-08-09 22:06:18
*/
'use strict';
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const _ = require('lodash')
const glob = require('glob')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin
const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin
const DefinePlugin = webpack.DefinePlugin
const srcDir = path.resolve(process.cwd(), 'src')
const assets = path.resolve(process.cwd(), 'assets')
const nodeModPath = path.resolve(__dirname, './node_modules')
const pathMap = require('./src/pathmap.json')
let entries = (() => {
let jsDir = path.resolve(srcDir, 'js')
let entryFiles = glob.sync(jsDir + '/*.{js,jsx}')
let map = {}
entryFiles.forEach((filePath) => {
let filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
map[filename] = filePath
})
return map
})()
let chunks = Object.keys(entries)
module.exports = (options) => {
options = options || {}
let dev = options.dev !== undefined ? options.dev : true
// 这里publicPath要使用绝对路径,不然scss/css最终生成的css图片引用路径是错误的,应该是scss-loader的bug
let publicPath = '/'
let extractCSS
let cssLoader
let sassLoader
// generate entry html files
// 自动生成入口文件,入口js名必须和入口文件名相同
// 例如,a页的入口文件是a.html,那么在js目录下必须有一个a.js作为入口文件
let plugins = (() => {
let entryHtml = glob.sync(srcDir + '/*.html')
let r = []
entryHtml.forEach((filePath) => {
let filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
let conf = {
template: 'html!' + filePath,
filename: filename + '.html'
}
if(filename in entries) {
conf.inject = 'body'
conf.chunks = ['vender', 'common', filename]
}
if('b' === filename || 'c' === filename) conf.chunks.splice(2, 0, 'common-b-c')
// dll打包过的库,只能通过script标签引入??
// if('react-demo' === filename) conf.chunks.splice(2, 0, 'reactStuff')
r.push(new HtmlWebpackPlugin(conf))
})
return r
})()
// 没有真正引用也会加载到runtime,如果没安装这些模块会导致报错,有点坑
/*plugins.push(
new webpack.ProvidePlugin({
React: 'react',
ReactDOM: 'react-dom',
_: 'lodash', 按需引用
$: 'jquery'
})
)*/
if(dev) {
extractCSS = new ExtractTextPlugin('css/[name].css?[contenthash]')
cssLoader = extractCSS.extract('style', ['css'])
sassLoader = extractCSS.extract('style', ['css', 'sass'])
plugins.push(extractCSS, new webpack.HotModuleReplacementPlugin())
} else {
extractCSS = new ExtractTextPlugin('css/[contenthash:8].[name].min.css', {
// 当allChunks指定为false时,css loader必须指定怎么处理
// additional chunk所依赖的css,即指定`ExtractTextPlugin.extract()`
// 第一个参数`notExtractLoader`,一般是使用style-loader
// @see https://github.com/webpack/extract-text-webpack-plugin
allChunks: false
})
cssLoader = extractCSS.extract('style', ['css?minimize'])
sassLoader = extractCSS.extract('style', ['css?minimize', 'sass'])
plugins.push(
extractCSS,
new UglifyJsPlugin({
compress: {
warnings: false
},
output: {
comments: false
},
mangle: {
except: ['$', 'exports', 'require']
}
}),
// use `production` mode
new DefinePlugin({'process.env': {'NODE_ENV': JSON.stringify('production')}}),
// new AssetsPlugin({
// filename: path.resolve(assets, 'source-map.json')
// }),
new webpack.optimize.DedupePlugin(),
new webpack.NoErrorsPlugin()
)
}
let config = {
entry: Object.assign(entries, {
// 用到什么公共lib(例如React.js),就把它加进vender去,目的是将公用库单独提取打包
'vender': ['zepto'],
// 'reactStuff': 'assets/dll/js/reactStuff.js'
}),
output: {
path: assets,
filename: dev ? '[name].js' : 'js/[chunkhash:8].[name].min.js',
chunkFilename: dev ? '[chunkhash:8].chunk.js' : 'js/[chunkhash:8].chunk.min.js',
hotUpdateChunkFilename: dev ? '[id].js' : 'js/[id].[chunkhash:8].min.js',
publicPath: publicPath
},
resolve: {
root: [srcDir, nodeModPath],
alias: pathMap,
extensions: ['', '.js', '.css', '.scss', '.tpl', '.png', '.jpg']
},
module: {
loaders: [
{
test: /\.((woff2?|svg)(\?v=[0-9]\.[0-9]\.[0-9]))|(woff2?|svg|jpe?g|png|gif|ico)$/,
loaders: [
// url-loader更好用,小于10KB的图片会自动转成dataUrl,
// 否则则调用file-loader,参数直接传入
'url?limit=10000&name=img/[hash:8].[name].[ext]',
'image?{bypassOnDebug:true, progressive:true,optimizationLevel:3,pngquant:{quality:"65-80",speed:4}}'
]
},
{
test: /\.((ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9]))|(ttf|eot)$/,
loader: 'url?limit=10000&name=fonts/[hash:8].[name].[ext]'
},
{test: /\.(tpl|ejs)$/, loader: 'ejs'},
{test: /\.css$/, loader: cssLoader},
{test: /\.scss$/, loader: sassLoader},
{test: /\.jsx?$/, loader: 'babel?presets[]=react,presets[]=es2015'}
]
},
plugins: [
/*new webpack.DllReferencePlugin({
context: process.cwd(),
manifest: require(path.join(assets, 'dll', 'js', 'reactStuff-manifest.json')),
sourceType: 'var',
// name: 'assets/dll/js/reactStuff.js'
}),*/
new CommonsChunkPlugin({
name: 'common-b-c',
chunks: ['b', 'c']
}),
new CommonsChunkPlugin({
name: 'common',
chunks: ['common-b-c', 'a']
}),
new CommonsChunkPlugin({
name: 'vender',
chunks: ['common']
})
].concat(plugins),
devServer: {
// hot: true,
noInfo: false,
inline: true,
publicPath: publicPath,
stats: {
cached: false,
colors: true
}
}
}
if (dev) {
// 为实现webpack-hot-middleware做相关配置
// @see https://github.com/glenjamin/webpack-hot-middleware
((entry) => {
for (let key of Object.keys(entry)) {
if (! Array.isArray(entry[key])) {
entry[key] = Array.of(entry[key])
}
entry[key].push('webpack-hot-middleware/client?reload=true')
}
})(config.entry)
config.plugins.push( new webpack.HotModuleReplacementPlugin() )
config.plugins.push( new webpack.NoErrorsPlugin() )
}
return config
}