-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
290 lines (263 loc) · 6.25 KB
/
index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*!
* esminify: index.js
* Authors : 枫弦 <[email protected]> (https://github.com/yuzhigang33)
* Create : 2016-01-08 16:30:28
* CopyRight 2016 (c) Alibaba Group
*/
'use strict';
const fs = require('xfs');
const litelog = require('litelog');
const _ = require('lodash');
const path = require('path');
const babel = require('@babel/core');
const babelMinify = require('babel-preset-minify');
const defaultConfig = {
removeDebugger: true,
mangle: {
blacklist: {},
eval: true,
keepFnName: false,
topLevel: true,
keepClassName: false
},
// simplifyComparisons: false
};
function copyFile(s, d) {
let st = fs.lstatSync(s);
if (st.isSymbolicLink()) {
var link = fs.readlinkSync(s);
fs.sync().mkdir(path.dirname(d));
fs.symlinkSync(link, d);
} else {
fs.sync().save(d, fs.readFileSync(s));
}
}
function checkRequiredOpt(opt) {
if (!opt.input) {
opt.input = opt.srcDir;
}
if (!opt.output) {
opt.output = opt.destDir;
}
if (typeof opt !== 'object') {
throw new Error('option must be an object.');
}
if (!opt.input && !opt.code && !opt.ast) {
throw new Error('missing dir in option.');
}
}
function transform(opt) {
opt = opt || {};
if (opt.onFileProcess) {
let pres = opt.onFileProcess(opt);
if (pres === false) {
return;
}
}
var code;
var res = {};
var sheBang = false;
var option = {
// minified: true,
presets: [
[
babelMinify,
opt.config
]
],
comments: false,
generatorOpts: {
jsescOption: {
quotes: 'single'
}
}
};
try {
if (opt.ast) {
res = babel.transformFromAstSync(opt.ast, '', option);
} else {
code = opt.code || fs.readFileSync(opt.input).toString().trim();
// cut utf-8 bom header
if (code.charCodeAt(0) === 65279) {
code = code.substr(1);
}
// cut the shebang
if (code.indexOf('#!') === 0) {
let firstLineEnd = code.indexOf('\n');
sheBang = code.substr(0, firstLineEnd + 1);
code = code.substr(firstLineEnd + 1);
}
res = babel.transformSync(code, option);
}
} catch (e) {
if (e.code === 'BABEL_PARSE_ERROR' || e.code === 'BABEL_TRANSFORM_ERROR') {
console.log('[minify error] file:', opt.input, e.message, 'skip minify this file');
res = {code};
}
}
if (sheBang) {
res.code = sheBang + res.code;
}
if (opt.output) {
fs.sync().save(opt.output, res.code);
} else {
return res.code;
}
}
function genRule(rule) {
if (rule.indexOf('/') === 0) {
rule = '^' + rule;
}
return new RegExp(rule.replace(/\./g, '\\.').replace(/\*/g, '.*'));
}
/**
* minify code
* @param {Object} opt
* - input {ABSPath} filepath as input
* - output {ABSPath} filepath as output
* - code {String} js code as iinput
* - ast {Object}
* - onFileProcess {Function}
* - exclude {Array} exclude paths
* - overrideExclude {Boolean}
* - config {Object} mangle config, see babili
*/
function minify(opt, callback) {
checkRequiredOpt(opt);
let src = opt.input && opt.input.replace(/(\/|\\)$/, '');
let dest = opt.output;
let exclude = opt.exclude || [];
let log = opt.log || litelog.create({
minify: {
level: 'INFO'
}
}).get('minify');
exclude.forEach((v, i, a)=> {
if (typeof v === 'string') {
a[i] = genRule(v);
}
});
opt.config = _.merge({}, defaultConfig, opt.config);
if (opt.keepTopLevel) {
opt.config.mangle.topLevel = false;
}
if (!dest) {
if (/\.\w+$/.test(src)) {
dest = src.replace(/\.(\w+)$/, '.min.$1');
} else {
dest = src + '.min';
}
}
if (!opt.overrideExclude) {
exclude = exclude.concat([
/\.git\//,
/\.svn\//,
/node_modules\//
]);
}
function checkExclude(file) {
var flag = false;
exclude.forEach(function (rule) {
if (rule.test(file)) {
flag = true;
}
});
return flag;
}
if (opt.ast || opt.code) {
return transform(opt);
}
let stats;
try {
stats = fs.statSync(src);
} catch (e) {
if (callback) {
callback(new Error('input error' + e.message));
} else {
log.error('input error' + e.message);
}
return;
}
if (stats.isDirectory()) {
var errs = [];
fs.walk(src, function (file) {
var relfile = file.substr(src.length);
if (checkExclude(relfile)) {
// console.log('exclude:', relfile);
return false;
}
return true;
}, function (err, file, done) {
if (err) {
log.error(err.stack);
return done();
}
var relfile = file.substr(src.length);
if (!/\.js$/.test(file)) {
if (opt.onFileProcess) {
let res = opt.onFileProcess({
input: file,
output: path.join(dest, relfile)
});
if (res === false) {
return done();
}
}
log.info('copy file:', relfile);
copyFile(file, path.join(dest, relfile));
return done();
}
try {
transform(_.merge({}, opt, {
input: file,
output: path.join(dest, relfile),
}));
} catch (e) {
log.error('error, file:', relfile, e.message, e.stack);
e.file = relfile;
errs.push(e);
}
done();
}, function (err) {
if (err) {
if (callback) {
callback(err);
} else {
log.error('compress file error', err.message);
}
return;
}
if (errs.length) {
log.error('====== Error files ========');
errs.forEach(function (err) {
log.error('file:', err.file, err.message);
});
log.error('===========================');
}
callback && callback(errs.length ? errs : null);
});
} else {
let code;
let err = null;
try {
code = transform(opt);
} catch (e) {
err = e;
}
callback && callback(err, code);
}
}
module.exports = minify;
// module.exports.compile = compile;
minify.processFiles = minify;
minify.minify = minify;
minify.copyFile = copyFile;
/**
* parse code into ast
*/
minify.parse = function (str, opt) {
return babel.transformSync(str, {
ast: true,
code: false
}).ast;
};