🐛 Fix safelist

This commit is contained in:
Timothy Yin 2024-11-18 13:19:26 +08:00
parent d4a601105d
commit a542bc82b9
3 changed files with 18 additions and 40 deletions

View File

@ -10,6 +10,16 @@ import { installTailwind } from "./tailwind";
import type { config } from "process"; import type { config } from "process";
import type { Strategy, DeepPartial } from "./runtime/types/utils"; import type { Strategy, DeepPartial } from "./runtime/types/utils";
import { createTemplates } from "./template"; import { createTemplates } from "./template";
import { createRequire } from "node:module";
const _require = createRequire(import.meta.url);
const defaultColors = _require("tailwindcss/colors.js");
delete defaultColors.lightBlue;
delete defaultColors.warmGray;
delete defaultColors.trueGray;
delete defaultColors.coolGray;
delete defaultColors.blueGray;
type RayUI = { type RayUI = {
primary?: string; primary?: string;

View File

@ -1,17 +1,5 @@
import type { Config as TwConfig } from "tailwindcss"; import type { Config as TwConfig } from "tailwindcss";
import defaultColors from "tailwindcss/colors.js"; import defaultColors from "tailwindcss/colors.js";
import type { SafelistConfig } from "tailwindcss/types/config";
// @ts-ignore
delete defaultColors.lightBlue;
// @ts-ignore
delete defaultColors.warmGray;
// @ts-ignore
delete defaultColors.trueGray;
// @ts-ignore
delete defaultColors.coolGray;
// @ts-ignore
delete defaultColors.blueGray;
const colorsToRegex = (colors: string[]): string => colors.join("|"); const colorsToRegex = (colors: string[]): string => colors.join("|");
@ -136,32 +124,12 @@ const safelistForComponent: Record<
], ],
}; };
export const generateSafelist = ( export const generateSafelist = (colors: string[], globalColors: string[]) => {
colors: string[], const safelist = Object.keys(safelistForComponent).flatMap((component) =>
globalColors: string[] safelistForComponent[component](colorsToRegex(colors))
): string[] => { );
const safelist = Object.keys(safelistForComponent)
.flatMap((component) =>
safelistForComponent[component](colorsToRegex(colors))
)
.filter(
(item): item is Exclude<SafelistConfig, string> => item !== undefined
);
const extractColorsFromPattern = (pattern: RegExp): string[] => { return [
const matches = pattern.source.match(/\(([^)]+)\)/); ...safelist
if (!matches) return []; ]
return matches[1].split("|").map((color) =>
pattern.source.replace(matches[0], color).replace(/[\^\$]/g, "")
);
};
return safelist.flatMap((item) => {
const replacedStrings = extractColorsFromPattern(item.pattern);
return replacedStrings.concat(
item.variants?.flatMap((variant) =>
replacedStrings.map((str) => `${variant}:${str}`)
) || []
);
});
}; };

View File

@ -10,7 +10,7 @@ export const createTemplates = (nuxt = useNuxt()) => {
const typesTemplate = addTemplate({ const typesTemplate = addTemplate({
filename: "ray.colors.d.ts", filename: "ray.colors.d.ts",
getContents: () => getContents: () =>
`declare module '#ui-colors' { const defaultExport: ${JSON.stringify( `declare module '#ray-colors' { const defaultExport: ${JSON.stringify(
nuxt.options.appConfig.rayui.colors nuxt.options.appConfig.rayui.colors
)}; export default defaultExport; }`, )}; export default defaultExport; }`,
write: true, write: true,