UNPKG

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