Skip to content

Commit 2bcabb5

Browse files
committed
feat: rewrite jsHandler
1 parent 117d7ad commit 2bcabb5

File tree

15 files changed

+157
-134
lines changed

15 files changed

+157
-134
lines changed

apps/solid-app/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"tailwindcss-patch": "workspace:*",
1919
"typescript": "^5.0.4",
2020
"unplugin-tailwindcss-mangle": "workspace:*",
21-
"vite": "^4.3.2",
21+
"vite": "^4.3.3",
2222
"vite-plugin-solid": "^2.5.0"
2323
},
2424
"dependencies": {

apps/vite-lit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
"tailwindcss-patch": "workspace:*",
2828
"typescript": "^5.0.4",
2929
"unplugin-tailwindcss-mangle": "workspace:*",
30-
"vite": "^4.3.2"
30+
"vite": "^4.3.3"
3131
}
3232
}

apps/vite-react/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"tailwindcss-patch": "workspace:*",
2424
"typescript": "^5.0.4",
2525
"unplugin-tailwindcss-mangle": "workspace:*",
26-
"vite": "^4.3.2"
26+
"vite": "^4.3.3"
2727
}
2828
}

apps/vite-svelte/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
"tslib": "^2.5.0",
2222
"typescript": "^5.0.4",
2323
"unplugin-tailwindcss-mangle": "workspace:*",
24-
"vite": "^4.3.2"
24+
"vite": "^4.3.3"
2525
}
2626
}

apps/vite-vanilla/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"tailwindcss-patch": "workspace:*",
1717
"typescript": "^5.0.4",
1818
"unplugin-tailwindcss-mangle": "workspace:*",
19-
"vite": "^4.3.2"
19+
"vite": "^4.3.3"
2020
}
2121
}

apps/vite-vue/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"tailwindcss-patch": "workspace:*",
2121
"typescript": "^5.0.4",
2222
"unplugin-tailwindcss-mangle": "workspace:*",
23-
"vite": "^4.3.2",
24-
"vue-tsc": "^1.4.4"
23+
"vite": "^4.3.3",
24+
"vue-tsc": "^1.6.0"
2525
}
2626
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"devDependencies": {
2020
"@icebreakers/eslint-config-ts": "^1.0.4",
21-
"@icebreakers/rollup": "^0.3.0",
21+
"@icebreakers/rollup": "^0.4.0",
2222
"@icebreakers/tsconfig": "^0.0.6",
2323
"@tsconfig/recommended": "^1.0.2",
2424
"@types/jest": "^29.5.1",

packages/tailwindcss-patch/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"devDependencies": {
3737
"@types/babel__generator": "^7.6.4",
38-
"@types/babel__traverse": "^7.18.4",
38+
"@types/babel__traverse": "^7.18.5",
3939
"@types/resolve": "^1.20.2",
4040
"@types/semver": "^7.3.13",
4141
"defu": "^6.1.2",

packages/unplugin-tailwindcss-mangle/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ export interface IClassGeneratorOptions {
116116

117117
### include / exclude
118118

119-
type: glob string
119+
Type: `string | string[]`
120120

121-
allow you to control the mangle range of bundles.
121+
Default: `undefined`
122+
123+
`glob string` allow you to control the mangle range of bundles.
122124

123125
## Notice
124126

packages/unplugin-tailwindcss-mangle/package.json

+4-7
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@
6969
"author": "SonOfMagic <qq1324318532@gmail.com>",
7070
"license": "MIT",
7171
"dependencies": {
72-
"@babel/generator": "^7.21.4",
73-
"@babel/parser": "^7.21.4",
74-
"@babel/traverse": "^7.21.4",
72+
"@babel/core": "^7.21.4",
7573
"@babel/types": "^7.21.4",
7674
"micromatch": "^4.0.5",
7775
"parse5": "^7.1.2",
@@ -87,16 +85,15 @@
8785
},
8886
"devDependencies": {
8987
"@parse5/tools": "^0.1.0",
90-
"@types/babel__generator": "^7.6.4",
91-
"@types/babel__traverse": "^7.18.4",
88+
"@types/babel__core": "^7.20.0",
9289
"@types/escodegen": "^0.0.7",
9390
"@types/micromatch": "^4.0.2",
9491
"@types/semver": "^7.3.13",
9592
"pkg-types": "^1.0.2",
9693
"tailwindcss": "^3.3.2",
9794
"tslib": "^2.5.0",
98-
"vite": "^4.3.2",
99-
"webpack": "^5.80.0"
95+
"vite": "^4.3.3",
96+
"webpack": "^5.81.0"
10097
},
10198
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
10299
"repository": {
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import _generate from '@babel/generator'
2-
import { parse } from '@babel/parser'
3-
import _traverse from '@babel/traverse'
4-
5-
import type { Node, StringLiteral, TemplateElement } from '@babel/types'
6-
import type { TraverseOptions, IHandlerOptions } from '../types'
1+
import type { StringLiteral, TemplateElement } from '@babel/types'
2+
import { transformSync, type BabelFileResult, type NodePath } from '@babel/core'
3+
import type { IHandlerOptions } from '../types'
74
import { escapeStringRegexp } from '../utils'
85
import { splitCode } from './split'
96

10-
function getDefaultExportFromNamespaceIfPresent(n: any) {
11-
return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n.default : n
12-
}
13-
const generate = getDefaultExportFromNamespaceIfPresent(_generate) as typeof _generate
14-
const traverse = getDefaultExportFromNamespaceIfPresent(_traverse) as typeof _traverse
15-
167
export function makeRegex(str: string) {
178
return new RegExp('(?<=^|[\\s"])' + escapeStringRegexp(str), 'g')
189
}
@@ -39,25 +30,33 @@ export function handleValue(str: string, node: StringLiteral | TemplateElement,
3930
}
4031

4132
export function jsHandler(rawSource: string, options: IHandlerOptions) {
42-
const ast = parse(rawSource)
43-
44-
const topt: TraverseOptions<Node> = {
45-
StringLiteral: {
46-
enter(p) {
47-
const n = p.node
48-
n.value = handleValue(n.value, n, options)
33+
const result = transformSync(rawSource, {
34+
babelrc: false,
35+
ast: true,
36+
plugins: [
37+
() => {
38+
return {
39+
visitor: {
40+
StringLiteral: {
41+
enter(p: NodePath<StringLiteral>) {
42+
const n = p.node
43+
n.value = handleValue(n.value, n, options)
44+
}
45+
},
46+
TemplateElement: {
47+
enter(p: NodePath<TemplateElement>) {
48+
const n = p.node
49+
n.value.raw = handleValue(n.value.raw, n, options)
50+
}
51+
}
52+
// noScope: true
53+
}
54+
}
4955
}
50-
},
51-
TemplateElement: {
52-
enter(p) {
53-
const n = p.node
54-
n.value.raw = handleValue(n.value.raw, n, options)
55-
}
56-
},
57-
noScope: true
58-
}
59-
60-
traverse(ast, topt)
56+
],
57+
sourceMaps: false,
58+
configFile: false
59+
})
6160

62-
return generate(ast)
61+
return result as BabelFileResult
6362
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ export interface IClassGenerator {
2121
context: Record<string, any>
2222
}
2323

24-
export type { TraverseOptions } from '@babel/traverse'
25-
2624
export interface IHandlerOptions {
2725
runtimeSet: Set<string>
2826
classGenerator: ClassGenerator

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { IClassGeneratorOptions, IClassGenerator } from './types'
2-
import { isMatch } from 'micromatch'
2+
import micromatch from 'micromatch'
3+
const { isMatch } = micromatch
34
export function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]> {
45
if (!Array.isArray(arr)) {
56
throw new Error('expected an array for first argument')

packages/unplugin-tailwindcss-mangle/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4+
"target": "ESNext",
5+
"module": "ESNext",
46
"outDir": "dist",
7+
"moduleResolution":"nodenext",
58
"declarationDir": "dist",
69
"baseUrl": ".",
710
"paths": {

0 commit comments

Comments
 (0)