uglify.js
4.3 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* grunt-contrib-uglify
* http://gruntjs.com/
*
* Copyright (c) 2013 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
// Internal lib.
var contrib = require('grunt-lib-contrib').init(grunt);
var uglify = require('./lib/uglify').init(grunt);
grunt.registerMultiTask('uglify', 'Minify files with UglifyJS.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
banner: '',
footer: '',
compress: {
warnings: false
},
mangle: {},
beautify: false,
report: false
});
// Process banner.
var banner = grunt.template.process(options.banner);
var footer = grunt.template.process(options.footer);
var mapNameGenerator, mapInNameGenerator, mappingURLGenerator;
// Iterate over all src-dest file pairs.
this.files.forEach(function(f) {
var src = f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
});
if (src.length === 0) {
grunt.log.warn('Destination (' + f.dest + ') not written because src files were empty.');
return;
}
// function to get the name of the sourceMap
if (typeof options.sourceMap === "function") {
mapNameGenerator = options.sourceMap;
}
// function to get the name of the sourceMap
if (typeof options.sourceMapIn === "function") {
if (src.length !== 1) {
grunt.fail.warn('Cannot generate `sourceMapIn` for multiple source files.');
}
mapInNameGenerator = options.sourceMapIn;
}
// function to get the sourceMappingURL
if (typeof options.sourceMappingURL === "function") {
mappingURLGenerator = options.sourceMappingURL;
}
// dynamically create destination sourcemap name
if (mapNameGenerator) {
try {
options.sourceMap = mapNameGenerator(f.dest);
} catch (e) {
var err = new Error('SourceMapName failed.');
err.origError = e;
grunt.fail.warn(err);
}
}
// dynamically create incoming sourcemap names
if (mapInNameGenerator) {
try {
options.sourceMapIn = mapInNameGenerator(src[0]);
} catch (e) {
var err = new Error('SourceMapInName failed.');
err.origError = e;
grunt.fail.warn(err);
}
}
// dynamically create sourceMappingURL
if (mappingURLGenerator) {
try {
options.sourceMappingURL = mappingURLGenerator(f.dest);
} catch (e) {
var err = new Error('SourceMappingURL failed.');
err.origError = e;
grunt.fail.warn(err);
}
}
// Minify files, warn and fail on error.
var result;
try {
result = uglify.minify(src, f.dest, options);
} catch (e) {
console.log(e);
var err = new Error('Uglification failed.');
if (e.message) {
err.message += '\n' + e.message + '. \n';
if (e.line) {
err.message += 'Line ' + e.line + ' in ' + src + '\n';
}
}
err.origError = e;
grunt.log.warn('Uglifying source "' + src + '" failed.');
grunt.fail.warn(err);
}
// Concat minified source + footer
var output = result.min + footer;
// Only prepend banner if uglify hasn't taken care of it as part of the preamble
if (!options.sourceMap) {
output = banner + output;
}
// Write the destination file.
grunt.file.write(f.dest, output);
// Write source map
if (options.sourceMap) {
grunt.file.write(options.sourceMap, result.sourceMap);
grunt.log.writeln('Source Map "' + options.sourceMap + '" created.');
}
// Print a success message.
grunt.log.writeln('File "' + f.dest + '" created.');
// ...and report some size information.
if (options.report) {
contrib.minMaxInfo(output, result.max, options.report);
}
});
});
};