-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGruntfile.js
More file actions
620 lines (580 loc) · 19.4 KB
/
Copy pathGruntfile.js
File metadata and controls
620 lines (580 loc) · 19.4 KB
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
var chalk = require('chalk');
var child_process = require('child_process');
var fs = require('fs');
var _ = require('lodash');
var path = require('path');
var pyodide = require('pyodide');
var sass = require('sass');
var envConstants = require('./envConstants');
const {VALID_KARMA_CLI_FLAGS} = require('./karma.conf');
var checkEntryPoints = require('./script/checkEntryPoints');
const {createWebpackConfig} = require('./webpack.config');
const {ALL_APPS, appsEntriesFor} = require('./webpackEntryPoints');
// Review every couple of years to see if an increase improves test performance
const MEM_PER_KARMA_PROCESS_MB = 4300;
module.exports = function (grunt) {
var config = {};
/**
* Interval for filesystem polling in watch mode.
* Warning: 100ms hits 75% CPU on OS X. 700ms is around 10%.
* See https://github.com/gruntjs/grunt-contrib-watch/issues/145
* If OS X polling remains a CPU issue, can try grunt-este-watch
* @const {number}
*/
var DEV_WATCH_INTERVAL = parseInt(grunt.option('delay')) || 700;
/** @const {string} */
var SINGLE_APP = grunt.option('app') || envConstants.APP;
var appsToBuild = SINGLE_APP ? [SINGLE_APP] : ALL_APPS;
var ace_suffix = envConstants.DEV ? '' : '-min';
var piskelRootStdout = child_process.execSync('npx piskel-root');
var piskelRoot = String(piskelRootStdout).replace(/\s+$/g, '');
var PISKEL_DEVELOPMENT_MODE = grunt.option('piskel-dev');
if (PISKEL_DEVELOPMENT_MODE) {
var localNodeModulesRoot = String(
child_process.execSync('npm prefix')
).replace(/\s+$/g, '');
if (piskelRoot.indexOf(localNodeModulesRoot) === -1) {
// Piskel has been linked to a local development repo, we're good to go.
piskelRoot = path.resolve(piskelRoot, '..', 'dev');
console.log(chalk.bold.yellow('-- PISKEL DEVELOPMENT MODE --'));
console.log(
chalk.yellow('Make sure you have a local development build of piskel')
);
console.log(chalk.yellow('Inlining PISKEL_DEVELOPMENT_MODE=true'));
console.log(
chalk.yellow(
'Copying development build of Piskel instead of release build'
)
);
} else {
console.log(chalk.bold.red('Unable to enable Piskel development mode.'));
console.log(
chalk.red(
'In order to use Piskel development mode, your apps ' +
'package must be linked to a local development copy of the Piskel ' +
'repository with a complete dev build.' +
'\n' +
'\n 1. git clone https://github.com/code-dot-org/piskel.git <new-directory>' +
'\n 2. cd <new-directory>' +
'\n 3. npm install && grunt build-dev' +
'\n 4. npm link' +
'\n 5. cd <code-dot-org apps directory>' +
'\n 6. npm link @code-dot-org/piskel' +
'\n 7. rerun your previous command' +
'\n'
)
);
process.exitCode = 1; // Failure!
return;
}
}
config.copy = {
src: {
files: [
{
expand: true,
cwd: 'src/',
src: ['**/*.js', '**/*.jsx'],
dest: 'build/js',
},
],
},
static: {
files: [
{
expand: true,
cwd: 'static/',
src: ['**'],
dest: 'build/package/media',
},
{
expand: true,
cwd: 'lib/blockly/media',
src: ['**'],
//TODO: Would be preferrable to separate Blockly media.
dest: 'build/package/media',
},
{
expand: true,
cwd: 'node_modules/blockly/media',
src: ['**'],
dest: 'build/package/media/google_blockly',
},
{
expand: true,
cwd: 'node_modules/@code-dot-org/craft/dist/assets',
src: ['**'],
dest: 'build/package/media/skins/craft',
},
{
expand: true,
cwd: 'node_modules/@code-dot-org/ml-activities/dist/assets',
src: ['**'],
dest: 'build/package/media/skins/fish',
},
{
expand: true,
cwd: 'node_modules/@code-dot-org/ml-playground/dist/assets',
src: ['**'],
dest: 'build/package/media/skins/ailab',
},
// We have to do some weird stuff to get our fallback video player working.
// video.js expects some of its own files to be served by the application, so
// we include them in our build and access them via static (non-fingerprinted)
// root-relative paths.
// We may have to do something similar with ace editor later, but generally
// we'd prefer to avoid this way of doing things.
// TODO: At some point, we may want to better rationalize the package
// structure for all of our different assets (including vendor assets,
// blockly media, etc).
{
expand: true,
cwd: './node_modules/video.js/dist',
src: ['**'],
dest: 'build/package/video-js',
},
],
},
lib: {
files: [
{
expand: true,
cwd: 'lib/blockly',
src: ['*_*.js'],
dest: 'build/locales',
// e.g., ar_sa.js -> ar_sa/blockly_locale.js
rename: function (dest, src) {
var outputPath = src.replace(
/(.+_.+)\.js/g,
'$1/blockly_locale.js'
);
return path.join(dest, outputPath);
},
},
// minifying ace code requires some advanced configuration:
// https://github.com/ajaxorg/ace/blob/b808ac14ec6d6afa74b36ff5c03452a2832b32a4/Makefile.dryice.js#L620-L638
// instead of replicating that configuration here, we keep minified
// and unminified js in our repo and provide the correct one
// based on whether we are in development or production mode.
{
expand: true,
cwd: 'lib/ace/src' + ace_suffix + '-noconflict/',
src: ['**/*.js'],
dest: 'build/package/js/ace/',
},
// Pull p5.js and p5.play.js into the package from our forks. These are
// needed by the gamelab exporter code in production and development.
{
expand: true,
cwd: './node_modules/@code-dot-org/p5/lib',
src: ['p5.js'],
dest: 'build/package/js/p5play/',
},
{
expand: true,
cwd: './node_modules/@code-dot-org/p5.play/lib',
src: ['p5.play.js'],
dest: 'build/package/js/p5play/',
},
// Piskel must not be minified or digested in order to work properly.
{
expand: true,
// For some reason, if we provide piskel root as an absolute path here,
// our dest ends up with an empty set of directories matching the path
// If we provide it as a relative path, that does not happen
cwd: './' + path.relative(process.cwd(), piskelRoot),
src: ['**'],
dest: 'build/package/js/piskel/',
},
{
expand: true,
cwd: 'lib/droplet',
src: ['droplet-full*.js'],
dest: 'build/minifiable-lib/droplet/',
},
{
expand: true,
cwd: 'lib/droplet',
src: ['droplet.min.css'],
dest: 'build/package/css/droplet/',
},
{
expand: true,
cwd: 'lib/tooltipster',
src: ['*.js'],
dest: 'build/minifiable-lib/tooltipster/',
},
{
expand: true,
cwd: 'lib/phaser',
src: ['*.js'],
dest: 'build/minifiable-lib/phaser/',
},
{
expand: true,
cwd: 'lib/tooltipster',
src: ['tooltipster.min.css'],
dest: 'build/package/css/tooltipster/',
},
{
expand: true,
cwd: 'lib/fileupload',
src: ['*.js'],
dest: 'build/minifiable-lib/fileupload/',
},
{
expand: true,
cwd: 'lib/pyodide',
src: ['*.whl', '*.zip'],
dest: `build/package/js/pyodide/${pyodide.version}`,
},
],
},
unhash: {
files: [
{
expand: true,
cwd: 'build/package/js',
// The applab and gamelab exporters need unhashed copies of these files.
src: [
'webpack-runtimewp*.js',
'webpack-runtimewp*.min.js',
'applab-apiwp*.js',
'applab-apiwp*.min.js',
'gamelab-apiwp*.js',
'gamelab-apiwp*.min.js',
],
dest: 'build/package/js',
// e.g. webpack-runtimewp0123456789aabbccddee.min.js --> webpack-runtime.min.js
rename: function (dest, src) {
var outputFile = src.replace(/wp[0-9a-f]{20}/, '');
return path.join(dest, outputFile);
},
},
],
},
};
config.sass = {
all: {
options: {
// Compression currently occurs at the ../dashboard sprockets layer.
// dart-sass: Only the "expanded" and "compressed" values of outputStyle are supported.
outputStyle: 'expanded',
includePaths: ['node_modules', '../shared/css/'],
implementation: sass,
quietDeps: true,
},
files: _.fromPairs(
[
['build/package/css/common.css', 'style/common.scss'],
[
'build/package/css/code-studio.css',
'style/code-studio/code-studio.scss',
],
[
'build/package/css/certificates.css',
'style/curriculum/certificates.scss',
],
['build/package/css/courses.css', 'style/curriculum/courses.scss'],
['build/package/css/scripts.css', 'style/curriculum/scripts.scss'],
['build/package/css/lessons.css', 'style/curriculum/lessons.scss'],
['build/package/css/markdown.css', 'style/curriculum/markdown.scss'],
['build/package/css/levels.css', 'style/curriculum/levels.scss'],
['build/package/css/rollups.css', 'style/curriculum/rollups.scss'],
[
'build/package/css/curriculum_table_styling.css',
'style/curriculum/curriculum_table_styling.scss',
],
[
'build/package/css/curriculum_navigation.css',
'style/curriculum/navigation.scss',
],
[
'build/package/css/levelbuilder.css',
'style/code-studio/levelbuilder.scss',
],
[
'build/package/css/leveltype_widget.css',
'style/code-studio/leveltype_widget.scss',
],
['build/package/css/plc.css', 'style/code-studio/plc.scss'],
['build/package/css/pd.css', 'style/code-studio/pd.scss'],
['build/package/css/petition.css', 'style/code-studio/petition.scss'],
[
'build/package/css/publicKeyCryptography.css',
'style/publicKeyCryptography/publicKeyCryptography.scss',
],
[
'build/package/css/foorm_editor.css',
'style/code-studio/foorm_editor.scss',
],
].concat(
appsToBuild.map(function (app) {
return [
'build/package/css/' + app + '.css', // dst
'style/' + app + '/style.scss', // src
];
})
)
),
},
};
// Takes a key-value .json file and runs it through MessageFormat to create a localized .js file.
config.messages = {
all: {
options: {
dest: 'build/locales',
},
files: [
{
// e.g., build/js/i18n/bounce/ar_sa.json -> build/package/js/ar_sa/bounce_locale.js
rename: function (dest, src) {
var outputPath = src.replace(
/(build\/)?i18n\/(\w*)\/(\w+_\w+).json/g,
'$3/$2_locale.js'
);
return path.join(dest, outputPath);
},
expand: true,
src: ['i18n/**/*.json'],
dest: 'build/locales',
},
],
},
};
config.ejs = {
all: {
srcBase: 'src',
destBase: 'build/js',
},
};
config.exec = {
convertScssVars: './script/convert-scss-variables.js',
generateSharedConstants: 'bundle exec ./script/generateSharedConstants.rb',
};
grunt.registerTask('karma', ['preconcatForKarma', 'karma start']);
grunt.registerTask('karma start', () => {
// Forward select grunt command-line flags to `karma start`
const KARMA_CLI_FLAGS = VALID_KARMA_CLI_FLAGS.flatMap(arg =>
grunt.option(arg) ? [`--${arg}`, grunt.option(arg)] : []
);
console.log(chalk.green(`>> npx karma start ${KARMA_CLI_FLAGS.join(' ')}`));
child_process.spawnSync('npx', ['karma', 'start', ...KARMA_CLI_FLAGS], {
stdio: 'inherit',
env: {
...process.env,
NODE_OPTIONS: `--max-old-space-size=${MEM_PER_KARMA_PROCESS_MB}`,
},
});
});
grunt.registerTask('preconcatForKarma', [
'newer:messages',
'exec:convertScssVars',
'exec:generateSharedConstants',
'newer:copy:static',
]);
config.clean = {
build: ['build'],
karma: {
options: {force: true},
src: ['build/karma'],
},
};
const piskelDevMode = PISKEL_DEVELOPMENT_MODE;
// Create a webpack entry point for each of the apps in `appsToBuild`.
// See `ALL_APPS` in webpackEntryPoints.js for a list of valid apps, e.g.
// gamelab, maze, etc.
var appsEntries = appsEntriesFor(appsToBuild);
config.webpack = {
build: createWebpackConfig({
appsEntries,
piskelDevMode,
}),
uglify: createWebpackConfig({
appsEntries,
piskelDevMode,
minify: true,
}),
};
// This is started by `yarn start`, and is the normal dev mode with HMR
config['webpack-dev-server'] = {
dev: createWebpackConfig({
appsEntries,
piskelDevMode,
}),
};
config.uglify = {
lib: {
files: _.fromPairs(
['p5play/p5.play.js', 'p5play/p5.js'].map(function (src) {
return [
'build/package/js/' + src.replace(/\.js$/, '.min.js'), // dst
'build/package/js/' + src, // src
];
})
),
},
};
config.watch = {
// JS files watched by webpack
style: {
files: ['style/**/*.scss', 'style/**/*.sass'],
tasks: ['newer:sass', 'notify:sass'],
options: {
interval: DEV_WATCH_INTERVAL,
interrupt: true,
},
},
content: {
files: ['static/**/*'],
tasks: ['newer:copy', 'notify:content'],
options: {
interval: DEV_WATCH_INTERVAL,
},
},
vendor_js: {
files: ['lib/**/*.js'],
tasks: ['newer:copy:lib', 'notify:vendor_js'],
options: {
interval: DEV_WATCH_INTERVAL,
},
},
messages: {
files: ['i18n/**/*.json'],
tasks: ['messages', 'notify:messages'],
options: {
interval: DEV_WATCH_INTERVAL,
},
},
};
config.concurrent = {
// run our two watch tasks concurrently so that they dont block each other
watch: {
tasks: ['watch', 'webpack-dev-server'],
options: {
logConcurrentOutput: true,
},
},
};
config.notify = {
'js-build': {options: {message: 'JS build completed.'}},
sass: {options: {message: 'SASS build completed.'}},
content: {options: {message: 'Content build completed.'}},
ejs: {options: {message: 'EJS build completed.'}},
messages: {options: {message: 'i18n messages build completed.'}},
vendor_js: {options: {message: 'vendor JS copy done.'}},
};
grunt.initConfig(config);
// Autoload grunt tasks
require('load-grunt-tasks')(grunt, {
pattern: ['grunt-*', '!grunt-lib-contrib'],
});
grunt.loadTasks('tasks');
grunt.registerTask('noop', function () {});
// Generate locale stub files in the build/locale/current folder
grunt.registerTask('locales', function () {
var current = path.resolve('build/locale/current');
child_process.execSync('mkdir -p ' + current);
appsToBuild
.concat(
'common',
'tutorialExplorer',
'regionalPartnerSearch',
'regionalPartnerMiniContact'
)
.map(function (item) {
var localeType = item === 'common' ? 'locale' : 'appLocale';
var localeString =
'/*' +
item +
'*/ ' +
'module.exports = window.locales.' +
localeType +
';';
fs.writeFileSync(path.join(current, item + '.js'), localeString);
});
});
// Checks the size of Droplet to ensure it's built with LANGUAGE=javascript
grunt.registerTask('checkDropletSize', function () {
var bytes = fs.statSync('lib/droplet/droplet-full.min.js').size;
if (bytes > 500 * 1000) {
grunt.warn(
'"droplet-full.min.js" is larger than 500kb. Did you build with LANGUAGE=javascript?'
);
}
});
grunt.registerTask('prebuild', [
'checkDropletSize',
// 'lint-entry-points',
'newer:messages',
'exec:convertScssVars',
'exec:generateSharedConstants',
'newer:copy:src',
'newer:copy:lib',
'locales',
'ejs',
'detect-production-webpack-chunks',
]);
grunt.registerTask('check-entry-points', function () {
const done = this.async();
checkEntryPoints(config.webpack.build, {verbose: true}).then(stats =>
done()
);
});
grunt.registerTask('lint-entry-points', function () {
const done = this.async();
checkEntryPoints(config.webpack.build).then(stats => {
console.log(
[
chalk.green(`[${stats.passed} passed]`),
stats.silenced && chalk.yellow(`[${stats.silenced} silenced]`),
stats.failed && chalk.red(`[${stats.failed} failed]`),
]
.filter(f => f)
.join(' ')
);
if (stats.failed > 0) {
grunt.warn(
`${stats.failed} entry points do not conform to naming conventions.\n` +
`Run grunt check-entry-points for details.\n`
);
}
done();
});
});
grunt.registerTask('detect-production-webpack-chunks', function () {
if (
process.env.DEV &&
fs.existsSync('./build/package/js/code-studio-common.js')
) {
grunt.warn(
'You are building in dev mode (DEV=1), but the build/ directory already contains production Webpack chunks, such as code-studio-common.js.\n' +
'These will not be overwritten by a dev build, and their presence will cause loading errors in some labs (e.g., Applab).\n' +
"Note that after cleaning your build directory, some static assets that aren't rebuilt via yarn start may not load.\n" +
'These can be regenerated via yarn build.\n' +
'Run yarn clean (then yarn build) and try again.'
);
}
});
grunt.registerTask('postbuild', ['newer:copy:static', 'newer:sass']);
grunt.registerTask('build', [
'prebuild',
// For any minifiable libs, generate minified sources if they do not already
// exist in our repo. Skip minification in development environment.
envConstants.DEV ? 'noop' : 'uglify:lib',
envConstants.DEV ? 'webpack:build' : 'webpack:uglify',
'notify:js-build',
'postbuild',
envConstants.DEV ? 'noop' : 'newer:copy:unhash',
]);
grunt.registerTask('rebuild', ['clean', 'build']);
grunt.registerTask('dev', function () {
// Unless explicitly overridden, set HOT=1 and DEV=1 when running `grunt dev`
process.env.HOT ||= 1;
process.env.DEV ||= 1;
grunt.task.run(['prebuild', 'newer:sass', 'concurrent:watch', 'postbuild']);
});
grunt.registerTask('default', ['rebuild', 'test']);
};
// Exported for matching use in `run-tests-in-parallel.sh`
module.exports.MEM_PER_KARMA_PROCESS_MB = MEM_PER_KARMA_PROCESS_MB;