-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (32 loc) · 1.15 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
const Path = require("path");
const fse = require("fs-extra");
class htmlSplitWebpackPlugin {
constructor() {}
apply(compiler) {
compiler.hooks.compilation.tap("htmlSplitWebpackPlugin", compilation => {
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync("htmlSplitWebpackPlugin", (data, cb) => {
var filePath = Path.resolve(compiler.outputPath, Path.dirname(data.outputName));
var reg = new RegExp("<#--\\s+build:([\\s\\S]*?)\\s+-->([\\s\\S]*?)<#--\\s+endbuild\\s+-->", "ig");
var content = "";
var fileName = "";
data.html = data.html.replace(reg, function(str, a, b) {
content = b;
var arr = a.split(":");
fileName = arr[0];
if (!fileName) throw new Error("文件名未定义");
return arr[1] || "";
});
if (fileName && content) {
fse.outputFile(Path.join(filePath, fileName), content, err => {
if (err) {
console.error(err);
}
});
}
cb(null, data);
}
);
});
}
}
module.exports = htmlSplitWebpackPlugin;