import EventEmitter from 'node:events' import { writeFile } from 'node:fs/promises' import { join } from 'node:path/posix' import type { Metafile } from 'esbuild' import type { ResolvedDankConfig } from './config.ts' import type { PageMapping, WebsiteManifest } from './dank.ts' import { LOG } from './debug_log.ts' import { Resolver, type DankDirectories } from './dirs.ts' import { DankError } from './errors.ts' import type { EsbuildEntrypoint } from './esbuild.ts' import { HtmlEntrypoint } from './html.ts' export type EntrypointManifest = { cssBundle?: `/${string}` // matches EsbuildEntrypoint['in'] of WebpageRegistration entrypoint: string // outputs paths in dist dir sharedChunks: Array<`/${string}`> urlPath: `/${string}` } export type WorkerManifest = { // instances of scripts using worker clients: Array // entrypoint of worker entrypoint: EsbuildEntrypoint placeholderCtorSrc: `/${string}` } export type WorkerClientManifest = { // entrypoint or chunk build/dist subpath bundle: `/${string}` ctor: 'Worker' | 'SharedWorker' // original ctor url replaced with placeholderCtorSrc originalCtorSrc: string // path to script with Worker ctor call // matches `esbuild.Metafile.outputs` entrypoint or inputs path script: string } type WebpageRegistration = { pageUrl: `/${string}` fsPath: string html: HtmlEntrypoint bundleEntrypoints: Array urlRewrite?: UrlRewrite } export type UrlRewrite = { pattern: RegExp url: `/${string}` } export type WebsiteRegistryEvents = { // webpage css & js entrypoints changed entrypoints: [] // webpage added to website registry webpage: [entrypoint: HtmlEntrypoint] // website worker entrypoints changed workers: [] } // manages website resources during `dank build` and `dank serve` export class WebsiteRegistry extends EventEmitter { // paths of bundled esbuild outputs mapped to entrypoint metadata #bundles: Record<`/${string}`, EntrypointManifest> = {} #c: ResolvedDankConfig // public dir assets #copiedAssets: Set<`/${string}`> | null = null // map of entrypoints to their hashed output paths // includes all hrefs found in