Created
March 9, 2014 21:11
-
-
Save hilios/9454624 to your computer and use it in GitHub Desktop.
Grunt builmap task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
var globalOptions = { | |
ext: '.map.json', | |
resultMap: { | |
javascripts: ['js', 'coffee'], | |
stylesheets: ['css', 'less'] | |
}, | |
relativePath: true | |
}; | |
grunt.registerTask('buildmap', 'Search for build maps and set on config', | |
function() { | |
var path = require('path'), | |
options = grunt.config.get('buildmap'), | |
dest = options.hasOwnProperty('dest') ? options.dest : '', | |
maps = options.hasOwnProperty('files') ? options.files : [], | |
files = [], | |
javascripts = {}, | |
stylesheets = {}; | |
// Cast objects to array | |
if (grunt.util.kindOf(maps) != 'array') { | |
maps = [maps]; | |
} | |
// Expand all maps | |
grunt.util.recurse(maps, function(map) { | |
map = path.join(map, '**/*' + globalOptions.ext) | |
files.push(grunt.file.expand(map)); | |
}); | |
// Read each map and set on the right place | |
grunt.util.recurse(files, function(file) { | |
var fileName = path.basename(file.replace(/\.map\.json$/gi, '')), | |
fileExt = path.extname(fileName), | |
dirName = path.dirname(file), | |
isCss = file.match(/\.(css|less)\.map\.json$/), | |
isJs = file.match(/\.js\.map\.json$/), | |
jsonMap = grunt.file.readJSON(file), | |
result = {}; | |
if (isCss) { | |
result = stylesheets; | |
} | |
if (isJs) { | |
result = javascripts; | |
} | |
if (jsonMap.hasOwnProperty('files')) { | |
result[path.join(dest, fileName)] = jsonMap.files; | |
} else { | |
grunt.fail.warn('Missing `files` property at `%s`'.replace('%s', | |
path.basename(file))); | |
return; | |
} | |
}); | |
// Set buildmap on config | |
grunt.config.set('buildmap.result.javascripts', javascripts); | |
grunt.config.set('buildmap.result.stylesheets', stylesheets); | |
// Log | |
grunt.log.oklns('Build map completed!') | |
} | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
grunt.initConfig({ | |
pkg: grunt.file.readJSON('package.json'), | |
buildmap: { | |
files: ['<%= source.root %>'], | |
dest: '<%= source.dest %>' | |
} | |
}); | |
grunt.registerTask('default', ['buildmap'], function() { | |
// Buildmap will set two variables into config | |
grunt.config.get('buildmap.result.javascripts'); | |
grunt.config.get('buildmap.result.stylesheets'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment