UNPKG

1.67 kBJavaScriptView Raw
1"use strict";
2const parse5_1 = require("parse5");
3const _htmlSymbol = Symbol("HTML dependencies");
4function loader(content) {
5 this.cacheable && this.cacheable();
6 this._module[_htmlSymbol] = loader.modules(content);
7 return content;
8}
9(function (loader) {
10 loader.htmlSymbol = _htmlSymbol;
11 loader.attributes = {
12 "require": ["from"],
13 "compose": ["view", "view-model"],
14 "router-view": ["layout-view", "layout-view-model"],
15 };
16 function traverse(tree, cb) {
17 tree.childNodes && tree.childNodes.forEach((n) => {
18 const ne = n;
19 ne.attrs && ne.attrs.forEach(attr => {
20 cb(ne.tagName, attr);
21 });
22 if (ne.childNodes)
23 traverse(ne, cb);
24 // For <template> tag
25 if (n.content && n.content.childNodes)
26 traverse(n.content, cb);
27 });
28 }
29 function parse(html, cb) {
30 const tree = (0, parse5_1.parseFragment)(html);
31 const attrs = [];
32 traverse(tree, (tag, attr) => {
33 if (cb(tag, attr)) {
34 attrs.push(attr);
35 }
36 });
37 return attrs;
38 }
39 function modules(html) {
40 return parse(html, (tag, attr) => {
41 const attrs = loader.attributes[tag];
42 return attrs && attrs.includes(attr.name);
43 })
44 // Ignore values that contain interpolated values
45 .filter(attr => !/(^|[^\\])\$\{/.test(attr.value))
46 .map(attr => attr.value);
47 }
48 loader.modules = modules;
49})(loader || (loader = {}));
50module.exports = loader;