next-yak
Version:
next-yak is a CSS-in-JS solution tailored for Next.js that seamlessly combines the expressive power of styled-components syntax with efficient build-time extraction of CSS using Next.js's built-in CSS configuration
36 lines (31 loc) • 1.16 kB
text/typescript
import { YakConfigOptions } from "../../withYak/index.js";
/**
* Extracts CSS content from code that contains YAK-generated CSS comments.
* Parses the input code and returns the extracted CSS, optionally adding
* a cssmodules directive based on the transpilation mode.
*/
export function extractCss(
code: string | Buffer<ArrayBufferLike>,
transpilationMode: NonNullable<YakConfigOptions["experiments"]>["transpilationMode"],
): string {
let codeString: string;
if (typeof code === "string") {
codeString = code;
} else if (code instanceof Buffer) {
codeString = code.toString("utf-8");
} else if (code instanceof ArrayBuffer) {
codeString = new TextDecoder("utf-8").decode(code);
} else {
throw new Error("Invalid input type: code must be string, Buffer, or ArrayBuffer");
}
const codeParts = codeString.split("/*YAK Extracted CSS:\n");
let result = "";
for (let i = 1; i < codeParts.length; i++) {
const codeUntilEnd = codeParts[i].split("*/")[0];
result += codeUntilEnd;
}
if (result && transpilationMode !== "Css") {
result = "/* cssmodules-pure-no-check */\n" + result;
}
return result;
}