@hyperse/html-webpack-plugin-loader
Version:
A custom template loader that parses HTML templates for the `html-webpack-plugin` package
212 lines (207 loc) • 5.42 kB
TypeScript
import { DefaultTreeAdapterTypes } from 'parse5';
export { DefaultTreeAdapterTypes } from 'parse5';
/**
* The position of the script
*/
type Position = 'beginning' | 'end';
/**
* The base script item
*/
type HtmlItemBase = {
/**
* The id of the script
*/
id: string;
/**
* The position of the script, 'beginning' for beginning of head, 'end' for end of head
* The position of the script, 'beginning' for beginning of body, 'end' for end of body
*/
position: Position;
/**
* The order of the script, smaller numbers are loaded first
*/
order?: number;
};
type StyleItem = HtmlItemBase & {
/**
* The href of the style
*/
href: string;
};
/**
* The inline style item
*/
type StyleInlineItem = HtmlItemBase & {
/**
* The content of the style
*/
content: string;
};
/**
* The inline script item
*/
type ScriptInlineItem = HtmlItemBase & {
/**
* The content of the script
*/
content: string;
};
type ScriptItem = HtmlItemBase & {
/**
* The src of the script, the id
*/
src: string;
/**
* The type of the script
*/
type?: string;
/**
* Whether the script should be loaded asynchronously
*/
async?: boolean;
/**
* Whether the script should be loaded after the page has loaded
*/
defer?: boolean;
/**
* The cross origin of the script
*/
crossOrigin?: string;
/**
* The integrity of the script
*/
integrity?: string;
/**
* The nonce of the script
*/
nonce?: string;
};
/**
* The favicon item
*/
type FaviconItem = {
/**
* The href of the favicon
*/
href: string;
/**
* The rel attribute of the favicon tag
*/
rel: string;
/**
* The attributes of the favicon tag
*/
attributes: Record<string, string>;
};
/**
* The options for the template
*/
interface TemplateOptions {
/**
* The title of the page
*/
title?: string;
/**
* The favicon of the page
*/
favicon?: FaviconItem;
/**
* The head html meta tags of the page
*/
headMetaTags?: string[];
/**
* The head styles of the page
*/
headStyles?: StyleItem[];
/**
* The head scripts of the page
*/
headScripts?: ScriptItem[];
/**
* The body scripts of the page
*/
bodyScripts?: ScriptItem[];
/**
* The head inline scripts
*/
headInlineScripts?: ScriptInlineItem[];
/**
* The head inline styles
*/
headInlineStyles?: StyleInlineItem[];
}
declare class TemplateParser {
protected readonly document: DefaultTreeAdapterTypes.Document;
protected readonly head: DefaultTreeAdapterTypes.Element;
protected readonly body: DefaultTreeAdapterTypes.Element;
constructor(htmlSource: string);
/**
* Upsert the title tag
* @param title - The title to upsert
* @returns The TemplateParser instance
*/
upsertTitleTag(title: string): TemplateParser;
/**
* Upsert the favicon tag
* @param href - The favicon to upsert
* @param rel - The rel attribute of the favicon tag
* @param attributes - The attributes of the favicon tag
* @returns The TemplateParser instance
*/
upsertFaviconTag(href: string, rel?: string, attributes?: Record<string, string>): TemplateParser;
/**
* Upsert the head before html tags
* @param tags - The tags to upsert
* @returns The TemplateParser instance
*/
upsertHeadMetaTags(tags: string[]): TemplateParser;
/**
* Upsert the head before styles
* @param styles - The styles to upsert
* @returns The TemplateParser instance
*/
upsertHeadStyles(styles: StyleItem[]): TemplateParser;
/**
* Upsert the head inline styles
* @param styles - The styles to upsert
* @returns The TemplateParser instance
*/
upsertHeadInlineStyles(styles: StyleInlineItem[]): TemplateParser;
/**
* Upsert the head before scripts
* @param scripts - The scripts to upsert
* @returns The TemplateParser instance
*/
upsertHeadScripts(scripts: ScriptItem[]): TemplateParser;
/**
* Upsert the inline scripts
* @param scripts - The scripts to upsert
* @returns The TemplateParser instance
*/
upsertHeadInlineScripts(scripts: ScriptInlineItem[]): TemplateParser;
/**
* Upsert the body after scripts
* @param scripts - The scripts to upsert
* @returns The TemplateParser instance
*/
upsertBodyScripts(scripts: ScriptItem[]): TemplateParser;
/**
* Serialize the document to html string
* @returns The serialized html string
*/
serialize(): string;
/**
* Parse a fragment of html
* @param html - The html fragment to parse
* @returns The parsed document fragment
*/
parseFragment(html: string): DefaultTreeAdapterTypes.DocumentFragment;
}
/**
* Parse the template
* @param htmlSource - The html source
* @param options - The options
* @returns The parsed template
*/
declare const parseTemplate: (htmlSource: string, options?: TemplateOptions) => TemplateParser;
export { type FaviconItem, type HtmlItemBase, type Position, type ScriptInlineItem, type ScriptItem, type StyleInlineItem, type StyleItem, type TemplateOptions, TemplateParser, parseTemplate };