MDL-49817 grunt: Refactor the uglify task
authorDan Poltawski <dan@moodle.com>
Thu, 14 Jan 2016 12:10:02 +0000 (12:10 +0000)
committerDan Poltawski <dan@moodle.com>
Thu, 28 Jan 2016 08:27:30 +0000 (08:27 +0000)
This is in preperation for the watch task and doing a few things:

1) Switch away from 'grunt.file.expandMapping' to the declarative syntax
   for doing the same thing, as recommended in docs:
   http://gruntjs.com/api/grunt.file#grunt.file.expandmapping

2) Factor the renaming function into shared function so it can be used
   from the watch task too.

3) Shared the file glob'ing pattern for match AMD js files between
   jshint and uglify task (this removes the node_modules exception..
   we should add it to both if we need it).

4) Rename the task configuration - I am just doing this because it looks
   like 'dynamic_mappings' is some how magic config option when you
   look at the manual:
   http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically

   But in fact, it's just a task confguration name.

Gruntfile.js

index 904ef3a..2f5a5cc 100644 (file)
@@ -29,29 +29,39 @@ module.exports = function(grunt) {
         cwd = process.env.PWD || process.cwd(),
         inAMD = path.basename(cwd) == 'amd';
 
+    // Globbing pattern for matching all AMD JS source files.
+    var amdSrc = [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js'];
+
+    /**
+     * Function to generate the destination for the uglify task
+     * (e.g. build/file.min.js). This function will be passed to
+     * the rename property of files array when building dynamically:
+     * http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
+     *
+     * @param {String} destPath the current destination
+     * @param {String} srcPath the  matched src path
+     * @return {String} The rewritten destination path.
+     */
+    var uglify_rename = function (destPath, srcPath) {
+        destPath = srcPath.replace('src', 'build');
+        destPath = destPath.replace('.js', '.min.js');
+        destPath = path.resolve(cwd, destPath);
+        return destPath;
+    };
+
     // Project configuration.
     grunt.initConfig({
         jshint: {
             options: {jshintrc: '.jshintrc'},
-            amd: {
-                src: [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js']
-            }
+            amd: { src: amdSrc }
         },
         uglify: {
-            dynamic_mappings: {
-                files: grunt.file.expandMapping(
-                    ['**/src/*.js', '!**/node_modules/**'],
-                    '',
-                    {
-                        cwd: cwd,
-                        rename: function(destBase, destPath) {
-                            destPath = destPath.replace('src', 'build');
-                            destPath = destPath.replace('.js', '.min.js');
-                            destPath = path.resolve(cwd, destPath);
-                            return destPath;
-                        }
-                    }
-                )
+            amd: {
+                files: [{
+                    expand: true,
+                    src: amdSrc,
+                    rename: uglify_rename
+                }]
             }
         },
         less: {