Skip to content

Commit 6327b01

Browse files
committed
feat: add classGenerator options
1 parent 5ce3967 commit 6327b01

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ClassGenerator implements IClassGenerator {
1717
this.classPrefix = opts.classPrefix ?? 'tw-'
1818
}
1919

20-
defaultClassGenerator() {
20+
defaultClassGenerate() {
2121
const chars = []
2222
let rest = (this.newClassSize - (this.newClassSize % acceptChars.length)) / acceptChars.length
2323
if (rest > 0) {
@@ -80,11 +80,11 @@ class ClassGenerator implements IClassGenerator {
8080
if (cn) return cn
8181

8282
let newClassName
83-
if (opts.classGenerator) {
84-
newClassName = opts.classGenerator(original, opts, this.context)
83+
if (opts.customGenerate && typeof opts.customGenerate === 'function') {
84+
newClassName = opts.customGenerate(original, opts, this.context)
8585
}
8686
if (!newClassName) {
87-
newClassName = this.defaultClassGenerator()
87+
newClassName = this.defaultClassGenerate()
8888
}
8989

9090
if (opts.reserveClassName && regExpTest(opts.reserveClassName, newClassName)) {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import ClassGenerator from './classGenerator'
88
import { htmlHandler } from './html'
99
import { jsHandler } from './js'
1010
import { cssHandler } from './css'
11-
import type { } from 'webpack'
11+
import type {} from 'webpack'
1212
export const unplugin = createUnplugin((options: Options | undefined = {}, meta) => {
13-
1413
const isMangleClass = (className: string) => {
1514
// ignore className like 'filter','container'
1615
// it may be dangerous to mangle/rename all StringLiteral , so use /-/ test for only those with /-/ like:
@@ -19,9 +18,8 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
1918
}
2019
let classSet: Set<string>
2120
// let cached: boolean
22-
const classGenerator = new ClassGenerator()
21+
const classGenerator = new ClassGenerator(options.classGenerator)
2322
function getCachedClassSet() {
24-
2523
const set = getClassCacheSet()
2624
set.forEach((c) => {
2725
if (!isMangleClass(c)) {

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

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

3-
export interface Options {
4-
// define your plugin options here
5-
}
6-
73
export interface IMangleContextClass {
84
name: string
95
usedBy: any[]
@@ -14,7 +10,7 @@ export interface IMangleOptions {
1410
reserveClassName?: (string | RegExp)[]
1511
// ignorePrefix?: string[]
1612
// ignorePrefixRegExp?: string
17-
classGenerator?: (original: string, opts: IMangleOptions, context: Record<string, any>) => string | undefined
13+
customGenerate?: (original: string, opts: IMangleOptions, context: Record<string, any>) => string | undefined
1814
log?: boolean
1915
exclude?: (string | RegExp)[]
2016
include?: (string | RegExp)[]
@@ -34,3 +30,7 @@ export interface IHandlerOptions {
3430
runtimeSet: Set<string>
3531
classGenerator: ClassGenerator
3632
}
33+
34+
export interface Options {
35+
classGenerator?: IMangleOptions
36+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('classGenerator', () => {
55
const result: string[] = []
66
const clsGen = new ClassGenerator()
77
for (let i = 0; i < 26 * 27 + 1; i++) {
8-
result.push(clsGen.defaultClassGenerator())
8+
result.push(clsGen.defaultClassGenerate())
99
clsGen.newClassSize++
1010
}
1111
expect(result).toMatchSnapshot()
@@ -14,6 +14,6 @@ describe('classGenerator', () => {
1414
it('26*27+1', () => {
1515
const clsGen = new ClassGenerator()
1616
clsGen.newClassSize = 26 * 27
17-
expect(clsGen.defaultClassGenerator()).toBe('tw-aaa')
17+
expect(clsGen.defaultClassGenerate()).toBe('tw-aaa')
1818
})
1919
})

0 commit comments

Comments
 (0)