Skip to content

Commit 66b1482

Browse files
committed
chore: add shared utils deps
1 parent 676cf94 commit 66b1482

File tree

6 files changed

+49
-44
lines changed

6 files changed

+49
-44
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"lint": "pnpm run -r lint",
1717
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
1818
"preinstall": "npx only-allow pnpm",
19+
"release": "bumpp -r",
1920
"sync": "cnpm sync tailwindcss-patch unplugin-tailwindcss-mangle tailwindcss-mangle-core"
2021
},
2122
"devDependencies": {
@@ -25,6 +26,7 @@
2526
"@tsconfig/recommended": "^1.0.2",
2627
"@types/jest": "^29.5.1",
2728
"@types/node": "^20.1.3",
29+
"bumpp": "^9.1.0",
2830
"cross-env": "^7.0.3",
2931
"eslint": "^8.40.0",
3032
"jest": "^29.5.0",

packages/unplugin-tailwindcss-mangle/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
"micromatch": "^4.0.5",
7272
"tailwindcss-mangle-core": "workspace:^",
7373
"tailwindcss-patch": "workspace:^",
74-
"unplugin": "^1.3.1"
74+
"unplugin": "^1.3.1",
75+
"tailwindcss-mangle-shared": "workspace:^"
7576
},
7677
"publishConfig": {
7778
"access": "public",

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { Options, ClassSetOutputOptions, ClassMapOutputOptions } from './types'
22
import { TailwindcssPatcher } from 'tailwindcss-patch'
33
import { ClassGenerator } from 'tailwindcss-mangle-core'
4-
import { createGlobMatcher, isMangleClass, cacheDump } from './utils'
4+
import { createGlobMatcher, defaultMangleClassFilter, cacheDump } from './utils'
55

66
export function getOptions(options: Options | undefined = {}) {
77
const includeMatcher = createGlobMatcher(options.include, true)
88
const excludeMatcher = createGlobMatcher(options.exclude, false)
9-
9+
const currentMangleClassFilter = options.mangleClassFilter ?? defaultMangleClassFilter
1010
function isInclude(file: string) {
1111
return includeMatcher(file) && !excludeMatcher(file)
1212
}
@@ -38,7 +38,7 @@ export function getOptions(options: Options | undefined = {}) {
3838
cacheDump(classSetOutputOptions.filename, set, classSetOutputOptions.dir)
3939
}
4040
set.forEach((c) => {
41-
if (!isMangleClass(c)) {
41+
if (!currentMangleClassFilter(c)) {
4242
set.delete(c)
4343
}
4444
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface ClassMapOutputOptions {
3030
dir?: string
3131
}
3232
export interface Options {
33+
mangleClassFilter?: (className: string) => boolean
3334
classGenerator?: IClassGeneratorOptions
3435
exclude?: string[]
3536
include?: string[]

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

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,10 @@ import fs from 'fs'
33
import path from 'path'
44

55
import { pluginName } from './constants'
6+
import { defaultMangleClassFilter, groupBy, isMap, isRegexp } from 'tailwindcss-mangle-shared'
67
const { isMatch } = micromatch
78

8-
export const isMangleClass = (className: string) => {
9-
// ignore className like 'filter','container'
10-
// it may be dangerous to mangle/rename all StringLiteral , so use /-/ test for only those with /-/ like:
11-
// bg-[#123456] w-1 etc...
12-
return /[-:]/.test(className)
13-
}
14-
15-
export function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]> {
16-
if (!Array.isArray(arr)) {
17-
throw new Error('expected an array for first argument')
18-
}
19-
20-
if (typeof cb !== 'function') {
21-
throw new Error('expected a function for second argument')
22-
}
23-
24-
const result: Record<string, T[]> = {}
25-
for (let i = 0; i < arr.length; i++) {
26-
const item = arr[i]
27-
const bucketCategory = cb(item)
28-
const bucket = result[bucketCategory]
29-
30-
if (!Array.isArray(bucket)) {
31-
result[bucketCategory] = [item]
32-
} else {
33-
result[bucketCategory].push(item)
34-
}
35-
}
36-
37-
return result
38-
}
9+
export { defaultMangleClassFilter, isMap, isRegexp }
3910

4011
export function getGroupedEntries<T>(
4112
entries: [string, T][],
@@ -78,14 +49,6 @@ export function getGroupedEntries<T>(
7849
return groupedEntries as Record<'css' | 'html' | 'js' | 'other', [string, T][]>
7950
}
8051

81-
export function isRegexp(value: unknown) {
82-
return Object.prototype.toString.call(value) === '[object RegExp]'
83-
}
84-
85-
export function isMap(value: unknown) {
86-
return Object.prototype.toString.call(value) === '[object Map]'
87-
}
88-
8952
export function createGlobMatcher(pattern: string | string[] | undefined, fallbackValue: boolean = false) {
9053
if (typeof pattern === 'undefined') {
9154
return function (file: string) {

pnpm-lock.yaml

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)