WIP: transforming through handlebars, parsing the html and resolving the imports

This commit is contained in:
2023-04-27 15:18:14 +02:00
parent da9dc3bdc1
commit 240d5cfe9a
8 changed files with 335 additions and 126 deletions

71
types/index.d.ts vendored
View File

@@ -1,24 +1,67 @@
import type { Plugin, OutputChunk, OutputAsset, OutputBundle } from 'rollup';
import type {Plugin, OutputChunk, OutputAsset, OutputBundle, TransformModuleJSON, } from 'rollup';
import {FilterPattern} from "@rollup/pluginutils";
import type {DefaultTreeAdapterMap} from "parse5";
import {PreRenderedChunk} from "rollup";
export interface RollupHtmlTemplateOptions {
title: string;
attributes: Record<string, any>;
publicPath: string;
meta: Record<string, any>[];
bundle: OutputBundle;
files: Record<string, (OutputChunk | OutputAsset)[]>;
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 {
title?: string;
attributes?: Record<string, any>;
fileName?: string;
meta?: Record<string, any>[];
publicPath?: string;
template?: (templateoptions?: RollupHtmlTemplateOptions) => string;
/**
* Follows the same logic as rollup's [entryFileNames](https://rollupjs.org/configuration-options/#output-entryfilenames).
*/
htmlFileNames?: string|((chunkInfo: PreRenderedChunk) => string);
/**
* Transform a source file passed into this plugin to HTML. For example: a handlebars transform
* ```
* transform(source){
* return handlebars.compile(source)({myVar:'example'})
* }
* ```
*/
transform?: TransformCallback;
/**
* 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
*/
load?: LoadNodeCallback;
/**
* Callback to filter which references actually need to be resolved. Here you can filter out:
* - Links to extensions that don't need to be handled through rollup
* - Resources that are external to the app (for example non-relative paths)
* - Page navigation within the app
*
* Return a falsey value to skip this reference. Return true to resolve as is. (or string to transform the id)
*/
resolve?: ResolveCallback;
include?: FilterPattern;
exclude?: FilterPattern
}
export function makeHtmlAttributes(attributes: Record<string, string>): string;
/**
* A Rollup plugin which creates HTML files to serve Rollup bundles.