UNPKG

5.01 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _uglifyEs = require('uglify-es');
8
9var _uglifyEs2 = _interopRequireDefault(_uglifyEs);
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13var buildUglifyOptions = function buildUglifyOptions() {
14 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
15 ecma = _ref.ecma,
16 warnings = _ref.warnings,
17 _ref$parse = _ref.parse,
18 parse = _ref$parse === undefined ? {} : _ref$parse,
19 _ref$compress = _ref.compress,
20 compress = _ref$compress === undefined ? {} : _ref$compress,
21 mangle = _ref.mangle,
22 output = _ref.output,
23 toplevel = _ref.toplevel,
24 nameCache = _ref.nameCache,
25 ie8 = _ref.ie8,
26 keep_classnames = _ref.keep_classnames,
27 keep_fnames = _ref.keep_fnames,
28 safari10 = _ref.safari10;
29
30 return {
31 ecma,
32 warnings,
33 parse,
34 compress,
35 mangle: mangle == null ? true : mangle,
36 output: Object.assign({
37 shebang: true,
38 comments: false,
39 beautify: false,
40 semicolons: true
41 }, output),
42 // Ignoring sourcemap from options
43 sourceMap: null,
44 toplevel,
45 nameCache,
46 ie8,
47 keep_classnames,
48 keep_fnames,
49 safari10
50 };
51}; /* eslint-disable
52 arrow-body-style
53 */
54
55
56var buildComments = function buildComments(options, uglifyOptions, extractedComments) {
57 var condition = {};
58 var commentsOpts = uglifyOptions.output.comments;
59
60 // /^\**!|@preserve|@license|@cc_on/
61 if (typeof options.extractComments === 'boolean') {
62 condition.preserve = commentsOpts;
63 condition.extract = /^\**!|@preserve|@license|@cc_on/;
64 } else if (typeof options.extractComments === 'string' || options.extractComments instanceof RegExp) {
65 // extractComments specifies the extract condition and commentsOpts specifies the preserve condition
66 condition.preserve = commentsOpts;
67 condition.extract = options.extractComments;
68 } else if (typeof options.extractComments === 'function') {
69 condition.preserve = false;
70 condition.extract = options.extractComments;
71 } else if (Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')) {
72 // Extract condition is given in extractComments.condition
73 condition.preserve = commentsOpts;
74 condition.extract = options.extractComments.condition;
75 } else {
76 // No extract condition is given. Extract comments that match commentsOpts instead of preserving them
77 condition.preserve = false;
78 condition.extract = commentsOpts;
79 }
80
81 // Ensure that both conditions are functions
82 ['preserve', 'extract'].forEach(function (key) {
83 var regexStr = void 0;
84 var regex = void 0;
85
86 switch (typeof condition[key]) {
87 case 'boolean':
88 condition[key] = condition[key] ? function () {
89 return true;
90 } : function () {
91 return false;
92 };
93
94 break;
95 case 'function':
96 break;
97 case 'string':
98 if (condition[key] === 'all') {
99 condition[key] = function () {
100 return true;
101 };
102
103 break;
104 }
105
106 if (condition[key] === 'some') {
107 condition[key] = function (astNode, comment) {
108 return comment.type === 'comment2' && /@preserve|@license|@cc_on/i.test(comment.value);
109 };
110
111 break;
112 }
113
114 regexStr = condition[key];
115
116 condition[key] = function (astNode, comment) {
117 return new RegExp(regexStr).test(comment.value);
118 };
119
120 break;
121 default:
122 regex = condition[key];
123
124 condition[key] = function (astNode, comment) {
125 return regex.test(comment.value);
126 };
127 }
128 });
129
130 // Redefine the comments function to extract and preserve
131 // comments according to the two conditions
132 return function (astNode, comment) {
133 if (condition.extract(astNode, comment)) {
134 extractedComments.push(comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`);
135 }
136
137 return condition.preserve(astNode, comment);
138 };
139};
140
141var minify = function minify(options) {
142 var file = options.file,
143 input = options.input,
144 inputSourceMap = options.inputSourceMap,
145 extractComments = options.extractComments;
146 // Copy uglify options
147
148 var uglifyOptions = buildUglifyOptions(options.uglifyOptions);
149
150 // Add source map data
151 if (inputSourceMap) {
152 uglifyOptions.sourceMap = {
153 content: inputSourceMap
154 };
155 }
156
157 var extractedComments = [];
158
159 if (extractComments) {
160 uglifyOptions.output.comments = buildComments(options, uglifyOptions, extractedComments);
161 }
162
163 var _uglify$minify = _uglifyEs2.default.minify({ [file]: input }, uglifyOptions),
164 error = _uglify$minify.error,
165 map = _uglify$minify.map,
166 code = _uglify$minify.code,
167 warnings = _uglify$minify.warnings;
168
169 return { error, map, code, warnings, extractedComments };
170};
171
172exports.default = minify;
\No newline at end of file