-
Notifications
You must be signed in to change notification settings - Fork 6
/
gruntfile.js
327 lines (309 loc) · 11 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
module.exports = function () {
var grunt = this;
grunt.initConfig({
remove: {
default_options: {
trace: true,
dirList: [
'build',
'dist',
],
},
},
// this is so backwards
run: {
coverage: {
cmd: 'npm',
args: ['run','coverage','--silent'],
},
lint: {
cmd: 'npm',
args: ['run','lint','--silent'],
},
lintdev: { // allows console/debugger
cmd: 'npm',
args: ['run','lintdev','--silent'],
},
jsbeautify: {
cmd: 'node_modules/.bin/js-beautify',
args: [
'-s 4',
'-f', 'build/finitedomain-es5.js',
'-o', 'build/finitedomain-es5-beautified.js',
],
},
},
// we only use this babel for manual inspection. not part of build chain.
babel: {
options: { // http://babeljs.io/docs/usage/options/
// set from package.json (this way it's global, not just this grunt task)
},
build: {
files: [
{
expand: true,
cwd: 'src/',
src: ['**/*.js'],
dest: 'build/src/',
},
{
expand: true,
cwd: 'tests/specs/',
src: ['**/*.js'],
dest: 'build/specs/',
},
],
},
concat: {
files: {
'build/finitedomain-es5.js': ['build/finitedomain.es6.concat.js'],
},
},
},
jsdoc : {
bare: { // out-of-the-box template. very basic.
src: [
// Sources only. Tests are not very relevant.
'src/**/*.js',
// Clone [email protected]:design-systems/ds-architecture.git into same dir
// as your project (so not the project root! one dir up). Optional.
'../ds-architecture/Types/**/*.js',
],
options: {
destination: 'build/jsdocs',
},
},
dist : { // uses ink-docstrap. prettier than basic.
src: [
// Sources only. Tests are not very relevant.
'src/**/*.js',
// Clone [email protected]:design-systems/ds-architecture.git into same dir
// as your project (so not the project root! one dir up). Optional.
'../ds-architecture/Types/**/*.js',
],
options: {
destination: 'build/jsdocs',
// this requires ink-docstrap in your package.json
template : 'node_modules/ink-docstrap/template',
configure : 'node_modules/ink-docstrap/template/jsdoc.conf.json',
},
},
},
watch: {
p: { // build for perf in browser
files: [
'src/**/*.js',
],
tasks: [
'distperf',
],
},
q: { // quick dist, no linting, testing, or minifying. mostly for debugging quickly.
files: [
'src/**/*.js',
],
tasks: [
'distq',
],
},
b: { // quick dist WITH asserts, no linting, testing, or minifying. mostly for debugging quickly.
files: [
'src/**/*.js',
],
tasks: [
'distbug',
],
},
h: { // quick dist WITHOUT asserts, no linting, testing, or minifying. mostly for debugging quickly.
files: [
'src/**/*.js',
],
tasks: [
'distheat',
],
},
},
mochaTest: {
all: {
src: ['tests/specs/**/*.spec.js'],
options: {
bail: true,
require: [
'babel-core/register', // translate es6 syntax to es5
'babel-polyfill', // babel only translates, doesnt add new libs
],
// it appears that babel supports an option to redirect the rc but no idea here
// for now it uses a default config inlined into package.json
//babelrc: 'config/babelrc',
timeout: 6000,
reporter: 'spec',
},
},
nobail: {
src: ['tests/specs/**/*.spec.js'],
options: {
require: [
'babel-core/register', // translate es6 syntax to es5
'babel-polyfill', // babel only translates, doesnt add new libs
],
// it appears that babel supports an option to redirect the rc but no idea here
// for now it uses a default config inlined into package.json
//babelrc: 'config/babelrc',
timeout: 6000,
reporter: 'spec',
},
},
},
browserify: {
options: {
browserifyOptions: {
debug: true,
// the `standalone` option allows browserified modules to
// be imported through es6 import/babel (browser too). without
// this the module would be private and inaccessible forever.
standalone: 'module.exports',
noParse: [
// Include browserified dependency builds
// Target the file name directly with absolute path (-> __dirname)
__dirname + '/node_modules/gom/browser/gom.js',
__dirname + '/node_modules/@the-grid/multiversejson/browser/Multiverse.js',
// Note: This doesn't work properly in browserify yet but it may just be what we want.
// However in that case we should allow chai for phantomjs builds
//function(absPath) {
// var nmPath = __dirname + '/node_modules';
// return absPath.slice(0, nmPath.length) === nmPath;
//},
],
},
transform: [
['babelify', {presets: ['es2015'], sourceMaps: true}],
//["reactify", {"es6": true}],
],
},
phantom: {
files: {
'dist/finitedomain.dev.js': 'src/index.js',
// note: this will include chai and mocha and all that but that's fine (I think?) and workarounds are difficult with es6 static modules anyways
'build/specs-browserified.js': 'tests/specs/**/*.spec.js',
},
},
},
mocha_phantomjs: {
all: ['tests/mocha-runner.html'],
},
uglify: {
dist: {
options: {
report: 'gzip', // false, 'none', 'min', 'gzip'. gzip is a little slower but not significant and good to see.
sourceMap: true,
},
files: {
'dist/finitedomain.dist.min.js': ['build/finitedomain-es5.js'],
},
},
},
concat: {
build: {
options: {
// https://github.com/gruntjs/grunt-contrib-concat
banner: 'let Solver = (function(){',
footer: '\n return Solver;\n})();\nexport default Solver;\n',
sourceMap: true,
sourceMapStyle: 'inline', // embed link inline
process: function(code, path){
if (path === 'src/index.js') return '';
console.log('concatting', path);
code = removeHeaderFooter(code);
code = removeAsserts(code);
code = removeDists(code);
return concatFile(code, path);
},
},
files: {
'build/finitedomain.es6.concat.js': ['src/**/*'],
},
},
test: {
options: {
// https://github.com/gruntjs/grunt-contrib-concat
banner: '',
footer: '\nexport default Solver;',
sourceMap: true,
sourceMapStyle: 'inline', // embed link inline
process: function(code, path){
if (path === 'src/index.js') return '';
console.log('concatting', path);
code = removeHeaderFooter(code);
return concatFile(code, path);
},
},
files: {
'build/finitedomain.es6.concat.js': ['src/**/*'],
},
},
},
});
function removeHeaderFooter(code) {
var match = code.match(/^[\s\S]*?BODY_START([\s\S]*?)\/\/ BODY_STOP/);
if (!match) {
console.error('unable to find body start/stop pragmas in ' + path);
throw 'No body found in ' + path;
}
return match[1];
}
function removeAsserts(code) {
code = code.replace(/^\s*\/\/\s*__REMOVE_BELOW_FOR_ASSERTS__[\s\S]*?__REMOVE_ABOVE_FOR_ASSERTS__[\s\S]*?$/gm, function(match) {
console.log(' - removing ' + match.length + 'b/'+code.length+'b for asserts');
return '';
});
code = code.replace(/^\s*ASSERT.*$/gm, '');
return code;
}
function removeDists(code) {
code = code.replace(/^\s*\/\/\s*__REMOVE_BELOW_FOR_DIST__[\s\S]*?__REMOVE_ABOVE_FOR_DIST__[\s\S]*?$/gm, function(match, before, after) {
console.log(' - removing ' + match.length + 'b/' + code.length + 'b for dist');
return '';
});
return code;
}
function concatFile(code, path) {
return '' +
'\n// from: ' + path + '\n\n' +
code + '\n\n' +
'// end of ' + path + '\n' +
'';
}
grunt.loadNpmTasks('grunt-babel'); // we dont really need this but can be handy for debugging
grunt.loadNpmTasks('grunt-browserify'); // used to build packages for testing in phantomjs
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mocha-phantomjs');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-run'); // runs npm scripts
grunt.loadNpmTasks('grunt-remove');
grunt.loadNpmTasks('grunt-replace');
grunt.loadNpmTasks('grunt-jsdoc');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('concat-dist-to-browserjs', function() {
console.log('- Copying dist to browser.js');
grunt.file.copy('dist/finitedomain.dist.min.js', 'dist/browser.js');
});
grunt.registerTask('concat-bug-to-browserjs', function() {
console.log('- Copying build to browser.js');
grunt.file.copy('build/finitedomain-es5-beautified.js', 'dist/browser.js');
grunt.file.copy('build/finitedomain-es5-beautified.js', 'dist/finitedomain.dist.min.js');
});
grunt.registerTask('clean', ['remove']);
grunt.registerTask('build', 'alias for dist', ['dist']);
grunt.registerTask('dist', 'lint, test, build, minify', ['clean', 'run:lint', 'mochaTest:all', '_dist']);
grunt.registerTask('_dist', 'just build dist', ['clean', 'concat:build', 'babel:concat', 'uglify:dist']);
grunt.registerTask('distq', 'create dist (inc browser.js) without testing', ['_dist', 'concat-dist-to-browserjs']);
grunt.registerTask('distbug', 'create dist for browser debugging, keeps asserts', ['clean', 'concat:test', 'babel:concat', 'run:jsbeautify', 'concat-bug-to-browserjs']);
grunt.registerTask('distheat', 'create dist for heatmap inspection, no asserts', ['clean', 'concat:build', 'babel:concat', 'run:jsbeautify', 'concat-bug-to-browserjs']);
grunt.registerTask('coverage', ['clean', 'run:coverage']);
grunt.registerTask('test', 'lint then test', ['clean', 'run:lintdev', 'mochaTest:all']);
grunt.registerTask('testq', 'test without linting', ['clean', 'mochaTest:nobail']);
// it works in the browser, the phantom test build just needs some love (TODO)
//grunt.registerTask('testp', 'lint then test in phantomjs', ['clean', 'run:lintdev', 'browserify:phantom', 'mocha_phantomjs']);
grunt.registerTask('default', ['test']);
};