forked from processing/p5.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
498 lines (469 loc) · 15 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
/**
* This is the Gruntfile for p5.js. Grunt is a task runner/builder
* which is what p5.js uses to build the source code into the library
* and handle other housekeeping tasks.
*
* There are three main tasks:
*
* grunt - This is the default task, which builds the code, tests it
* using both jslint and mocha, and then minifies it.
*
* grunt yui - This will build the inline documentation for p5.js.
* The generated documentation is assumed to be
* served from the /reference/ folder of the p5js
* website (https://github.com/processing/p5.js-website).
*
* grunt test - This rebuilds the source and runs the automated tests on
* both the minified and unminified code. If you need to debug
* a test suite in a browser, `grunt test --keepalive` will
* start the connect server and leave it running; the tests
* can then be opened at localhost:9001/test/test.html
*
* grunt yui:dev - This rebuilds the inline documentation. It also rebuilds
* each time a change to the source is detected. You can preview
* the reference at localhost:9001/docs/reference/
*
* Note: `grunt test:nobuild` will skip the build step when running the tests,
* and only runs the test files themselves through the linter: this can save
* a lot of time when authoring test specs without making any build changes.
*
* And there are several secondary tasks:
*
*
* grunt watch - This watches the source for changes and rebuilds on
* every file change, running the linter and tests.
*
* grunt watch:main - This watches the source for changes and rebuilds on
* every file change, but does not rebuild the docs.
* It's faster than the default watch.
*
* grunt watch:quick - This watches the source for changes and rebuilds
* p5.js on every file change, but does not rebuild
* docs, and does not perform linting, minification,
* or run tests. It's faster than watch:main.
*
* grunt karma - This runs the performance benchmarks in
* multiple real browsers on the developers local machine.
* It will automatically detect which browsers are
* installed from the following list (Chrome, Firefox,
* Safari, Edge, IE) and run the benchmarks in all installed
* browsers and report the results. Running "grunt karma"
* will execute ALL the benchmarks. If you want to run a
* specific benchmark you can by specifying the target e.g.
* "grunt karma:random-dev". The available targets are
* defined in grunt-karma.js.
*
* Contributors list can be updated using all-contributors-cli:
* https://www.npmjs.com/package/all-contributors-cli
*
* all-contributors generate - Generates new contributors list for README
*/
function getYuidocOptions() {
const BASE_YUIDOC_OPTIONS = {
name: '<%= pkg.name %>',
description: '<%= pkg.description %>',
version: '<%= pkg.version %>',
url: '<%= pkg.homepage %>',
options: {
paths: ['src/', 'lib/addons/'],
themedir: 'docs/yuidoc-p5-theme/',
helpers: [],
preprocessor: './docs/preprocessor.js',
outdir: 'docs/reference/'
}
};
// note dev is no longer used, prod is used to build both testing and production ready docs
const o = {
prod: JSON.parse(JSON.stringify(BASE_YUIDOC_OPTIONS)),
dev: JSON.parse(JSON.stringify(BASE_YUIDOC_OPTIONS))
};
o.prod.options.helpers.push('docs/yuidoc-p5-theme/helpers/helpers_prod.js');
o.dev.options.helpers.push('docs/yuidoc-p5-theme/helpers/helpers_dev.js');
return o;
}
module.exports = grunt => {
// Specify what reporter we'd like to use for Mocha
const quietReport = process.env.TRAVIS || grunt.option('quiet');
const reporter = quietReport ? 'spec' : 'Nyan';
// Load karma tasks from an external file to keep this file clean
const karmaTasks = require('./grunt-karma.js');
// For the static server used in running tests, configure the keepalive.
// (might not be useful at all.)
let keepalive = false;
if (grunt.option('keepalive')) {
keepalive = true;
}
const mochaConfig = {
yui: {
options: {
urls: ['http://localhost:9001/test/test-reference.html'],
reporter: reporter,
run: false,
log: true,
logErrors: true,
growlOnSuccess: false
}
},
test: {
options: {
urls: [
'http://localhost:9001/test/test.html',
'http://localhost:9001/test/test-minified.html'
],
reporter: reporter,
run: true,
log: true,
logErrors: true,
timeout: 100000,
growlOnSuccess: false
}
}
};
let gruntConfig = {
// read in the package, used for knowing the current version, et al.
pkg: grunt.file.readJSON('package.json'),
// Configure style consistency checking for this file, the source, and the tests.
eslint: {
options: {
format: 'unix'
},
build: {
src: [
'Gruntfile.js',
'grunt-karma.js',
'docs/preprocessor.js',
'utils/**/*.js',
'tasks/**/*.js'
]
},
fix: {
// src: is calculated below...
options: {
rules: {
'no-undef': 0,
'no-unused-vars': 0
},
fix: true
}
},
source: {
options: {
parserOptions: {
ecmaVersion: 5
}
},
src: ['src/**/*.js', 'lib/addons/p5.dom.js']
},
test: {
src: ['bench/**/*.js', 'test/**/*.js', '!test/js/*.js']
}
},
'eslint-samples': {
options: {
parserOptions: {
ecmaVersion: 6
},
format: 'unix'
},
source: {
src: ['src/**/*.js', 'lib/addons/p5.dom.js']
},
fix: {
options: {
fix: true
}
}
},
// Set up the watch task, used for live-reloading during development.
// This watches both the codebase and the yuidoc theme. Changing the
// code touches files within the theme, so it will also recompile the
// documentation.
watch: {
quick: {
files: ['src/**/*.js', 'src/**/*.frag', 'src/**/*.vert'],
tasks: ['browserify'],
options: {
livereload: true
}
},
// Watch the codebase for changes
main: {
files: ['src/**/*.js'],
tasks: ['newer:eslint:source', 'test'],
options: {
livereload: true
}
},
// watch the theme for changes
reference_build: {
files: ['docs/yuidoc-p5-theme/**/*'],
tasks: ['yuidoc'],
options: {
livereload: true,
interrupt: true
}
},
// Watch the codebase for doc updates
// launch with 'grunt requirejs connect watch:yui'
yui: {
files: [
'src/**/*.js',
'lib/addons/*.js',
'src/**/*.frag',
'src/**/*.vert'
],
tasks: [
'browserify',
'browserify:min',
'yuidoc:prod',
'clean:reference',
'minjson',
'uglify'
],
options: {
livereload: true
}
}
},
// Set up node-side (non-browser) mocha tests.
mochaTest: {
test: {
src: ['test/node/**/*.js'],
options: {
reporter: reporter,
ui: 'tdd'
}
}
},
// Set up the mocha task, used for running the automated tests.
mocha: mochaConfig,
mochaChrome: mochaConfig,
// This minifies the javascript into a single file and adds a banner to the
// front of the file.
uglify: {
options: {
compress: {
global_defs: {
IS_MINIFIED: true
}
},
banner:
'/*! p5.js v<%= pkg.version %> <%= grunt.template.today("mmmm dd, yyyy") %> */ '
},
dist: {
files: {
'lib/p5.min.js': 'lib/p5.pre-min.js',
'lib/addons/p5.dom.min.js': 'lib/addons/p5.dom.js'
}
}
},
// this builds the documentation for the codebase.
yuidoc: getYuidocOptions(),
// Clean up unused files generated by yuidoc
clean: {
reference: {
src: [
'docs/reference/classes/',
'docs/reference/elements/',
'docs/reference/files/',
'docs/reference/modules/',
'docs/reference/api.js'
]
}
},
// This runs benchmarks in multiple real browsers for developing
// performance optimizations
karma: karmaTasks,
// This is a static server which is used when testing connectivity for the
// p5 library. This avoids needing an internet connection to run the tests.
// It serves all the files in the test directory at http://localhost:9001/
connect: {
server: {
options: {
directory: {
path: './',
options: {
icons: true
}
},
port: 9001,
keepalive: keepalive,
middleware: function(connect, options, middlewares) {
middlewares.unshift(
require('connect-modrewrite')([
'^/assets/js/p5(\\.min)?\\.js(.*) /lib/p5$1.js$2 [L]',
'^/assets/js/p5\\.(dom|sound)(\\.min)?\\.js(.*) /lib/addons/p5.$1$2.js$3 [L]'
]),
function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', '*');
return next();
}
);
return middlewares;
}
}
},
yui: {
options: {
directory: {
path: './',
options: {
icons: true
}
},
port: 9001,
open: 'http://127.0.0.1:9001/docs/reference/',
keepalive: keepalive,
middleware: function(connect, options, middlewares) {
middlewares.unshift(
require('connect-modrewrite')([
'^/assets/js/p5(\\.min)?\\.js(.*) /lib/p5$1.js$2 [L]',
'^/assets/js/p5\\.(dom|sound)(\\.min)?\\.js(.*) /lib/addons/p5.$1$2.js$3 [L]'
]),
function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', '*');
return next();
}
);
return middlewares;
}
}
},
test: {
options: {
directory: {
path: './',
options: {
icons: true
}
},
port: 9001,
open: 'http://127.0.0.1:9001/test/',
keepalive: keepalive,
middleware: function(connect, options, middlewares) {
middlewares.unshift(
require('connect-modrewrite')([
'^/assets/js/p5(\\.min)?\\.js(.*) /lib/p5$1.js$2 [L]',
'^/assets/js/p5\\.(dom|sound)(\\.min)?\\.js(.*) /lib/addons/p5.$1$2.js$3 [L]'
]),
function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', '*');
return next();
}
);
return middlewares;
}
}
}
},
'saucelabs-mocha': {
all: {
options: {
urls: ['http://127.0.0.1:9001/test/test.html'],
tunnelTimeout: 5,
build: process.env.TRAVIS_JOB_ID,
concurrency: 3,
browsers: [
{ browserName: 'chrome' },
{ browserName: 'firefox', platform: 'Linux', version: '42.0' },
{ browserName: 'safari' }
],
testname: 'p5.js mocha tests',
tags: ['master']
}
}
},
minjson: {
compile: {
files: {
'./docs/reference/data.min.json': './docs/reference/data.json'
}
}
}
};
// eslint fixes everything it checks:
gruntConfig.eslint.fix.src = Object.keys(gruntConfig.eslint)
.map(s => gruntConfig.eslint[s].src)
.reduce((a, b) => a.concat(b), [])
.filter(a => a);
/* not yet
gruntConfig['eslint-samples'].fix.src = Object.keys(
gruntConfig['eslint-samples']
)
.map(s => gruntConfig['eslint-samples'][s].src)
.reduce((a, b) => a.concat(b), [])
.filter(a => a);
*/
grunt.initConfig(gruntConfig);
// Load build tasks.
// This contains the complete build task ("browserify")
// and the task to generate user select modules of p5
// ("combineModules") which can be invoked directly by
// `grunt combineModules:module_1:module_2` where core
// is included by default in all combinations always.
// NOTE: "module_x" is the name of it's folder in /src.
grunt.loadTasks('tasks/build');
// Load release task
grunt.loadTasks('tasks/release');
// Load tasks for testing
grunt.loadTasks('tasks/test');
// Load the external libraries used.
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-eslint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-yuidoc');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-minjson');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-newer');
grunt.loadNpmTasks('grunt-release-it');
grunt.loadNpmTasks('grunt-saucelabs');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-contrib-clean');
// Create the multitasks.
grunt.registerTask('build', ['browserify', 'browserify:min', 'uglify']);
grunt.registerTask('lint-no-fix', [
'yui', // required for eslint-samples
'eslint:build',
'eslint:source',
'eslint:test',
'eslint-samples:source'
]);
grunt.registerTask('lint-fix', ['eslint:fix']);
grunt.registerTask('test', [
'lint-no-fix',
//'yuidoc:prod', // already done by lint-no-fix
'build',
'connect:server',
'mochaChrome',
'mochaTest'
]);
grunt.registerTask('test:nobuild', [
'eslint:test',
'connect:server',
'mochaChrome',
'mochaTest'
]);
grunt.registerTask('yui', ['yuidoc:prod', 'clean:reference', 'minjson']);
grunt.registerTask('yui:test', [
'yuidoc:prod',
'clean:reference',
'connect:yui',
'mochaChrome:yui'
]);
grunt.registerTask('yui:dev', [
'yui:prod',
'clean:reference',
'build',
'connect:yui',
'watch:yui'
]);
grunt.registerTask('yui:build', ['yui']);
grunt.registerTask('default', ['test']);
grunt.registerTask('saucetest', ['connect:server', 'saucelabs-mocha']);
};