esbuild-plugin-lit
Version:
Import CSS, SVG, HTML, XLIFF files as tagged-template literals. Optionally minify with esbuild minifier.
53 lines • 2.25 kB
JavaScript
import { readFile } from "fs/promises";
import * as path from "path";
import { CSSLoader } from "./css-loader.js";
import { HTMLLoader } from "./html-loader.js";
import { SVGLoader } from "./svg-loader.js";
import { XLFLoader } from "./xlf-loader.js";
function esbuildPluginLit(options = {}) {
const { filter = /\.(css|svg|html|xlf)$/, specifier = "lit", } = options;
return {
name: "eslint-plugin-lit",
async setup(build) {
const svgo = options.svg?.svgo
? await import("svgo")
: undefined;
const htmlMinifier = options.html?.htmlMinifier
? await import("html-minifier")
: undefined;
const loaders = [
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("txml");
loaders.push(new XLFLoader(build, options.xlf, specifier, txml?.parse));
}
const cache = new Map();
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;
//# sourceMappingURL=mod.js.map