Skip to content

Commit 4101c07

Browse files
committed
refactor(plugin): reorg code structure
1 parent 2b66714 commit 4101c07

File tree

13 files changed

+388
-874
lines changed

13 files changed

+388
-874
lines changed

packages/playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@vue-styled-components/playground",
3+
"type": "module",
34
"scripts": {
45
"dev": "vite"
56
},

packages/playground/postcss.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
plugins: {
33
tailwindcss: {},
44
autoprefixer: {},

packages/playground/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default defineConfig({
1010
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.less'],
1111
},
1212
plugins: [
13-
p(),
13+
p() as any,
1414
vue({
1515
script: {
1616
defineModel: true,

packages/plugins/typescript-syntax/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"test": "vitest"
3737
},
3838
"peerDependencies": {
39-
"vite": "^4.0.0 || ^5.0.0"
39+
"vite": "^5.0.0"
4040
},
4141
"publishConfig": {
4242
"access": "public",
@@ -50,14 +50,13 @@
5050
"@babel/traverse": "^7.26.10",
5151
"@babel/types": "^7.26.10",
5252
"@vue/compiler-sfc": "^3.5.13",
53-
"magic-string": "^0.30.17",
54-
"typescript": "^5.7.2",
55-
"vitest": "^3.0.8",
56-
"vue": "^3.5.13"
53+
"magic-string": "^0.30.17"
5754
},
5855
"devDependencies": {
5956
"@types/babel__generator": "^7.6.8",
6057
"@types/babel__traverse": "^7.20.6",
61-
"@vitest/ui": "^3.0.8"
58+
"@vitest/ui": "^3.0.8",
59+
"typescript": "^5.7.2",
60+
"vitest": "^3.0.8"
6261
}
6362
}
Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
import type { TransformResult } from './types/types'
2-
import * as parser from '@babel/parser'
3-
import MagicString from 'magic-string'
4-
import {
5-
collectTypesFromAST,
6-
endTimer,
7-
hasStyledComponents,
8-
logDebug,
9-
resetContext,
10-
startTimer,
11-
transformStyledComponents,
12-
useContext,
13-
} from './utils'
2+
import { transformCore } from './utils'
143

154
/**
165
* 转换styled组件语法
@@ -20,70 +9,10 @@ import {
209
* @returns 转换结果,如果没有变化则为null
2110
*/
2211
export function transformStyledSyntax(code: string, id: string): TransformResult | null {
23-
// 开始整体转换计时
24-
startTimer('transformStyledSyntax')
25-
26-
try {
27-
if (!hasStyledComponents(code)) {
28-
return null
29-
}
30-
31-
// 使用 MagicString 进行精确替换
32-
const s = new MagicString(code)
33-
34-
// 重置并获取类型上下文
35-
resetContext(id)
36-
const typeContext = useContext(false, id)
37-
38-
// 解析代码计时
39-
startTimer('parseCode')
40-
// 使用Babel解析代码
41-
const ast = parser.parse(code, {
42-
sourceType: 'module',
43-
plugins: [
44-
'jsx',
45-
'typescript',
46-
['decorators', { decoratorsBeforeExport: true }],
47-
],
48-
errorRecovery: true,
49-
})
50-
endTimer('parseCode')
51-
52-
// 收集类型信息计时
53-
startTimer('collectTypes')
54-
// 收集所有类型信息
55-
collectTypesFromAST(ast, true)
56-
endTimer('collectTypes')
57-
58-
// 转换 styled 组件计时
59-
startTimer('transformStyled')
60-
// 使用公共函数处理styled组件转换
61-
const { hasChanges, props } = transformStyledComponents(ast, code, s, 0)
62-
endTimer('transformStyled')
63-
64-
// 如果没有变更,返回null
65-
if (!hasChanges) {
66-
endTimer('transformStyledSyntax')
67-
return null
68-
}
69-
70-
const result = {
71-
code: s.toString(),
72-
map: s.generateMap({ source: id, includeContent: true }),
73-
props,
74-
}
75-
76-
// 记录转换完成
77-
logDebug(`转换完成: ${id}, 文件大小: ${code.length} -> ${result.code.length} 字节`)
78-
79-
// 结束整体转换计时
80-
endTimer('transformStyledSyntax')
81-
82-
return result
83-
}
84-
catch (err) {
85-
// 发生错误时也结束计时
86-
endTimer('transformStyledSyntax')
87-
throw err
88-
}
12+
// 使用通用转换核心函数处理
13+
return transformCore({
14+
code,
15+
id,
16+
timerLabel: 'transformStyledSyntax',
17+
})
8918
}

packages/plugins/typescript-syntax/src/utils/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export {
5454
processNestedGeneric,
5555
} from './styled-transformers'
5656

57+
// 导出 transform-core.ts 的函数
58+
export { transformCore } from './transform-core'
59+
5760
export * from './type-collectors'
5861

5962
export {

0 commit comments

Comments
 (0)