-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
130 lines (113 loc) · 3.5 KB
/
index.ts
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
import * as serializer from "dom-serializer";
import {
DomHandler,
DomUtils,
parseDocument,
ElementType,
Parser,
} from "htmlparser2";
import { BuildOptions, BuildResult, Metafile } from "esbuild";
import * as path from "path";
export class HTML2ESBuild {
dom: ReturnType<typeof parseDocument>;
generate(
source: string,
resolve: (...relativePath: string[]) => string
): BuildOptions {
const dom = parseDocument(source);
this.dom = dom;
const config: BuildOptions = {
bundle: true,
metafile: true,
entryPoints: [],
};
let src = "";
for (let script of DomUtils.getElementsByTagName("script", dom)) {
src = script.attribs["src"];
if (src && !src.includes("://")) {
src = resolve(src);
this.scripts.set(src, script);
config.entryPoints.push(src);
}
}
for (let link of DomUtils.getElementsByTagName("link", dom)) {
if (
(!link.attribs["rel"] || link.attribs["rel"] === "stylesheet") &&
link.attribs["href"] &&
!link.attribs["href"].includes("://")
) {
src = resolve(link.attribs["href"]);
this.links.set(src, link);
config.entryPoints.push(src);
}
}
this.config = config;
return config;
}
scripts: Map<string, ReturnType<typeof DomUtils.getElementById>> = new Map();
links: Map<string, ReturnType<typeof DomUtils.getElementById>> = new Map();
config: BuildOptions;
renderToString(
build: BuildResult,
config: BuildOptions = this.config,
resolveFrom: (...relativePath: string[]) => string,
resolveTo: (
outpath: string,
node?: ReturnType<typeof DomUtils.getElementById>
) => string
) {
if (!build.metafile) throw "Build is missing metafile.";
const { links, scripts } = this;
let meta: Metafile = build.metafile;
const cssOutputs = new Map();
let file;
for (let output in meta.outputs) {
file = meta.outputs[output];
if (path.extname(output) === ".css") {
cssOutputs.set(output, file);
}
}
const stylesheetsToInsert = new Map<
string,
ReturnType<typeof DomUtils.getElementById>
>();
const prefix = config.publicPath ? config.publicPath : "";
for (let output in meta.outputs) {
file = meta.outputs[output];
if (!file.entryPoint) continue;
const entryPoint = resolveFrom(file.entryPoint);
if (scripts.has(entryPoint)) {
// CSS imports from JS
const ext = path.extname(output);
const basename = output.substring(0, output.length - ext.length);
const cssName = basename + ".css";
const script = scripts.get(entryPoint);
if (
cssOutputs.has(cssName) &&
(!cssOutputs.get(cssName).entryPoint ||
!links.has(cssOutputs.get(cssName).entryPoint))
) {
stylesheetsToInsert.set(cssName, script);
}
const _output = resolveTo(output, script);
if (_output) {
script.attribs["src"] = _output;
}
} else if (links.has(entryPoint)) {
links.get(entryPoint).attribs["href"] = resolveTo(output);
}
}
for (let [stylesheetName, above] of stylesheetsToInsert.entries()) {
var parser = new Parser(
new DomHandler((err, elems) => {
DomUtils.prepend(above, elems[0]);
})
);
parser.write(
`<link rel="stylesheet" href="${resolveTo(stylesheetName)}" />`
);
parser.end();
}
return serializer.default(this.dom, {});
}
}