vite-plugin-virtual-html
Version:
Vite plugin to load html anywhere
224 lines (214 loc) • 6.79 kB
TypeScript
import { Plugin, UserConfig, ViteDevServer } from 'vite';
import { IncomingMessage } from 'http';
import { Rewrite } from 'connect-history-api-fallback';
import debug from 'debug';
type PageObject = {
template: string;
data?: VirtualHtmlTemplateData;
render?: VirtualHtmlTemplateRender;
};
/**
* describe a page
*/
type VirtualHtmlPage = string | PageObject | VirtualPageOptions;
/**
* html template render
*/
type VirtualHtmlTemplateRender = (template: string, data: Record<string, any>, htmlName?: string) => string;
type VirtualHtmlTemplateData = Record<string, any>;
type Pages = {
[key: string]: VirtualHtmlPage;
};
type VirtualPageOptions = {
entry: string;
title?: string;
body?: string;
};
type UrlTransformerFunction = (resolvedUrl: string, req: IncomingMessage) => string;
/**
* plugin config options
*/
type HtmlPluginOptions = {
/**
* config html-entries' path
* if it is true, plugin will use glob to find all the html page in project to generate a json like {a: /src/a/a.html,}
*/
pages?: Pages | true;
/**
* transform url to another url by user.
* This is ONLY apply in dev mode.
* @param url
*/
urlTransformer?: UrlTransformerFunction;
/**
* define the index page,to replace default index.html
* this page will trigger `transformIndexHtml` hook.
*/
indexPage?: string;
/**
* use for template. as global inject data
*/
data?: Record<string, unknown>;
/**
* function to render template
*/
render?: VirtualHtmlTemplateRender;
/**
* when pages set to true, customize fast-glob's pattern
* default value is ['**\\*.html', '!node_modules\\**\\*.html', '!.**\\*.html']
*/
extraGlobPattern?: Array<string>;
/**
* inject code to html
* key: html name, can be *
*/
injectCode?: Record<string, InjectCode>;
/**
* is set appType to custom?
*/
useCustom?: boolean;
cwd?: string;
};
/**
* inject code to tag's before or after
*/
declare enum POS {
before = 0,
after = 1
}
/**
* inject code config
*/
type InjectCode = {
pos: POS;
find: string;
replacement: string;
};
type HistoryRewrites = Rewrite;
type HistoryApiOptions = {
/**
* option to connect-history-api-fallback's rewrites
*/
rewrites?: Array<HistoryRewrites>;
usePreview?: boolean;
};
declare const VirtualHtmlPlugin: (virtualHtmlOptions: HtmlPluginOptions & HistoryApiOptions) => Plugin;
declare class Base {
_config?: UserConfig;
_pages: Pages;
_indexPage: string;
_globalRender: VirtualHtmlTemplateRender;
_globalData: Record<string, unknown>;
_injectCode: Record<string, InjectCode>;
cwd: string;
logger: debug.Debugger;
_filter: (id: string | unknown) => boolean;
constructor(virtualHtmlOptions: HtmlPluginOptions);
/**
* load html file
* @param args
*/
_load: (args_0: string, args_1: unknown) => Promise<string | undefined>;
/**
* transform code to inject some code into original code
* @param args
*/
_transform: (args_0: string, args_1: string, args_2: unknown) => Promise<string | null>;
/**
* get html file's name
* @param id
* @param root
*/
getHtmlName: (id: string, root?: string) => string;
/**
* add trailing slash on path
* @param {string} path
* @returns {string}
*/
addTrailingSlash: (path: string) => string;
/**
* generate URL
* @param url
*/
generateUrl: (url?: string) => string;
/**
* read HTML file from disk and generate code from template system(with render function)
* @param template
* @param data
* @param render
*/
readHtml: ({ template, data, render, }: PageObject) => Promise<string>;
/**
* render template
* @param templatePath
* @param render
* @param data
*/
renderTemplate: (templatePath: string, render: VirtualHtmlTemplateRender, data: VirtualHtmlTemplateData) => Promise<string>;
/**
* read html file's content to render with render function
* @param templatePath
*/
readTemplate: (templatePath: string) => Promise<string>;
/**
* generate page option from string/object to object
* @param page
* @param globalData
* @param globalRender
*/
generatePageOptions: (page: PageObject | string, globalData: Record<string, unknown>, globalRender: VirtualHtmlTemplateRender) => Promise<PageObject>;
/**
* directly use find\replacement / replacement\find to replace find
* @param {pos, find, replacement}
* @param code
*/
generateInjectCode: ({ pos, find, replacement }: InjectCode, code: string) => string;
/**
* generate page from virtual page
* @param vPages
*/
generateVirtualPage: (vPages: VirtualPageOptions) => Promise<string>;
/**
* find all html file in project and return it as Pages
*/
findAllHtmlInProject: (extraGlobPattern?: Array<string>) => Pages;
defaultRender: VirtualHtmlTemplateRender;
}
declare class Build extends Base {
_needRemove: Array<string>;
_distDir: string;
constructor(virtualHtmlOptions: HtmlPluginOptions);
/**
* check html file's parent directory
* @param html
* @param needRemove
*/
checkVirtualPath(html: string, needRemove: Array<string>, root: string): Promise<void>;
_buildConfig(config: UserConfig): Promise<void>;
_closeBundle(): void;
/**
* use pages' key as html name
* @param pages
*/
extractHtmlPath(pages: {
[p: string]: VirtualHtmlPage | VirtualPageOptions;
}): {
[key: string]: string;
};
htmlNameAddIndex(htmlName: string): string;
}
declare class Serve extends Base {
_rewrites?: Array<HistoryRewrites>;
_urlTransformer?: UrlTransformerFunction;
constructor(virtualHtmlOptions: HtmlPluginOptions & HistoryApiOptions);
_configureServer: (server: ViteDevServer) => () => void;
}
declare const historyApiFallbackPlugin: (historyApiOptions: HistoryApiOptions) => Plugin;
/**
* build a server
* @param server
* @param rewrites
*/
declare function buildHistoryApiFallback(server: ViteDevServer, rewrites: Array<HistoryRewrites>): void;
declare const _default: (virtualHtmlOptions: HtmlPluginOptions & HistoryApiOptions) => Plugin;
export { Build, type HistoryApiOptions, type HistoryRewrites, type HtmlPluginOptions, type InjectCode, POS, type PageObject, type Pages, Serve, type UrlTransformerFunction, type VirtualHtmlPage, VirtualHtmlPlugin, type VirtualHtmlTemplateData, type VirtualHtmlTemplateRender, type VirtualPageOptions, buildHistoryApiFallback, _default as default, historyApiFallbackPlugin };