MDL-63375 workshop: apply specific IE10/11 fix
[moodle.git] / Gruntfile.js
blobcd211c396969f8e87e9875023eb346e83b8638d8
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15 /* jshint node: true, browser: false */
16 /* eslint-env node */
18 /**
19  * @copyright  2014 Andrew Nicols
20  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21  */
23 /**
24  * Grunt configuration
25  */
27 /* eslint-env node */
28 module.exports = function(grunt) {
29     var path = require('path'),
30         tasks = {},
31         cwd = process.env.PWD || process.cwd(),
32         async = require('async'),
33         DOMParser = require('xmldom').DOMParser,
34         xpath = require('xpath'),
35         semver = require('semver'),
36         watchman = require('fb-watchman'),
37         watchmanClient = new watchman.Client(),
38         gruntFilePath = process.cwd();
40     // Verify the node version is new enough.
41     var expected = semver.validRange(grunt.file.readJSON('package.json').engines.node);
42     var actual = semver.valid(process.version);
43     if (!semver.satisfies(actual, expected)) {
44         grunt.fail.fatal('Node version not satisfied. Require ' + expected + ', version installed: ' + actual);
45     }
47     // Windows users can't run grunt in a subdirectory, so allow them to set
48     // the root by passing --root=path/to/dir.
49     if (grunt.option('root')) {
50         var root = grunt.option('root');
51         if (grunt.file.exists(__dirname, root)) {
52             cwd = path.join(__dirname, root);
53             grunt.log.ok('Setting root to ' + cwd);
54         } else {
55             grunt.fail.fatal('Setting root to ' + root + ' failed - path does not exist');
56         }
57     }
59     var files = null;
60     if (grunt.option('files')) {
61         // Accept a comma separated list of files to process.
62         files = grunt.option('files').split(',');
63     }
65     var inAMD = path.basename(cwd) == 'amd';
67     // Globbing pattern for matching all AMD JS source files.
68     var amdSrc = [];
69     if (inAMD) {
70         amdSrc.push(cwd + "/src/*.js");
71         amdSrc.push(cwd + "/src/**/*.js");
72     } else {
73         amdSrc.push("**/amd/src/*.js");
74         amdSrc.push("**/amd/src/**/*.js");
75     }
77     /**
78      * Function to generate the destination for the uglify task
79      * (e.g. build/file.min.js). This function will be passed to
80      * the rename property of files array when building dynamically:
81      * http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
82      *
83      * @param {String} destPath the current destination
84      * @param {String} srcPath the  matched src path
85      * @return {String} The rewritten destination path.
86      */
87     var babelRename = function(destPath, srcPath) {
88         destPath = srcPath.replace('src', 'build');
89         destPath = destPath.replace('.js', '.min.js');
90         return destPath;
91     };
93     /**
94      * Find thirdpartylibs.xml and generate an array of paths contained within
95      * them (used to generate ignore files and so on).
96      *
97      * @return {array} The list of thirdparty paths.
98      */
99     var getThirdPartyPathsFromXML = function() {
100         var thirdpartyfiles = grunt.file.expand('*/**/thirdpartylibs.xml');
101         var libs = ['node_modules/', 'vendor/'];
103         thirdpartyfiles.forEach(function(file) {
104           var dirname = path.dirname(file);
106           var doc = new DOMParser().parseFromString(grunt.file.read(file));
107           var nodes = xpath.select("/libraries/library/location/text()", doc);
109           nodes.forEach(function(node) {
110             var lib = path.join(dirname, node.toString());
111             if (grunt.file.isDir(lib)) {
112                 // Ensure trailing slash on dirs.
113                 lib = lib.replace(/\/?$/, '/');
114             }
116             // Look for duplicate paths before adding to array.
117             if (libs.indexOf(lib) === -1) {
118                 libs.push(lib);
119             }
120           });
121         });
122         return libs;
123     };
125     // Project configuration.
126     grunt.initConfig({
127         eslint: {
128             // Even though warnings dont stop the build we don't display warnings by default because
129             // at this moment we've got too many core warnings.
130             // To display warnings call: grunt eslint --show-lint-warnings
131             // To fail on warnings call: grunt eslint --max-lint-warnings=0
132             // Also --max-lint-warnings=-1 can be used to display warnings but not fail.
133             options: {
134                 quiet: (!grunt.option('show-lint-warnings')) && (typeof grunt.option('max-lint-warnings') === 'undefined'),
135                 maxWarnings: ((typeof grunt.option('max-lint-warnings') !== 'undefined') ? grunt.option('max-lint-warnings') : -1)
136             },
137             amd: {src: files ? files : amdSrc},
138             // Check YUI module source files.
139             yui: {src: files ? files : ['**/yui/src/**/*.js', '!*/**/yui/src/*/meta/*.js']}
140         },
141         babel: {
142             options: {
143                 sourceMaps: true,
144                 comments: false,
145                 plugins: [
146                     'transform-es2015-modules-amd-lazy',
147                     'system-import-transformer',
148                     // This plugin modifies the Babel transpiling for "export default"
149                     // so that if it's used then only the exported value is returned
150                     // by the generated AMD module.
151                     //
152                     // It also adds the Moodle plugin name to the AMD module definition
153                     // so that it can be imported as expected in other modules.
154                     path.resolve('babel-plugin-add-module-to-define.js'),
155                     '@babel/plugin-syntax-dynamic-import',
156                     '@babel/plugin-syntax-import-meta',
157                     ['@babel/plugin-proposal-class-properties', {'loose': false}],
158                     '@babel/plugin-proposal-json-strings'
159                 ],
160                 presets: [
161                     ['minify', {
162                         // This minification plugin needs to be disabled because it breaks the
163                         // source map generation and causes invalid source maps to be output.
164                         simplify: false,
165                         builtIns: false
166                     }],
167                     ['@babel/preset-env', {
168                         targets: {
169                             browsers: [
170                                 ">0.25%",
171                                 "last 2 versions",
172                                 "not ie <= 10",
173                                 "not op_mini all",
174                                 "not Opera > 0",
175                                 "not dead"
176                             ]
177                         },
178                         modules: false,
179                         useBuiltIns: false
180                     }]
181                 ]
182             },
183             dist: {
184                 files: [{
185                     expand: true,
186                     src: files ? files : amdSrc,
187                     rename: babelRename
188                 }]
189             }
190         },
191         sass: {
192             dist: {
193                 files: {
194                     "theme/boost/style/moodle.css": "theme/boost/scss/preset/default.scss",
195                     "theme/classic/style/moodle.css": "theme/classic/scss/classicgrunt.scss"
196                 }
197             },
198             options: {
199                 includePaths: ["theme/boost/scss/", "theme/classic/scss/"]
200             }
201         },
202         watch: {
203             options: {
204                 nospawn: true // We need not to spawn so config can be changed dynamically.
205             },
206             amd: {
207                 files: ['**/amd/src/**/*.js'],
208                 tasks: ['amd']
209             },
210             boost: {
211                 files: ['**/theme/boost/scss/**/*.scss'],
212                 tasks: ['scss']
213             },
214             rawcss: {
215                 files: ['**/*.css', '**/theme/**/!(moodle.css|editor.css)'],
216                 tasks: ['rawcss']
217             },
218             yui: {
219                 files: ['**/yui/src/**/*.js'],
220                 tasks: ['yui']
221             },
222             gherkinlint: {
223                 files: ['**/tests/behat/*.feature'],
224                 tasks: ['gherkinlint']
225             }
226         },
227         shifter: {
228             options: {
229                 recursive: true,
230                 paths: files ? files : [cwd]
231             }
232         },
233         gherkinlint: {
234             options: {
235                 files: files ? files : ['**/tests/behat/*.feature'],
236             }
237         },
238         stylelint: {
239             scss: {
240                 options: {syntax: 'scss'},
241                 src: files ? files : ['*/**/*.scss']
242             },
243             css: {
244                 src: files ? files : ['*/**/*.css'],
245                 options: {
246                     configOverrides: {
247                         rules: {
248                             // These rules have to be disabled in .stylelintrc for scss compat.
249                             "at-rule-no-unknown": true,
250                         }
251                     }
252                 }
253             }
254         }
255     });
257     /**
258      * Generate ignore files (utilising thirdpartylibs.xml data)
259      */
260     tasks.ignorefiles = function() {
261       // An array of paths to third party directories.
262       var thirdPartyPaths = getThirdPartyPathsFromXML();
263       // Generate .eslintignore.
264       var eslintIgnores = ['# Generated by "grunt ignorefiles"', '*/**/yui/src/*/meta/', '*/**/build/'].concat(thirdPartyPaths);
265       grunt.file.write('.eslintignore', eslintIgnores.join('\n'));
266       // Generate .stylelintignore.
267       var stylelintIgnores = [
268           '# Generated by "grunt ignorefiles"',
269           '**/yui/build/*',
270           'theme/boost/style/moodle.css',
271           'theme/classic/style/moodle.css',
272       ].concat(thirdPartyPaths);
273       grunt.file.write('.stylelintignore', stylelintIgnores.join('\n'));
274     };
276     /**
277      * Shifter task. Is configured with a path to a specific file or a directory,
278      * in the case of a specific file it will work out the right module to be built.
279      *
280      * Note that this task runs the invidiaul shifter jobs async (becase it spawns
281      * so be careful to to call done().
282      */
283     tasks.shifter = function() {
284         var done = this.async(),
285             options = grunt.config('shifter.options');
287         // Run the shifter processes one at a time to avoid confusing output.
288         async.eachSeries(options.paths, function(src, filedone) {
289             var args = [];
290             args.push(path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
292             // Always ignore the node_modules directory.
293             args.push('--excludes', 'node_modules');
295             // Determine the most appropriate options to run with based upon the current location.
296             if (grunt.file.isMatch('**/yui/**/*.js', src)) {
297                 // When passed a JS file, build our containing module (this happen with
298                 // watch).
299                 grunt.log.debug('Shifter passed a specific JS file');
300                 src = path.dirname(path.dirname(src));
301                 options.recursive = false;
302             } else if (grunt.file.isMatch('**/yui/src', src)) {
303                 // When in a src directory --walk all modules.
304                 grunt.log.debug('In a src directory');
305                 args.push('--walk');
306                 options.recursive = false;
307             } else if (grunt.file.isMatch('**/yui/src/*', src)) {
308                 // When in module, only build our module.
309                 grunt.log.debug('In a module directory');
310                 options.recursive = false;
311             } else if (grunt.file.isMatch('**/yui/src/*/js', src)) {
312                 // When in module src, only build our module.
313                 grunt.log.debug('In a source directory');
314                 src = path.dirname(src);
315                 options.recursive = false;
316             }
318             if (grunt.option('watch')) {
319                 grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead');
320             }
322             // Add the stderr option if appropriate
323             if (grunt.option('verbose')) {
324                 args.push('--lint-stderr');
325             }
327             if (grunt.option('no-color')) {
328                 args.push('--color=false');
329             }
331             var execShifter = function() {
333                 grunt.log.ok("Running shifter on " + src);
334                 grunt.util.spawn({
335                     cmd: "node",
336                     args: args,
337                     opts: {cwd: src, stdio: 'inherit', env: process.env}
338                 }, function(error, result, code) {
339                     if (code) {
340                         grunt.fail.fatal('Shifter failed with code: ' + code);
341                     } else {
342                         grunt.log.ok('Shifter build complete.');
343                         filedone();
344                     }
345                 });
346             };
348             // Actually run shifter.
349             if (!options.recursive) {
350                 execShifter();
351             } else {
352                 // Check that there are yui modules otherwise shifter ends with exit code 1.
353                 if (grunt.file.expand({cwd: src}, '**/yui/src/**/*.js').length > 0) {
354                     args.push('--recursive');
355                     execShifter();
356                 } else {
357                     grunt.log.ok('No YUI modules to build.');
358                     filedone();
359                 }
360             }
361         }, done);
362     };
364     tasks.gherkinlint = function() {
365         const done = this.async();
366         const options = grunt.config('gherkinlint.options');
368         // Grab the gherkin-lint linter and required scaffolding.
369         const linter = require('gherkin-lint/src/linter.js');
370         const featureFinder = require('gherkin-lint/src/feature-finder.js');
371         const configParser = require('gherkin-lint/src/config-parser.js');
372         const formatter = require('gherkin-lint/src/formatters/stylish.js');
374         // Run the linter.
375         const results = linter.lint(
376             featureFinder.getFeatureFiles(grunt.file.expand(options.files)),
377             configParser.getConfiguration(configParser.defaultConfigFileName)
378         );
380         // Print the results out uncondtionally.
381         formatter.printResults(results);
383         // Report on the results.
384         // The done function takes a bool whereby a falsey statement causes the task to fail.
385         done(results.every(result => result.errors.length === 0));
386     };
388     tasks.startup = function() {
389         // Are we in a YUI directory?
390         if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
391             grunt.task.run('yui');
392         // Are we in an AMD directory?
393         } else if (inAMD) {
394             grunt.task.run('amd');
395         } else {
396             // Run them all!.
397             grunt.task.run('css');
398             grunt.task.run('js');
399             grunt.task.run('gherkinlint');
400         }
401     };
403     /**
404      * This is a wrapper task to handle the grunt watch command. It attempts to use
405      * Watchman to monitor for file changes, if it's installed, because it's much faster.
406      *
407      * If Watchman isn't installed then it falls back to the grunt-contrib-watch file
408      * watcher for backwards compatibility.
409      */
410     tasks.watch = function() {
411         var watchTaskDone = this.async();
412         var watchInitialised = false;
413         var watchTaskQueue = {};
414         var processingQueue = false;
416         // Grab the tasks and files that have been queued up and execute them.
417         var processWatchTaskQueue = function() {
418             if (!Object.keys(watchTaskQueue).length || processingQueue) {
419                 // If there is nothing in the queue or we're already processing then wait.
420                 return;
421             }
423             processingQueue = true;
425             // Grab all tasks currently in the queue.
426             var queueToProcess = watchTaskQueue;
427             // Reset the queue.
428             watchTaskQueue = {};
430             async.forEachSeries(
431                 Object.keys(queueToProcess),
432                 function(task, next) {
433                     var files = queueToProcess[task];
434                     var filesOption = '--files=' + files.join(',');
435                     grunt.log.ok('Running task ' + task + ' for files ' + filesOption);
437                     // Spawn the task in a child process so that it doesn't kill this one
438                     // if it failed.
439                     grunt.util.spawn(
440                         {
441                             // Spawn with the grunt bin.
442                             grunt: true,
443                             // Run from current working dir and inherit stdio from process.
444                             opts: {
445                                 cwd: cwd,
446                                 stdio: 'inherit'
447                             },
448                             args: [task, filesOption]
449                         },
450                         function(err, res, code) {
451                             if (code !== 0) {
452                                 // The grunt task failed.
453                                 grunt.log.error(err);
454                             }
456                             // Move on to the next task.
457                             next();
458                         }
459                     );
460                 },
461                 function() {
462                     // No longer processing.
463                     processingQueue = false;
464                     // Once all of the tasks are done then recurse just in case more tasks
465                     // were queued while we were processing.
466                     processWatchTaskQueue();
467                 }
468             );
469         };
471         var watchConfig = grunt.config.get(['watch']);
472         watchConfig = Object.keys(watchConfig).reduce(function(carry, key) {
473             if (key == 'options') {
474                 return carry;
475             }
477             var value = watchConfig[key];
478             var fileGlobs = value.files;
479             var taskNames = value.tasks;
481             taskNames.forEach(function(taskName) {
482                 carry[taskName] = fileGlobs;
483             });
485             return carry;
486         }, {});
488         watchmanClient.on('error', function(error) {
489             // We have to add an error handler here and parse the error string because the
490             // example way from the docs to check if Watchman is installed doesn't actually work!!
491             // See: https://github.com/facebook/watchman/issues/509
492             if (error.message.match('Watchman was not found')) {
493                 // If watchman isn't installed then we should fallback to the other watch task.
494                 grunt.log.ok('It is recommended that you install Watchman for better performance using the "watch" command.');
496                 // Fallback to the old grunt-contrib-watch task.
497                 grunt.renameTask('watch-grunt', 'watch');
498                 grunt.task.run(['watch']);
499                 // This task is finished.
500                 watchTaskDone(0);
501             } else {
502                 grunt.log.error(error);
503                 // Fatal error.
504                 watchTaskDone(1);
505             }
506         });
508         watchmanClient.on('subscription', function(resp) {
509             if (resp.subscription !== 'grunt-watch') {
510                 return;
511             }
513             resp.files.forEach(function(file) {
514                 grunt.log.ok('File changed: ' + file.name);
516                 var fullPath = cwd + '/' + file.name;
517                 Object.keys(watchConfig).forEach(function(task) {
518                     var fileGlobs = watchConfig[task];
519                     var match = fileGlobs.every(function(fileGlob) {
520                         return grunt.file.isMatch(fileGlob, fullPath);
521                     });
522                     if (match) {
523                         // If we are watching a subdirectory then the file.name will be relative
524                         // to that directory. However the grunt tasks  expect the file paths to be
525                         // relative to the Gruntfile.js location so let's normalise them before
526                         // adding them to the queue.
527                         var relativePath = fullPath.replace(gruntFilePath + '/', '');
528                         if (task in watchTaskQueue) {
529                             if (!watchTaskQueue[task].includes(relativePath)) {
530                                 watchTaskQueue[task] = watchTaskQueue[task].concat(relativePath);
531                             }
532                         } else {
533                             watchTaskQueue[task] = [relativePath];
534                         }
535                     }
536                 });
537             });
539             processWatchTaskQueue();
540         });
542         process.on('SIGINT', function() {
543             // Let the user know that they may need to manually stop the Watchman daemon if they
544             // no longer want it running.
545             if (watchInitialised) {
546                 grunt.log.ok('The Watchman daemon may still be running and may need to be stopped manually.');
547             }
549             process.exit();
550         });
552         // Initiate the watch on the current directory.
553         watchmanClient.command(['watch-project', cwd], function(watchError, watchResponse) {
554             if (watchError) {
555                 grunt.log.error('Error initiating watch:', watchError);
556                 watchTaskDone(1);
557                 return;
558             }
560             if ('warning' in watchResponse) {
561                 grunt.log.error('warning: ', watchResponse.warning);
562             }
564             var watch = watchResponse.watch;
565             var relativePath = watchResponse.relative_path;
566             watchInitialised = true;
568             watchmanClient.command(['clock', watch], function(clockError, clockResponse) {
569                 if (clockError) {
570                     grunt.log.error('Failed to query clock:', clockError);
571                     watchTaskDone(1);
572                     return;
573                 }
575                 // Use the matching patterns specified in the watch config.
576                 var matches = Object.keys(watchConfig).map(function(task) {
577                     var fileGlobs = watchConfig[task];
578                     var fileGlobMatches = fileGlobs.map(function(fileGlob) {
579                         return ['match', fileGlob, 'wholename'];
580                     });
582                     return ['allof'].concat(fileGlobMatches);
583                 });
585                 var sub = {
586                     expression: ["anyof"].concat(matches),
587                     // Which fields we're interested in.
588                     fields: ["name", "size", "type"],
589                     // Add our time constraint.
590                     since: clockResponse.clock
591                 };
593                 if (relativePath) {
594                     /* eslint-disable camelcase */
595                     sub.relative_root = relativePath;
596                 }
598                 watchmanClient.command(['subscribe', watch, 'grunt-watch', sub], function(subscribeError) {
599                     if (subscribeError) {
600                         // Probably an error in the subscription criteria.
601                         grunt.log.error('failed to subscribe: ', subscribeError);
602                         watchTaskDone(1);
603                         return;
604                     }
606                     grunt.log.ok('Listening for changes to files in ' + cwd);
607                 });
608             });
609         });
610     };
612     // On watch, we dynamically modify config to build only affected files. This
613     // method is slightly complicated to deal with multiple changed files at once (copied
614     // from the grunt-contrib-watch readme).
615     var changedFiles = Object.create(null);
616     var onChange = grunt.util._.debounce(function() {
617         var files = Object.keys(changedFiles);
618         grunt.config('eslint.amd.src', files);
619         grunt.config('eslint.yui.src', files);
620         grunt.config('shifter.options.paths', files);
621         grunt.config('gherkinlint.options.files', files);
622         grunt.config('babel.dist.files', [{expand: true, src: files, rename: babelRename}]);
623         changedFiles = Object.create(null);
624     }, 200);
626     grunt.event.on('watch', function(action, filepath) {
627         changedFiles[filepath] = action;
628         onChange();
629     });
631     // Register NPM tasks.
632     grunt.loadNpmTasks('grunt-contrib-uglify');
633     grunt.loadNpmTasks('grunt-contrib-watch');
634     grunt.loadNpmTasks('grunt-sass');
635     grunt.loadNpmTasks('grunt-eslint');
636     grunt.loadNpmTasks('grunt-stylelint');
637     grunt.loadNpmTasks('grunt-babel');
639     // Rename the grunt-contrib-watch "watch" task because we're going to wrap it.
640     grunt.renameTask('watch', 'watch-grunt');
642     // Register JS tasks.
643     grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
644     grunt.registerTask('gherkinlint', 'Run gherkinlint against the current directory', tasks.gherkinlint);
645     grunt.registerTask('ignorefiles', 'Generate ignore files for linters', tasks.ignorefiles);
646     grunt.registerTask('watch', 'Run tasks on file changes', tasks.watch);
647     grunt.registerTask('yui', ['eslint:yui', 'shifter']);
648     grunt.registerTask('amd', ['eslint:amd', 'babel']);
649     grunt.registerTask('js', ['amd', 'yui']);
651     // Register CSS taks.
652     grunt.registerTask('css', ['stylelint:scss', 'sass', 'stylelint:css']);
653     grunt.registerTask('scss', ['stylelint:scss', 'sass']);
654     grunt.registerTask('rawcss', ['stylelint:css']);
656     // Register the startup task.
657     grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
659     // Register the default task.
660     grunt.registerTask('default', ['startup']);