Skip to content

Commit 84f2402

Browse files
committed
feat(core): remove replaceMap options
1 parent 738bd9e commit 84f2402

File tree

12 files changed

+114
-122
lines changed

12 files changed

+114
-122
lines changed

packages/core/package.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,32 @@
22
"name": "@tailwindcss-mangle/core",
33
"version": "2.2.2",
44
"description": "The core of tailwindcss-mangle",
5+
"author": "SonOfMagic <[email protected]>",
6+
"license": "MIT",
7+
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/sonofmagic/tailwindcss-mangle.git"
11+
},
12+
"bugs": {
13+
"url": "https://github.com/sonofmagic/tailwindcss-mangle/issues"
14+
},
15+
"keywords": [
16+
"tailwindcss",
17+
"patch",
18+
"core",
19+
"mangle"
20+
],
521
"exports": {
622
".": {
723
"types": "./dist/index.d.ts",
824
"import": "./dist/index.mjs",
925
"require": "./dist/index.cjs"
1026
}
1127
},
28+
"main": "./dist/index.cjs",
29+
"module": "./dist/index.mjs",
30+
"types": "./dist/index.d.ts",
1231
"typesVersions": {
1332
"*": {
1433
"*": [
@@ -17,9 +36,6 @@
1736
]
1837
}
1938
},
20-
"main": "./dist/index.cjs",
21-
"module": "./dist/index.mjs",
22-
"types": "./dist/index.d.ts",
2339
"files": [
2440
"dist"
2541
],
@@ -30,14 +46,6 @@
3046
"test:dev": "vitest",
3147
"coverage": "vitest run --coverage"
3248
},
33-
"keywords": [
34-
"tailwindcss",
35-
"patch",
36-
"core",
37-
"mangle"
38-
],
39-
"author": "SonOfMagic <[email protected]>",
40-
"license": "MIT",
4149
"publishConfig": {
4250
"access": "public",
4351
"registry": "https://registry.npmjs.org/"
@@ -66,15 +74,7 @@
6674
"@vue/compiler-core": "^3.4.29",
6775
"@vue/compiler-sfc": "^3.4.27"
6876
},
69-
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
70-
"repository": {
71-
"type": "git",
72-
"url": "git+https://github.com/sonofmagic/tailwindcss-mangle.git"
73-
},
74-
"bugs": {
75-
"url": "https://github.com/sonofmagic/tailwindcss-mangle/issues"
76-
},
7777
"directories": {
7878
"test": "test"
7979
}
80-
}
80+
}

packages/core/src/css/plugins.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ export function isVueScoped(s: parser.ClassName): boolean {
2323
}
2424

2525
export const transformSelectorPostcssPlugin: PluginCreator<ICssHandlerOptions> = function (options) {
26-
const { ignoreVueScoped, replaceMap, ctx } = defu(options, {
26+
const { ignoreVueScoped, ctx } = defu(options, {
2727
ignoreVueScoped: true,
2828
})
29+
const replaceMap = ctx.replaceMap
2930

3031
return {
3132
postcssPlugin,

packages/core/src/ctx/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs'
22
import { isAbsolute, resolve } from 'node:path'
3+
import process from 'node:process'
34
import { ClassGenerator } from '@tailwindcss-mangle/shared'
45
import { getConfig } from '@tailwindcss-mangle/config'
56
import type { MangleUserConfig } from '@tailwindcss-mangle/config'
@@ -17,7 +18,7 @@ export class Context {
1718
options: MangleUserConfig
1819
private includeMatcher: (file: string) => boolean
1920
private excludeMatcher: (file: string) => boolean
20-
private replaceMap: Map<string, string>
21+
public replaceMap: Map<string, string>
2122
classSet: Set<string>
2223

2324
classGenerator: ClassGenerator

packages/core/src/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
import process from 'node:process'
2+
13
export const isProd = () => process.env.NODE_ENV === 'production'
24
export const isDev = () => process.env.NODE_ENV === 'development'

packages/core/src/html/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import type { IHtmlHandlerOptions } from '../types'
44
import { makeRegex, splitCode } from '../shared'
55
// const { traverse } = await import('@parse5/tools')
66
export function htmlHandler(rawSource: string, options: IHtmlHandlerOptions) {
7-
const { replaceMap, ctx } = options
7+
const { ctx } = options
8+
const { replaceMap } = ctx
9+
810
const fragment = parse(rawSource)
911
traverse(fragment, {
1012
element(node) {

packages/core/src/js/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { isProd as isProduction } from '../env'
77
export { preProcessJs, preProcessRawCode } from './pre'
88

99
export function handleValue(raw: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions) {
10-
const { replaceMap, ctx, splitQuote = true } = options
11-
const clsGen = ctx.classGenerator
10+
const { ctx, splitQuote = true } = options
11+
const { replaceMap, classGenerator: clsGen } = ctx
12+
1213
const array = splitCode(raw, {
1314
splitQuote,
1415
})

packages/core/src/js/pre.ts

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import type { ParseResult } from '@babel/parser'
88
import { escapeStringRegexp } from '@/utils'
99
import type { Context } from '@/ctx'
1010
import { between } from '@/math'
11+
import type { IPreProcessJsOptions } from '@/types'
1112

1213
interface Options {
13-
replaceMap: Map<string, string>
1414
magicString: MagicString
1515
id: string
1616
ctx: Context
@@ -25,7 +25,8 @@ type HandleValueOptions = {
2525
} & Options
2626

2727
export function handleValue(options: HandleValueOptions) {
28-
const { ctx, id, path, magicString, raw, replaceMap, offset = 0, escape = false, markedArray } = options
28+
const { ctx, id, path, magicString, raw, offset = 0, escape = false, markedArray } = options
29+
const { replaceMap } = ctx
2930
const node = path.node
3031
let value = raw
3132
// why 字符串字面量只要开始和结束 在 方法节点内就保留, 另外不可能出现 字符串字面量 开始和结束的下标整个包括 方法体,这是不可能出现的事情
@@ -60,54 +61,41 @@ export function handleValue(options: HandleValueOptions) {
6061

6162
export const JsPlugin = declare((api, options: Options) => {
6263
api.assertVersion(7)
63-
const { magicString, replaceMap, id, ctx, markedArray } = options
64+
const { magicString, id, ctx, markedArray } = options
6465
return {
6566
visitor: {
6667
StringLiteral: {
6768
exit(p) {
68-
const opts: HandleValueOptions = {
69+
handleValue({
6970
ctx,
7071
id,
7172
magicString,
7273
path: p,
7374
raw: p.node.value,
74-
replaceMap,
7575
offset: 1,
7676
escape: true,
7777
markedArray,
78-
}
79-
80-
handleValue(opts)
78+
})
8179
},
8280
},
8381
TemplateElement: {
8482
exit(p) {
85-
const opts: HandleValueOptions = {
83+
handleValue({
8684
ctx,
8785
id,
8886
magicString,
8987
path: p,
9088
raw: p.node.value.raw,
91-
replaceMap,
9289
offset: 0,
9390
escape: false,
9491
markedArray,
95-
}
96-
97-
handleValue(opts)
92+
})
9893
},
9994
},
10095
},
10196
}
10297
})
10398

104-
interface IPreProcessJsOptions {
105-
code: string | MagicString
106-
replaceMap: Map<string, string>
107-
id: string
108-
ctx: Context
109-
}
110-
11199
function transformSync(ast: babel.types.Node, code: string, plugins: babel.PluginItem[] | null | undefined, filename: string | null | undefined) {
112100
babel.transformFromAstSync(ast, code, {
113101
presets: loadPresets(),
@@ -129,7 +117,8 @@ export function loadPresets() {
129117
}
130118

131119
export function preProcessJs(options: IPreProcessJsOptions): string {
132-
const { code, replaceMap, id, ctx } = options
120+
const { code, id, ctx } = options
121+
const { replaceMap } = ctx
133122
const magicString = typeof code === 'string' ? new MagicString(code) : code
134123
let ast: ParseResult<babel.types.File>
135124
try {
@@ -211,15 +200,9 @@ export function preProcessJs(options: IPreProcessJsOptions): string {
211200
return magicString.toString()
212201
}
213202

214-
interface IPreProcessRawCodeOptions {
215-
code: string | MagicString
216-
replaceMap: Map<string, string>
217-
id: string
218-
ctx: Context
219-
}
220-
221-
export function preProcessRawCode(options: IPreProcessRawCodeOptions): string {
222-
const { code, replaceMap, ctx } = options
203+
export function preProcessRawCode(options: IPreProcessJsOptions): string {
204+
const { code, ctx } = options
205+
const { replaceMap } = ctx
223206
const magicString = typeof code === 'string' ? new MagicString(code) : code
224207
const markArr: [number, number][] = []
225208
for (const regex of ctx.preserveFunctionRegexs) {

packages/core/src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type MagicString from 'magic-string'
12
import type { Context } from './ctx'
23

34
export interface IClassGeneratorContextItem {
@@ -16,7 +17,6 @@ export interface IClassGeneratorOptions {
1617
}
1718

1819
export interface IHandlerOptions {
19-
replaceMap: Map<string, string>
2020
ctx: Context
2121
}
2222

@@ -32,3 +32,9 @@ export interface ICssHandlerOptions extends IHandlerOptions {
3232
ignoreVueScoped?: boolean
3333
file?: string
3434
}
35+
36+
export interface IPreProcessJsOptions extends IHandlerOptions {
37+
code: string | MagicString
38+
id: string
39+
40+
}

packages/core/src/vue/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { parse } from '@vue/compiler-sfc'
2+
3+
export function vueHandler(raw: string) {
4+
parse(raw)
5+
}

packages/core/test/css.test.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ describe('css', () => {
1010
})
1111

1212
it('preserveClassNamesSet case 0', async () => {
13-
const replaceMap = new Map()
13+
const replaceMap = ctx.replaceMap
1414
replaceMap.set('gap-y-4', 'tw-a')
1515
ctx.classGenerator.generateClassName('gap-y-4')
1616
const testCase = `.gap-y-4 {color:red;}`
1717
ctx.addPreserveClass('gap-y-4')
1818
const { css } = await cssHandler(testCase, {
1919
ctx,
20-
replaceMap,
2120
})
2221
expect(css).toMatchSnapshot()
2322
})
@@ -30,13 +29,12 @@ describe('css', () => {
3029
ctx.addPreserveClass('gap-y-4')
3130
const { css } = await cssHandler(testCase, {
3231
ctx,
33-
replaceMap: ctx.getReplaceMap(),
3432
})
3533
expect(css).toMatchSnapshot()
3634
})
3735

3836
it('vue scoped .gap-y-4', async () => {
39-
const replaceMap = new Map()
37+
const replaceMap = ctx.replaceMap
4038
replaceMap.set('gap-y-4', 'tw-a')
4139
ctx.classGenerator.generateClassName('gap-y-4')
4240
const testCase = `@media (min-width: 768px) {
@@ -46,13 +44,12 @@ describe('css', () => {
4644

4745
const { css } = await cssHandler(testCase, {
4846
ctx,
49-
replaceMap,
5047
})
5148
expect(css).toMatchSnapshot()
5249
})
5350

5451
it('vue scoped .gap-y-4[data-v-0f84999b]', async () => {
55-
const replaceMap = new Map()
52+
const replaceMap = ctx.replaceMap
5653
replaceMap.set('gap-y-4', 'tw-a')
5754
ctx.classGenerator.generateClassName('gap-y-4')
5855
const testCase = `@media (min-width: 768px) {
@@ -62,13 +59,12 @@ describe('css', () => {
6259

6360
const { css } = await cssHandler(testCase, {
6461
ctx,
65-
replaceMap,
6662
})
6763
expect(css).toMatchSnapshot()
6864
})
6965

7066
it('vue scoped no ignore .gap-y-4[data-v-0f84999b]', async () => {
71-
const replaceMap = new Map()
67+
const replaceMap = ctx.replaceMap
7268
replaceMap.set('gap-y-4', 'tw-a')
7369
ctx.classGenerator.generateClassName('gap-y-4')
7470
const testCase = `@media (min-width: 768px) {
@@ -78,14 +74,13 @@ describe('css', () => {
7874

7975
const { css } = await cssHandler(testCase, {
8076
ctx,
81-
replaceMap,
8277
ignoreVueScoped: false,
8378
})
8479
expect(css).toMatchSnapshot()
8580
})
8681

8782
it('common with scoped', async () => {
88-
const replaceMap = new Map()
83+
const replaceMap = ctx.replaceMap
8984
replaceMap.set('bg-white', 'tw-a')
9085
ctx.classGenerator.generateClassName('bg-white')
9186
const testCase = `
@@ -96,21 +91,19 @@ describe('css', () => {
9691

9792
const { css } = await cssHandler(testCase, {
9893
ctx,
99-
replaceMap,
10094
})
10195
expect(css).toMatchSnapshot()
10296
})
10397

10498
it('vue.scoped.css', async () => {
10599
const list = JSON.parse(getTestCase('nuxt-app-partial-class-set.json'))
106-
const replaceMap: Map<string, any> = new Map()
100+
const replaceMap: Map<string, any> = ctx.replaceMap
107101
for (const cls of list) {
108102
replaceMap.set(cls, ctx.classGenerator.generateClassName(cls).name)
109103
}
110104
const testCase = getTestCase('vue.scoped.css')
111105
const { css } = await cssHandler(testCase, {
112106
ctx,
113-
replaceMap,
114107
})
115108
expect(css).toMatchSnapshot()
116109
})

0 commit comments

Comments
 (0)