Skip to content

Commit ee92caf

Browse files
committed
chore: add preProcessRawCode method
1 parent 553ef70 commit ee92caf

File tree

19 files changed

+429
-108
lines changed

19 files changed

+429
-108
lines changed

apps/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@tailwindcss-mangle/config": "link:../packages/config",
1616
"@tailwindcss-mangle/core": "link:../packages/core",
1717
"@tailwindcss-mangle/shared": "link:../packages/shared",
18+
"tailwind-merge": "^1.14.0",
1819
"tailwindcss-patch": "link:../packages/tailwindcss-patch",
1920
"unplugin-tailwindcss-mangle": "link:../packages/unplugin-tailwindcss-mangle"
2021
}

apps/pnpm-lock.yaml

+54-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/vite-vue/.tw-patch/tw-class-list.json

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"before:rounded-full",
2525
"before:to-transparent",
2626
"before:w-[480px]",
27+
"bg-[#B91C1C]",
2728
"bg-gradient-to-b",
2829
"bg-gradient-to-t",
2930
"bg-red-200",
@@ -91,12 +92,15 @@
9192
"motion-reduce:transform-none",
9293
"opacity-50",
9394
"p-24",
95+
"p-3",
9496
"p-8",
9597
"pb-6",
9698
"place-items-center",
9799
"pointer-events-none",
98100
"pt-8",
101+
"px-2",
99102
"px-5",
103+
"py-1",
100104
"py-4",
101105
"relative",
102106
"rounded-lg",

apps/vite-vue/src/App.vue

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<script setup lang="ts">
2+
import { twMerge } from 'tailwind-merge'
3+
4+
const aaa = twMerge('px-2 py-1 bg-red hover:bg-dark-red', 'p-3 bg-[#B91C1C]')
25
36
</script>
47

58
<template>
69
<main class="flex min-h-screen flex-col items-center justify-between p-24 ">
10+
<nav :class="aaa">aaa</nav>
711
<div class="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
812
<p
913
class="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { defineConfig } from 'tailwindcss-patch'
22

3-
export default defineConfig({})
3+
export default defineConfig({
4+
mangle: {
5+
preserveFunction: ['twMerge']
6+
}
7+
})

packages/core/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
"@ast-core/escape": "^1.0.1",
4646
"@babel/core": "^7.22.17",
4747
"@babel/helper-plugin-utils": "^7.22.5",
48+
"@babel/parser": "^7.22.16",
4849
"@babel/preset-typescript": "^7.22.15",
50+
"@babel/traverse": "^7.22.17",
4951
"@babel/types": "^7.22.17",
5052
"@tailwindcss-mangle/config": "workspace:^",
5153
"@tailwindcss-mangle/shared": "workspace:^",
@@ -60,6 +62,7 @@
6062
"devDependencies": {
6163
"@parse5/tools": "^0.3.0",
6264
"@types/babel__core": "^7.20.1",
65+
"@types/babel__traverse": "^7.20.1",
6366
"@types/micromatch": "^4.0.2"
6467
},
6568
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",

packages/core/src/css/plugins.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type PostcssMangleTailwindcssPlugin = PluginCreator<ICssHandlerOptions>
66

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

9+
const clonedKey = '__tw_mangle_cloned__'
10+
911
export function isVueScoped(s: parser.ClassName): boolean {
1012
if (s.parent) {
1113
const index = s.parent.nodes.indexOf(s)
@@ -19,17 +21,33 @@ export function isVueScoped(s: parser.ClassName): boolean {
1921
return false
2022
}
2123

22-
export const transformSelectorPostcssPlugin: PluginCreator<{
23-
replaceMap: Map<string, string>
24-
}> = function (options) {
25-
const { ignoreVueScoped, replaceMap } = defu(options, {
24+
export const transformSelectorPostcssPlugin: PluginCreator<ICssHandlerOptions> = function (options) {
25+
const { ignoreVueScoped, replaceMap, ctx } = defu(options, {
2626
ignoreVueScoped: true
2727
})
2828

29+
const utilitiesSet = new Set<string>()
30+
2931
return {
3032
postcssPlugin,
3133
async Rule(rule) {
34+
// @ts-ignore
35+
if (rule[clonedKey]) {
36+
return
37+
}
3238
await parser((selectors) => {
39+
let flag = false
40+
selectors.walkClasses((s) => {
41+
if (ctx.isPreserveClass(s.value) && !utilitiesSet.has(rule.selector)) {
42+
flag = true
43+
}
44+
})
45+
if (flag) {
46+
const r = rule.cloneBefore()
47+
// @ts-ignore
48+
r[clonedKey] = true
49+
}
50+
3351
selectors.walkClasses((s) => {
3452
if (s.value && replaceMap && replaceMap.has(s.value)) {
3553
if (ignoreVueScoped && isVueScoped(s)) {

packages/core/src/ctx/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import type { MangleUserConfig } from '@tailwindcss-mangle/config'
66
import { sort } from 'fast-sort'
77
import defu from 'defu'
88
import AhoCorasick from 'modern-ahocorasick'
9-
import { createGlobMatcher, defaultMangleClassFilter } from '@/utils'
9+
import { createGlobMatcher, defaultMangleClassFilter, escapeStringRegexp } from '@/utils'
10+
1011
export class Context {
1112
options: MangleUserConfig
1213
includeMatcher: (file: string) => boolean
@@ -18,6 +19,7 @@ export class Context {
1819
useAC: boolean
1920
preserveFunctionSet: Set<string>
2021
preserveClassNamesSet: Set<string>
22+
preserveFunctionRegexs: RegExp[]
2123
constructor(opts: MangleUserConfig = {}) {
2224
this.options = opts // defu(opts, getDefaultMangleUserConfig())
2325
this.classSet = new Set()
@@ -28,6 +30,9 @@ export class Context {
2830
this.useAC = false
2931
this.preserveFunctionSet = new Set(opts.preserveFunction)
3032
this.preserveClassNamesSet = new Set()
33+
this.preserveFunctionRegexs = [...this.preserveFunctionSet.values()].map((x) => {
34+
return new RegExp(escapeStringRegexp(x) + '\\(([^)]*)\\)', 'g')
35+
})
3136
}
3237

3338
isPreserveClass(className: string) {
@@ -139,4 +144,6 @@ export class Context {
139144
}
140145
return config
141146
}
147+
148+
// ["clsx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"]
142149
}

packages/core/src/js/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { transformSync, type BabelFileResult, type NodePath } from '@babel/core'
33
import type { IJsHandlerOptions } from '../types'
44
import { makeRegex, splitCode } from '../shared'
55
import { isProd as isProduction } from '../env'
6-
export { preProcessJs } from './pre'
6+
export { preProcessJs, preProcessRawCode } from './pre'
7+
78
export function handleValue(raw: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions) {
89
const { replaceMap, ctx, splitQuote = true } = options
910
const clsGen = ctx.classGenerator

0 commit comments

Comments
 (0)