diff --git a/.changeset/rotten-hotels-own.md b/.changeset/rotten-hotels-own.md new file mode 100644 index 0000000..4641b7e --- /dev/null +++ b/.changeset/rotten-hotels-own.md @@ -0,0 +1,5 @@ +--- +"css-var-extract": patch +--- + +Fix including line break diff --git a/packages/css-var-extract/src/extractCssVars.ts b/packages/css-var-extract/src/extractCssVars.ts index 7ef5dc6..9ba2d5a 100644 --- a/packages/css-var-extract/src/extractCssVars.ts +++ b/packages/css-var-extract/src/extractCssVars.ts @@ -6,7 +6,7 @@ export const extractCssVars = (css: string): CssVars => { while (true) { const root = rootRegex.exec(css); if (!root) break; - const condition = root[1]?.trim() ?? ""; + const condition = root[1]?.trim()?.replace("\n", " ") ?? ""; if (!root[2]) break; const cssVarRegex = /\s*(--[^;]*):([^;]*);?\n*/g; while (true) { diff --git a/packages/css-var-extract/tests/extractCssVars.test.ts b/packages/css-var-extract/tests/extractCssVars.test.ts index f1c10ee..0437347 100644 --- a/packages/css-var-extract/tests/extractCssVars.test.ts +++ b/packages/css-var-extract/tests/extractCssVars.test.ts @@ -81,4 +81,16 @@ describe("ExtractCssVars", () => { }, }); }); + + it.each([ + [[":root,", ".light { --primary: #fff }"]], + [[":root, .light", "{ --primary: #fff }"]], + [[":root, .light {", " --primary: #fff }"]], + ])("should be extracted including line break", (input) => { + expect(extractCssVars(input.join("\n"))).toEqual({ + "--primary": { + ":root, .light": "#fff", + }, + }); + }); });