-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
191 lines (148 loc) · 5.02 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
/*jshint node:true*/
'use strict';
/**
* Metalsmith plugin to uglify JS files
*
* @author George Borisov <git@gir.me.uk>
* @copyright George Borisov 2017
* @license LGPL-3.0
*/
const debug = require('debug');
const logGetJsFiles = debug('metalsmith-uglify:info:get_js_files');
const logCallUglify = debug('metalsmith-uglify:info:call_uglify');
const logCallUglifyDebug = debug('metalsmith-uglify:debug:call_uglify');
const logMain = debug('metalsmith-uglify:info:main');
const logMainDebug = debug('metalsmith-uglify:debug:main');
const logMinify = debug('metalsmith-uglify:info:minify');
const jsRe = new RegExp('.js$');
const jsMinRe = new RegExp('.min.js$');
let separator, uglify;
function get_root (names, opts) {
let root = '';
if (opts.concat && opts.concat.root) {
root = opts.concat.root;
} else if (opts.root) {
root = opts.root;
} else if (!opts.concat && names.length) {
root = names[0].split(separator).slice(0, -1).join(separator);
}
if (root && root.substr(-1) !== separator) {
root += separator;
}
return root;
}
function get_js_files (files, opts) {
const root = get_root([], opts);
let list = Object.keys(files);
logGetJsFiles('Root: %s', root || 'undefined');
if (opts.files) {
list = opts.files;
for (let i = 0; i < list.length; i += 1) {
if (!files[list[i]]) {
throw new Error('File not available: ' + list[i]);
}
}
} else if (opts.filter) {
list = list.filter(opts.filter);
} else {
if (root) {
list = list.filter(function (name) {
return (name.indexOf(root) === 0);
});
}
list = list.filter(function (name) {
return (name.match(jsRe) && !name.match(jsMinRe));
});
}
return list;
}
function get_min_path (root, names, opts) {
if (opts.concat) {
return root + ((opts.concat.file) ? opts.concat.file : 'scripts.min.js');
} else if (opts.sameName) {
return root + names[0].split(separator).pop();
} else {
return root + names[0].split(separator).pop().replace(jsRe, '.min.js');
}
}
async function call_uglify (src, opts) {
let result;
try {
result = await uglify.minify(src, opts);
} catch (e) {
result.error = e;
}
if (result.error) {
logCallUglify('UglifyJS Command: uglify.minify(%O, %O)', src, opts);
logCallUglify('UglifyJS Error: %O', result.error);
throw new Error(require('util').format('UglifyJS Error: %j', result.error));
}
logCallUglifyDebug('UglifyJS Command: uglify.minify(%O, %O)', src, opts);
logCallUglifyDebug('Result: %O', result);
return result;
}
async function minify (names, files, opts) {
if (!names.length) {
return;
}
const root = get_root(names, opts);
const pathMin = get_min_path(root, names, opts);
const src = {};
logMinify('Root: %s', root);
logMinify('pathMin: %s', pathMin);
for (let i = 0; i < names.length; i += 1) {
const name = names[i].substr(root.length);
src[names[i]] = files[names[i]].contents.toString();
}
let result;
if (opts.uglify.sourceMap) {
const nameMin = pathMin.split(separator).pop();
const pathMinMap = pathMin + '.map';
opts.uglify.sourceMap.filename = nameMin;
opts.uglify.sourceMap.url = nameMin + '.map';
result = await call_uglify(src, opts.uglify);
files[pathMinMap] = { contents: new Buffer.from(result.map) };
} else {
result = await call_uglify(src, opts.uglify);
}
files[pathMin] = { contents: new Buffer.from(result.code) };
if (opts.removeOriginal && !opts.sameName) {
for (let i = 0; i < names.length; i += 1) {
delete files[names[i]];
logMinify('Removed: %s', names[i]);
}
}
}
function plugin (opts) {
opts = opts || {};
opts.uglify = opts.uglify || {};
if (opts.es && opts.uglify.ecma === undefined) {
opts.uglify.ecma = 6;
}
// generate source maps by default
if (opts.uglify.sourceMap === undefined) {
opts.uglify.sourceMap = {};
}
if (opts.uglify.sourceMap && opts.uglify.sourceMap.includeSources === undefined) {
opts.uglify.sourceMap.includeSources = true;
}
separator = (opts.windows) ? '\\' : '/';
uglify = require((opts.es) ? 'terser' : 'uglify-js');
return function main (files, _metalsmith, done) {
logMain('Options: %O', opts);
logMainDebug('Input Files: %O', Object.keys(files));
const jsFiles = get_js_files(files, opts);
logMain('JS Files: %O', jsFiles);
logMain('Processing Started');
const promises = (opts.concat)
? [minify(jsFiles, files, opts)]
: jsFiles.map((f) => minify([f], files, opts));
Promise.all(promises)
.then(() => {
logMain('Processing Finished');
done();
})
.catch(done);
};
}
module.exports = plugin;