This repository has been archived by the owner on Jan 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Gruntfile.js
582 lines (529 loc) · 15.3 KB
/
Gruntfile.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/***
* Copyright (c) 2015, 2016 Alex Grant (@localnerve), LocalNerve LLC
* Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
var webpack = require('webpack');
var _nconfig;
/**
* Generate the webpack assets config
*
* @param {Object} self - A reference to the current webpack execution context
* @param {String} [statsJson] - A path to a file to collect the build stats.
*/
function webpackStatsPlugin(self, statsJson) {
self.plugin('done', function(stats) {
var path = require('path');
var fs = require('fs');
var data = stats.toJson();
var assets = data.assetsByChunkName;
var output = {
assets: {}
};
Object.keys(assets).forEach(function (key) {
var value = assets[key];
// if regex matched, use [name] for key
var matches = key.match(self.options.custom.CHUNK_REGEX);
if (matches) {
key = matches[1];
}
output.assets[key] = value;
});
fs.writeFileSync(
path.join(__dirname, self.options.custom.assetsJson),
JSON.stringify(output, null, 4)
);
if (statsJson) {
fs.writeFileSync(statsJson, JSON.stringify(data));
}
});
}
/**
* Create the webpack uglifyJSPlugin with its options.
*/
function createUglifyPlugin () {
return new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
output: {
comments: false
}
});
}
/**
* Create the main build config.
*
* @param {String} type - 'dev', 'prod', or 'perf'.
* @returns {Object} A webpack configuration for the main bundle.
*/
function mainConfig (type) {
var devtoolModuleFilenameTemplate = 'webpack:///main/[resource-path]';
var config = {
resolve: {
extensions: ['', '.js', '.jsx']
},
entry: './client.js',
output: {
path: '<%= project.dist.scripts %>',
publicPath: '<%= project.web.scripts %>/'
},
module: {
loaders: [
{ test: /\.jsx$/, loader: 'jsx-loader' }
]
},
node: {
setImmediate: false
},
plugins: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
// new webpack.NormalModuleReplacementPlugin(/lodash\.assign/, require.resolve('object-assign')),
new webpack.NormalModuleReplacementPlugin(/object\-assign/, require.resolve('object-assign')),
new webpack.NormalModuleReplacementPlugin(/ReactDOMServer/, require.resolve('./utils/serverStub')),
new webpack.NormalModuleReplacementPlugin(/^react\-?$/, require.resolve('react'))
],
stats: {
colors: true
}
};
if (type === 'prod' || type === 'perf') {
config.output.filename = '[name].[chunkhash].min.js';
config.output.chunkFilename = '[name].[chunkhash].min.js';
config.progress = false;
} else {
// dev only
config.output.filename = '[name].js';
config.output.chunkFilename = '[name].js';
config.keepalive = true;
config.watch = true;
}
if (type === 'dev' || type === 'perf') {
var definitions = type === 'dev' ? {
DEBUG: true
} : {
'process.env': {
NODE_ENV: JSON.stringify('production')
}
};
config.output.devtoolModuleFilenameTemplate = devtoolModuleFilenameTemplate;
config.devtool = 'source-map';
config.plugins = [
new webpack.DefinePlugin(definitions)
].concat(config.plugins).concat(
function () {
return webpackStatsPlugin(this);
}
);
} else {
// prod only
config.plugins = [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
})
].concat(config.plugins).concat(
createUglifyPlugin(),
function () {
return webpackStatsPlugin(this, 'webpack-stats-main.json');
}
);
}
return config;
}
/**
* The grunt function export
*/
module.exports = function (grunt) {
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
autoprefixer: {
options: {
browsers: ['last 2 versions', '> 2% in US']
},
all: {
src: '<%= project.dist.css %>'
}
},
clean: ['<%= project.dist.baseDir %>'],
copy: {
assets: {
files: [{
expand: true,
cwd: '<%= project.src.assets %>',
src: ['**', '!**/styles/**', '!images/*.svg', '!scripts/**'],
dest: '<%= project.dist.baseDir %>/'
}]
}
},
compass: {
options: {
sassDir: '<%= project.src.styles %>',
imagesDir: '<%= project.dist.images %>',
httpImagesPath: '<%= project.web.images %>',
fontsDir: '<%= project.dist.fonts %>',
httpFontsPath: '<%= project.web.fonts %>',
cssDir: '<%= project.dist.styles %>',
httpPath: '/',
importPath: [
'<%= project.vendor.css %>',
'<%= project.src.components %>',
'node_modules/react-spinner'
],
environment: 'development',
httpGeneratedImagesPath: '<%= project.web.images %>'
},
dev: {
options: {
watch: false
}
},
watch: {
options: {
watch: true
}
},
prod: {
options: {
outputStyle: 'compressed',
noLineComments: true,
environment: 'production'
}
}
},
concurrent: {
options: {
logConcurrentOutput: true
},
css: ['_cc-watch-ap', '_cc-watch-compass'],
dev: ['_cc-compass-dev', '_cc-nodemon-dev', '_cc-webpack-dev'],
debug: ['_cc-compass-dev', '_cc-nodemon-debug', '_cc-webpack-dev'],
prod: ['_cc-compass-prod', '_cc-nodemon-prod', '_cc-webpack-prod'],
perf: ['_cc-compass-prod', '_cc-nodemon-prod', '_cc-webpack-perf']
},
cssmin: {
prod: {
files: [{
src: '<%= project.dist.css %>',
dest: '<%= project.dist.css %>'
}]
}
},
fixtures: {
options: {
generators: 'tests/generators',
'routes-models.js': {
output: {
routes: 'tests/fixtures/routes-response.js',
models: 'tests/fixtures/models-response.js'
}
}
}
},
imagemin: {
all: {
files: [{
expand: true,
cwd: '<%= project.src.images %>/',
src: ['**/*.{jpg,jpeg,png}'],
dest: '<%= project.dist.images %>/'
}]
}
},
jshint: {
options: {
jshintrc: true
},
all: {
src: [
'*.js',
'{configs,utils,actions,components,services,stores,tests}/**/*.js'
]
}
},
nconfig: {
dev: {
options: {
overrides: {},
env: {
DEBUG: '*',
// ERR_HANDLER_MAINT_ENABLED: TRUE,
ERR_HANDLER_MAINT_RETRYAFTER: 7200
// Can add any env vars here to test out in dev
}
}
},
prod: {
options: {
env: {
NODE_ENV: 'production'
}
}
}
},
nodemon: {
app: {
options: {
ignore: ['node_modules/**', '<%= project.distbase %>/**'],
ext: 'js,jsx'
},
script: './<%= pkg.main %>'
},
debug: {
options: {
ignore: ['node_modules/**', '<%= project.distbase %>/**'],
ext: 'js,jsx',
nodeArgs: ['--debug-brk']
},
script: './<%= pkg.main %>'
}
},
perfbudget: {
options: {
url: process.env.DEPLOY_URL,
key: process.env.WPT_API_KEY,
location: 'Dulles:Chrome',
repeatView: false,
timeout: 300
},
mobile: {
options: {
connectivity: '3G',
emulateMobile: true,
runs: 3,
budget: {
// nominal is < 1500 on 3g, but bg-image doubles it
SpeedIndex: 3000
}
}
}
},
svg2png: {
all: {
files: [{
cwd: '<%= project.src.images %>/',
src: ['**/*.svg'],
dest: '<%= project.dist.images %>/'
}]
}
},
svgmin: {
options: {
},
all: {
files: [{
expand: true,
cwd: '<%= project.src.images %>/',
src: ['**/*.svg'],
dest: '<%= project.dist.images %>/'
}]
}
},
watch: {
ap: {
options: {
spawn: false
},
files: '<%= project.dist.css %>',
tasks: ['autoprefixer']
}
},
webpack: {
options: {
custom: {
assetsJson: '<%= project.src.assetsJson %>',
CHUNK_REGEX: /^([A-Za-z0-9_\-]+)\..*/
}
},
headerDev: {
entry: './<%= project.src.headerScript %>',
output: {
path: '<%= project.dist.scripts %>',
filename: 'header.js'
},
stats: {
colors: true
}
},
headerProd: {
entry: './<%= project.src.headerScript %>',
output: {
path: '<%= project.dist.scripts %>',
filename: 'header.js'
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
createUglifyPlugin()
]
},
dev: mainConfig('dev'),
prod: mainConfig('prod'),
perf: mainConfig('perf')
}
});
/**
* _runFixtureGenerators custom task
* Runs the fixture generators using backend data services.
* Backend data sources selected by environment -
* Must be run after nconfig
* This private task is only run by 'fixtures' task.
*
* @access private
*/
grunt.registerTask('_runFixtureGenerators', 'Subtask to generate test fixtures', function () {
var fs = require('fs');
var path = require('path');
var generator, options = this.options();
var async = this.async();
fs.readdirSync(options.generators).forEach(function (item) {
generator = './'+path.join(options.generators, item);
grunt.log.writeln(
'Executing '+generator + '(' +
require('util').inspect(options[item].output) + ', callback)'
);
require(generator)(options[item].output, async);
});
});
/**
* fixtures custom task
* Runs nconfig and _runFixtureGenerators in order.
* Syntax: fixtures:dev | fixtures:prod
*
* @access public
*/
grunt.registerTask('fixtures', 'Generate test fixtures', function () {
var isProd = this.args.shift() === 'prod';
var options = {
options: this.options()
};
var tasks = [
'nconfig:'+(isProd ? 'prod' : 'dev'),
'_runFixtureGenerators'
];
// Pass along the options to subtasks
grunt.config.set('_runFixtureGenerators', options);
grunt.task.run(tasks);
});
/**
* nconfig custom task
* Creates a config for the project, saves nconfig.settings to grunt.config('project')
* Must run per grunt process.
*
* Options:
* overrides: An object with config settings that overrides all.
* example: settings:dist:images
* env: Set environment variables for this process.
*
* @access public
*/
grunt.registerMultiTask('nconfig', 'Assign config settings to grunt project', function () {
_nconfig = true;
var configLib = require('./configs');
var options = this.options();
if (options.env) {
Object.keys(options.env).forEach(function(key) {
process.env[key] = options.env[key];
});
}
grunt.config('project', configLib.create(options.overrides).settings);
});
/**
* Debug the nconfig task.
* Dumps the computed config to the console.
* Private task used by dumpconfig.
*
* @access private
*/
grunt.registerTask('_dumpconfigTask', function() {
var util = require('util');
var config = require('./configs').create();
var dump = {
project: grunt.config('project'),
nconf: config
};
console.log(util.inspect(dump));
});
/**
* dumpconfig custom task.
* Dumps the computed nconfig to the console by env.
* Syntax: dumpconfig:prod | dumpconfig:dev
*
* @access public
*/
grunt.registerTask('dumpconfig', 'Debug nconfig', function() {
var isProd = this.args.shift() === 'prod';
var tasks = ['nconfig:'+(isProd ? 'prod' : 'dev')];
grunt.task.run(tasks.concat('_dumpconfigTask'));
});
/**
* scss compile custom task
* Sets the env config if req'd, runs required css build tasks, compiles, then runs post processing.
* Used only for standalone css builds outside of the main dev task.
* Syntax: ccss:prod | ccss:dev
*
* @access public
*/
grunt.registerTask('ccss', 'Compile scss', function() {
var isProd = this.args.shift() === 'prod';
var tasks = _nconfig ? [] : ['nconfig:'+(isProd ? 'prod' : 'dev')];
tasks = tasks.concat([
'svg2png', 'svgmin', 'compass:'+(isProd ? 'prod' : 'dev'), 'autoprefixer'
]);
if (!isProd) {
tasks = tasks.concat(['concurrent:css']);
}
grunt.task.run(isProd ? tasks.concat(['cssmin:prod']) : tasks);
});
/**
* Custom task to build the header script, standalone (w/o the dev task).
* For now, just uses webpack, but that makes it unnecessarily bigger.
* Syntax: header:dev | header:prod
*
* @access public
*/
grunt.registerTask('header', 'Build the header script', function() {
var isProd = this.args.shift() === 'prod';
var tasks;
if (isProd) {
tasks = ['nconfig:prod', 'webpack:headerProd'];
}
else {
tasks = ['nconfig:dev', 'webpack:headerDev'];
}
grunt.task.run(tasks);
});
// serial task sequences for concurrent, external grunt processes
grunt.registerTask('_cc-watch-compass', ['nconfig:dev', 'compass:watch']);
grunt.registerTask('_cc-watch-ap', ['nconfig:dev', 'watch:ap']);
grunt.registerTask('_cc-compass-dev', ['nconfig:dev', 'ccss:dev']);
grunt.registerTask('_cc-compass-prod', ['nconfig:prod', 'ccss:prod']);
grunt.registerTask('_cc-nodemon-dev', ['nconfig:dev', 'nodemon:app']);
grunt.registerTask('_cc-nodemon-debug', ['nconfig:dev', 'nodemon:debug']);
grunt.registerTask('_cc-nodemon-prod', ['nconfig:prod', 'nodemon:app']);
grunt.registerTask('_cc-webpack-dev', ['nconfig:dev', 'webpack:headerDev', 'webpack:dev']);
grunt.registerTask('_cc-webpack-prod', ['nconfig:prod', 'webpack:headerProd', 'webpack:prod']);
grunt.registerTask('_cc-webpack-perf', ['nconfig:prod', 'webpack:headerProd', 'webpack:perf']);
// Other development grunt commands commonly used:
// dumpconfig:dev | dumpconfig:prod - dump nconfig configuration
// script interface
grunt.registerTask('default', 'dev');
grunt.registerTask('dev', ['nconfig:dev', 'clean', 'copy', 'jshint', 'concurrent:dev']);
grunt.registerTask('debug', ['nconfig:dev', 'clean', 'copy', 'jshint', 'concurrent:debug']);
grunt.registerTask('prod', ['nconfig:prod', 'clean', 'copy', 'jshint', 'imagemin', 'concurrent:prod']);
grunt.registerTask('perf', ['nconfig:prod', 'clean', 'copy', 'jshint', 'imagemin', 'concurrent:perf']);
grunt.registerTask('build', [
'nconfig:prod', 'clean', 'copy', 'imagemin', 'ccss:prod', 'webpack:headerProd', 'webpack:prod'
]);
// Also used:
// 1. fixtures:dev | fixtures:prod - generate/update test fixtures from backend
// 2. jshint
};