import EmmetCompress from "./emmet-compress.js"; import type { IParserConstructor } from "./emmet-compress.js"; //import type { IEmmetConfig } from "./emmet-elem.js"; const wrap = (str: string) => { return `import { emmet } from "emmet-elem";const f=()=>emmet(${JSON.stringify(str)});export default f;`; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any type SourceMap = any; // NOSONAR export interface ILoaderCallback { // eslint-disable-next-line @typescript-eslint/no-explicit-any (err: null, result: string, map?: SourceMap, meta?: any): void; // eslint-disable-next-line @typescript-eslint/no-explicit-any (err: Error, result?: undefined, map?: SourceMap, meta?: any): void; } export interface ILoaderContext { async(): ILoaderCallback; callback(): ILoaderCallback; } export default function xmlLoader( this: ILoaderContext, source: string, sourceMap?: SourceMap, // eslint-disable-next-line @typescript-eslint/no-explicit-any meta?: any ) { const compr = new EmmetCompress(); const prom = compr.write(source); if (prom) { const callback = this.async(); prom.then(() => { callback(null, wrap(compr.end()), sourceMap, meta); }).catch(callback); } else { const callback = this.callback(); callback(null, wrap(compr.end()), sourceMap, meta); } } export interface RollupEmmetConfig { // emmet?: Partial; parser?: IParserConstructor | Promise; includeRaw?: string | RegExp | Array; // NOSONAR include?: string | RegExp | Array; exclude?: string | RegExp | Array; } function regArray(r: string | RegExp | Array): RegExp[] { if (typeof r === "string") { return [new RegExp(r)]; } else if (r instanceof RegExp) { return [r]; } else { return r.map((r) => { if (typeof r === "string") { return new RegExp(r); } else { return r; } }); } } export function rollupLoader( options?: RollupEmmetConfig ): import("rollup").Plugin { const opts = { ...(options || {}) }; const exclude = opts.exclude ? regArray(opts.exclude) : [/(^|\/)node_modules/]; const include = opts.include ? regArray(opts.include) : [/\.(?:xml|html|xhtml|svg|xsl|xslt|mathml|xsd)$/]; const includeRaw = opts.includeRaw ? regArray(opts.includeRaw) : [/\.emmet$/]; return { name: "emmet", load(id: string) { const nid = id.replace(/\\/g, "/"); if (exclude.some((r) => r.test(nid))) { return null; } if (includeRaw.some((r) => r.test(nid))) { return import("fs/promises") .then(({ readFile }) => readFile(id, "utf8")) .then((source) => wrap(source)); } if (include.some((r) => r.test(nid))) { return import("fs/promises") .then(({ readFile }) => readFile(id, "utf8")) .then((source) => { const compr = new EmmetCompress(opts.parser); const prom = compr.write(source); if (prom) { return prom.then(() => wrap(compr.end())); } else { return wrap(compr.end()); } }); } return null; }, }; }