Last active
February 6, 2017 15:12
-
-
Save clintconklin/7393526 to your computer and use it in GitHub Desktop.
Optimizing multiple files with grunt via grunt-contrib-requirejs: http://blog.clintconklin.com/optimizing-multiple-javascript-files-with-grunt-and-requirejs/
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 matches = grunt.file.expand('scripts/template-scripts/**/index.js'); | |
var requirejsOptions = {}; | |
if (matches.length > 0) { | |
for (var x = 0; x < matches.length; x++) { | |
var path = matches[x].replace(/\/index\.js/, ''); | |
requirejsOptions['task' + x] = { | |
"options": { | |
"baseUrl": "./", | |
"wrap": true, | |
"name": path + "/index", | |
"out": path + "/index.min.js", | |
"optimize": "uglify2", | |
"uglify2": { | |
"mangle": false | |
}, | |
"generateSourceMaps": true, | |
"preserveLicenseComments": false, | |
"done": function(done, output) { | |
done(); | |
} | |
} | |
}; | |
} | |
} | |
grunt.initConfig({ | |
pkg: grunt.file.readJSON('package.json'), | |
requirejs: requirejsOptions | |
}); | |
grunt.loadNpmTasks('grunt-contrib-requirejs'); | |
grunt.registerTask('default', ['requirejs']); | |
}; |
I'd use the reduce
instead of for
loop and drop the unnecessary if
statement:
module.exports = function (grunt) {
var matches = grunt.file.expand('scripts/template-scripts/**/index.js');
var requireJsOptions = matches.reduce(function (acc, match, index) {
var path = match.replace(/\/index\.js$/, '');
acc['task' + index] = {
options: {
baseUrl: './',
wrap: true,
name: path + '/index',
out: path + '/index.min.js',
optimize: 'uglify2',
uglify2: {
mangle: false
},
generateSourceMaps: true,
preserveLicenseComments: false,
done: function (done, output) {
done();
}
}
};
}, {});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
requirejs: requirejsOptions
});
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.registerTask('default', [ 'requirejs' ]);
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for your tutorial. I have used the grunt as you have mentioned above, i am able to minify the files yet they are not being combined. I am still able to see
After grunt:-> !function(){!function(){define("Module C",["Module A","Module B"]....
Before grunt-> define('Module C', ['Module A', 'Module B'], function(A, B) {... etc
These module are not getting merged in the current file. Can you please tell me if i am doing anything wrong? The Grunt.js code is below
var matches = grunt.file.expand('static/js/pages/*/.js');
var requirejsOptions = {};
if (matches.length > 0) {
for (var x = 0; x < matches.length; x++) {
//some-code-to-get-filename
requirejsOptions['task' + x] = {
"options": {
findNestedDependencies: true,
mainConfigFile: 'static/js/config.js',
"baseUrl": "./",
"wrap": true,
"name": 'path/with/file/name',
"out": 'path/with/file/name'+'.min.js',
"optimize": "uglify2",
"uglify2": {
"mangle": false
},
"done": function(done, output) {
done();
}
}
};
}
}