-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (31 loc) · 1011 Bytes
/
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
/* global hexo */
'use strict';
const { minify } = require('terser');
const { resolve } = require('path');
const crypto = require('crypto');
const hash = crypto.createHash('md5');
const { generator, injector, helper } = hexo.extend;
const url_for = helper.get('url_for').bind(hexo);
generator.register('bundler-js', () => {
const cf = Object.assign({code: undefined, options: {}}, hexo.config.bundler_js);
if (!cf.code) {
return;
}
Object.keys(cf.code).forEach(key => {
const str = cf.code[key];
if (str.endsWith('.js')) {
cf.code[key] = hexo.render.renderSync({path: resolve(hexo.base_dir, str)});
}
});
return minify(cf.code, cf.options)
.then(result => {
hash.update(result.code);
const md5 = hash.digest('hex');
const link = `/js/bundler.${md5}.js`;
injector.register('body_end', `<script src="${url_for(link)}" defer></script>\n`);
return {
path: `js/bundler.${md5}.js`,
data: result.code
};
});
});