Skip to content

Commit cef0dc1

Browse files
committed
feat: release 0.0.4
1 parent 76cf574 commit cef0dc1

File tree

7 files changed

+40
-18
lines changed

7 files changed

+40
-18
lines changed

packages/unplugin-tailwindcss-mangle/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ mangle tailwindcss utilities plugin
1313
- [4. register this plugin](#4-register-this-plugin)
1414
- [vite](#vite)
1515
- [webpack](#webpack)
16+
- [Options](#options)
17+
- [classGenerator](#classgenerator)
1618
- [Notice](#notice)
1719

1820
## Features
@@ -74,6 +76,8 @@ You will see all class was renamed to `tw-*`
7476

7577
#### webpack
7678

79+
> Experiment, not work right now
80+
7781
```js
7882
// esm
7983
import { webpackPlugin as utwm } from 'unplugin-tailwindcss-mangle'
@@ -83,6 +87,24 @@ const { webpackPlugin: utwm } = require('unplugin-tailwindcss-mangle')
8387
utwm()
8488
```
8589

90+
## Options
91+
92+
### classGenerator
93+
94+
custom class generator, if you want to custom class name (default 'tw-*'), use this options
95+
96+
```js
97+
export interface IClassGeneratorOptions {
98+
reserveClassName?: (string | RegExp)[]
99+
customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined
100+
log?: boolean
101+
exclude?: (string | RegExp)[]
102+
include?: (string | RegExp)[]
103+
ignoreClass?: (string | RegExp)[]
104+
classPrefix?: string
105+
}
106+
```
107+
86108
## Notice
87109

88110
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.

packages/unplugin-tailwindcss-mangle/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unplugin-tailwindcss-mangle",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "mangle tailwindcss utilities class",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

packages/unplugin-tailwindcss-mangle/src/classGenerator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// import chalk from 'chalk'
2-
import type { IMangleOptions, IMangleContextClass, IClassGenerator } from './types'
2+
import type { IClassGeneratorOptions, IClassGeneratorContextItem, IClassGenerator } from './types'
33

44
import { acceptChars, stripEscapeSequence, regExpTest } from './utils'
55

66
class ClassGenerator implements IClassGenerator {
7-
public newClassMap: Record<string, IMangleContextClass>
7+
public newClassMap: Record<string, IClassGeneratorContextItem>
88
public newClassSize: number
99
public context: Record<string, any>
10-
public opts: IMangleOptions
10+
public opts: IClassGeneratorOptions
1111
public classPrefix: string
12-
constructor(opts: IMangleOptions = {}) {
12+
constructor(opts: IClassGeneratorOptions = {}) {
1313
this.newClassMap = {}
1414
this.newClassSize = 0
1515
this.context = {}
@@ -72,7 +72,7 @@ class ClassGenerator implements IClassGenerator {
7272
return className
7373
}
7474

75-
generateClassName(original: string): IMangleContextClass {
75+
generateClassName(original: string): IClassGeneratorContextItem {
7676
const opts = this.opts
7777

7878
original = stripEscapeSequence(original)
@@ -97,7 +97,7 @@ class ClassGenerator implements IClassGenerator {
9797
if (opts.log) {
9898
console.log(`Minify class name from ${original} to ${newClassName}`)
9999
}
100-
const newClass: IMangleContextClass = {
100+
const newClass: IClassGeneratorContextItem = {
101101
name: newClassName,
102102
usedBy: []
103103
}

packages/unplugin-tailwindcss-mangle/src/css/plugins.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { IMangleContextClass } from '@/types'
1+
import { IClassGeneratorContextItem } from '@/types'
22
import type { PluginCreator } from 'postcss'
33
import parser from 'postcss-selector-parser'
44
export type PostcssMangleTailwindcssPlugin = PluginCreator<{
5-
newClassMap: Record<string, IMangleContextClass>
5+
newClassMap: Record<string, IClassGeneratorContextItem>
66
}>
77

88
const postcssPlugin = 'postcss-mangle-tailwindcss-plugin'
99

1010
const postcssMangleTailwindcssPlugin: PostcssMangleTailwindcssPlugin = (options) => {
1111
// must set newClassMap options
12-
let newClassMap: Record<string, IMangleContextClass> = {}
12+
let newClassMap: Record<string, IClassGeneratorContextItem> = {}
1313
if (options) {
1414
if (options.newClassMap) {
1515
newClassMap = options.newClassMap

packages/unplugin-tailwindcss-mangle/src/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type ClassGenerator from './classGenerator'
22

3-
export interface IMangleContextClass {
3+
export interface IClassGeneratorContextItem {
44
name: string
55
usedBy: any[]
66
}
77

8-
export interface IMangleOptions {
8+
export interface IClassGeneratorOptions {
99
// classNameRegExp?: string
1010
reserveClassName?: (string | RegExp)[]
1111
// ignorePrefix?: string[]
1212
// ignorePrefixRegExp?: string
13-
customGenerate?: (original: string, opts: IMangleOptions, context: Record<string, any>) => string | undefined
13+
customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined
1414
log?: boolean
1515
exclude?: (string | RegExp)[]
1616
include?: (string | RegExp)[]
@@ -19,7 +19,7 @@ export interface IMangleOptions {
1919
}
2020

2121
export interface IClassGenerator {
22-
newClassMap: Record<string, IMangleContextClass>
22+
newClassMap: Record<string, IClassGeneratorContextItem>
2323
newClassSize: number
2424
context: Record<string, any>
2525
}
@@ -32,5 +32,5 @@ export interface IHandlerOptions {
3232
}
3333

3434
export interface Options {
35-
classGenerator?: IMangleOptions
35+
classGenerator?: IClassGeneratorOptions
3636
}

packages/unplugin-tailwindcss-mangle/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IMangleOptions, IClassGenerator } from './types'
1+
import type { IClassGeneratorOptions, IClassGenerator } from './types'
22

33
export function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]> {
44
if (!Array.isArray(arr)) {
@@ -60,7 +60,7 @@ export function stripEscapeSequence(words: string) {
6060
return words.replace(/\\/g, '')
6161
}
6262

63-
export const validate = (opts: IMangleOptions, classGenerator: IClassGenerator) => {
63+
export const validate = (opts: IClassGeneratorOptions, classGenerator: IClassGenerator) => {
6464
if (!opts.log) return
6565
for (const className in classGenerator.newClassMap) {
6666
const c = classGenerator.newClassMap[className]

packages/unplugin-tailwindcss-mangle/test/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from 'fs'
44
import path from 'path'
55

66
export function getTestCase(caseName: string) {
7-
return fs.readFileSync(path.resolve(__dirname, 'fixtures', caseName), 'utf-8')
7+
return fs.readFileSync(path.resolve(__dirname, '../fixtures', caseName), 'utf-8')
88
}
99
// @tailwind base;
1010
// @tailwind components;

0 commit comments

Comments
 (0)