Refactoring to support inlined scripts

This commit is contained in:
2023-05-02 03:46:47 +02:00
parent e3a022d420
commit 4006f3954e
12 changed files with 563 additions and 231 deletions

23
types/index.d.ts vendored
View File

@@ -3,28 +3,19 @@ import {FilterPattern} from "@rollup/pluginutils";
import type {DefaultTreeAdapterMap} from "parse5";
import {PreRenderedChunk} from "rollup";
import type {LoadNodeCallback} from "./load.d.ts";
export type * from "./load.d.ts"
import type {ResolveCallback} from "./resolve.d.ts";
export type * from "./resolve.d.ts"
export interface RollupHtmlTransformContext {
id?: string;
// bundle: OutputBundle;
// files: Record<string, (OutputChunk | OutputAsset)[]>;
}
export interface RollupHtmlLoadContext {
node: DefaultTreeAdapterMap['element'];
sourceId: string;
}
export interface RollupHtmlResolveContext {
node: DefaultTreeAdapterMap['element'];
sourceId: string;
}
export type TransformCallback = (source: string, transformContext: RollupHtmlTransformContext) => string|Promise<string>;
export type LoadReference = {get: ()=>string, set: (id: string)=>void};
export type LoadResult = LoadReference|LoadReference[]|undefined|void|false;
export type LoadNodeCallback = (loadContext: RollupHtmlLoadContext) => LoadResult|Promise<LoadResult>;
export type ResolveResult = string|true|undefined|void|false;
export type ResolveCallback = (id: string, resolveContext: RollupHtmlResolveContext) => ResolveResult|Promise<ResolveResult>;
export interface RollupHtmlOptions {
publicPath?: string;
@@ -46,7 +37,7 @@ export interface RollupHtmlOptions {
* Detect which references (<a href="...">, <img src="...">) to resolve from a HTML node.
* This rarely needs to be overloaded, but can be used to support non-native attributes used by custom-elements.
*
* Return false to skip any further processing on this node. Else return the id's the resolve based on this node
* Return false to skip any further processing on this node. Use the load function to add any resources from this node, and replace the import with a placeholder so the plugin knows where to inject the end result
*/
load?: LoadNodeCallback;
/**

79
types/load.d.ts vendored 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 = {
/**
* Indiciate 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
types/resolve.d.ts vendored 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>;