Skip to content

Instantly share code, notes, and snippets.

@kaustubhmalgaonkar
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaustubhmalgaonkar/af532f6508bd178e80d9 to your computer and use it in GitHub Desktop.
Save kaustubhmalgaonkar/af532f6508bd178e80d9 to your computer and use it in GitHub Desktop.
Grunt File to minify and compress js file into one folder wise. Compile less file to css file and minify css file.
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-cssmin');
var devPath = 'public/js/dev';
var prodPath = 'public/js/prod';
var cssPath = 'public/css';
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
my_target: {
files: [{
expand: true,
cwd: devPath,
src: '**/*.js',
dest: prodPath,
ext: '.min.js'
}]
}
},
less:{
development:{
options:{
cleancss:true
},
files: [{
expand: true,
cwd: cssPath,
src: '*.less',
dest: cssPath,
ext: '.css'
}]
}
},
cssmin: {
add_banner: {
options: {
banner: '/* My minified css file */'
},
files: {
'public/css/styles.min.css': [cssPath+'/*.css','!'+cssPath+'/*.min.css']
}
}
},
clean: {
before: [prodPath + '/*'],
after: [prodPath + '/*','!'+prodPath+'/*.min.js']
},
watch: {
scripts: {
files: [devPath + '/**/*.js',cssPath+'/*.less'],
tasks: ['less','cssmin','clean:before','uglify','customConcat','clean:after']
}
}
});
grunt.registerTask('customConcat', 'prepare all files', function() {
grunt.file.expand(prodPath + '/*').forEach(function(dir) {
var folderArr = dir.split("/");
var fileName = folderArr[folderArr.length - 1];
grunt.log.writeln('fileName: ' + fileName);
// get the current concat config
var concat = grunt.config.get('concat') || {};
concat[dir] = {
src: dir + '/*.min.js',
dest: prodPath + '/' + fileName + '.min.js'
};
// save the new concat config
grunt.config.set('concat', concat);
});
//grunt.task.run('concat');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment