fix: twind duplicating css when className is used (#1658)
This commit is contained in:
@@ -43,6 +43,7 @@ export function setup(options: Options, sheet: Sheet) {
|
|||||||
}
|
}
|
||||||
if (props.className) {
|
if (props.className) {
|
||||||
classes.push(tw(props.className));
|
classes.push(tw(props.className));
|
||||||
|
props.className = undefined;
|
||||||
}
|
}
|
||||||
if (classes.length) {
|
if (classes.length) {
|
||||||
props.class = classes.join(" ");
|
props.class = classes.join(" ");
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export function setup({ selfURL: _selfURL, ...config }: Options, sheet: Sheet) {
|
|||||||
}
|
}
|
||||||
if (props.className) {
|
if (props.className) {
|
||||||
classes.push(tw(props.className));
|
classes.push(tw(props.className));
|
||||||
|
props.className = undefined;
|
||||||
}
|
}
|
||||||
if (classes.length) {
|
if (classes.length) {
|
||||||
props.class = classes.join(" ");
|
props.class = classes.join(" ");
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"lock": false,
|
||||||
|
"tasks": {
|
||||||
|
"start": "deno run -A --watch=static/,routes/ dev.ts"
|
||||||
|
},
|
||||||
|
"imports": {
|
||||||
|
"$fresh/": "../../",
|
||||||
|
"twind": "https://esm.sh/twind@0.16.19",
|
||||||
|
"twind/": "https://esm.sh/twind@0.16.19/",
|
||||||
|
"preact": "https://esm.sh/preact@10.15.1",
|
||||||
|
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||||
|
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
|
||||||
|
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
|
||||||
|
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
|
||||||
|
},
|
||||||
|
"compilerOptions": {
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
"jsxImportSource": "preact"
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+5
@@ -0,0 +1,5 @@
|
|||||||
|
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||||
|
|
||||||
|
import dev from "$fresh/dev.ts";
|
||||||
|
|
||||||
|
await dev(import.meta.url, "./main.ts");
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// DO NOT EDIT. This file is generated by Fresh.
|
||||||
|
// This file SHOULD be checked into source version control.
|
||||||
|
// This file is automatically updated during development when running `dev.ts`.
|
||||||
|
|
||||||
|
import * as $0 from "./routes/_app.tsx";
|
||||||
|
import * as $1 from "./routes/app_class.tsx";
|
||||||
|
import * as $2 from "./routes/index.tsx";
|
||||||
|
|
||||||
|
const manifest = {
|
||||||
|
routes: {
|
||||||
|
"./routes/_app.tsx": $0,
|
||||||
|
"./routes/app_class.tsx": $1,
|
||||||
|
"./routes/index.tsx": $2,
|
||||||
|
},
|
||||||
|
islands: {},
|
||||||
|
baseUrl: import.meta.url,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default manifest;
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
/// <reference no-default-lib="true" />
|
||||||
|
/// <reference lib="dom" />
|
||||||
|
/// <reference lib="dom.asynciterable" />
|
||||||
|
/// <reference lib="deno.ns" />
|
||||||
|
|
||||||
|
import { start } from "$fresh/server.ts";
|
||||||
|
import twind from "$fresh/plugins/twind.ts";
|
||||||
|
import twindV1 from "$fresh/plugins/twindv1.ts";
|
||||||
|
import manifest from "./fresh.gen.ts";
|
||||||
|
|
||||||
|
const twindPlugin = Deno.env.has("TWIND_V1")
|
||||||
|
? twindV1({
|
||||||
|
selfURL: import.meta.url,
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
} as any)
|
||||||
|
: twind({ selfURL: import.meta.url });
|
||||||
|
|
||||||
|
await start(manifest, { plugins: [twindPlugin] });
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { AppProps } from "$fresh/server.ts";
|
||||||
|
|
||||||
|
export default function App({ Component }: AppProps) {
|
||||||
|
return (
|
||||||
|
<html className="bg-slate-800">
|
||||||
|
<head className="bg-slate-800">
|
||||||
|
<meta charSet="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>fresh-foo</title>
|
||||||
|
</head>
|
||||||
|
<body className="bg-slate-800">
|
||||||
|
<Component />
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export default function Page() {
|
||||||
|
return <h1>it works</h1>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import * as path from "https://deno.land/std@0.192.0/path/mod.ts";
|
||||||
|
|
||||||
|
const __dirname = path.dirname(path.fromFileUrl(import.meta.url));
|
||||||
|
|
||||||
|
const links: string[] = [];
|
||||||
|
for (const file of Deno.readDirSync(__dirname)) {
|
||||||
|
if (file.name.startsWith("index")) continue;
|
||||||
|
const name = path.basename(file.name, path.extname(file.name));
|
||||||
|
links.push(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
return (
|
||||||
|
<div class="mx-auto max-w-md">
|
||||||
|
<h1 class="text-3xl font-bold my-8">Tests</h1>
|
||||||
|
<ul class="pl-4">
|
||||||
|
{links.map((link) => {
|
||||||
|
return (
|
||||||
|
<li key={link} class="list-disc">
|
||||||
|
<a href={`/${link}`} class="underline">{link}</a>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
+13
-2
@@ -128,11 +128,22 @@ function _printDomNode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function withFresh(
|
export async function withFresh(
|
||||||
name: string,
|
name: string | { name: string; options: Omit<Deno.CommandOptions, "args"> },
|
||||||
fn: (address: string) => Promise<void>,
|
fn: (address: string) => Promise<void>,
|
||||||
) {
|
) {
|
||||||
|
let file: string;
|
||||||
|
let options = {};
|
||||||
|
|
||||||
|
if (typeof name === "object") {
|
||||||
|
file = name.name;
|
||||||
|
options = name.options ?? {};
|
||||||
|
} else {
|
||||||
|
file = name;
|
||||||
|
}
|
||||||
|
|
||||||
const { lines, serverProcess, address } = await startFreshServer({
|
const { lines, serverProcess, address } = await startFreshServer({
|
||||||
args: ["run", "-A", name],
|
...options,
|
||||||
|
args: ["run", "-A", file],
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
+49
-2
@@ -1,7 +1,7 @@
|
|||||||
import { assert, assertEquals, delay, puppeteer } from "./deps.ts";
|
import { assert, assertEquals, assertMatch, delay, puppeteer } from "./deps.ts";
|
||||||
|
|
||||||
import { cmpStringArray } from "./fixture_twind_hydrate/utils/utils.ts";
|
import { cmpStringArray } from "./fixture_twind_hydrate/utils/utils.ts";
|
||||||
import { startFreshServer, withPageName } from "./test_utils.ts";
|
import { startFreshServer, withFresh, withPageName } from "./test_utils.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the server with the main file.
|
* Start the server with the main file.
|
||||||
@@ -350,3 +350,50 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Test for: https://github.com/denoland/fresh/issues/1655
|
||||||
|
Deno.test({
|
||||||
|
name: "don't duplicate css class",
|
||||||
|
async fn() {
|
||||||
|
await withFresh(
|
||||||
|
"./tests/fixture_twind_app/main.ts",
|
||||||
|
async (address) => {
|
||||||
|
const res = await fetch(`${address}/app_class`);
|
||||||
|
assertEquals(res.status, 200);
|
||||||
|
|
||||||
|
// Don't use an HTML parser here which would de-duplicate the
|
||||||
|
// class names automatically
|
||||||
|
const html = await res.text();
|
||||||
|
assertMatch(html, /html class="bg-slate-800">/);
|
||||||
|
assertMatch(html, /head class="bg-slate-800">/);
|
||||||
|
assertMatch(html, /body class="bg-slate-800">/);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
});
|
||||||
|
// Test for: https://github.com/denoland/fresh/issues/1655
|
||||||
|
Deno.test("don't duplicate css class with twindV1", async () => {
|
||||||
|
await withFresh(
|
||||||
|
{
|
||||||
|
name: "./tests/fixture_twind_app/main.ts",
|
||||||
|
options: {
|
||||||
|
env: {
|
||||||
|
TWIND_V1: "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async (address) => {
|
||||||
|
const res = await fetch(`${address}/app_class`);
|
||||||
|
assertEquals(res.status, 200);
|
||||||
|
|
||||||
|
// Don't use an HTML parser here which would de-duplicate the
|
||||||
|
// class names automatically
|
||||||
|
const html = await res.text();
|
||||||
|
assertMatch(html, /html class="bg-slate-800">/);
|
||||||
|
assertMatch(html, /head class="bg-slate-800">/);
|
||||||
|
assertMatch(html, /body class="bg-slate-800">/);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user