]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Adding minify + comments
authorTim Wood <washwithcare@gmail.com>
Fri, 31 Aug 2012 16:39:19 +0000 (09:39 -0700)
committerTim Wood <washwithcare@gmail.com>
Fri, 31 Aug 2012 16:39:19 +0000 (09:39 -0700)
grunt.js

index 8a737b50c9c07831499d4dfe7f9fff472ca88cfa..44345558448d14fcb72434b85f19bf8e96f4ee2f 100644 (file)
--- 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);
+    });
+};