-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsvg-loader.ts
45 lines (41 loc) · 1.3 KB
/
svg-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
38
39
40
41
42
43
44
45
import { AssetLoader, LoaderOptions } from "./asset-loader.ts";
import type {
OptimizedSvg,
OptimizeOptions,
optimizeSvg,
PluginBuild,
} from "./deps.ts";
type SVGLoaderOptions = LoaderOptions & {
svgo?: OptimizeOptions;
};
export class SVGLoader extends AssetLoader {
extension = /\.svg$/;
declare options: SVGLoaderOptions;
declare minifier?: typeof optimizeSvg;
constructor(
build: PluginBuild,
options: SVGLoaderOptions = {},
specifier = "lit",
minifier?: typeof optimizeSvg,
) {
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) {
const transformed =
(this.minifier!(output, this.options.svgo || {}) as OptimizedSvg)
.data;
if (!transformed) return Promise.resolve(``); //TODO: error
output = transformed;
}
output = this.sanitize(output);
return Promise.resolve(`import { html } from '${this.specifier}';
export const template = html\`${output}\`;
export default template;`);
}
}