chore: more readable snapshots

This commit is contained in:
2024-03-24 18:04:47 +01:00
parent 4cb8daf908
commit 93f99c732f
37 changed files with 1258 additions and 1096 deletions

34
src/types/html-module.ts Normal file
View File

@@ -0,0 +1,34 @@
// The HTML-Module is an internal helper structure to track the processing of an HTML file
// This is intended to be serialized into chunk-meta, so it can be cached. (thus keep any functions and circular references out of it)
// TODO: Actually making this serialiable (check rollupResolved, node, as we might no longer need them)
import type {
ModuleInfo,
ResolvedId,
} from 'rollup';
import type {
LoadedReference
} from "./loader.d.ts";
import type {DefaultTreeAdapterMap} from "parse5";
// Internal type
export type HtmlImport = LoadedReference & {
id: string;
resolved: ResolvedId|null;
// loaded: ModuleInfo|null;
node: DefaultTreeAdapterMap['element'];
referenceId: string|null;
placeholder: string,
index: number;
}
export type HtmlModule = {
// TODO might want to impose an own unique id, in case this changes after multiple builds
id: string;
name: string;
importers: Set<string|undefined>,
imports: HtmlImport[];
assetId?: string|null;
document?: DefaultTreeAdapterMap['document'];
}

3
src/types/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export type * from "./html-module.ts";
export type * from "./loader.ts";
export type * from "./resolve.ts";

79
src/types/loader.ts Normal file
View File

@@ -0,0 +1,79 @@
import type {DefaultTreeAdapterMap} from "parse5";
// Load hook types
export interface RollupHtmlLoadContext {
node: DefaultTreeAdapterMap['element'];
sourceId: string;
}
export type AttributeReference = {
attr: string;
};
export type BodyReference = {
/**
* Indicate this is an inlined reference (node body)
*/
body: boolean;
/**
* Describes what the content type is. I.e 'js' for inlined <script>, 'css' for inlined <style>
*/
ext?: string;
};
/**
* Describes how a resource should be loaded.
*/
export type LoadReference = AttributeReference | BodyReference
/**
* Indicate how to load this resource:
* - 'default' uses the default export of the referenced id
* - 'chunk' use the rendered chunk of this file (e.g inlined JS)
* - 'entryChunk' mark this resource as its own entry-chunk and use its rendered output path
* // TODO: add a type 'asset' here, in which we use rollups emitFile({type:'asset'} feature (which reduces the need for plugin-url, and probably makes more sense as the default option instead of 'default' in zero-config scenarios)
*/
export type LoadType = 'default'|'chunk'|'entryChunk';
export type LoadedReference = (
{
// External (virtual) reference
id: string; // path/url referenced. Or identifier for the virtual source
source?: string; // Source to use for this id, for inlined chunks
} | {
// Inline
id?: string; // A unique identifier for snippet
source: string; // Source to use for this id, for inlined chunks
}
) & {
type?: LoadType
};
export type LoadResult = undefined|void|false;
export type LoadFunction = (reference: LoadedReference)=>Promise<string>
export type LoadNodeCallback = (loadContext: RollupHtmlLoadContext, load: LoadFunction) => LoadResult|Promise<LoadResult>;
// Make load hook mapping
/**
* Describes which DOM nodes to extract references from
*/
export type NodeMapping = {
tagName?: string;
/** Filter to specific properties to DOM node must have nodes. TODO allowing a callback here probably makes sense */
match?: ({
/** Whether the element must have a non-null body */
body?: boolean
/** Which additional attributes the element must have to match */
attr?: {[attrName: string]: (string|RegExp|((value:string)=>boolean))}
} | ((el: DefaultTreeAdapterMap['element'])=>boolean));
/**
* Indicate how to load this resource:
* - 'default' uses the default export of the referenced id
* - 'chunk' use the rendered chunk of this file (e.g inlined JS)
* - 'entryChunk' mark this resource as its own entry-chunk and use its rendered output path
*/
loadType?: LoadType
} & LoadReference;

8
src/types/resolve.ts Normal file
View File

@@ -0,0 +1,8 @@
import type {DefaultTreeAdapterMap} from "parse5";
export interface RollupHtmlResolveContext {
node: DefaultTreeAdapterMap['element'];
sourceId: string;
}
export type ResolveResult = string|true|undefined|void|false;
export type ResolveCallback = (id: string, resolveContext: RollupHtmlResolveContext) => ResolveResult|Promise<ResolveResult>;