Skip to content

Commit 6cf745d

Browse files
committed
chore: fix merge options
1 parent 441f8e7 commit 6cf745d

File tree

14 files changed

+470
-317
lines changed

14 files changed

+470
-317
lines changed

packages/config/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dist"
2525
],
2626
"scripts": {
27+
"dev": "unbuild --sourcemap",
2728
"build": "unbuild",
2829
"test": "vitest run --coverage.enabled",
2930
"test:dev": "vitest"

packages/core/package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dist"
2525
],
2626
"scripts": {
27+
"dev": "unbuild --sourcemap",
2728
"build": "unbuild",
2829
"test": "vitest run --coverage.enabled",
2930
"test:dev": "vitest",
@@ -51,11 +52,9 @@
5152
"@babel/types": "^7.22.17",
5253
"@tailwindcss-mangle/config": "workspace:^",
5354
"@tailwindcss-mangle/shared": "workspace:^",
54-
"@vue/compiler-sfc": "^3.3.4",
5555
"fast-sort": "^3.4.0",
5656
"magic-string": "^0.30.3",
5757
"micromatch": "^4.0.5",
58-
"modern-ahocorasick": "^1.0.0",
5958
"parse5": "^7.1.2",
6059
"postcss": "^8.4.29",
6160
"postcss-selector-parser": "^6.0.13"
@@ -65,11 +64,18 @@
6564
"@types/babel__core": "^7.20.1",
6665
"@types/babel__traverse": "^7.20.1",
6766
"@types/micromatch": "^4.0.2",
68-
"@vue/compiler-core": "^3.3.4"
67+
"@vue/compiler-core": "^3.3.4",
68+
"@vue/compiler-sfc": "^3.3.4"
6969
},
7070
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
7171
"repository": {
7272
"type": "git",
7373
"url": "git+https://github.com/sonofmagic/tailwindcss-mangle.git"
74+
},
75+
"bugs": {
76+
"url": "https://github.com/sonofmagic/tailwindcss-mangle/issues"
77+
},
78+
"directories": {
79+
"test": "test"
7480
}
7581
}

packages/core/src/ctx/index.ts

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,36 @@ import { getConfig } from '@tailwindcss-mangle/config'
55
import type { MangleUserConfig } from '@tailwindcss-mangle/config'
66
import { sort } from 'fast-sort'
77
import defu from 'defu'
8-
import AhoCorasick from 'modern-ahocorasick'
98
import { createGlobMatcher, defaultMangleClassFilter, escapeStringRegexp } from '@/utils'
109

10+
interface InitConfigOptions {
11+
cwd?: string
12+
classList?: string[]
13+
mangleOptions?: MangleUserConfig
14+
}
15+
1116
export class Context {
1217
options: MangleUserConfig
13-
includeMatcher: (file: string) => boolean
14-
excludeMatcher: (file: string) => boolean
18+
private includeMatcher: (file: string) => boolean
19+
private excludeMatcher: (file: string) => boolean
20+
private replaceMap: Map<string, string>
1521
classSet: Set<string>
16-
replaceMap: Map<string, string>
22+
1723
classGenerator: ClassGenerator
18-
ahoCorasick?: AhoCorasick
19-
useAC: boolean
24+
2025
preserveFunctionSet: Set<string>
2126
preserveClassNamesSet: Set<string>
2227
preserveFunctionRegexs: RegExp[]
23-
constructor(opts: MangleUserConfig = {}) {
24-
this.options = opts // defu(opts, getDefaultMangleUserConfig())
28+
constructor() {
29+
this.options = {}
2530
this.classSet = new Set()
2631
this.replaceMap = new Map()
27-
this.includeMatcher = createGlobMatcher(this.options.include, true)
28-
this.excludeMatcher = createGlobMatcher(this.options.exclude, false)
29-
this.classGenerator = new ClassGenerator(this.options.classGenerator)
30-
this.useAC = false
31-
this.preserveFunctionSet = new Set(opts.preserveFunction)
32+
this.includeMatcher = () => true
33+
this.excludeMatcher = () => false
34+
this.classGenerator = new ClassGenerator()
35+
this.preserveFunctionSet = new Set()
3236
this.preserveClassNamesSet = new Set()
33-
this.preserveFunctionRegexs = [...this.preserveFunctionSet.values()].map((x) => {
34-
return new RegExp(escapeStringRegexp(x) + '\\(([^)]*)\\)', 'g')
35-
})
37+
this.preserveFunctionRegexs = []
3638
}
3739

3840
isPreserveClass(className: string) {
@@ -50,12 +52,16 @@ export class Context {
5052
return this.preserveFunctionSet.has(calleeName)
5153
}
5254

53-
mergeOptions(opts?: MangleUserConfig) {
55+
private mergeOptions(opts?: MangleUserConfig) {
5456
// 配置选项优先
5557
this.options = defu(this.options, opts)
5658
this.includeMatcher = createGlobMatcher(this.options.include, true)
5759
this.excludeMatcher = createGlobMatcher(this.options.exclude, false)
5860
this.classGenerator = new ClassGenerator(this.options.classGenerator)
61+
this.preserveFunctionSet = new Set(opts?.preserveFunction ?? [])
62+
this.preserveFunctionRegexs = [...this.preserveFunctionSet.values()].map((x) => {
63+
return new RegExp(escapeStringRegexp(x) + '\\(([^)]*)\\)', 'g')
64+
})
5965
}
6066

6167
isInclude(file: string) {
@@ -87,57 +93,42 @@ export class Context {
8793
}
8894
}
8995

90-
search(str: string) {
91-
const arr = this.ahoCorasick?.search(str) ?? []
92-
const map = new Map<string, [number, number][]>()
93-
for (const [end, classNames] of arr) {
94-
for (const className of classNames) {
95-
if (map.has(className)) {
96-
const v = map.get(className)
97-
if (v) {
98-
v.push([end - className.length + 1, end + 1])
99-
}
100-
} else {
101-
map.set(className, [[end - className.length + 1, end + 1]])
102-
}
103-
}
104-
}
105-
// end - str.length + 1, end + 1, value
106-
return {
107-
map,
108-
arr: sort([...map.entries()]).desc((x) => x[0].length)
109-
}
110-
}
111-
112-
async initConfig(cwd?: string) {
96+
async initConfig(opts: InitConfigOptions = {}) {
97+
const { cwd, classList: _classList, mangleOptions } = opts
11398
const { config, cwd: configCwd } = await getConfig(cwd)
114-
const mangleConfig = config?.mangle
99+
const mangleConfig = mangleOptions ?? config?.mangle
115100
this.mergeOptions(mangleConfig)
116-
117-
let jsonPath = this.options.classListPath ?? resolve(process.cwd(), config?.patch?.output?.filename as string)
118-
if (!isAbsolute(jsonPath)) {
119-
jsonPath = resolve(configCwd ?? process.cwd(), jsonPath)
120-
}
121-
122-
if (jsonPath && fs.existsSync(jsonPath)) {
123-
const rawClassList = fs.readFileSync(jsonPath, 'utf8')
124-
const list = JSON.parse(rawClassList) as string[]
125-
// why?
126-
// cause bg-red-500 and bg-red-500/50 same time
127-
// transform bg-red-500/50 first
128-
const classList = sort(list).desc((c) => c.length)
101+
if (_classList) {
102+
const classList = sort(_classList).desc((c) => c.length)
129103
for (const className of classList) {
130104
if (this.currentMangleClassFilter(className)) {
131105
this.classSet.add(className)
132106
}
133107
}
108+
} else {
109+
let jsonPath = this.options.classListPath ?? resolve(process.cwd(), config?.patch?.output?.filename as string)
110+
if (!isAbsolute(jsonPath)) {
111+
jsonPath = resolve(configCwd ?? process.cwd(), jsonPath)
112+
}
113+
114+
if (jsonPath && fs.existsSync(jsonPath)) {
115+
const rawClassList = fs.readFileSync(jsonPath, 'utf8')
116+
const list = JSON.parse(rawClassList) as string[]
117+
// why?
118+
// cause bg-red-500 and bg-red-500/50 same time
119+
// transform bg-red-500/50 first
120+
const classList = sort(list).desc((c) => c.length)
121+
for (const className of classList) {
122+
if (this.currentMangleClassFilter(className)) {
123+
this.classSet.add(className)
124+
}
125+
}
126+
}
134127
}
135-
const keywords: string[] = []
128+
136129
for (const cls of this.classSet) {
137130
this.classGenerator.generateClassName(cls)
138-
keywords.push(cls)
139131
}
140-
this.ahoCorasick = new AhoCorasick(keywords)
141132

142133
for (const x of Object.entries(this.classGenerator.newClassMap)) {
143134
this.replaceMap.set(x[0], x[1].name)

0 commit comments

Comments
 (0)