-
Notifications
You must be signed in to change notification settings - Fork 3
/
html-loader.ts
37 lines (33 loc) · 1.22 KB
/
html-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { AssetLoader, LoaderOptions } from "./asset-loader.ts";
import type { HTMLMinifierOptions, PluginBuild } from "./deps.ts";
type HTMLMinifier = (text: string, options?: HTMLMinifierOptions) => string;
type HTMLLoaderOptions = LoaderOptions & {
htmlMinifier?: HTMLMinifierOptions;
};
export class HTMLLoader extends AssetLoader {
extension = /\.html/;
declare options: HTMLLoaderOptions;
declare minifier?: HTMLMinifier;
constructor(
build: PluginBuild,
options: HTMLLoaderOptions = {},
specifier = "lit",
minifier?: HTMLMinifier,
) {
super(build, options, specifier, minifier);
if (options.extension) this.extension = options.extension;
if (options.transform) this.transform = options.transform;
this.minify = !!build.initialOptions.minify && options.minify !== false &&
!!this.minifier;
}
load(input: string, filename: string): Promise<string> {
let output = this.transform(input, filename);
if (this.minify) {
output = this.minifier!(output, this.options.htmlMinifier);
}
output = output = this.sanitize(input);
return Promise.resolve(`import { html } from '${this.specifier}';
export const template = html\`${output}\`;
export default template;`);
}
}