forked from moment/moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify-lang.js
82 lines (67 loc) · 2.42 KB
/
minify-lang.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*jshint onevar:false*/
var fs = require('fs'),
uglifyjs = require('uglify-js');
module.exports = function (grunt) {
var helpers = require('grunt-lib-legacyhelpers').init(grunt);
var START = [
"(function(){",
" function onload (moment) {",
""
].join('\n');
var END = [
"",
" }",
" if (typeof define === \"function\" && define.amd) {",
" define([\"moment\"], onload);",
" }",
" if (typeof window !== \"undefined\" && window.moment) {",
" onload(window.moment);",
" }",
"})()",
""
].join('\n');
// UglifyJS does not support keeping the first line comments unless using the CLI.
// This multi-task ensures that the first comments are kept.
grunt.registerMultiTask('minlang', 'Minify lang files with UglifyJS.', function () {
var files = grunt.file.expand(this.data.src),
min,
code,
comments,
tok,
result;
// Concat specified files. This should really be a single, pre-built (and
// linted) file, but it supports any number of files.
code = helpers.concat(files, {separator: this.data.separator});
// Add the first comments
//tok = uglifyjs.parse(code);
tok = uglifyjs.parse(code);
min = showCopyright(tok.start.comments_before);
// Add the minified source.
result = uglifyjs.minify(wrapFile(code), grunt.config('uglify.options'));
min += result.code;
grunt.file.write(this.data.dest, min);
// Fail task if errors were logged.
if (this.errorCount) { return false; }
// Otherwise, print a success message....
grunt.log.writeln('File "' + this.data.dest + '" created.');
// ...and report some size information.
helpers.min_max_info(min, code);
});
// Helper for the 'mincomment' multitask
function showCopyright(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;
}
function wrapFile(code) {
code = code.replace(/require\([\'\"]\.\.\/moment[\'\"]\)/g, "moment");
return START + code + END;
}
};