This repository was archived by the owner on Dec 5, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathminify.js
147 lines (125 loc) · 3.6 KB
/
minify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* eslint-disable
arrow-body-style
*/
import uglify from 'uglify-es';
const buildUglifyOptions = ({
ie8,
ecma,
parse = {},
mangle,
output,
compress = {},
warnings,
toplevel,
nameCache,
} = {}) => ({
ie8,
ecma,
parse,
mangle: mangle == null ? true : mangle,
output: {
shebang: true,
comments: /^\**!|@preserve|@license|@cc_on/,
beautify: false,
semicolons: true,
...output,
},
compress,
warnings,
toplevel,
nameCache,
// Ignoring sourcemap from options
sourceMap: null,
});
const buildComments = (options, uglifyOptions, extractedComments) => {
const condition = {};
const commentsOpts = uglifyOptions.output.comments;
if (
typeof options.extractComments === 'boolean' ||
typeof options.extractComments === 'string' ||
options.extractComments instanceof RegExp
) {
// extractComments specifies the extract condition and commentsOpts specifies the preserve condition
condition.preserve = commentsOpts;
condition.extract = options.extractComments;
} else if (typeof options.extractComments === 'function') {
condition.preserve = false;
condition.extract = options.extractComments;
} else if (
Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')
) {
// Extract condition is given in extractComments.condition
condition.preserve = commentsOpts;
condition.extract = options.extractComments.condition;
} else {
// No extract condition is given. Extract comments that match commentsOpts instead of preserving them
condition.preserve = false;
condition.extract = commentsOpts;
}
// Ensure that both conditions are functions
['preserve', 'extract'].forEach((key) => {
let regexStr;
let regex;
switch (typeof (condition[key])) {
case 'boolean':
condition[key] = condition[key] ? () => true : () => false;
break;
case 'function':
break;
case 'string':
if (condition[key] === 'all') {
condition[key] = () => true;
break;
}
if (condition[key] === 'some') {
condition[key] = (astNode, comment) => {
return comment.type === 'comment2' && /@preserve|@license|@cc_on/i.test(comment.value);
};
break;
}
regexStr = condition[key];
condition[key] = (astNode, comment) => {
return new RegExp(regexStr).test(comment.value);
};
break;
default:
regex = condition[key];
condition[key] = (astNode, comment) => (regex.test(comment.value));
}
});
// Redefine the comments function to extract and preserve
// comments according to the two conditions
return (astNode, comment) => {
if (condition.extract(astNode, comment)) {
extractedComments.push(
comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`,
);
}
return condition.preserve(astNode, comment);
};
};
const minify = (options) => {
const { file, input, inputSourceMap, extractComments } = options;
// Copy uglify options
const uglifyOptions = buildUglifyOptions(options.uglifyOptions);
// Add source map data
if (inputSourceMap) {
uglifyOptions.sourceMap = {
content: inputSourceMap,
};
}
const extractedComments = [];
if (extractComments) {
uglifyOptions.output.comments = buildComments(
options,
uglifyOptions,
extractedComments,
);
}
const { error, map, code, warnings } = uglify.minify(
{ [file]: input },
uglifyOptions,
);
return { error, map, code, warnings, extractedComments };
};
export default minify;