-
Notifications
You must be signed in to change notification settings - Fork 18
/
generate_assets.ts
439 lines (386 loc) · 11.9 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* This file contains the functions which create assets from the given paths.
* An asset is the unit of build target.
*
* There are 5 types of assets
* - HtmlAsset
* - CssAsset
* - ScssAsset
* - ScriptAsset - represents javascript or typescript
* - ImageAsset
*/
import {
basename,
dirname,
Document,
DOMParser,
Element,
join,
posixPathJoin,
} from "./deps.ts";
import {
decoder,
encoder,
getLocalDependencyPaths,
isLocalUrl,
md5,
qs,
} from "./util.ts";
import { bundleByEsbuild } from "./bundle_util.ts";
import { logger } from "./logger_util.ts";
import { compile as compileSass } from "./sass_util.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;
publicUrl: string;
};
/**
* 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 { pageName, base } = htmlAsset;
const pathPrefix = opts.publicUrl || ".";
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
const files = await a.createFileObject({ pageName, base, pathPrefix });
for (const file of files) yield file;
}
// This needs to be the last.
const files = await htmlAsset.createFileObject({
pageName,
base,
pathPrefix,
});
for (const file of files) yield file;
if (opts.mainAs404) {
const file =
(await htmlAsset.createFileObject({ pageName, base, pathPrefix }))[0];
yield new File([await file.arrayBuffer()], "404", {
lastModified: 0,
});
}
logger.log(`${path} bundled 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 CreateFileObjectParams = {
pageName: string;
base: string;
pathPrefix: string;
};
type Asset = {
getWatchPaths(base: string): Promise<string[]>;
createFileObject(params: CreateFileObjectParams): Promise<File[]>;
};
const docType = encoder.encode("<!DOCTYPE html>");
/** HtmlAsset represents the 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(_params: CreateFileObjectParams) {
return Promise.resolve([
new File(
[docType, encoder.encode(this.#doc.documentElement!.outerHTML)],
this.#filename,
{
lastModified: 0,
},
),
]);
}
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);
}
}
/** ScssAsset represents a <link rel="stylesheet"> tag in the html */
class CssAsset implements Asset {
static create(link: Element): CssAsset | null {
const href = link.getAttribute("href");
const rel = link.getAttribute("rel");
if (rel !== "stylesheet") {
return null;
}
if (!href) {
logger.warn(
"<link> tag has rel=stylesheet attribute, but doesn't have href attribute",
);
return null;
}
if (href.startsWith("https://") || href.startsWith("http://")) {
// If href starts with http(s):// schemes, we consider these as
// external reference. So skip handling these
return null;
}
if (href.endsWith(".scss")) {
return new ScssAsset(href, link);
}
return new CssAsset(href, link);
}
_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, base, pathPrefix }: CreateFileObjectParams,
): Promise<File[]> {
const data = await Deno.readFile(join(base, this._href));
this._dest = `${pageName}.${md5(data)}.css`;
this._el.setAttribute("href", posixPathJoin(pathPrefix, this._dest));
return [
new File([data], this._dest, { lastModified: 0 }),
];
}
}
/** ScssAsset represents a <link rel="stylesheet"> tag
* with href having .scss extension in the html */
class ScssAsset extends CssAsset {
// TODO(kt3k): implement getWatchPaths correctly
async createFileObject(
{ pageName, base, pathPrefix }: CreateFileObjectParams,
): Promise<File[]> {
const scss = await Deno.readFile(join(base, this._href));
this._dest = `${pageName}.${md5(scss)}.css`;
this._el.setAttribute("href", posixPathJoin(pathPrefix, this._dest));
return [
new File([await compileSass(decoder.decode(scss))], this._dest, {
lastModified: 0,
}),
];
}
}
/** ScriptAsset represents a <script> tag in the html */
class ScriptAsset implements Asset {
static create(script: Element): ScriptAsset | null {
const src = script.getAttribute("src");
if (!src) {
// this <script> should contain inline scripts.
return null;
}
if (src.startsWith("http://") || src.startsWith("https://")) {
// If "src" starts with http(s):// schemes, we consider these as
// external reference. So skip handling these
return null;
}
return new ScriptAsset(src, script);
}
#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,
base,
pathPrefix,
}: CreateFileObjectParams): Promise<File[]> {
const path = join(base, this.#src);
const data = await bundleByEsbuild(path);
this.#dest = `${pageName}.${md5(data)}.js`;
this.#el.setAttribute("src", posixPathJoin(pathPrefix, this.#dest));
return [
new File([data], this.#dest, { lastModified: 0 }),
];
}
}
/** ImageAsset represents a `<img>` tag in the html */
class ImageAsset implements Asset {
static create(img: Element): ImageAsset | null {
let sources: string[] = [];
const src = img.getAttribute("src");
const srcset = img.getAttribute("srcset");
if (img.tagName === "IMG" && !src) {
logger.warn("<img> tag doesn't have src attribute");
return null;
}
if (src && isLocalUrl(src)) sources.push(src);
if (srcset) {
sources.push(
...srcset
.split(",") // Separate the different srcset
.filter(Boolean) // Remove empty strings
.map((src) => src.trim()) // Remove white spaces
.map((src) => src.split(" ")[0]) // Separate the source from the size
.filter(isLocalUrl), // Remove external references
);
}
// Remove duplicates
sources = [...new Set(sources)];
// If "src" or "srcset" only have external references, skip handling
if (sources.length === 0) return null;
return new ImageAsset(sources, img);
}
#sources: string[];
#el: Element;
constructor(sources: string[], image: Element) {
this.#sources = sources;
this.#el = image;
}
async getWatchPaths(base: string): Promise<string[]> {
const localDependencyPaths: Promise<string[]>[] = [];
for (const src of this.#sources) {
localDependencyPaths.push(getLocalDependencyPaths(join(base, src)));
}
return (await Promise.all(localDependencyPaths))
.flatMap((path) => path); // Flatten result
}
async createFileObject(
{ pageName, base, pathPrefix }: CreateFileObjectParams,
): Promise<File[]> {
// TODO(tjosepo): Find a way to avoid creating copies of the same image
// when creating a bundle
const files: File[] = [];
for (const src of this.#sources) {
const data = await Deno.readFile(join(base, src));
const [, extension] = src.match(/\.([\w]+)$/) ?? [];
const dest = `${pageName}.${md5(data)}.${extension}`;
if (this.#el.getAttribute("src")?.match(src)) {
this.#el.setAttribute("src", posixPathJoin(pathPrefix, dest));
}
const srcset = this.#el.getAttribute("srcset");
if (srcset?.includes(src)) {
// TODO(tjosepo): Find a better way to replace the old src with the new
// dest without only using `string.replace()`
this.#el.setAttribute(
"srcset",
srcset.replace(src, posixPathJoin(pathPrefix, dest)),
);
}
files.push(
new File([data], dest, { lastModified: 0 }),
);
}
return files;
}
}
export function* extractReferencedAssets(
doc: Document,
): Generator<Asset, void, void> {
yield* extractReferencedScripts(doc);
yield* extractReferencedStyleSheets(doc);
yield* extractReferencedImages(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;
}
}
function* extractReferencedImages(
doc: Document,
): Generator<Asset, void, void> {
for (const img of [...qs(doc, "img"), ...qs(doc, "source")]) {
const asset = ImageAsset.create(img);
if (asset) yield asset;
}
}