Skip to content

Commit b8ec2d4

Browse files
feat(index): add <import src="./file.html"> (HTML Imports) support (options.import)
1 parent ac18b3d commit b8ec2d4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Diff for: src/lib/plugins/import.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* eslint-disable */
2+
// External URL (Protocol URL)
3+
const TEST_URL = /^\w+:\/\//;
4+
5+
// TODO(michael-ciniawsky)
6+
// add filter method for urls (e.g `options.import`) (#158)
7+
const filter = (url, options) => {
8+
return TEST_URL.test(url) || url.startsWith('//');
9+
}
10+
11+
export default function (options = {}) {
12+
return function (tree) {
13+
let idx = 0;
14+
const imports = {};
15+
16+
tree.match([ { tag: 'import' }, { tag: 'include' } ], (node) => {
17+
if (node.attrs && node.attrs.src) {
18+
// Remove <import>/<include> tag
19+
node.tag = false;
20+
21+
// TODO(michael-ciniawky)
22+
// add warning about invalid use of external urls within <import> (#?)
23+
24+
// Ignore external && filtered urls
25+
if (filter(node.attrs.src, options)) return;
26+
// Add url to messages.imports
27+
imports[`HTML__IMPORT__${idx}`] = node.attrs.src;
28+
// Add content placeholders to HTML
29+
node.content = options.template
30+
? '${' + `HTML__IMPORT__${idx}(${options.template})` + '}'
31+
: '${' + `HTML__IMPORT__${idx}` + '}';
32+
33+
idx++;
34+
}
35+
36+
return node;
37+
});
38+
39+
// Add imports to result.messages
40+
tree.messages.push(imports)
41+
42+
return tree;
43+
};
44+
}

0 commit comments

Comments
 (0)