transform-to-tailwindcss
Version:
đ¨ Revolutionary CSS-to-TailwindCSS migration tool with surgical precision. Transform legacy stylesheets to utility-first classes instantly across Vue, React, Svelte, and Astro projects.
383 lines (284 loc) âĸ 10.6 kB
Markdown
<div align="center">
# đ¨ Transform to TailwindCSS
<img height="200" src="./assets/kv.png" alt="to tailwindcss">
[](https://badge.fury.io/js/transform-to-tailwindcss)
[](https://www.npmjs.com/package/transform-to-tailwindcss)
[](https://opensource.org/licenses/MIT)
[](https://github.com/Simon-He95/transformToTailwindcss)
**đ Effortlessly migrate legacy CSS to TailwindCSS**
_Automatically transform existing CSS styles into utility-first TailwindCSS classes_
[English](./README.md) | [įŽäŊ䏿](./README_zh.md)
</div>
---
## ⨠Why Choose Transform to TailwindCSS?
đ¯ **Performance First** - Reduce bundle size with TailwindCSS utility classes
đ **Legacy Migration** - Seamlessly upgrade old projects to modern TailwindCSS
⥠**Developer Experience** - Maintain design system consistency
đ ī¸ **Framework Agnostic** - Support for Vue, React, Svelte, Astro and vanilla HTML
đ **Auto Safelist** - đ Automatically collect generated class names, never lose any
đ **Circular Protection** - đ Smart protection to prevent infinite build loops
đ **Build Optimized** - đ Smart skip, only regenerate when changes occur
> Want to use UnoCSS? Try [transformToUnocss](https://github.com/Simon-He95/transformToUnocss)!
## đ Quick Start
### Global Installation
```bash
npm install -g transform-to-tailwindcss
# or
yarn global add transform-to-tailwindcss
# or
pnpm add -g transform-to-tailwindcss
```
### đ¯ CSS Preprocessor Dependencies
From v0.0.49+, install CSS preprocessors you use in your project:
```bash
# For Sass/SCSS support
npm install sass
# For Less support
npm install less less-plugin-module-resolver
# For Stylus support
npm install stylus
```
> **Why?** Using `peerDependencies` ensures compatibility with your project's preprocessor versions and avoids conflicts.
### CLI Usage
```bash
# Transform a directory
totailwindcss playground
# Revert changes
totailwindcss playground --revert
```
### đ Quick Example: Auto Safelist Generation
Transform your CSS and automatically generate a safelist for TailwindCSS in just 3 steps:
> â ī¸ **For Tailwind CSS v3.x and below users**: Run `npm run build` once before starting development to generate the initial safelist file.
```ts
// 1. Configure your build tool
// vite.config.ts
export default defineConfig({
plugins: [
viteTransformToTailwindcss({
collectClasses: true, // đ Enable auto-collection
outputPath: './safelist-classes.js',
}),
],
})
```
```ts
// 2. Use in your TailwindCSS config
// tailwind.config.js
const { safelistClasses } = require('./safelist-classes.js')
module.exports = {
safelist: [...safelistClasses], // đ¯ Never lose classes again!
// ... rest of your config
}
```
```vue
<!-- 3. Write your components normally -->
<template>
<div class="card">
<h1 class="title">
Hello World
</h1>
</div>
</template>
<style scoped>
.card {
padding: 20px;
background: #f3f4f6;
}
.title {
font-size: 24px;
color: #1f2937;
}
</style>
<!-- ⨠Auto-generated: ["p-5", "bg-gray-100", "text-2xl", "text-gray-800"] -->
```
## đ Usage
<details>
<summary>Vite</summary>
```ts
// vite.config.ts
import { vitePluginTransformTotailwindcss } from 'transform-to-tailwindcss'
export default defineConfig({
plugins: [vitePluginTransformTotailwindcss(/* options */)],
})
```
</details>
<br>
<details>
<summary>Rollup</summary>
```ts
// rollup.config.js
import { resolve } from 'node:path'
import { rollupTransformTotailwindcss } from 'transform-to-tailwindcss'
export default {
plugins: [rollupTransformTotailwindcss(/* options */)],
}
```
</details>
<br>
<details>
<summary>Webpack</summary>
```ts
// webpack.config.js
module.exports = {
/* ... */
plugins: [
require('transform-to-tailwindcss').webpackTransformTotailwindcss({
/* options */
}),
],
}
```
</details>
<br>
<details>
<summary>Vue CLI</summary>
```ts
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
require('transform-to-tailwindcss').webpackTransformTotailwindcss({
/* options */
}),
],
},
}
```
</details>
<br>
<details>
<summary>Esbuild</summary>
```ts
// esbuild.config.js
import { build } from 'esbuild'
import { esbuildTransformTotailwindcss } from 'transform-to-tailwindcss'
build({
plugins: [esbuildTransformTotailwindcss(/* options */)],
})
```
</details>
## đ§ Configuration Options
<div align="center">
| Option | Type | Default | Description |
| -------------------- | --------------- | ------------------------- | ----------------------------------------- |
| đ `debug` | `boolean` | `false` | Enable detailed transformation logs |
| đ `isRem` | `boolean` | `false` | Convert px units to rem |
| đ¯ `include` | `FilterPattern` | - | Files to process |
| đĢ `exclude` | `FilterPattern` | - | Files to ignore |
| đ `collectClasses` | `boolean` | `false` | **New** Auto-collect generated classes |
| đ `outputPath` | `string` | `'./safelist-classes.js'` | **New** Output path for collected classes |
| ⥠`skipIfNoChanges` | `boolean` | `true` | **New** Skip generation if no changes |
</div>
> â ī¸ **Important Notes for Different Tailwind CSS Versions**:
>
> - **Tailwind CSS v4.x+**: Works out of the box with dynamic class generation
> - **Tailwind CSS v3.x and below**: Requires initial build step
> 1. Configure plugin: `collectClasses: true`
> 2. Run build: `npm run build` (generates safelist file)
> 3. Start dev: `npm run dev` (uses generated safelist)
> 4. Repeat step 2 when adding new CSS transformations
### đ Class Collection Feature
> đ¯ **New Feature**: Automatically collect all generated TailwindCSS class names for `safelist` configuration!
When using dynamic CSS transformations, TailwindCSS might not detect the generated classes during purging. The class collection feature solves this by automatically generating a safelist file containing all transformed classes.
> â ī¸ **Important for Tailwind CSS v3.x and below users**: You need to run a **build once** first to generate the safelist file, then start your dev server for normal behavior. This is because Tailwind CSS v3.x and below require the safelist to be available during the initial compilation process.
<details>
<summary><strong>đ Auto-Generate Safelist - Never Lose Classes Again</strong></summary>
```ts
// vite.config.js
import { viteTransformToTailwindcss } from 'transform-to-tailwindcss'
export default defineConfig({
plugins: [
viteTransformToTailwindcss({
collectClasses: true, // â
Enable class collection
outputPath: './config/safelist-classes.js', // đ Custom output path
skipIfNoChanges: true, // ⥠Performance optimization
exclude: [
'config/**/*', // đĢ Exclude config directory
'safelist-classes.js', // đĢ Exclude generated file
'tailwind.config.js', // đĢ Exclude Tailwind config
],
}),
],
})
```
```ts
// tailwind.config.js
const { safelistClasses } = require('./config/safelist-classes.js')
module.exports = {
content: ['./src/**/*.{html,js,vue,ts,tsx}'],
safelist: [
...safelistClasses, // đ¯ Auto-generated classes
// Your other safelist items...
],
}
```
**Generated file (`safelist-classes.js`):**
```js
/**
* Auto-generated safelist classes for Tailwind CSS
* Generated at: 2024-01-15T10:30:00.000Z
* Total classes: 156
* Skip if no changes: true
*/
const safelistClasses = [
'bg-blue-500',
'text-white',
'hover:bg-blue-600',
'md:p-6',
// ... more classes
]
module.exports = { safelistClasses }
export { safelistClasses }
```
**Key Benefits:**
- đ **Circular Dependency Protection**: Smart detection prevents infinite build loops
- ⥠**Performance Optimized**: Only regenerates when classes actually change
- đĄī¸ **Build-Safe**: Multiple safeguards prevent duplicate generations
- đ **Comprehensive**: Collects classes from all transformation processes
> â ī¸ **Workflow for Tailwind CSS v3.x and below**:
>
> 1. Configure the plugin with `collectClasses: true`
> 2. Run `npm run build` (or your build command) **once** to generate safelist
> 3. Start your dev server with `npm run dev`
> 4. The generated classes will now be available during development
</details>
### Other Configuration Options
### `debug`
- **Type:** `boolean`
- **Default:** `false`
Enable debug mode to output detailed debugging logs during the transformation process. This is useful for troubleshooting and understanding the style transformation process.
```ts
// Example usage with debug mode enabled
viteTransformToTailwindcss({
debug: true,
isRem: false,
})
```
### `isRem`
- **Type:** `boolean`
- **Default:** `false`
Convert px units to rem units during the transformation process.
### `include`/`exclude`
- **Type:** `FilterPattern`
Filter patterns for including or excluding files during the transformation process.
## đ¯ Supported Features
â
**File Formats** - `.html` | `.tsx` | `.vue` | `.astro` | `.svelte`
â
**CSS Preprocessors** - Sass, Less, Stylus
â
**Build Tools** - Vite, Rollup, Webpack, Vue CLI, ESBuild
â
**IDE Support** - [VS Code Extension](https://github.com/Simon-He95/totailwindcss)
## đ Related Projects
- [transform-to-tailwindcss-core](https://github.com/Simon-He95/transform-to-tailwindcss-core) - Browser-side CSS transformation engine
## đ Before & After Comparison
<div align="center">
### Before đ¤

### After đ

</div>
## đ Support the Project
<div align="center">
**If this project helps you, please consider giving it a â!**
[](https://github.com/Simon-He95/sponsor)
</div>
## đ License
[MIT](./license) Š 2024-PRESENT [Simon He](https://github.com/Simon-He95)