-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
260 lines (246 loc) · 6.52 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
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
253
254
255
256
257
258
259
260
// External dependencies
var webpack = require('webpack'),
path = require('path'),
argv = require('minimist')(process.argv.slice(2)),
qs = require('qs'),
autoprefixer = require('autoprefixer'),
Clean = require("clean-webpack-plugin"),
AssetsPlugin = require('assets-webpack-plugin'),
ExtractTextPlugin = require('extract-text-webpack-plugin'),
cssnano = require('cssnano');
// Internal dependencies
var config = require('./src/config');
// Internal variables
var scriptsFilename = (argv.release) ? 'js/[name]_[hash].js' : 'js/[name].js',
stylesFilename = (argv.release) ? 'styles/[name]_[hash].css' : 'styles/[name].css',
sourceMapQueryStr = (argv.release) ? '-sourceMap' : '+sourceMap',
jsLoader,
webpackConfig;
jsLoader = {
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loaders: [
//'es3ify',
'babel?' + JSON.stringify({
cacheDirectory: true,
presets: ['es2015'],
plugins: [
["add-module-exports"]
/*
,
["transform-es2015-classes", {
"loose": true
}],
['transform-es2015-modules-commonjs', { "loose": true }],
['transform-es3-property-literals', {}],
['transform-es3-member-expression-literals', {}]
*/
]
})
]
}
if (argv.watch) { // '--watch' to add monkey-hot
jsLoader.loaders.unshift('monkey-hot');
}
/**
* Process AssetsPlugin output and format it
* for Sage: {"[name].[ext]":"[name]_[hash].[ext]"}
* @param {Object} assets passed by processOutput
* @return {String} JSON
*/
var assetsPluginProcessOutput = function (assets) {
var name,
ext,
filename,
results = {};
for (name in assets) {
if (assets.hasOwnProperty(name)) {
if (path.extname(assets[name]) === '') {
for (ext in assets[name]) {
if (assets[name].hasOwnProperty(ext)) {
filename = name + '.' + ext;
results[filename] = path.basename(assets[name][ext]);
}
}
}
}
}
return JSON.stringify(results);
}
/**
* Loop through webpack entry
* and add the hot middleware
* @param {Object} entry webpack entry
* @return {Object} entry with hot middleware
*/
var addHotMiddleware = function (entry) {
var name,
results = {},
hotMiddlewareScript = 'webpack-hot-middleware/client?' + qs.stringify({
//path: 'http://localhost:3000/__webpack_hmr',
timeout: 20000,
reload: true
});
for (name in entry) {
if (entry.hasOwnProperty(name)) {
if (entry[name] instanceof Array !== true) {
results[name] = [];
results[name].push('webpack-dev-server/client?http://localhost:3000');
results[name].push('webpack/hot/only-dev-server');
results[name].push(entry[name]);
} else {
results[name] = entry[name].slice(0);
}
results[name].push(hotMiddlewareScript);
}
}
return results;
}
webpackConfig = {
context: path.resolve(config.context),
entry: config.entry,
output: {
path: path.join(__dirname, config.output.path),
publicPath: config.output.publicPath,
filename: scriptsFilename
},
module: {
/*
preLoaders: [
{
test: /\.js?$/,
exclude: /(node_modules|bower_components)/,
loader: 'eslint'
}
],
*/
loaders: [
jsLoader,
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', [
'css?' + sourceMapQueryStr,
'postcss'
])
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', [
'css?' + sourceMapQueryStr,
'postcss',
'resolve-url?' + sourceMapQueryStr,
'sass?' + sourceMapQueryStr
])
},
{
test: /\.(png|jpg|jpeg|gif)(\?.*)?$/,
loaders: [
'file?' + qs.stringify({
name: '[path][name].[ext]'
}),
'image-webpack?' + JSON.stringify({
bypassOnDebug:true,
progressive: true,
optimizationLevel: 7,
interlaced: true,
pngquant: {
quality: "65-90",
speed: 4
},
svgo: {
removeUnknownsAndDefaults: false,
cleanupIDs: false
}
})
]
},
{
test: /\.(ttf|eot|svg)(\?.*)?$/,
loader: 'file?' + qs.stringify({
limit: 0,
name: '[path][name].[ext]'
})
},
{
test: /\.woff(2)?(\?.*)?$/,
loader: 'url?' + qs.stringify({
limit: 0,
mimetype: "application/font-woff",
name: "[path][name].[ext]"
})
}
]
},
resolve: {
extensions: [ '', '.js', '.json' ],
modulesDirectories: [
'node_modules',
'bower_components',
'src/js/modules'
],
},
/*
externals: {
jquery: 'jQuery'
},*/
plugins: [
new Clean([config.output.path]),
new ExtractTextPlugin(stylesFilename, {
allChunks: true,
disable: (argv.watch === true) // '--watch' disable ExtractTextPlugin
})/*,
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
'window.Tether': 'tether'
})
*/
],
postcss: [
autoprefixer({
browsers: [
'last 2 versions',
'android 4',
'opera 12'
]
})
],
eslint: {
failOnWarning: false,
failOnError: true
},
stats: {
colors: true
}
};
// '--watch' to push additional plugins to webpackConfig
if (argv.watch) {
webpackConfig.entry = addHotMiddleware(webpackConfig.entry);
webpackConfig.output.pathinfo = true;
webpackConfig.debug = true;
webpackConfig.devtool = '#cheap-module-source-map';
webpackConfig.plugins.push(new webpack.optimize.OccurenceOrderPlugin());
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
webpackConfig.plugins.push(new webpack.NoErrorsPlugin());
}
// '--release' to push additional plugins to webpackConfig
if (argv.release) {
webpackConfig.plugins.push(new AssetsPlugin({
path: path.join(__dirname, config.output.path),
filename: 'assets.json',
fullPath: false,
processOutput: assetsPluginProcessOutput
}));
webpackConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
'drop_debugger': true
}
}));
webpackConfig.plugins.push(new OptimizeCssAssetsPlugin({
cssProcessor: cssnano,
cssProcessorOptions: { discardComments: { removeAll: true } },
canPrint: true
}));
}
module.exports = webpackConfig;