1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _uglifyJs = require('uglify-js');
|
8 |
|
9 | var _uglifyJs2 = _interopRequireDefault(_uglifyJs);
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12 |
|
13 | const buildUglifyOptions = ({
|
14 | warnings,
|
15 | parse = {},
|
16 | compress = {},
|
17 | mangle,
|
18 | output,
|
19 | toplevel,
|
20 | nameCache,
|
21 | ie8,
|
22 |
|
23 | keep_fnames
|
24 |
|
25 | } = {}) => ({
|
26 | warnings,
|
27 | parse: Object.assign({}, parse),
|
28 | compress: typeof compress === 'boolean' ? compress : Object.assign({}, compress),
|
29 |
|
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 |
|
38 | sourceMap: null,
|
39 | toplevel,
|
40 | nameCache,
|
41 | ie8,
|
42 | keep_fnames
|
43 | }); |
44 |
|
45 |
|
46 |
|
47 |
|
48 | const buildComments = (options, uglifyOptions, extractedComments) => {
|
49 | const condition = {};
|
50 | const commentsOpts = uglifyOptions.output.comments;
|
51 |
|
52 |
|
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 |
|
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 |
|
65 | condition.preserve = commentsOpts;
|
66 | condition.extract = options.extractComments.condition;
|
67 | } else {
|
68 |
|
69 | condition.preserve = false;
|
70 | condition.extract = commentsOpts;
|
71 | }
|
72 |
|
73 |
|
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 |
|
115 |
|
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 |
|
125 | const 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 |
|
139 | const uglifyOptions = buildUglifyOptions(options.uglifyOptions);
|
140 |
|
141 |
|
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 |
|
159 | exports.default = minify; |
\ | No newline at end of file |