-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-webpack.config.js
More file actions
192 lines (161 loc) · 6.17 KB
/
make-webpack.config.js
File metadata and controls
192 lines (161 loc) · 6.17 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
/*
* @Author: dmyang
* @Date: 2015-08-02 14:16:41
* @Last Modified by: dmyang
* @Last Modified time: 2015-09-14 15:08:13
*/
'use strict';
// @see http://christianalfoni.github.io/javascript/2014/12/13/did-you-know-webpack-and-react-is-awesome.html
// @see https://github.com/webpack/react-starter/blob/master/make-webpack-config.js
var path = require('path');
var fs = require('fs');
var webpack = require('webpack');
var _ = require('lodash');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
var srcDir = path.resolve(process.cwd(), 'src');
var assets = 'assets/';
var sourceMap = require('./src/sourcemap.json');
var excludeFromStats = [
/node_modules[\\\/]/
];
function makeConf(options) {
options = options || {};
var debug = options.debug !== undefined ? options.debug : true;
var entries = genEntries();
var chunks = Object.keys(entries);
var config = {
entry: entries,
output: {
// 在debug模式下,__build目录是虚拟的,webpack的dev server存储在内存里
path: path.resolve(debug ? '__build' : assets),
filename: debug ? '[name].js' : 'js/[chunkhash:8].[name].min.js',
chunkFilename: debug ? '[chunkhash:8].chunk.js' : 'js/[chunkhash:8].chunk.min.js',
hotUpdateChunkFilename: debug ?'[id].[chunkhash:8].js' : 'js/[id].[chunkhash:8].min.js',
publicPath: debug ? '/__build/' : ''
},
resolve: {
root: [srcDir, './node_modules'],
alias: sourceMap,
extensions: ['', '.js', '.css', '.scss', '.tpl', '.png', '.jpg']
},
resolveLoader: {
root: path.join(__dirname, 'node_modules')
},
module: {
noParse: ['zepto'],
loaders: [
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'image?{bypassOnDebug: true, progressive:true, \
optimizationLevel: 3, pngquant:{quality: "65-80", speed: 4}}',
// url-loader更好用,小于10KB的图片会自动转成dataUrl,
// 否则则调用file-loader,参数直接传入
'url?limit=10000&name=img/[hash:8].[name].[ext]',
]
},
{
test: /\.(woff|eot|ttf)$/i,
loader: 'url?limit=10000&name=fonts/[hash:8].[name].[ext]'
},
{test: /\.(tpl|ejs)$/, loader: 'ejs'},
{test: /\.js$/, exclude: /node_modules/, loader: 'jsx'}
]
},
plugins: [
new CommonsChunkPlugin({
name: 'vendors',
chunks: chunks,
// Modules must be shared between all entries
minChunks: chunks.length // 提取所有chunks共同依赖的模块
}),
new CommonsChunkPlugin({
name: 'common-bc',
chunks: ['vendors', 'b', 'c'],
minChunks: 2
})
],
devServer: {
stats: {
cached: false,
exclude: excludeFromStats,
colors: true
}
}
};
if(debug) {
// 开发阶段,css直接内嵌
var cssLoader = {
test: /\.css$/,
loader: 'style!css'
};
var sassLoader = {
test: /\.scss$/,
loader: 'style!css!sass'
};
config.module.loaders.push(cssLoader);
config.module.loaders.push(sassLoader);
} else {
// 编译阶段,css分离出来单独引入
var cssLoader = {
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css?minimize') // enable minimize
};
var sassLoader = {
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css?minimize', 'sass')
};
config.module.loaders.push(cssLoader);
config.module.loaders.push(sassLoader);
config.plugins.push(
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
})
);
// 自动生成入口文件,入口js名必须和入口文件名相同
// 例如,a页的入口文件是a.html,那么在js目录下必须有一个a.js作为入口文件
var pages = fs.readdirSync(srcDir);
pages.forEach(function(filename) {
var m = filename.match(/(.+)\.html$/);
if(m) {
// @see https://github.com/kangax/html-minifier
var conf = {
template: path.resolve(srcDir, filename),
// @see https://github.com/kangax/html-minifier
// minify: {
// collapseWhitespace: true,
// removeComments: true
// },
filename: filename
};
if(m[1] in config.entry) {
conf.inject = 'body';
conf.chunks = ['vendors', m[1]];
}
config.plugins.push(new HtmlWebpackPlugin(conf));
}
});
// config.plugins.push(new UglifyJsPlugin());
}
return config;
}
function genEntries() {
var jsDir = path.resolve(srcDir, 'js');
var names = fs.readdirSync(jsDir);
var map = {};
names.forEach(function(name) {
var m = name.match(/(.+)\.js$/);
var entry = m ? m[1] : '';
var entryPath = entry ? path.resolve(jsDir, name) : '';
if(entry) map[entry] = entryPath;
});
return map;
}
module.exports = makeConf;