-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.config.js
326 lines (296 loc) · 8.77 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
326
/* global __dirname, module, process */
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpack = require('webpack');
var DefinePlugin = webpack.DefinePlugin;
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var os = require('os');
var gitRev = require('git-rev-sync');
var marked = require('marked');
// This webpack config is known to be very anti-webpack in how it
// works. This is for legacy reasons and is being gradually updated.
//
// Lots of the angular dependencies are very slow to build, so they
// are manually configured to just be directly copied over from the
// pre-minimized files they are shipped with.
function toPath(p) {
return path.resolve(path.join(__dirname, p));
}
var SRC = toPath('src');
var OUT = toPath('dist');
function fileExists(f) {
try {
return fs.statSync(f).isFile();
} catch (err) {
return !(err && err.code === 'ENOENT');
}
}
var markdownRenderer = new marked.Renderer();
markdownRenderer.link = function (href, title, text) {
var out = '<a href="' + href + '" target="_blank"';
if (title && title !== 'newWindow') {
out += ' title="' + title + '"';
}
out += '>' + text + '</a>';
return out;
};
var configFile = [
'src/app/app.config.js',
'app.config.js',
'app.config.json',
'src/app/app.config.js.template',
'app.config.js.template',
'app.config.template.json',
].map(toPath).find(fileExists);
var babelSettings = {
presets: ['es2015'],
plugins: [
'transform-flow-strip-types',
// Include the Babel runtime functions once for all source
// files
'transform-runtime',
// Breaks lodash imports into only using the needed
// functions.
'lodash',
],
cacheDirectory: true,
};
var svgoSettings = require('./svgo.config.js');
var extractAppStyles = new ExtractTextPlugin('app.css');
var extractVendorStyles = new ExtractTextPlugin('vendor.css');
var isDev = process.env.NODE_ENV !== 'production';
var ext = (isDev ? '' : '.min') + '.js';
if (process.env.BUILD_QUIET !== 'true') {
console.log('Using config file:', configFile);
console.log('Build type:', isDev ? 'development' : 'production');
}
module.exports = {
context: SRC,
babelSettings: babelSettings,
devtool: (isDev ? 'inline-' : '') + 'source-map',
quiet: false,
noInfo: false,
stats: {
assets: false,
colors: false,
version: false,
hash: false,
timings: false,
chunks: false,
chunkModules: false,
},
entry: {
app: ['babel-polyfill', './app/app'],
admin: './admin/app',
vendor: [
'angular', // see angular-min alias and comment in lib/angular-min.js
'angular-animate',
'angular-ui-router',
'../node_modules/angular-material/angular-material.js',
'angular-aria',
'angular-material-data-table',
'angular-messages',
'angular-bindonce',
'angular-ui-validate',
'../node_modules/ng-media-events/src/ng-media-events.js',
'react',
'lodash',
'babel-polyfill',
'xss-filters',
'clipboard',
'kefir',
'moment',
'../node_modules/angular-material/angular-material.min.css',
'../node_modules/angular-material-data-table/dist/md-data-table.min.css',
],
},
noParse: isDev ? [] : [
/node_modules\/angular/,
/node_modules\/angular-material/,
/node_modules\/angular-material-data-table/,
/node_modules\/angular-animate/,
/node_modules\/angular-aria/,
/node_modules\/angular-messages/,
/node_modules\/angular-bindonce/,
/node_modules\/angular-ui-router/,
/node_modules\/angular-ui-validate/,
/node_modules\/moment/,
/node_modules\/kefir/,
/node_modules\/wsevent.js/,
/node_modules\/clipboard/,
/node_modules\/ng-media-events/,
],
resolve: {
alias: {
'app-config': configFile,
angular: toPath('lib/angular-min.js'),
'angular-min': toPath('/node_modules/angular/angular' + ext),
'angular-material':
toPath('/node_modules/angular-material/angular-material' + ext),
'angular-material-data-table':
toPath('/node_modules/angular-material-data-table/' +
'dist/md-data-table' + ext),
'angular-aria': toPath('/node_modules/angular-aria/angular-aria' + ext),
'angular-animate':
toPath('/node_modules/angular-animate/angular-animate' + ext),
'angular-ui-router':
toPath('/node_modules/angular-ui-router/release/angular-ui-router' + ext),
'angular-ui-validate':
toPath('/node_modules/angular-ui-validate/dist/validate' + ext),
'angular-messages':
toPath('/node_modules/angular-messages/angular-messages' + ext),
'angular-bindonce':
toPath('/node_modules/angular-bindonce/bindonce' + ext),
kefir: toPath('/node_modules/kefir/dist/kefir' + ext),
moment: toPath('/node_modules/moment/' + (isDev ? '' : 'min/')
+ 'moment' + ext),
},
},
output: {
path: OUT,
filename: '[name]-[hash].js',
},
module: {
loaders: [{
test: /\.js$/,
exclude: /(moment|node_modules|bower_components|lib|angular|angular-material)/,
loaders: [
'ng-annotate',
'babel?' + JSON.stringify(babelSettings),
],
}, {
test: /\.json$/,
loader: 'json',
}, {
test: /\.svg$/,
loaders: [
'file?name=[hash].[ext]',
'svgo?' + JSON.stringify(svgoSettings),
],
}, {
test: /\.(png|jpg|jpeg|gif|woff|woff2|ttf|eot|otf|webm|mp4|ogg|wav)$/,
loader: 'file?name=[hash].[ext]',
}, {
test: /\.html$/,
include: [
toPath('src/app/pages'),
toPath('src/app/shared'),
],
loader: 'ngtemplate?relativeTo=app/&prefix=app/!html',
}, {
test: /\.md$/,
loaders: [
'ngtemplate?relativeTo=app/&prefix=app/',
'html',
'markdown?config=markdownLoader',
],
}, {
test: /\.css$/,
include: /(material\.css|angular|angular-material)/,
loader: extractVendorStyles.extract('raw'),
}, {
test: /\.css$/,
exclude: /(material\.css|angular|angular-material)/,
loader: extractAppStyles.extract('style', 'css'),
}, {
test: /\.scss$/,
loader: extractAppStyles.extract('style', 'css?sourceMap!sass?sourceMap'),
}],
},
plugins: [
extractAppStyles,
extractVendorStyles,
new CommonsChunkPlugin({
name: 'vendor',
filename: 'vendor.js',
chunks: ['app', 'vendor'],
}),
new CommonsChunkPlugin({
name: 'admin',
filename: 'admin.js',
chunks: ['admin'],
}),
new DefinePlugin({
'process.env': JSON.stringify(_.pick(process.env, 'NODE_ENV')),
'__BUILD_STATS__': JSON.stringify({
gitCommit: {
hash: gitRev.long() + '',
branch: gitRev.branch() + '',
},
host: os.hostname(),
time: +(new Date()),
}),
}),
new HtmlWebpackPlugin({
template: 'maintenance.html',
filename: 'maintenance.html',
hash: true,
minify: {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
},
inject: false,
excludeChunks: ['vendor', 'app'],
}),
new HtmlWebpackPlugin({
template: 'index.html',
hash: true,
minify: {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
},
chunks: ['vendor', 'app'],
}),
new HtmlWebpackPlugin({
template: 'admin/index.html',
filename: 'admin.html',
hash: true,
minify: {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
},
chunks: ['admin'],
}),
// The copy plugin is a hack until we get all assets properly require'd by JS
new CopyWebpackPlugin([{
from: 'assets/',
to: 'assets/',
ignore: ['*.svg', '*.png', '*.webm', '*.mp4', '*.ogg', '*.wav'],
}]),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
// Ignore meaningless moment build warnings:
new webpack.IgnorePlugin(/locale/, /moment/),
].concat(
isDev ? [] : [
new UglifyJsPlugin({
compress: {
// I'm not a fan of hiding warnings, but UglifyJS's are often
// both hard to avoid and rarely useful
warnings: false,
},
}),
new webpack.NormalModuleReplacementPlugin(
/^angular$/,
toPath('/lib/angular-min.js')
),
]
),
devServer: {
historyApiFallback: true,
contentBase: toPath('dist/'),
port: parseInt(_.get(process.env, 'DEV_PORT', '8080'), 10),
stats: 'minimal',
},
markdownLoader: {
renderer: markdownRenderer,
},
};