Skip to content

Commit 58d2971

Browse files
committed
fix(core): jsStringEscape
1 parent eacaddf commit 58d2971

File tree

9 files changed

+370
-103
lines changed

9 files changed

+370
-103
lines changed

apps/next-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@types/node": "20.4.9",
14-
"@types/react": "18.2.19",
14+
"@types/react": "18.2.20",
1515
"@types/react-dom": "18.2.7",
1616
"autoprefixer": "10.4.14",
1717
"eslint": "8.46.0",

apps/remix-app/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
"prepare": "tw-patch install"
1111
},
1212
"dependencies": {
13-
"@remix-run/node": "^1.19.2",
14-
"@remix-run/react": "^1.19.2",
15-
"@remix-run/serve": "^1.19.2",
13+
"@remix-run/node": "^1.19.3",
14+
"@remix-run/react": "^1.19.3",
15+
"@remix-run/serve": "^1.19.3",
1616
"isbot": "^3.6.13",
1717
"react": "^18.2.0",
1818
"react-dom": "^18.2.0"
1919
},
2020
"devDependencies": {
2121
"@remix-run/dev": "^1.19.2",
2222
"@remix-run/eslint-config": "^1.19.2",
23-
"@types/react": "^18.2.19",
23+
"@types/react": "^18.2.20",
2424
"@types/react-dom": "^18.2.7",
2525
"eslint": "^8.46.0",
2626
"tailwindcss": "^3.3.3",

apps/solid-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"vite-plugin-solid": "^2.5.0"
2424
},
2525
"dependencies": {
26-
"solid-js": "^1.7.9"
26+
"solid-js": "^1.7.10"
2727
}
2828
}

apps/vite-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react-dom": "^18.2.0"
1515
},
1616
"devDependencies": {
17-
"@types/react": "^18.2.19",
17+
"@types/react": "^18.2.20",
1818
"@types/react-dom": "^18.2.7",
1919
"@vitejs/plugin-react": "^4.0.4",
2020
"autoprefixer": "^10.4.14",

apps/webpack5-vue3/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"eslint-plugin-import": "^2.28.0",
3030
"eslint-plugin-node": "^11.1.0",
3131
"eslint-plugin-promise": "^6.1.1",
32-
"eslint-plugin-vue": "^9.16.1",
32+
"eslint-plugin-vue": "^9.17.0",
3333
"postcss": "^8.4.27",
34-
"sass": "^1.64.2",
34+
"sass": "^1.65.1",
3535
"sass-loader": "^13.3.2",
3636
"tailwindcss": "^3.3.3",
3737
"tailwindcss-patch": "workspace:*",

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tailwindcss-mangle/core",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"description": "The core of tailwindcss-mangle",
55
"exports": {
66
".": {

packages/core/src/js/pre.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,36 @@ interface Options {
1010
addToUsedBy: (key: string, file: string) => void
1111
}
1212

13-
export function handleValue(options: { raw: string; node: babel.types.StringLiteral | babel.types.TemplateElement; offset: number } & Options) {
14-
const { addToUsedBy, id, magicString, node, raw, replaceMap, offset = 0 } = options
13+
export function jsStringEscape(str: unknown) {
14+
return ('' + str).replaceAll(/[\n\r"'\\\u2028\u2029]/g, (character) => {
15+
switch (character) {
16+
case '"':
17+
case "'":
18+
case '\\': {
19+
return '\\' + character
20+
}
21+
22+
case '\n': {
23+
return '\\n'
24+
}
25+
case '\r': {
26+
return '\\r'
27+
}
28+
case '\u2028': {
29+
return '\\u2028'
30+
}
31+
case '\u2029': {
32+
return '\\u2029'
33+
}
34+
default: {
35+
return character
36+
}
37+
}
38+
})
39+
}
40+
41+
export function handleValue(options: { raw: string; node: babel.types.StringLiteral | babel.types.TemplateElement; offset: number; escape: boolean } & Options) {
42+
const { addToUsedBy, id, magicString, node, raw, replaceMap, offset = 0, escape = false } = options
1543
let value = raw
1644
const arr = sort(splitCode(value)).desc((x) => x.length)
1745

@@ -25,7 +53,7 @@ export function handleValue(options: { raw: string; node: babel.types.StringLite
2553
}
2654
}
2755
if (typeof node.start === 'number' && typeof node.end === 'number' && value) {
28-
magicString.update(node.start + offset, node.end - offset, value)
56+
magicString.update(node.start + offset, node.end - offset, escape ? jsStringEscape(value) : value)
2957
}
3058
}
3159

@@ -44,7 +72,8 @@ export const plugin = declare((api, options: Options) => {
4472
node,
4573
raw: node.value,
4674
replaceMap,
47-
offset: 1
75+
offset: 1,
76+
escape: true
4877
})
4978
}
5079
},
@@ -58,7 +87,8 @@ export const plugin = declare((api, options: Options) => {
5887
node,
5988
raw: node.value.raw,
6089
replaceMap,
61-
offset: 0
90+
offset: 0,
91+
escape: false
6292
})
6393
}
6494
}

packages/core/test/js.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCss, getTestCase } from './utils'
2-
import { jsHandler } from '@/js'
2+
import { jsHandler, preProcessJs } from '@/js'
33
import { ClassGenerator } from '@/shared'
44

55
describe('js handler', () => {
@@ -194,4 +194,30 @@ describe('js handler', () => {
194194
}).code
195195
expect(code).toMatchSnapshot()
196196
})
197+
198+
it('LINEFEED case', () => {
199+
const testCase = 'const LINEFEED = "\\n";'
200+
const replaceMap = new Map()
201+
// replaceMap.set('bg-red-500/50', true)
202+
// replaceMap.set('bg-red-500', true)
203+
const code = jsHandler(testCase, {
204+
classGenerator,
205+
replaceMap
206+
}).code
207+
expect(code).toBe('const LINEFEED="\\n";')
208+
})
209+
210+
it('preProcessJs case', () => {
211+
const testCase = 'const LINEFEED = "\\n";'
212+
const replaceMap = new Map()
213+
// replaceMap.set('bg-red-500/50', true)
214+
// replaceMap.set('bg-red-500', true)
215+
const code = preProcessJs({
216+
code: testCase,
217+
addToUsedBy: () => {},
218+
id: 'xxx',
219+
replaceMap
220+
})
221+
expect(code).toBe(testCase)
222+
})
197223
})

0 commit comments

Comments
 (0)