-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmod.ts
80 lines (72 loc) · 2.48 KB
/
mod.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { readFile } from "https://deno.land/[email protected]/node/fs/promises.ts";
import * as path from "https://deno.land/[email protected]/node/path.ts";
import { CSSLoader } from "./css-loader.ts";
import { HTMLLoader } from "./html-loader.ts";
import { SVGLoader } from "./svg-loader.ts";
import { XLFLoader } from "./xlf-loader.ts";
import type {
HTMLMinifierOptions,
OptimizeOptions,
PluginBuild,
} from "./deps.ts";
import { AssetLoader, LoaderOptions } from "./asset-loader.ts";
export interface Options {
filter?: RegExp;
specifier?: string;
css?: LoaderOptions;
html?: LoaderOptions & {
htmlMinifier?: HTMLMinifierOptions;
};
svg?: LoaderOptions & {
svgo?: OptimizeOptions;
};
xlf?: LoaderOptions;
}
function esbuildPluginLit(options: Options = {}) {
const {
filter = /\.(css|svg|html|xlf)$/,
specifier = "lit",
} = options;
return {
name: "eslint-plugin-lit",
async setup(build: PluginBuild) {
const svgo = options.svg?.svgo
? await import("https://cdn.skypack.dev/svgo?dts")
: undefined;
const htmlMinifier = options.html?.htmlMinifier
? await import("https://cdn.skypack.dev/html-minifier?dts")
: undefined;
const loaders: Array<AssetLoader> = [
new CSSLoader(build, options.css, specifier),
new SVGLoader(build, options.svg, specifier, svgo?.optimize),
new HTMLLoader(build, options.html, specifier, htmlMinifier?.minify),
];
if (options.xlf) {
const txml = await import("https://cdn.skypack.dev/txml?dts");
loaders.push(new XLFLoader(build, options.xlf, specifier, txml?.parse));
}
const cache = new Map<string, { input: string; output: string }>();
build.onResolve({ filter }, (args) => {
return { path: path.resolve(args.resolveDir, args.path) };
});
build.onLoad({ filter }, async (args) => {
const key = args.path;
const input = await readFile(key, "utf8");
let value = cache.get(key);
if (!value || value.input !== input) {
for (const loader of loaders) {
if (loader.extension.test(key)) {
const filename = path.basename(key);
const output = await loader.load(input, filename);
value = { input, output };
break;
}
}
if (!value) value = { input, output: input };
}
return { contents: value.output, loader: "js" };
});
},
};
}
export default esbuildPluginLit;