mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-29 00:59:37 +07:00
35 lines
904 B
TypeScript
35 lines
904 B
TypeScript
import postcssPlugin from "postcss";
|
|
import tailwindcss from "tailwindcss";
|
|
import cssnano from "cssnano";
|
|
import { fileExists, getProjectDir } from "~/server/lib/utils";
|
|
import { FileSchema } from "~/server/db/schema/file";
|
|
import { unpackProject } from "~/server/lib/unpack-project";
|
|
|
|
export const postcss = async (fileData: FileSchema) => {
|
|
const content = fileData.content || "";
|
|
|
|
const projectDir = getProjectDir();
|
|
if (!fileExists(projectDir)) {
|
|
return content;
|
|
}
|
|
|
|
try {
|
|
await unpackProject({ ext: "ts,tsx,js,jsx,html" });
|
|
|
|
const result = await postcssPlugin([
|
|
tailwindcss({
|
|
content: [projectDir + "/**/*.{ts,tsx,js,jsx,html}"],
|
|
}),
|
|
cssnano({
|
|
preset: ["default", { discardComments: { removeAll: true } }],
|
|
}),
|
|
]).process(content, {
|
|
from: undefined,
|
|
});
|
|
|
|
return result.css;
|
|
} catch (err) {
|
|
return content;
|
|
}
|
|
};
|