docs: Add migrating to Tailwind CSS page (#2077)

This commit is contained in:
Marvin Hagemeister
2023-11-28 13:39:05 +01:00
committed by GitHub
parent 9ee6ab72e0
commit 79fd85ef93
4 changed files with 142 additions and 1 deletions
@@ -0,0 +1,137 @@
---
description: |
Migrating from twind to Tailwind CSS
---
Starting with version 1.6 Fresh comes with a proper Tailwind CSS plugin out of
the box. When you create a new Fresh project, checking the Tailwind CSS option
will now install the Tailwind CSS plugin instead of twind like it did before.
## Migrating to Tailwind CSS
1. Create a `<project>/tailwind.config.ts` file in your project folder:
```ts tailwind.config.ts
import { type Config } from "tailwindcss";
export default {
content: [
"{routes,islands,components}/**/*.{ts,tsx}",
],
} as Config;
```
2. Create a css file in your static directory `<project>/static/styles.css`:
```css static/styles.css
@tailwind base;
@tailwind components;
@tailwind utilities;
```
3. Add the created stylesheet in your HTML in `<project>/routes/_app.tsx`:
```diff routes/_app.tsx
import { AppProps } from "$fresh/server.ts";
export default function App({ Component }: AppProps) {
return (
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Fresh Project</title>
+ <link rel="stylesheet" href="/styles.css" />
</head>
<body>
<Component />
</body>
</html>
);
}
```
4. Replace the `twind` plugin with `tailwind`
```diff fresh.config.ts
import { defineConfig } from "$fresh/server.ts";
- import twind from "$fresh/plugins/twind.ts";
+ import tailwind from "$fresh/plugins/tailwind.ts";
export default defineConfig({
- plugins: [twind()],
+ plugins: [tailwind()],
});
```
5. Update your `deno.json` file and add the following `tailwindcss` imports. To
make the
[vscode Tailwind CSS extension](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss)
work, we also need to set `"nodeModulesDir": true`. This will create a
`node_modules` directory in your project folder:
```diff deno.json
{
+ "nodeModulesDir": true,
"imports": {
"$fresh/": "https://deno.land/x/fresh@1.5.2/",
"preact": "https://esm.sh/preact@10.19.2",
"preact/": "https://esm.sh/preact@10.19.2/",
- "twind": "https://esm.sh/twind@0.16.19",
- "twind/": "https://esm.sh/twind@0.16.19/",
+ "tailwindcss": "npm:tailwindcss@3.3.5",
+ "tailwindcss/": "npm:/tailwindcss@3.3.5/",
+ "tailwindcss/plugin": "npm:/tailwindcss@3.3.5/plugin.js"
}
}
```
6. Add `node_modules` to your `.gitignore` or create one if the file is not
present in your project root directory.
```diff .gitignore
+ node_modules/
```
7. Set up
[Ahead of Time builds](http://localhost:8000/docs/canary/concepts/ahead-of-time-builds)
if you haven't already.
That's it! Now you can use Tailwind CSS in your project.
> [info]: If you're a vscode user, be sure to install the
> [official Tailwind CSS extension](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss)
> to get full intellisense support. For it to work you also need to set
> `"nodeModulesDir": true` in your `deno.json`.
> [warn]: Tailwind CSS doesn't support the grouping syntax from twind:
> `text(lg uppercase gray-100)`. These need to be rewritten to their expanded
> values like `text-lg uppercase text-gray-100`.
## Frequently Asked Questions (FAQ)
### What are the differences between twind and Tailwind CSS?
Twind is a project that tries to enable you to use Tailwind-like styling
capabilities in a single script that can also be used in the browser. The key
difference between the two is that twind generates CSS on the fly on every
request and was shipped to the browser to make newly generated classes by
islands work in Fresh. Overall, this wasn't an ideal setup for building
performant sites.
In contrast to that, Tailwind CSS extracts generates the resulting CSS file
ahead of time, which only happens once per deployment. There is no runtime
component needed, which makes your Fresh project respond faster to requests.
During the Tailwind CSS v2 days twind pushed a lot of great ideas like allowing
any number to be used for classes like `opacity-82` and others, but it hasn't
kept up with recent developments of Tailwind CSS. In fact, twind has been
unmaintained for more than a year by now. We never could get autocompletion with
twind to work either.
### Why did Fresh use twind instead of Tailwind CSS?
When Fresh was originally built, Deno didn't support npm modules or node APIs.
This meant that Tailwind CSS didn't work with Deno. Now, many years later, Deno
does ship with support for both of that and we can use the same npm
`tailwindcss` module as everyone else.
+1
View File
@@ -72,6 +72,7 @@ const toc: RawTableOfContents = {
title: "Examples",
link: "latest",
pages: [
["migrating-to-tailwind", "Migrating to Tailwind", "link:canary"],
["modifying-the-head", "Modifying the <head>", "link:latest"],
["writing-tests", "Writing tests", "link:latest"],
[
+3
View File
@@ -521,6 +521,9 @@
.gfm-highlight .token.keyword {
color: var(--color-prettylights-syntax-keyword);
}
.highlight .atrule .rule {
color: var(--color-prettylights-syntax-keyword);
}
.markdown-body .highlight .token.tag .token.class-name,
.markdown-body .highlight .token.tag .token.script .token.punctuation,
.gfm-highlight .token.tag .token.class-name,
+1 -1
View File
@@ -67,7 +67,7 @@ class DefaultRenderer extends Marked.Renderer {
// format: tsx "This is my title"
let lang = "";
let title = "";
const match = info?.match(/^(\w+)\s*(.*)?$/);
const match = info?.match(/^([\w_-]+)\s*(.*)?$/);
if (match) {
lang = match[1].toLocaleLowerCase();
title = match[2] ?? "";