Skip to content

Commit 0bbb4ac

Browse files
committed
chore: move ctx to core
1 parent 818c670 commit 0bbb4ac

File tree

18 files changed

+114
-133
lines changed

18 files changed

+114
-133
lines changed

packages/config/src/defaults.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function getDefaultMangleUserConfig(): MangleUserConfig {
2323
enable: false,
2424
filename: '.tw-patch/tw-map-list.json',
2525
loose: true
26-
}
26+
},
27+
preserveFunction: []
2728
}
2829
}
2930

packages/config/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface MangleUserConfig {
1414
classListPath?: string
1515
classMapOutput?: ClassMapOutputOptions
1616
disabled?: boolean
17+
preserveFunction?: string[]
1718
}
1819

1920
export interface PatchUserConfig {

packages/config/test/__snapshots__/defaults.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ exports[`defaults > getDefaultUserConfig 1`] = `
2929
"**/*.{js,jsx,ts,tsx,svelte,vue}",
3030
],
3131
"mangleClassFilter": [Function],
32+
"preserveFunction": [],
3233
},
3334
"patch": {
3435
"output": {

packages/config/test/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ exports[`config > 2.mangle-options 1`] = `
2121
"**/*.{js,jsx,ts,tsx,svelte,vue}",
2222
],
2323
"mangleClassFilter": [Function],
24+
"preserveFunction": [],
2425
},
2526
"patch": {
2627
"output": {

packages/core/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,20 @@
4747
"@babel/helper-plugin-utils": "^7.22.5",
4848
"@babel/preset-typescript": "^7.22.15",
4949
"@babel/types": "^7.22.17",
50+
"@tailwindcss-mangle/config": "workspace:^",
5051
"@tailwindcss-mangle/shared": "workspace:^",
52+
"fast-sort": "^3.4.0",
5153
"magic-string": "^0.30.3",
54+
"micromatch": "^4.0.5",
55+
"modern-ahocorasick": "^1.0.0",
5256
"parse5": "^7.1.2",
5357
"postcss": "^8.4.29",
5458
"postcss-selector-parser": "^6.0.13"
5559
},
5660
"devDependencies": {
5761
"@parse5/tools": "^0.3.0",
58-
"@types/babel__core": "^7.20.1"
62+
"@types/babel__core": "^7.20.1",
63+
"@types/micromatch": "^4.0.2"
5964
},
6065
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
6166
"repository": {

packages/unplugin-tailwindcss-mangle/src/core/context.ts renamed to packages/core/src/ctx/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export class Context {
1616
classGenerator: ClassGenerator
1717
ahoCorasick?: AhoCorasick
1818
useAC: boolean
19+
preserveFunctionSet: Set<string>
20+
preserveClassNamesSet: Set<string>
1921
constructor(opts: MangleUserConfig = {}) {
2022
this.options = opts // defu(opts, getDefaultMangleUserConfig())
2123
this.classSet = new Set()
@@ -24,6 +26,16 @@ export class Context {
2426
this.excludeMatcher = createGlobMatcher(this.options.exclude, false)
2527
this.classGenerator = new ClassGenerator(this.options.classGenerator)
2628
this.useAC = false
29+
this.preserveFunctionSet = new Set(opts.preserveFunction)
30+
this.preserveClassNamesSet = new Set()
31+
}
32+
33+
isPreserveClass(className: string) {
34+
return this.preserveClassNamesSet.has(className)
35+
}
36+
37+
addPreserveClass(className: string) {
38+
return this.preserveClassNamesSet.add(className)
2739
}
2840

2941
mergeOptions(opts?: MangleUserConfig) {
@@ -47,7 +59,13 @@ export class Context {
4759
}
4860

4961
getReplaceMap() {
50-
return this.replaceMap
62+
const map = new Map<string, string>()
63+
for (const [key, value] of this.replaceMap) {
64+
if (!this.isPreserveClass(key)) {
65+
map.set(key, value)
66+
}
67+
}
68+
return map
5169
}
5270

5371
addToUsedBy(key: string, file: string) {
@@ -93,7 +111,7 @@ export class Context {
93111
const rawClassList = fs.readFileSync(jsonPath, 'utf8')
94112
const list = JSON.parse(rawClassList) as string[]
95113
// why?
96-
// case bg-red-500 and bg-red-500/50
114+
// cause bg-red-500 and bg-red-500/50 same time
97115
// transform bg-red-500/50 first
98116
const classList = sort(list).desc((c) => c.length)
99117
for (const className of classList) {

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './ctx'
12
export * from './css'
23
export * from './html'
34
export * from './js'

packages/core/src/js/pre.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ import MagicString from 'magic-string'
44
import { splitCode } from '@tailwindcss-mangle/shared'
55
import { sort } from 'fast-sort'
66
import { jsStringEscape } from '@ast-core/escape'
7+
import type { Context } from '@/ctx'
78

89
interface Options {
910
replaceMap: Map<string, string>
1011
magicString: MagicString
1112
id: string
12-
addToUsedBy: (key: string, file: string) => void
13+
ctx: Context
1314
}
1415

1516
export function handleValue(options: { raw: string; node: babel.types.StringLiteral | babel.types.TemplateElement; offset: number; escape: boolean } & Options) {
16-
const { addToUsedBy, id, magicString, node, raw, replaceMap, offset = 0, escape = false } = options
17+
const { ctx, id, magicString, node, raw, replaceMap, offset = 0, escape = false } = options
1718
let value = raw
1819
const arr = sort(splitCode(value)).desc((x) => x.length)
1920

2021
for (const str of arr) {
2122
if (replaceMap.has(str)) {
22-
addToUsedBy(str, id)
23+
ctx.addToUsedBy(str, id)
2324
const v = replaceMap.get(str)
2425
if (v) {
2526
value = value.replaceAll(str, v)
@@ -37,14 +38,14 @@ export function handleValue(options: { raw: string; node: babel.types.StringLite
3738

3839
export const plugin = declare((api, options: Options) => {
3940
api.assertVersion(7)
40-
const { magicString, replaceMap, id, addToUsedBy } = options
41+
const { magicString, replaceMap, id, ctx } = options
4142
return {
4243
visitor: {
4344
StringLiteral: {
4445
enter(p) {
4546
const node = p.node
4647
handleValue({
47-
addToUsedBy,
48+
ctx,
4849
id,
4950
magicString,
5051
node,
@@ -59,7 +60,7 @@ export const plugin = declare((api, options: Options) => {
5960
enter(p) {
6061
const node = p.node
6162
handleValue({
62-
addToUsedBy,
63+
ctx,
6364
id,
6465
magicString,
6566
node,
@@ -74,8 +75,8 @@ export const plugin = declare((api, options: Options) => {
7475
}
7576
})
7677

77-
export function preProcessJs(options: { code: string | MagicString; replaceMap: Map<string, string>; id: string; addToUsedBy: (key: string, file: string) => void }) {
78-
const { code, replaceMap, id, addToUsedBy } = options
78+
export function preProcessJs(options: { code: string | MagicString; replaceMap: Map<string, string>; id: string; ctx: Context }) {
79+
const { code, replaceMap, id, ctx } = options
7980
const magicString = typeof code === 'string' ? new MagicString(code) : code
8081

8182
babel.transformSync(magicString.original, {
@@ -96,7 +97,7 @@ export function preProcessJs(options: { code: string | MagicString; replaceMap:
9697
magicString,
9798
replaceMap,
9899
id,
99-
addToUsedBy
100+
ctx
100101
}
101102
]
102103
],

packages/core/src/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import micromatch from 'micromatch'
2+
3+
const { isMatch } = micromatch
4+
5+
export function createGlobMatcher(pattern: string | string[] | undefined, fallbackValue: boolean = false) {
6+
if (pattern === undefined) {
7+
return function () {
8+
return fallbackValue
9+
}
10+
}
11+
return function (file: string) {
12+
return isMatch(file, pattern)
13+
}
14+
}
15+
16+
export { defaultMangleClassFilter, isMap, isRegexp } from '@tailwindcss-mangle/shared'

packages/core/test/js.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ describe('js handler', () => {
216216
// replaceMap.set('bg-red-500', true)
217217
const code = preProcessJs({
218218
code: testCase,
219-
addToUsedBy: () => {},
219+
// @ts-ignore
220+
ctx: {
221+
addToUsedBy: () => {}
222+
},
220223
id: 'xxx',
221224
replaceMap
222225
})
@@ -230,7 +233,10 @@ describe('js handler', () => {
230233
replaceMap.set('bg-red-500', 'b')
231234
const code = preProcessJs({
232235
code: testCase,
233-
addToUsedBy: () => {},
236+
// @ts-ignore
237+
ctx: {
238+
addToUsedBy: () => {}
239+
},
234240
id: 'xxx',
235241
replaceMap
236242
})
@@ -244,7 +250,10 @@ describe('js handler', () => {
244250
replaceMap.set('bg-red-500', 'b')
245251
const code = preProcessJs({
246252
code: testCase,
247-
addToUsedBy: () => {},
253+
// @ts-ignore
254+
ctx: {
255+
addToUsedBy: () => {}
256+
},
248257
id: 'xxx',
249258
replaceMap
250259
})

0 commit comments

Comments
 (0)