-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
53 lines (41 loc) · 6.14 KB
/
main.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
const Path = require('path');
const fs = require('fs');
const { JSDOM } = require('jsdom');
const Through = require('through2');
const importTemplate = function(source, filePath) {
let matches = null;
let findImports = /<template[^>]*( src="([^"]+)")[^>]*><\/template>/g;
let htmlTarget = source;
while ((matches = findImports.exec(source))) {
let [template, src, path] = matches;
let newTemplate = template.replace(src, '');
let templateContent = null;
// reslove path
path = Path.resolve(Path.parse(filePath).dir, path);
try {
// read template content
templateContent = fs.readFileSync(path).toString('utf-8');
templateContent = (new JSDOM(templateContent)).window.document.querySelector('template').innerHTML;
// check of new imports in the loaded file
templateContent = importTemplate(templateContent, path);
// insert into current file
newTemplate = newTemplate.replace(/></, `>${templateContent}<`);
htmlTarget = htmlTarget.replace(template, newTemplate);
} catch (e) {
if (e.code === 'ENOENT') {
throw new Error(`can't find template at ${e.path}!`);
} else {
throw e;
}
}
}
return htmlTarget;
};
module.exports = function() {
return Through.obj(function(file, enc, cb) {
let source = file.contents.toString('utf-8');
source = importTemplate(source, file.path);
file.contents = new Buffer(source);
cb(null, file);
});
} ;