-
Notifications
You must be signed in to change notification settings - Fork 171
/
bundle-html.js
48 lines (43 loc) · 1.41 KB
/
bundle-html.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
const fs = require("fs");
const path = require("path");
const parse5 = require("parse5");
const typescript = require("typescript");
const Node = {
map: (transform) => (node) => {
const transformed = transform(node);
const { childNodes } = transformed;
return {
...transformed,
childNodes: childNodes && childNodes.map(Node.map(transform)),
};
},
};
const ENTRY = process.argv[2];
const entryPath = require.resolve(ENTRY);
const entryContent = fs.readFileSync(entryPath, "utf-8");
const parsed = parse5.parse(entryContent);
const transformed = Node.map((node) => {
if (node.nodeName === "script") {
const src = node.attrs.find((attr) => attr.name === "src");
if (src.value) {
const scriptPath = path.resolve(path.dirname(entryPath), src.value);
const scriptRawContent = fs.readFileSync(scriptPath, "utf-8");
const transpileOutput = typescript.transpileModule(scriptRawContent, {
compilerOptions: {
allowJs: true,
checkJs: false,
removeComments: true,
},
});
const scriptContent = transpileOutput.outputText;
const [newScript] = parse5.parseFragment(
`<script>${scriptContent}</script>`,
node.parent,
).childNodes;
return newScript;
}
}
return node;
})(parsed);
const newContent = parse5.serialize(transformed);
fs.writeFileSync(`${entryPath}.js`, `export default \`${newContent}\``);