From: Tim Wood Date: Fri, 31 Aug 2012 16:39:19 +0000 (-0700) Subject: Adding minify + comments X-Git-Tag: 2.0.0~77^2^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1170675f0ba47a2db5c0e604d628324562628d99;p=thirdparty%2Fmoment.git Adding minify + comments --- diff --git a/grunt.js b/grunt.js index 8a737b50c..443455584 100644 --- a/grunt.js +++ b/grunt.js @@ -1,4 +1,5 @@ -var fs = require('fs'); +var fs = require('fs'), + uglifyjs = require('uglify-js'); module.exports = function(grunt) { @@ -6,6 +7,10 @@ module.exports = function(grunt) { langs: { src: ['min/lang-all.js'], dest: 'min/lang-all.min.js' + }, + moment: { + src: ['moment.js'], + dest: 'min/moment.min.js' } }; @@ -24,7 +29,14 @@ module.exports = function(grunt) { dest: 'min/lang-all.js' } }, - min : min, + mincomment : min, + uglify: { + mangle: {toplevel: true}, + squeeze: {dead_code: false}, + codegen: { + ascii_only: true + } + }, test : { files : ["test/**/*.js"] }, @@ -34,6 +46,34 @@ module.exports = function(grunt) { 'lang/**/*.js' ] }, + jshint: { + options: { + "node" : true, + "es5" : true, + "browser" : true, + "boss" : false, + "curly" : true, + "debug" : false, + "devel" : false, + "eqeqeq" : true, + "eqnull" : true, + "evil" : false, + "forin" : false, + "immed" : false, + "laxbreak" : false, + "newcap" : true, + "noarg" : true, + "noempty" : false, + "nonew" : false, + "onevar" : true, + "plusplus" : false, + "regexp" : false, + "undef" : true, + "sub" : true, + "strict" : false, + "white" : true + } + }, watch : { test : { files : [ @@ -53,5 +93,45 @@ module.exports = function(grunt) { // Default task. grunt.registerTask('default', 'test'); - grunt.registerTask('release', 'lint test concat '); -}; \ No newline at end of file + grunt.registerTask('release', 'test concat mincomment'); + + function show_copyright(comments) { + var ret = "", i, c; + for (i = 0; i < comments.length; ++i) { + c = comments[i]; + if (c.type === "comment1") { + ret += "//" + c.value + "\n"; + } else { + ret += "/*" + c.value + "*/"; + } + } + return ret; + } + + grunt.registerMultiTask('mincomment', 'Minify files with UglifyJS. (with comments)', function () { + var files = grunt.file.expandFiles(this.file.src), + max, min, + tok; + + // Concat specified files. This should really be a single, pre-built (and + // linted) file, but it supports any number of files. + max = grunt.helper('concat', files, {separator: this.data.separator}); + + // Add the first comments + tok = uglifyjs.parser.tokenizer(max); + min = show_copyright(tok().comments_before); + + // Add the minified source. + min += grunt.helper('uglify', max, grunt.config('uglify')); + grunt.file.write(this.file.dest, min); + + // Fail task if errors were logged. + if (this.errorCount) { return false; } + + // Otherwise, print a success message.... + grunt.log.writeln('File "' + this.file.dest + '" created.'); + + // ...and report some size information. + grunt.helper('min_max_info', min, max); + }); +};