UNPKG

eslint-plugin-readable-tailwind

Version:

auto-wraps tailwind classes after a certain print width or class count into multiple lines to improve readability.

123 lines 4.56 kB
import { readFile } from "node:fs/promises"; import path, { dirname } from "node:path"; import { pathToFileURL } from "node:url"; import { createJiti } from "jiti"; import postcss from "postcss"; import postcssImport from "postcss-import"; import { isCommonJSModule } from "../utils/module.js"; import { isWindows } from "../utils/platform.js"; import { cjsResolver, cssResolver, esmResolver } from "../utils/resolvers.js"; function resolveJsFrom(base, id) { try { return esmResolver.resolveSync({}, base, id) || id; } catch (err) { return cjsResolver.resolveSync({}, base, id) || id; } } function resolveCssFrom(base, id) { return cssResolver.resolveSync({}, base, id) || id; } function createLoader({ filepath, jiti, legacy, onError }) { const cacheKey = `${+Date.now()}`; async function loadFile(id, base, resourceType) { try { const resolved = resolveJsFrom(base, id); const url = pathToFileURL(resolved); url.searchParams.append("t", cacheKey); return await jiti.import(url.href, { default: true }); } catch (err) { return onError(id, err, resourceType); } } if (legacy) { const baseDir = path.dirname(filepath); return async (id) => loadFile(id, baseDir, "module"); } return async (id, base, resourceType) => { return { base, module: await loadFile(id, base, resourceType) }; }; } const CACHE = new Map(); export async function createTailwindContextFromEntryPoint(entryPoint, invalidate) { if (CACHE.has(entryPoint) && !invalidate) { return CACHE.get(entryPoint); } // Create a Jiti instance that can be used to load plugins and config files const jiti = createJiti(getCurrentFilename(), { fsCache: false, moduleCache: false }); const importBasePath = dirname(entryPoint); const tailwindPath = isCommonJSModule() ? cjsResolver.resolveSync({}, importBasePath, "tailwindcss") : esmResolver.resolveSync({}, importBasePath, "tailwindcss"); if (!tailwindPath) { throw new Error("Could not find Tailwind CSS"); } const tailwindUrl = isWindows() ? pathToFileURL(tailwindPath).toString() : tailwindPath; // eslint-disable-next-line eslint-plugin-typescript/naming-convention const { __unstable__loadDesignSystem } = await import(tailwindUrl); let css = await readFile(entryPoint, "utf-8"); // Determine if the v4 API supports resolving `@import` let supportsImports = false; try { await __unstable__loadDesignSystem('@import "./empty";', { loadStylesheet: async () => { supportsImports = true; return { base: importBasePath, content: "" }; } }); } catch { } if (!supportsImports) { const resolveImports = postcss([postcssImport()]); const result = await resolveImports.process(css, { from: entryPoint }); css = result.css; } // Load the design system and set up a compatible context object that is // usable by the rest of the plugin const design = await __unstable__loadDesignSystem(css, { base: importBasePath, loadModule: createLoader({ filepath: entryPoint, jiti, legacy: false, onError: (id, err, resourceType) => { console.error(`Unable to load ${resourceType}: ${id}`, err); if (resourceType === "config") { return {}; } else if (resourceType === "plugin") { return () => { }; } } }), loadStylesheet: async (id, base) => { const resolved = resolveCssFrom(base, id); return { base: path.dirname(resolved), content: await readFile(resolved, "utf-8") }; } }); const context = { getClassOrder: (classList) => design.getClassOrder(classList), getVariants: (className) => design.getVariants(className) }; CACHE.set(entryPoint, context); return context; } function getCurrentFilename() { // eslint-disable-next-line eslint-plugin-typescript/prefer-ts-expect-error // @ts-ignore - `import.meta` doesn't exist in CommonJS -> will be transformed in build step return import.meta.url; } //# sourceMappingURL=context.js.map