UNPKG

transform-to-unocss

Version:

🚀 Effortlessly transform CSS, inline styles, and preprocessors (Sass/Less/Stylus) to UnoCSS with smart conflict resolution and debug support

111 lines (109 loc) • 4.34 kB
import { transformAstro, transformCode, transformHtml, transformJsx, transformSvelte, transformVue } from "./transformCode-EA1ucqBL.js"; import { createFilter } from "@rollup/pluginutils"; import { createUnplugin } from "unplugin"; //#region src/unplugin.ts const unplugin = createUnplugin((options) => { const defaultExclude = ["**/node_modules/**"]; const userExclude = (options === null || options === void 0 ? void 0 : options.exclude) ? Array.isArray(options.exclude) ? options.exclude : [options.exclude] : []; const finalExclude = [...defaultExclude, ...userExclude]; const filter = createFilter(options === null || options === void 0 ? void 0 : options.include, finalExclude); let globalCss = null; let resolveAlias = null; return [{ name: "unplugin-transform-to-unocss", enforce: "pre", async configResolved(config) { var _config$css, _config$resolve; globalCss = (_config$css = config.css) === null || _config$css === void 0 ? void 0 : _config$css.preprocessorOptions; resolveAlias = ((_config$resolve = config.resolve) === null || _config$resolve === void 0 ? void 0 : _config$resolve.alias) ?? null; }, transformInclude(id) { if (id.includes("node_modules")) return false; return filter(id); }, async transform(code, id) { let suffix; if (id.endsWith(".vue")) suffix = "vue"; else if (id.endsWith("lang.tsx")) {} else if (id.endsWith(".tsx")) suffix = "tsx"; if (!suffix) return code; let transformedCode = code; try { if (suffix === "vue" && typeof this.resolve === "function") { const styleBlockRegex = /<style\b[^>]*>([\s\S]*?)<\/style>/g; const blocks = []; for (const m of code.matchAll(styleBlockRegex)) { if (!m.index) continue; blocks.push({ full: m[0], content: m[1], start: m.index, end: m.index + m[0].length }); } if (blocks.length) { const replacements = []; for (const b of blocks) { const content = b.content; const impRegex = /@(import|use|forward)\s+(?:url\()?['"]([^'"]+)['"]\)?/g; const matches = []; for (const im of content.matchAll(impRegex)) { if (typeof im.index !== "number") continue; matches.push({ index: im.index, length: im[0].length, path: im[2] }); } if (!matches.length) continue; let newContent = ""; let lastIndex = 0; for (const m of matches) { const original = content.slice(m.index, m.index + m.length); const before = content.slice(lastIndex, m.index); lastIndex = m.index + m.length; let resolvedId = null; try { const r = await this.resolve(m.path, id); if (r && r.id && !r.id.startsWith("\0")) resolvedId = r.id; } catch (e) {} if (resolvedId) { const replaced = original.replace(m.path, resolvedId); newContent += before + replaced; } else newContent += before + original; } newContent += content.slice(lastIndex); const newFull = b.full.replace(b.content, newContent); replacements.push({ start: b.start, end: b.end, text: newFull }); } transformedCode = code; for (let i = replacements.length - 1; i >= 0; i--) { const r = replacements[i]; transformedCode = transformedCode.slice(0, r.start) + r.text + transformedCode.slice(r.end); } } } } catch (e) { if (globalCss && globalCss.debug) console.warn("[transform-to-unocss] resolver error", e); } return await transformCode(transformedCode, { filepath: id, type: suffix, globalCss, resolveAlias }); } }]; }); const viteTransformToUnocss = unplugin.vite; const rollupTransformToUnocss = unplugin.rollup; const webpackTransformToUnocss = unplugin.webpack; const esbuildTransformToUnocss = unplugin.esbuild; const rspackTransformToUnocss = unplugin.rspack; const farmTransformToUnocss = unplugin.farm; const rolldownTransformToUnocss = unplugin.rolldown; //#endregion export { esbuildTransformToUnocss, farmTransformToUnocss, rolldownTransformToUnocss, rollupTransformToUnocss, rspackTransformToUnocss, transformAstro, transformCode, transformHtml, transformJsx, transformSvelte, transformVue, viteTransformToUnocss, webpackTransformToUnocss };