fix: twind including unused CSS rules (#1415)

This commit is contained in:
Marvin Hagemeister
2023-07-11 23:45:03 +02:00
committed by GitHub
parent c32f0cf9ce
commit 0d141b7153
6 changed files with 95 additions and 7 deletions
+60 -1
View File
@@ -1,7 +1,8 @@
import { assertEquals } from "https://deno.land/std@0.190.0/testing/asserts.ts";
import { assert, delay, puppeteer } from "./deps.ts";
import { cmpStringArray } from "./fixture_twind_hydrate/utils/utils.ts";
import { startFreshServer } from "./test_utils.ts";
import { startFreshServer, withPageName } from "./test_utils.ts";
/**
* Start the server with the main file.
@@ -292,3 +293,61 @@ Deno.test({
await server.terminate();
},
});
Deno.test({
name: "Excludes classes from unused vnodes",
async fn() {
await withPageName(
"./tests/fixture_twind_hydrate/main.ts",
async (page, address) => {
await page.goto(`${address}/unused`);
await page.waitForSelector("#__FRSH_TWIND");
const hasUnusedRules = await page.$eval("#__FRSH_TWIND", (el) => {
return el.textContent.includes(".text-red-600");
});
assert(
!hasUnusedRules,
"Unused CSS class '.text-red-600' found.",
);
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Always includes classes from tw-helper",
async fn() {
await withPageName(
"./tests/fixture_twind_hydrate/main.ts",
async (page, address) => {
await page.goto(`${address}/unused_tw`);
await page.waitForSelector("#__FRSH_TWIND");
const styles = await page.$eval(
"#__FRSH_TWIND",
(el: HTMLStyleElement) => {
const text = el.textContent!;
return {
"text-red-600": text.includes(".text-red-600"),
"text-green-500": text.includes("text-green-500"),
"text-blue-500": text.includes("text-blue-500"),
};
},
);
assertEquals(
styles,
{
"text-red-600": false,
"text-green-500": true,
"text-blue-500": true,
},
);
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});