Skip to content

Commit cf8fa93

Browse files
committed
refactor(plugin): reorg code structure
1 parent a6de4c0 commit cf8fa93

26 files changed

+2773
-1845
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default antfu(
2222
'regexp/no-super-linear-backtracking': 'warn',
2323
'unicorn/prefer-number-properties': 'warn',
2424
'style/no-mixed-operators': 'warn',
25+
'no-case-declarations': 'warn',
2526
},
2627
},
2728
)

packages/plugins/typescript-syntax/__tests__/basic.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { transformStyledSyntax } from '../src/transform'
2+
import { transformStyledSyntax } from '../src/ts-transformer'
33
import { normalizeString } from './normalize'
44

55
describe('基本语法转换', () => {

packages/plugins/typescript-syntax/__tests__/complex.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { transformStyledSyntax } from '../src/transform'
2+
import { transformStyledSyntax } from '../src/ts-transformer'
33
import { normalizeString } from './normalize'
44

55
describe('复杂泛型语法转换', () => {

packages/plugins/typescript-syntax/__tests__/edge-cases.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { transformStyledSyntax } from '../src/transform'
2+
import { transformStyledSyntax } from '../src/ts-transformer'
33
import { normalizeString } from './normalize'
44

55
describe('边缘情况处理', () => {

packages/plugins/typescript-syntax/__tests__/real-world.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { transformStyledSyntax } from '../src/transform'
2+
import { transformStyledSyntax } from '../src/ts-transformer'
33
import { normalizeString } from './normalize'
44

55
describe('实际使用场景', () => {

packages/plugins/typescript-syntax/__tests__/transform-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { transformStyledSyntax } from '../src/transform'
2+
import { transformStyledSyntax } from '../src/ts-transformer'
33

44
describe('transformStyledSyntax', () => {
55
it('应该正确转换styled.tag<Props>模式', () => {

packages/plugins/typescript-syntax/__tests__/transform.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { transformStyledSyntax } from '../src/transform'
2+
import { transformStyledSyntax } from '../src/ts-transformer'
33

44
describe('transformStyledSyntax', () => {
55
it('should transform styled.tag<Props> to styled("tag", Props)', () => {

packages/plugins/typescript-syntax/index.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Plugin } from 'vite'
2-
import { compileScript, parse } from '@vue/compiler-sfc'
3-
import { transformStyledSyntax } from './src/transform'
2+
import { transformStyledSyntax } from './src/ts-transformer'
43
import { transformVueSFC } from './src/vue-transformer'
54

65
export interface VueStyledComponentsTypescriptSyntaxOptions {
@@ -43,19 +42,8 @@ export default function vueStyledComponentsTypescriptSyntax(
4342
// 对.vue文件进行特殊处理
4443
if (id.endsWith('.vue')) {
4544
try {
46-
// 使用@vue/compiler-sfc解析Vue文件
47-
const { descriptor } = parse(code)
48-
49-
// 如果没有script块,则不需要转换
50-
if (!descriptor.script && !descriptor.scriptSetup) {
51-
return null
52-
}
53-
54-
// 编译script,获取内容和位置信息
55-
const scriptBlock = compileScript(descriptor, { id })
56-
5745
// 调用专门的Vue SFC转换函数(基于AST实现)
58-
return transformVueSFC(code, id, descriptor, scriptBlock)
46+
return transformVueSFC(code, id)
5947
}
6048
catch (error) {
6149
console.error('处理Vue文件失败:', error)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import type { Plugin } from 'vite'
2+
import type { LogLevel } from './utils'
3+
import { transformStyledSyntax } from './ts-transformer'
4+
import { setConfig, setLogLevel } from './utils'
5+
import { transformVueSFC } from './vue-transformer'
6+
7+
/**
8+
* 插件选项
9+
*/
10+
export interface PluginOptions {
11+
/**
12+
* 是否启用调试模式
13+
* @default false
14+
*/
15+
debug?: boolean
16+
17+
/**
18+
* 日志级别
19+
* @default 'error'
20+
*/
21+
logLevel?: 'error' | 'warn' | 'info' | 'debug' | 'none'
22+
23+
/**
24+
* 是否启用类型缓存
25+
* @default true
26+
*/
27+
enableCache?: boolean
28+
29+
/**
30+
* 其他配置选项
31+
*/
32+
[key: string]: any
33+
}
34+
35+
/**
36+
* Vue Styled Components TypeScript 语法插件
37+
* @param options 插件配置选项
38+
*/
39+
export default function typescriptSyntaxPlugin(options: PluginOptions = {}): Plugin {
40+
const {
41+
debug = false,
42+
logLevel = 'error',
43+
enableCache = true,
44+
...otherOptions
45+
} = options
46+
47+
// 初始化配置
48+
setConfig({
49+
debug,
50+
enableCache,
51+
strictTypeChecking: true,
52+
optimizeAstTransform: true,
53+
enablePerfTracking: debug,
54+
logLevel: logLevel as LogLevel,
55+
maxCacheItems: 100,
56+
})
57+
58+
// 设置日志级别
59+
setLogLevel(logLevel as LogLevel)
60+
61+
// 返回 Vite 插件配置
62+
return {
63+
name: 'vue-styled-components:typescript-syntax',
64+
enforce: 'pre',
65+
66+
transform(code, id) {
67+
// 处理 Vue 单文件组件
68+
if (id.endsWith('.vue')) {
69+
return transformVueSFC(code, id)
70+
}
71+
72+
// 处理 TypeScript 文件
73+
if (id.endsWith('.ts') || id.endsWith('.tsx')) {
74+
return transformStyledSyntax(code, id)
75+
}
76+
77+
return null
78+
},
79+
}
80+
}

0 commit comments

Comments
 (0)