We have do something smarter and automate with grunt after JS optimization. Let's install "grunt-contrib-watch" that run predefined tasks whenever watched file patterns are added, changed or deleted.
How to install ?
1. Run "npm install grunt-contrib-watch --save-dev" in ur project location.
2. Add "grunt.loadNpmTasks('grunt-contrib-watch');" in Gruntfile.js loated in ur project location.
Gruntfile.js - The sample use Closure complier to optimize JS file. »see more
module.exports = function(grunt) {
grunt.initConfig({
file: {
min: {
dest: 'js/opt/min.js',
src: [
'js/jquery.tools/overlay/overlay.js',
'js/jquery.tools/scrollable/scrollable.js',
]
}
},
'closure-compiler': {
min: {
closurePath:'/Library/closure/',
js: ['<%= file.min.src %>'
],
jsOutputFile: '<%= file.min.dest %>',
noreport: true,
options: {
compilation_level: 'SIMPLE_OPTIMIZATIONS',
//compilation_level: 'ADVANCED_OPTIMIZATIONS',
//compilation_level: 'WHITESPACE_ONLY',
warning_level:"DEFAULT"
}
}
},
watch: {
min: {
files: ['<%=file.min.src%>'],
tasks: ['closure-compiler:min'],
options: {
spawn: false,
},
}
}
});
grunt.loadNpmTasks('grunt-closure-compiler');
grunt.loadNpmTasks('grunt-contrib-watch');
};
留言
張貼留言