-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (39 loc) · 979 Bytes
/
index.js
File metadata and controls
44 lines (39 loc) · 979 Bytes
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
const path = require('path');
const fs = require('fs');
const pattern1 = /<include.*?\/>/gi;
const pattern2 = /src=["']?([^'"]+)['"]?/i;
// html-include-loader
module.exports = function(source) {
if (source) {
source = includes(this, source);
}
this.callback(null, source);
};
function includes(ctx, text) {
//console.log(ctx.rootContext);
// D:\JOYZL\sites\joyzl
//console.log(ctx.context);
// D:\JOYZL\sites\joyzl\src
return text.replace(pattern1, function(item) {
let src = item.match(pattern2);
if (src && src.length > 1) {
src = src[1];
if (src.startsWith("/")) {
src = ctx.rootContext + src;
} else if (src.startsWith("./")) {
src = ctx.context + src.substr(1);
} else {
src = path.resolve(ctx.context, src);
}
if (fs.existsSync(src)) {
ctx.addDependency(src);
text = fs.readFileSync(src, {
encoding: "utf-8"
});
return includes(ctx, text);
} else {
return "<!-- " + src + " -->";
}
}
});
}