Last active
December 10, 2015 15:39
-
-
Save cowboy/4456153 to your computer and use it in GitHub Desktop.
grunt: why you might want to use file src-dest mapping expansions
This file contains hidden or 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) { | |
// Project configuration. | |
grunt.initConfig({ | |
minify: { | |
nofiles: {}, | |
manual: { | |
files: [ | |
// Why specify all src-dest mappings manually... | |
{src: 'lib/grunt.js', dest: 'build/grunt.min.js'}, | |
{src: 'lib/grunt/cli.js', dest: 'build/grunt/cli.min.js'}, | |
{src: 'lib/grunt/config.js', dest: 'build/grunt/config.min.js'}, | |
{src: 'lib/grunt/event.js', dest: 'build/grunt/event.min.js'}, | |
{src: 'lib/grunt/fail.js', dest: 'build/grunt/fail.min.js'}, | |
{src: 'lib/grunt/file.js', dest: 'build/grunt/file.min.js'}, | |
{src: 'lib/grunt/help.js', dest: 'build/grunt/help.min.js'}, | |
{src: 'lib/grunt/log.js', dest: 'build/grunt/log.min.js'}, | |
{src: 'lib/grunt/option.js', dest: 'build/grunt/option.min.js'}, | |
{src: 'lib/grunt/task.js', dest: 'build/grunt/task.min.js'}, | |
{src: 'lib/grunt/template.js', dest: 'build/grunt/template.min.js'}, | |
{src: 'lib/grunt/util.js', dest: 'build/grunt/util.min.js'}, | |
] | |
}, | |
automatic: { | |
files: [ | |
// When you can have grunt create them for you dynamically? | |
{ | |
expand: true, // Enable dynamic expansion. | |
cwd: 'lib/', // Matches are relative to this path. | |
src: ['**/*.js', '!util/**'], // Actual pattern(s) to match. | |
dest: 'build/', // Destination path prefix. | |
ext: '.min.js', // You can change the extension, too. | |
} | |
] | |
}, | |
}, | |
}); | |
grunt.registerMultiTask('minify', function() { | |
// Warn on and remove invalid source files (if nonull was set). | |
var files = this.files.filter(function(f) { | |
if (!grunt.file.exists(f.src)) { | |
grunt.log.warn('Source file "' + f.src + '" not found.'); | |
return false; | |
} else { | |
return true; | |
} | |
}); | |
// Minify all files. | |
files.forEach(function(f) { | |
grunt.log.writeln('Minify ' + f.src + ' -> ' + f.dest); | |
// actually minify here, etc | |
}); | |
// Report when done. | |
grunt.log.writeln('Minified ' + files.length + ' files.'); | |
}); | |
grunt.registerTask('default', ['minify']); | |
}; |
This file contains hidden or 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
$ grunt | |
Running "minify:nofiles" (minify) task | |
Minified 0 files. | |
Running "minify:manual" (minify) task | |
Minify lib/grunt.js -> build/grunt.min.js | |
Minify lib/grunt/cli.js -> build/grunt/cli.min.js | |
Minify lib/grunt/config.js -> build/grunt/config.min.js | |
Minify lib/grunt/event.js -> build/grunt/event.min.js | |
Minify lib/grunt/fail.js -> build/grunt/fail.min.js | |
Minify lib/grunt/file.js -> build/grunt/file.min.js | |
Minify lib/grunt/help.js -> build/grunt/help.min.js | |
Minify lib/grunt/log.js -> build/grunt/log.min.js | |
Minify lib/grunt/option.js -> build/grunt/option.min.js | |
Minify lib/grunt/task.js -> build/grunt/task.min.js | |
Minify lib/grunt/template.js -> build/grunt/template.min.js | |
Minify lib/grunt/util.js -> build/grunt/util.min.js | |
Minified 12 files. | |
Running "minify:automatic" (minify) task | |
Minify lib/grunt.js -> build/grunt.min.js | |
Minify lib/grunt/cli.js -> build/grunt/cli.min.js | |
Minify lib/grunt/config.js -> build/grunt/config.min.js | |
Minify lib/grunt/event.js -> build/grunt/event.min.js | |
Minify lib/grunt/fail.js -> build/grunt/fail.min.js | |
Minify lib/grunt/file.js -> build/grunt/file.min.js | |
Minify lib/grunt/help.js -> build/grunt/help.min.js | |
Minify lib/grunt/log.js -> build/grunt/log.min.js | |
Minify lib/grunt/option.js -> build/grunt/option.min.js | |
Minify lib/grunt/task.js -> build/grunt/task.min.js | |
Minify lib/grunt/template.js -> build/grunt/template.min.js | |
Minify lib/grunt/util.js -> build/grunt/util.min.js | |
Minified 12 files. | |
Done, without errors. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment