-
Notifications
You must be signed in to change notification settings - Fork 16
/
generate_assets.ts
263 lines (231 loc) · 6.92 KB
/
generate_assets.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {
basename,
dirname,
Document,
DOMParser,
Element,
join,
} from "./deps.ts";
import { decoder, encoder, getLocalDependencyPaths, md5, qs } from "./util.ts";
import { wasmPath } from "./install_util.ts";
import { bundleByEsbuild } from "./bundle_util.ts";
import { logger } from "./logger_util.ts";
import type { File } from "./types.ts";
/**
* Options for asset generation.
*
* @property watchPaths true when the system is watching the paths i.e. packup serve
* @property onBuild The hook which is called when the build is finished. Used when `packup serve`
*/
type GenerateAssetsOptions = {
watchPaths?: boolean;
onBuild?: () => void;
insertLivereloadScript?: boolean;
livereloadPort?: number;
mainAs404?: boolean;
};
/**
* Generates assets from the given entrypoint path (html).
* Also returns watch paths when `watchPaths` option is true.
*
* Used both in `packup build` and `packup serve`.
*/
export async function generateAssets(
path: string,
opts: GenerateAssetsOptions = {},
): Promise<[AsyncGenerator<File, void, void>, string[]]> {
const buildStarted = Date.now();
const htmlAsset = await HtmlAsset.create(path);
const assets = [...htmlAsset.extractReferencedAssets()];
if (opts.insertLivereloadScript) {
htmlAsset.insertScriptTag(
`http://localhost:${opts.livereloadPort!}/livereload.js`,
);
}
const generator = (async function* () {
for (const a of assets) {
// TODO(kt3k): These can be concurrent
yield await a.createFileObject(htmlAsset.pageName, htmlAsset.base);
}
// This needs to be the last.
yield htmlAsset.createFileObject(htmlAsset.pageName, htmlAsset.base);
if (opts.mainAs404) {
yield Object.assign(
await htmlAsset.createFileObject(htmlAsset.pageName, htmlAsset.base),
{ name: "404" },
);
}
logger.log(`Built in ${Date.now() - buildStarted}ms`);
// If build hook is set, call it. Used for live reloading.
opts.onBuild?.();
logger.debug("onBuild");
})();
const watchPaths = opts.watchPaths
? (await Promise.all(assets.map((a) => a.getWatchPaths(htmlAsset.base))))
.flat()
: [];
return [generator, [path, ...watchPaths]];
}
/**
* Builds the entrypoint and watches the all files referenced from the entrypoint.
* If any change is happend in any of the watched paths, then builds again and update
* the watching paths. Used in `packup serve`.
*/
export async function* watchAndGenAssets(
path: string,
opts: GenerateAssetsOptions = {},
): AsyncGenerator<File, void, void> {
opts = {
...opts,
watchPaths: true,
insertLivereloadScript: true,
};
let [assets, watchPaths] = await generateAssets(path, opts);
while (true) {
for await (const file of assets) {
yield file;
}
const watcher = Deno.watchFs(watchPaths);
for await (const e of watcher) {
logger.log("Changed: " + e.paths.join(""));
break;
// watcher.close();
}
logger.log("Rebuilding");
await new Promise<void>((resolve) => setTimeout(() => resolve(), 100));
[assets, watchPaths] = await generateAssets(path, opts);
}
}
type Asset = {
getWatchPaths(base: string): Promise<string[]>;
createFileObject(
pageName: string,
base: string,
): Promise<File>;
};
/**
* Build asset which represents the entrypoint html file.
*/
class HtmlAsset implements Asset {
static async create(path: string): Promise<HtmlAsset> {
logger.debug("Reading", path);
const html = decoder.decode(await Deno.readFile(path));
return new HtmlAsset(html, path);
}
#doc: Document;
#path: string;
base: string;
#filename: string;
pageName: string;
constructor(html: string, path: string) {
this.#doc = new DOMParser().parseFromString(html, "text/html")!;
this.#path = path;
this.base = dirname(path);
this.#filename = basename(path);
if (!this.#filename.endsWith(".html")) {
throw new Error(`Entrypoint needs to be an html file: ${path}`);
}
this.pageName = this.#filename.replace(/\.html$/, "");
if (!this.pageName) {
throw new Error(`Bad entrypoint name: ${path}`);
}
}
extractReferencedAssets() {
return extractReferencedAssets(this.#doc);
}
createFileObject(
_pageName: string,
_base: string,
) {
return Promise.resolve(Object.assign(
new Blob([encoder.encode(this.#doc.body.parentElement!.outerHTML)]),
{ name: this.#filename },
));
}
getWatchPaths() {
return Promise.resolve([this.#path]);
}
insertScriptTag(path: string) {
const script = this.#doc.createElement("script");
script.setAttribute("src", path);
this.#doc.body.insertBefore(script, null);
}
}
class CssAsset implements Asset {
static create(link: Element): CssAsset | null {
const href = link.getAttribute("href");
if (link.getAttribute("rel") === "stylesheet" && href) {
return new CssAsset(href, link);
}
return null;
}
#el: Element;
#href: string;
#dest?: string;
constructor(href: string, link: Element) {
this.#el = link;
this.#href = href;
}
getWatchPaths(base: string): Promise<string[]> {
return Promise.resolve([join(base, this.#href)]);
}
async createFileObject(pageName: string, base: string): Promise<File> {
const data = await Deno.readFile(join(base, this.#href));
this.#dest = `${pageName}.${md5(data)}.css`;
this.#el.setAttribute("href", this.#dest);
return Object.assign(new Blob([data]), { name: this.#dest });
}
}
class ScriptAsset implements Asset {
static create(script: Element): ScriptAsset | null {
const src = script.getAttribute("src");
if (src) {
return new ScriptAsset(src, script);
}
return null;
}
#src: string;
#dest?: string;
#el: Element;
constructor(src: string, script: Element) {
this.#src = src;
this.#el = script;
}
async getWatchPaths(base: string): Promise<string[]> {
return await getLocalDependencyPaths(join(base, this.#src));
}
async createFileObject(
pageName: string,
base: string,
): Promise<File> {
const path = join(base, this.#src);
const data = await bundleByEsbuild(path, wasmPath());
// TODO(kt3k): Maybe align this asset naming to parcel.
// Note: parcel uses a shorter name.
this.#dest = `${pageName}.${md5(data)}.js`;
this.#el.setAttribute("src", this.#dest);
return Object.assign(new Blob([data]), { name: this.#dest });
}
}
export function* extractReferencedAssets(
doc: Document,
): Generator<Asset, void, void> {
yield* extractReferencedScripts(doc);
yield* extractReferencedStyleSheets(doc);
}
function* extractReferencedScripts(
doc: Document,
): Generator<Asset, void, void> {
for (const s of qs(doc, "script")) {
const asset = ScriptAsset.create(s);
if (asset) yield asset;
}
}
function* extractReferencedStyleSheets(
doc: Document,
): Generator<Asset, void, void> {
for (const link of qs(doc, "link")) {
const asset = CssAsset.create(link);
if (asset) yield asset;
}
}