UNPKG

5.38 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _uglifyJs = _interopRequireDefault(require("uglify-js"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
13
14function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
16const buildUglifyOptions = ({
17 warnings,
18 parse = {},
19 compress = {},
20 mangle,
21 output,
22 toplevel,
23 nameCache,
24 ie8,
25
26 /* eslint-disable camelcase */
27 keep_fnames
28 /* eslint-enable camelcase */
29
30} = {}) => ({
31 warnings,
32 parse: _objectSpread({}, parse),
33 compress: typeof compress === 'boolean' ? compress : _objectSpread({}, compress),
34 // eslint-disable-next-line no-nested-ternary
35 mangle: mangle == null ? true : typeof mangle === 'boolean' ? mangle : _objectSpread({}, mangle),
36 output: _objectSpread({
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_fnames
48});
49
50const buildComments = (options, uglifyOptions, extractedComments) => {
51 const condition = {};
52 const commentsOpts = uglifyOptions.output.comments; // Use /^\**!|@preserve|@license|@cc_on/i RegExp
53
54 if (typeof options.extractComments === 'boolean') {
55 condition.preserve = commentsOpts;
56 condition.extract = /^\**!|@preserve|@license|@cc_on/i;
57 } else if (typeof options.extractComments === 'string' || options.extractComments instanceof RegExp) {
58 // extractComments specifies the extract condition and commentsOpts specifies the preserve condition
59 condition.preserve = commentsOpts;
60 condition.extract = options.extractComments;
61 } else if (typeof options.extractComments === 'function') {
62 condition.preserve = commentsOpts;
63 condition.extract = options.extractComments;
64 } else if (Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')) {
65 // Extract condition is given in extractComments.condition
66 condition.preserve = commentsOpts;
67 condition.extract = options.extractComments.condition;
68 } else {
69 // No extract condition is given. Extract comments that match commentsOpts instead of preserving them
70 condition.preserve = false;
71 condition.extract = commentsOpts;
72 } // Ensure that both conditions are functions
73
74
75 ['preserve', 'extract'].forEach(key => {
76 let regexStr;
77 let regex;
78
79 switch (typeof condition[key]) {
80 case 'boolean':
81 condition[key] = condition[key] ? () => true : () => false;
82 break;
83
84 case 'function':
85 break;
86
87 case 'string':
88 if (condition[key] === 'all') {
89 condition[key] = () => true;
90
91 break;
92 }
93
94 if (condition[key] === 'some') {
95 condition[key] = (astNode, comment) => {
96 return comment.type === 'comment2' && /^\**!|@preserve|@license|@cc_on/i.test(comment.value);
97 };
98
99 break;
100 }
101
102 regexStr = condition[key];
103
104 condition[key] = (astNode, comment) => {
105 return new RegExp(regexStr).test(comment.value);
106 };
107
108 break;
109
110 default:
111 regex = condition[key];
112
113 condition[key] = (astNode, comment) => regex.test(comment.value);
114
115 }
116 }); // Redefine the comments function to extract and preserve
117 // comments according to the two conditions
118 // Redefine the comments function to extract and preserve
119 // comments according to the two conditions
120
121 return (astNode, comment) => {
122 if (condition.extract(astNode, comment)) {
123 const commentText = comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
124
125 if (!extractedComments.includes(commentText)) {
126 extractedComments.push(commentText);
127 }
128 }
129
130 return condition.preserve(astNode, comment);
131 };
132};
133
134const minify = options => {
135 const {
136 file,
137 input,
138 inputSourceMap,
139 extractComments,
140 minify: minifyFn
141 } = options;
142
143 if (minifyFn) {
144 return minifyFn({
145 [file]: input
146 }, inputSourceMap);
147 } // Copy uglify options
148
149
150 const uglifyOptions = buildUglifyOptions(options.uglifyOptions); // Add source map data
151
152 if (inputSourceMap) {
153 uglifyOptions.sourceMap = {
154 content: inputSourceMap
155 };
156 }
157
158 const extractedComments = [];
159
160 if (extractComments) {
161 uglifyOptions.output.comments = buildComments(options, uglifyOptions, extractedComments);
162 }
163
164 const {
165 error,
166 map,
167 code,
168 warnings
169 } = _uglifyJs.default.minify({
170 [file]: input
171 }, uglifyOptions);
172
173 return {
174 error,
175 map,
176 code,
177 warnings,
178 extractedComments
179 };
180};
181
182var _default = minify;
183exports.default = _default;
\No newline at end of file