Skip to content

Commit

Permalink
MDL-49817 grunt: Refactor the uglify task
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danpoltawski committed Jan 28, 2016
1 parent dbf6b40 commit 5cc5f31
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 5cc5f31

Please sign in to comment.