Skip to content

Commit 7dc5e1d

Browse files
XhmikosRvladikoff
authored andcommitted
Log fixes. (#454) r=vladikoff
Now the output is similar to grunt-contrib-cssmin.
1 parent 48fab36 commit 7dc5e1d

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

docs/uglify-options.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ Default: `false`
3131
Parse a single expression, rather than a program (for parsing JSON)
3232

3333
## report
34-
Choices: `'none'`, `'min'`, `'gzip'`
34+
Type: `string`
35+
Choices: `'min'`, `'gzip'`
3536
Default: `'min'`
3637

37-
Either report only minification result or report minification and gzip results.
38-
This is useful to see exactly how well UglifyJS is performing but using `'gzip'` will make the task take 5-10x longer to complete. [Example output](https://github.com/sindresorhus/maxmin#readme).
39-
If `'none'` is used the report will be generated on the verbose output.
38+
Report minification result or both minification and gzip results.
39+
This is useful to see exactly how well uglify-js is performing but using `'gzip'` will make the task take 5-10x longer to complete. [Example output](https://github.com/sindresorhus/maxmin#readme).
4040

4141
## sourceMap
4242
Type: `Boolean`

tasks/uglify.js

+40-39
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ function relativePath(file1, file2) {
2424
return '';
2525
}
2626

27-
function reportFacility(grunt, options) {
28-
var reporter;
29-
switch (options.report) {
30-
case 'none':
31-
reporter = grunt.verbose;
32-
break;
33-
default:
34-
case 'min':
35-
case 'gzip':
36-
reporter = grunt.log;
37-
}
38-
return reporter;
39-
}
40-
4127
// Converts \r\n to \n
4228
function normalizeLf(string) {
4329
return string.replace(/\r\n/g, '\n');
@@ -47,6 +33,16 @@ module.exports = function(grunt) {
4733
// Internal lib.
4834
var uglify = require('./lib/uglify').init(grunt);
4935

36+
var getAvailableFiles = function (filesArray) {
37+
return filesArray.filter(function (filepath) {
38+
if (!grunt.file.exists(filepath)) {
39+
grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found');
40+
return false;
41+
}
42+
return true;
43+
});
44+
};
45+
5046
grunt.registerMultiTask('uglify', 'Minify files with UglifyJS.', function() {
5147
// Merge task-specific and/or target-specific options with these defaults.
5248
var options = this.options({
@@ -64,25 +60,23 @@ module.exports = function(grunt) {
6460
screwIE8: true,
6561
quoteStyle: 0
6662
});
67-
var log = reportFacility(grunt, options);
6863

6964
var footer = normalizeLf(options.footer);
7065
var mapNameGenerator, mapInNameGenerator;
71-
var createdFiles = 0;
72-
var createdMaps = 0;
66+
var created = {
67+
maps: 0,
68+
files: 0
69+
};
70+
var size = {
71+
before: 0,
72+
after: 0
73+
};
7374

7475
// Iterate over all src-dest file pairs.
7576
this.files.forEach(function (f) {
76-
var src = f.src.filter(function (filepath) {
77-
// Warn on and remove invalid source files (if nonull was set).
78-
if (!grunt.file.exists(filepath)) {
79-
grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found.');
80-
return false;
81-
}
82-
return true;
83-
});
77+
var availableFiles = getAvailableFiles(f.src);
8478

85-
if (src.length === 0) {
79+
if (availableFiles.length === 0) {
8680
grunt.log.warn('Destination ' + chalk.cyan(f.dest) + ' not written because src files were empty.');
8781
return;
8882
}
@@ -101,7 +95,7 @@ module.exports = function(grunt) {
10195

10296
// function to get the name of the sourceMapIn file
10397
if (typeof options.sourceMapIn === 'function') {
104-
if (src.length !== 1) {
98+
if (availableFiles.length !== 1) {
10599
grunt.fail.warn('Cannot generate `sourceMapIn` for multiple source files.');
106100
}
107101
mapInNameGenerator = options.sourceMapIn;
@@ -126,7 +120,7 @@ module.exports = function(grunt) {
126120
// Dynamically create incoming sourcemap names
127121
if (mapInNameGenerator) {
128122
try {
129-
options.sourceMapIn = mapInNameGenerator(src[0]);
123+
options.sourceMapIn = mapInNameGenerator(availableFiles[0]);
130124
} catch (e) {
131125
err = new Error('SourceMapInName failed.');
132126
err.origError = e;
@@ -151,46 +145,53 @@ module.exports = function(grunt) {
151145
// Minify files, warn and fail on error.
152146
var result;
153147
try {
154-
result = uglify.minify(src, f.dest, options);
148+
result = uglify.minify(availableFiles, f.dest, options);
155149
} catch (e) {
156150
console.log(e);
157151
err = new Error('Uglification failed.');
158152
if (e.message) {
159153
err.message += '\n' + e.message + '. \n';
160154
if (e.line) {
161-
err.message += 'Line ' + e.line + ' in ' + src + '\n';
155+
err.message += 'Line ' + e.line + ' in ' + availableFiles + '\n';
162156
}
163157
}
164158
err.origError = e;
165-
grunt.log.warn('Uglifying source ' + chalk.cyan(src) + ' failed.');
159+
grunt.log.warn('Uglifying source ' + chalk.cyan(availableFiles) + ' failed.');
166160
grunt.fail.warn(err);
167161
}
168162

169163
// Concat minified source + footer
170164
var output = result.min + footer;
171165

166+
var unCompiledJSString = availableFiles.map(function (file) {
167+
return grunt.file.read(file);
168+
}).join('');
169+
172170
// Write the destination file.
173171
grunt.file.write(f.dest, output);
174172

173+
size.before += unCompiledJSString.length;
174+
175175
// Write source map
176176
if (options.sourceMap) {
177177
grunt.file.write(options.generatedSourceMapName, result.sourceMap);
178-
log.writeln('File ' + chalk.cyan(options.generatedSourceMapName) + ' created (source map).');
179-
createdMaps++;
178+
grunt.verbose.writeln('File ' + chalk.cyan(options.generatedSourceMapName) + ' created (source map).');
179+
created.maps++;
180180
}
181181

182182
var outputSize = maxmin(result.max, output, options.report === 'gzip');
183-
log.writeln('File ' + chalk.cyan(f.dest) + ' created: ' + outputSize);
183+
grunt.verbose.writeln('File ' + chalk.cyan(f.dest) + ' created: ' + chalk.dim(outputSize));
184184

185-
createdFiles++;
185+
created.files++;
186+
size.after += output.length;
186187
});
187188

188-
if (createdMaps > 0) {
189-
grunt.log.ok(createdMaps + ' source' + grunt.util.pluralize(createdMaps, 'map/maps') + ' created.');
189+
if (created.maps > 0) {
190+
grunt.log.ok(created.maps + ' source' + grunt.util.pluralize(created.maps, 'map/maps') + ' created.');
190191
}
191192

192-
if (createdFiles > 0) {
193-
grunt.log.ok(createdFiles + ' ' + grunt.util.pluralize(this.files.length, 'file/files') + ' created.');
193+
if (created.files > 0) {
194+
grunt.log.ok(created.files + ' ' + grunt.util.pluralize(this.files.length, 'file/files') + ' created ' + chalk.dim(maxmin(size.before, size.after)));
194195
} else {
195196
grunt.log.warn('No files created.');
196197
}

0 commit comments

Comments
 (0)