-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
146 lines (124 loc) · 4.1 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const cheerio = require("cheerio");
const outdent = require("outdent");
const prettier = require("prettier");
const fs = require("fs");
const objectHash = require("object-hash");
const path = require("path");
const uppercamelcase = require("uppercamelcase");
const importMap = {};
const getHash = el => objectHash(el.html()).substring(0, 7);
const getName = el => el.attr("name") || `x-${getHash(el)}`;
const imp = (i, p, d) => `import ${i} from '${resolve(p, d)}';`;
const boilerplate = dirname => `
${imp("{ props, withProps }", "skatejs/esnext/with-props", dirname)}
${imp("{ withRender }", "skatejs/esnext/with-render", dirname)}
${imp("{ html, render }", "lit-html", dirname)}
function withLitHtml(Base) {
return class extends withRender(withProps(Base || HTMLElement)) {
rendererCallback (renderRoot, renderCallback) {
render(renderCallback(), renderRoot);
}
};
}
const Base = withLitHtml();
`;
function resolve(modulePath, relativeTo) {
try {
return `./${path.relative(relativeTo, require.resolve(modulePath))}`;
} catch (e) {
return `./${modulePath.replace(".html", ".js")}`;
}
}
function parseElement($element, dirname) {
const name = getName($element);
const props = ($element.attr("props") || "").split(" ").filter(Boolean);
const className = (name && uppercamelcase(name)) || "CustomElement";
const imports = $element.find("import");
const scripts = $element.find("script");
// Imports and scripts should not appear in the template.
$element.remove(imports);
$element.remove(scripts);
return prettier.format(`
${boilerplate(dirname)}
${imports.toArray().map(e => parseImport($(e), dirname)).join(";\n")}
${scripts.toArray().map(e => parseScript($(e))).join(";\n")}
export default class ${className} extends Base {
renderCallback({ ${props.map(parsePropName).join(",")} }) {
return html\`${$element.html()}\`;
}
}
${props.length
? `${className}.props = { ${props.map(parseProp).join(",")} };`
: ""}
customElements.define('${name}', ${className});
`);
}
function parseImport($import, dirname) {
const name = $import.attr("as");
const src = $import.attr("src");
const className = uppercamelcase(name || src);
// Parse the import and map the alias to the acutal element name.
if (name) {
importMap[name] = getName(
cheerio.load(fs.readFileSync(path.join(dirname, src)))("element").eq(0)
);
}
return prettier.format(
`import ${className} from '${resolve(src, dirname)}';`
);
}
function parseElementNameFromImportSource(source) {
const $elements = cheerio.load(source);
return $elements.eq(0).attr("name");
}
function parseScript($script, dirname) {
return $script.html();
}
function parseProp(prop) {
const [name, type] = prop.split(":");
return `${name}${type ? `: props.${type} || ${type}` : ""}`;
}
function parsePropName(prop) {
return prop.split(":")[0];
}
function parseAndReplace($, dirname, using) {
return function(i, e) {
const $e = $(e);
$e.replaceWith(outdent`
<script type="module">
${using($e, dirname).trim()}
</script>
`);
};
}
function html(ch) {
return ch("head").html() + ch("body").html();
}
function parse(file) {
const dirname = path.dirname(file);
const content = fs.readFileSync(file);
const sfc = content.indexOf("<!DOCTYPE sfc>") > -1;
const $ = cheerio.load(content);
const elements = $("element");
const imports = $("import");
const scripts = $("script");
if (sfc) {
body = prettier.format(outdent`
${imports.toArray().map(e => parseImport($(e), dirname)).join(";\n")}
${scripts.toArray().map(e => parseScript($(e))).join(";\n")}
${elements.toArray().map(e => parseElement($(e), dirname)).join(";\n")}
`);
} else {
imports.each(parseAndReplace($, dirname, parseImport));
elements.each(parseAndReplace($, dirname, parseElement));
body = html($);
}
Object.keys(importMap).forEach(key => {
body = body.replace(
new RegExp(`\<([\s\/]*)${key}([\s\>]*)`, "gim"),
`<$1${importMap[key]}$2`
);
});
return { body, sfc };
}
module.exports.parse = parse;