-
Notifications
You must be signed in to change notification settings - Fork 18
/
Gruntfile.js
291 lines (266 loc) · 10.2 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
/**
* Copyright 2015, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
// format `*.[chunkhash].min.js`
var CHUNK_REGEX = /^([A-Za-z0-9_\-]+)\..*/;
module.exports = function (grunt) {
grunt.initConfig({
// project variables
project: {
build: './build',
public: '/public',
cdnPath: 'http://l.yimg.com/os/flx/'
},
// clean build
clean: ['build'],
// ------------------------------------------------------------------------------
// DEV TASKS --------------------------------------------------------------------
// ------------------------------------------------------------------------------
jshint: {
all: [
'*.js',
'{actions,components,services,stores}/**/*.js'
],
options: {
jshintrc: true
}
},
copy: {
images: {
files: [{
expand: true,
cwd: 'assets/',
src: ['images/**'],
dest: '<%= project.build %>/'
}]
}
},
// run nodemon and watch concurrently
concurrent: {
dev: ['nodemon:app', 'watch', 'webpack:dev'],
options: {
logConcurrentOutput: true
}
},
// watch for changes in static files (does not require a server restart) run tasks then reload
// .rebooted is written by the nodemon task, it's written to force a browser reload
watch: {
atomizer: {
files: ['configs/atomic.js', '.rebooted', './assets/css/*.css', './components/*.jsx'],
tasks: ['atomizer', 'cssmin:dev'],
options: {
interrupt: true,
livereload: false
}
}
},
// atomizer: initial task to generate the config
atomizer: {
app: {
options: {
namespace: '#atomic',
configFile: './configs/atomic.js'
},
files: [
{
src: ['./components/*.jsx'],
dest: '<%= project.build %>/css/atomic.css'
}
]
}
},
// cssmin for production (atomizer needs to run first)
cssmin: {
dev: {
options: {
report: 'gzip',
compatibility: 'ie8',
sourceMap: true
},
files: [{
src: [
'<%= project.build %>/css/atomic.css',
'./assets/css/base.css',
'./assets/css/helpers.css',
'./assets/css/custom.css',
'./assets/css/mq.css',
'./assets/css/syntax.css'
],
dest: '<%= project.build %>/css/bundle.css'
}, {
src: [ './assets/css/ie.css'],
dest: '<%= project.build %>/css/ie.css'
}]
},
prod: {
options: {
report: 'gzip',
compatibility: 'ie8',
sourceMap: false
},
files: [{
src: [
'<%= project.build %>/css/atomic.css',
'./assets/css/base.css',
'./assets/css/helpers.css',
'./assets/css/custom.css',
'./assets/css/mq.css',
'./assets/css/syntax.css'
],
dest: '<%= project.build %>/css/bundle.css'
}, {
src: [ './assets/css/ie.css'],
dest: '<%= project.build %>/css/ie.css'
}]
}
},
// functional testing
protractor_webdriver: {
options: {
path: './node_modules/.bin/'
},
all: {}
},
protractor: {
options: {
configFile: './tests/spec/protractor.conf.js',
args: {
baseUrl: 'http://127.0.0.1:3000/'
}
},
all: {}
},
// restart server on changes
nodemon: {
app: {
script: './start.js',
options: {
ignore: ['<%= project.build %>/**'],
ext: 'js,jsx,md'
}
}
},
// webpack bundling
webpack: {
dev: {
resolve: {
extensions: ['', '.js', '.jsx']
},
entry: './client.js',
output: {
path: '<%= project.build %>/js',
publicPath: '/public/js/',
filename: '[name].js',
chunkFilename: '[name].[chunkhash].js'
},
module: {
loaders: [
{ test: /\.css$/, loader: 'style!css' },
{ test: /\.jsx?$/, exclude: /node_modules/, loader: require.resolve('babel-loader') },
{ test: /\.json$/, loader: 'json-loader'}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development')
}
}),
new webpack.optimize.CommonsChunkPlugin('common.js', undefined, 2),
new webpack.NormalModuleReplacementPlugin(/^react(\/addons)?$/, require.resolve('react/addons'))
],
stats: {
colors: true
},
devtool: 'source-map',
watch: true,
keepalive: true
},
prod: {
resolve: {
extensions: ['', '.js', '.jsx']
},
entry: './client.js',
output: {
path: '<%= project.build %>/js',
publicPath: '<%= project.cdnPath %>js/',
filename: '[name].[chunkhash].min.js',
chunkFilename: '[name].[chunkhash].min.js'
},
module: {
loaders: [
{ test: /\.css$/, loader: 'style!css' },
{ test: /\.jsx?$/, exclude: /node_modules/, loader: require.resolve('babel-loader') },
{ test: /\.json$/, loader: 'json-loader'}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
// These are performance optimizations for your bundles
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.CommonsChunkPlugin('common.[hash].min.js', 2),
// This ensures requires for `react` and `react/addons` normalize to the same requirement
new webpack.NormalModuleReplacementPlugin(/^react(\/addons)?$/, require.resolve('react/addons')),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
// generates webpack assets config to use hashed assets in production mode
function webpackStatsPlugin() {
this.plugin('done', function(stats) {
var data = stats.toJson();
var assets = data.assetsByChunkName;
var output = {
assets: {},
cdnPath: this.options.output.publicPath
};
Object.keys(assets).forEach(function eachAsset(key) {
var value = assets[key];
// if `*.[chunkhash].min.js` regex matched, then use file name for key
var matches = key.match(CHUNK_REGEX);
if (matches) {
key = matches[1];
}
output.assets[key] = value;
});
fs.writeFileSync(
path.join(process.cwd(), 'build', 'assets.json'),
JSON.stringify(output, null, 4)
);
});
}
],
// removes verbosity from builds
progress: false
}
}
});
// libs
grunt.loadNpmTasks('grunt-atomizer');
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-nodemon');
grunt.loadNpmTasks('grunt-protractor-webdriver');
grunt.loadNpmTasks('grunt-protractor-runner');
grunt.loadNpmTasks('grunt-webpack');
// tasks
grunt.registerTask('default', 'dev');
grunt.registerTask('dev', ['clean', 'copy', 'jshint', 'atomizer:app', 'cssmin:dev', 'concurrent:dev']);
grunt.registerTask('build', ['clean', 'copy', 'atomizer:app', 'cssmin:prod', 'webpack:prod']);
grunt.registerTask('func', ['protractor_webdriver', 'protractor']);
};