mangle tailwindcss utilities plugin
Now Support
vite
andwebpack
<!-- before -->
<div class="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex"></div>
<!-- after -->
<div class="tw-g tw-h tw-i tw-d tw-e tw-j tw-k tw-l"></div>
<npm|yarn|pnpm> i -D unplugin-tailwindcss-mangle tailwindcss-patch
npx tw-patch
"scripts": {
"prepare": "tw-patch"
},
// for example: vue vite project
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { vitePlugin as utwm } from 'unplugin-tailwindcss-mangle'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), utwm()]
})
then run script:
# generate bundle
yarn build
# preview
yarn preview
You will see all class was renamed to tw-*
// esm
import { webpackPlugin as utwm } from 'unplugin-tailwindcss-mangle'
// or cjs
const { webpackPlugin: utwm } = require('unplugin-tailwindcss-mangle')
// use this webpack plugin
// for example next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
webpack: (config) => {
config.plugins.push(utwm())
return config
}
}
module.exports = nextConfig
custom class generator, if you want to custom class name (default 'tw-*'), use this options
export interface IClassGeneratorOptions {
reserveClassName?: (string | RegExp)[]
customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined
log?: boolean
exclude?: (string | RegExp)[]
include?: (string | RegExp)[]
ignoreClass?: (string | RegExp)[]
classPrefix?: string
}
Type: string | string[]
Default: undefined
glob string
allow you to control the mangle range of bundles.
This plugin only transform those classes which name contain -
or :
, like w-32
, before:h-[300px]
,after:dark:via-[#0141ff]/40
. some classes like flex
,relative
will not be mangled.
because plugin will traverse all html class attr
and js StringLiteral
to find utilities
generated by tailwindcss
.
it's dangerous to mangle some js StringLiteral
like:
const innerHTML = "i'm flex and relative and grid"
document.body.innerHTML = innerHTML
so only strings with -
or :
will be transformed.