-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
325 lines (294 loc) · 8.32 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var path = require('path');
var webpack = require('webpack');
/* =============================================================
* config for path
* ============================================================ */
var node_modules_dir = path.join(__dirname, 'node_modules');
var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, 'app');
var BUILD_PATH = path.resolve(ROOT_PATH, 'build');
/* =============================================================
* plugins settings
* ============================================================ */
/* minimize js,css,img... */
var UglifyJsPlugin = new webpack.optimize.UglifyJsPlugin({
minimize: true,
output: {
comments: false
},
compress: {
warnings: false,
screw_ie8: true
}
});
/* html generate automatically */
var HtmlWebpackPlugin = require('html-webpack-plugin');
/* generate all your favicons and icons for you */
var FaviconsWebpackPlugin = require('favicons-webpack-plugin');
/* run any shell commands before or after webpack builds */
var WebpackShellPlugin = require('webpack-shell-plugin');
/* expose the value to global */
var ProvidePlugin = new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
});
/* browserSync */
var BrowserSyncPlugin = require('browser-sync-webpack-plugin');
var BrowserSyncPlugin = new BrowserSyncPlugin({
host: 'localhost',
port: 3000,
server: { baseDir: ['build/'] }
});
/* common chunks */
var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
/* ExtractText */
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var CssExtractPlugin = new ExtractTextPlugin("[name].css");
/* postcss loader */
var precss = require('precss');
var autoprefixer = require('autoprefixer');
/* merge */
var merge = require('webpack-merge');
var TARGET = process.env.npm_lifecycle_event;
/* automatically launch it's application on a browser */
const WebpackBrowserPlugin = require('webpack-browser-plugin-fork');
/* get ip */
function getServerIp() {
var os = require('os');
var ifaces = os.networkInterfaces();
var values = Object.keys(ifaces).map(function(name) {
return ifaces[name];
});
values = [].concat.apply([], values).filter(function(val){
return val.family == 'IPv4' && val.internal == false;
});
return values.length ? values[0].address : '0.0.0.0';
}
var ipAddress = getServerIp();
/* makes webpack builds faster by allowing you to transform multiple files in parallel. */
var HappyPack = require('happypack');
/* =============================================================
* base settings
* ============================================================ */
var common = {
entry: {
app: ['react', 'index']
// app: path.resolve(ROOT_PATH, 'app/components/app/app.js')
},
output: {
filename: '[name].js',
chunkFilename: '[id].chunk.js'
},
devServer: {
disableHostCheck: true
},
module: {
loaders: [
//jsx
{
test: /\.jsx?$/,
loaders: [ 'happypack/loader?id=jsx' ],
include: APP_PATH
},
//css
{
test: /\.css$/,
loader: 'style!css!postcss'
},
//font
{
test : /\.woff|\.woff2|\.svg|.eot|\.ttf/,
loader : 'url?prefix=font/&limit=10000'
},
//url loader
{
test: /\.(png|jpg|jpeg|gif)$/,
loader: 'url?limit=10000',
include: APP_PATH
},
//json loader
{
test: /\.json$/,
loader: 'json',
include: APP_PATH
}
]
},
postcss: function () {
return [precss, autoprefixer];
},
resolve: {
root: path.resolve(ROOT_PATH, 'app'),
extensions: ['', '.js', '.jsx', '.scss'],
alias: {
react: 'react',
index: path.resolve(ROOT_PATH, 'app/index.js')
}
},
plugins: [
//Common Chunk
new CommonsChunkPlugin('app', 'app.js'),
// new CommonsChunkPlugin('header-main.js', ['header','main']),
//html
new HtmlWebpackPlugin({
title: 'index page',
template: path.resolve(ROOT_PATH, 'app/tmpl/index.html'),
inject: 'body',
filename: '../index.html',
hash: false,
chunks: ['app'],
minify: {
minifyJS: true,
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true
}
}),
new HappyPack({
id: 'jsx',
threads: 4,
loaders: [ 'babel?cacheDirectory=true' ],
cache: true
}),
// ProvidePlugin enable this if you want to expose soem global variable
]
}
/* =============================================================
* dev
* ============================================================ */
if(TARGET === 'dev') {
module.exports = merge(common, {
output: {
path: path.resolve(BUILD_PATH, 'build/assets/'),
publicPath: 'http://' +ipAddress + ':8080/'
},
devtool: "cheap-module-eval-source-map",
module: {
loaders: [
//jsx
{
test: /\.jsx?$/,
loaders: ['react-hot', 'babel'],
include: APP_PATH
},
//less
{
test: /\.less$/,
loader: 'style!css?sourceMap!postcss!less?sourceMap'
},
//sass
{
test: /\.scss$/,
loader: 'style!css?sourceMap!postcss!sass?sourceMap'
}
]
},
plugins: [
new WebpackBrowserPlugin({
url: 'http://' + ipAddress
}),
CssExtractPlugin,
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('development'),
},
'__DEV__': true,
'__PRODUCTION__': false
}),
],
cache: true
});
}
/* =============================================================
* browser sync
* ============================================================ */
if(TARGET === 'browsersync') {
module.exports = merge(common, {
output: {
path: path.resolve(ROOT_PATH, 'build/assets/'),
publicPath: 'assets/'
},
devtool: "cheap-module-eval-source-map",
module: {
loaders: [
//less
{
test: /\.less$/,
loader: 'style!css?sourceMap!postcss!less?sourceMap',
include: APP_PATH
},
//sass
{
test: /\.scss$/,
loader: 'style!css?sourceMap!postcss!sass?sourceMap',
include: APP_PATH
}
]
},
plugins: [
CssExtractPlugin,
BrowserSyncPlugin,
new webpack.DefinePlugin({
'process.env': {NODE_ENV: '"production"'},
'__DEV__': false,
'__PRODUCTION__': false
})
]
});
}
/* =============================================================
* deploy
* ============================================================ */
var deployCommon = merge(common, {
output: {
path: path.resolve(ROOT_PATH, 'dist/assets/'),
publicPath: '/assets/'
},
module: {
loaders: [
//less
{
test: /\.less$/,
loader: ExtractTextPlugin.extract('style', 'css!postcss!less')
},
//sass
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css!postcss!sass')
}
]
},
plugins: [
UglifyJsPlugin,
CssExtractPlugin,
new webpack.NoErrorsPlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new FaviconsWebpackPlugin({
logo: './app/public/react-logo.png',
title: 'react app'
}),
new webpack.optimize.DedupePlugin(),
new webpack.DefinePlugin({
'process.env': {NODE_ENV: '"production"'},
'__DEV__': false,
'__PRODUCTION__': true
})
]
});
if (TARGET === 'deploy') {
module.exports = merge(deployCommon, {
plugins: [
new WebpackShellPlugin({onBuildExit:['static dist/ -a ' + ipAddress]}),
new WebpackBrowserPlugin({
url: 'http://' + ipAddress
})
]
});
}
/* =============================================================
* stats
* ============================================================ */
if (TARGET === 'stats') {
module.exports = deployCommon;
}