123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
-
-
- 'use strict';
-
-
- var path = require('path');
- var fs = require('fs');
- var UglifyJS = require('uglify-js');
- var _ = require('lodash');
- var uriPath = require('uri-path');
-
- exports.init = function(grunt) {
- var exports = {};
-
-
-
-
- exports.minify = function(files, dest, options) {
- options = options || {};
-
- grunt.verbose.write('Minifying with UglifyJS...');
-
- var topLevel = null;
- var totalCode = '';
- var sourcesContent = {};
-
- var outputOptions = getOutputOptions(options, dest);
- var output = UglifyJS.OutputStream(outputOptions);
-
-
- files.forEach(function(file){
-
- var code = grunt.file.read(file);
- totalCode += code;
-
-
- var basename = path.basename(file);
- var fileDir = path.dirname(file);
- var sourceMapDir = path.dirname(options.generatedSourceMapName);
- var relativePath = path.relative(sourceMapDir, fileDir);
- var pathPrefix = relativePath ? (relativePath+path.sep) : '';
-
-
- file = uriPath(pathPrefix + basename);
-
- sourcesContent[file] = code;
- topLevel = UglifyJS.parse(code, {
- filename: file,
- toplevel: topLevel,
- expression: options.expression
- });
- });
-
-
- if (options.wrap) {
- topLevel = topLevel.wrap_commonjs(options.wrap, options.exportAll);
- }
-
-
- if (options.enclose) {
- var argParamList = _.map(options.enclose, function(val, key) {
- return key + ':' + val;
- });
-
- topLevel = topLevel.wrap_enclose(argParamList);
- }
-
-
-
- if (options.expression === false) {
- topLevel.figure_out_scope();
- }
-
- if (options.compress !== false) {
- if (options.compress.warnings !== true) {
- options.compress.warnings = false;
- }
- var compressor = UglifyJS.Compressor(options.compress);
- topLevel = topLevel.transform(compressor);
-
-
- topLevel.figure_out_scope();
- }
-
- if (options.mangle !== false) {
-
-
-
-
-
-
-
-
- topLevel.mangle_names(options.mangle);
- }
-
- if (options.sourceMap && options.sourceMapIncludeSources) {
- for (var file in sourcesContent) {
- if (sourcesContent.hasOwnProperty(file)) {
- outputOptions.source_map.get().setSourceContent(file, sourcesContent[file]);
- }
- }
- }
-
-
- topLevel.print(output);
-
- var min = output.get();
-
-
- if (options.sourceMap) {
-
- min += "\n//# sourceMappingURL=" + uriPath(options.destToSourceMap);
- }
-
- var result = {
- max: totalCode,
- min: min,
- sourceMap: outputOptions.source_map
- };
-
- grunt.verbose.ok();
-
- return result;
- };
-
- var getOutputOptions = function(options, dest) {
- var outputOptions = {
- beautify: false,
- source_map: null
- };
-
- if (options.preserveComments) {
- if (options.preserveComments === 'all' || options.preserveComments === true) {
-
-
- outputOptions.comments = true;
- } else if (options.preserveComments === 'some') {
-
- outputOptions.comments = /^!|@preserve|@license|@cc_on/i;
- } else if (_.isFunction(options.preserveComments)) {
-
-
- outputOptions.comments = options.preserveComments;
- }
- }
-
- if (options.banner && options.sourceMap) {
- outputOptions.preamble = options.banner;
- }
-
- if (options.beautify) {
- if (_.isObject(options.beautify)) {
-
-
- _.assign(outputOptions, options.beautify);
- } else {
- outputOptions.beautify = true;
- }
- }
-
-
- if (options.sourceMap) {
-
- var destBasename = path.basename(dest);
- var destPath = path.dirname(dest);
- var sourceMapIn;
- if (options.sourceMapIn) {
- sourceMapIn = grunt.file.readJSON(options.sourceMapIn);
- }
- outputOptions.source_map = UglifyJS.SourceMap({
- file: destBasename,
- orig: sourceMapIn
- });
- if (options.sourceMapIncludeSources && sourceMapIn && sourceMapIn.sourcesContent) {
- sourceMapIn.sourcesContent.forEach(function(content, idx) {
- outputOptions.source_map.get().setSourceContent(sourceMapIn.sources[idx], content);
- });
- }
-
- }
-
- if (options.indentLevel !== undefined) {
- outputOptions.indent_level = options.indentLevel;
- }
-
- if (options.maxLineLen !== undefined) {
- outputOptions.max_line_len = options.maxLineLen;
- }
-
- if (options.ASCIIOnly !== undefined) {
- outputOptions.ascii_only = options.ASCIIOnly;
- }
-
- return outputOptions;
- };
-
- return exports;
- };
|