UNPKG

@hyperse/html-webpack-plugin-loader

Version:

A custom template loader that parses HTML templates for the `html-webpack-plugin` package

213 lines (208 loc) 5.86 kB
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; protected readonly html: DefaultTreeAdapterTypes.Element; constructor(htmlSource: string); /** * Upsert the title tag - inserts at beginning of <head> * @param title - The title to upsert * @returns The TemplateParser instance */ upsertTitleTag(title: string): TemplateParser; /** * Upsert the favicon tag - inserts at beginning of <head> after title 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 meta tags in <head> - inserts at beginning for SEO priority * @param tags - The meta tags to upsert * @returns The TemplateParser instance */ upsertHeadMetaTags(tags: string[]): TemplateParser; /** * Upsert external stylesheets in <head> - supports position-based insertion * @param styles - The external styles to upsert * @returns The TemplateParser instance */ upsertHeadStyles(styles: StyleItem[]): TemplateParser; /** * Upsert inline styles in <head> - supports position-based insertion * @param styles - The inline styles to upsert * @returns The TemplateParser instance */ upsertHeadInlineStyles(styles: StyleInlineItem[]): TemplateParser; /** * Upsert external scripts in <head> - supports position-based insertion * @param scripts - The external scripts to upsert * @returns The TemplateParser instance */ upsertHeadScripts(scripts: ScriptItem[]): TemplateParser; /** * Upsert inline scripts in <head> - supports position-based insertion * @param scripts - The inline scripts to upsert * @returns The TemplateParser instance */ upsertHeadInlineScripts(scripts: ScriptInlineItem[]): TemplateParser; /** * Upsert scripts in <body> - supports position-based insertion, typically at end for performance * @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 };