-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (30 loc) · 1.06 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
var fs = require('fs');
function JS2Html(options) {
this.options = options || {};
}
JS2Html.prototype.apply = function (compiler) {
var template = this.options.template;
if (!template) return;
var html = fs.readFileSync(template, 'utf8');
compiler.plugin("emit", function (compilation, callback) {
Object.keys(compilation.assets).forEach(function (item, index) {
var temp = '';
if (/^.*\.js$/g.test(item)) {
temp = html.replace(/(<\/body>)/i, function (match) {
return '<script>' + compilation.assets[item].source() + '<\/script>' + match;
});
compilation.assets[item.replace('.js', '') + '.html'] = {
source: function () {
return temp;
},
size: function () {
return temp.length;
}
};
delete compilation.assets[item];
}
});
callback();
});
};
module.exports = JS2Html;