@vureact/compiler-core 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,150 @@
1
1
  import { GeneratorOptions as GeneratorOptions$1 } from '@babel/generator';
2
+ import { ParseResult as ParseResult$1 } from '@babel/parser';
2
3
  import * as t from '@babel/types';
3
4
  import { Expression, ArrayExpression, JSXIdentifier } from '@babel/types';
4
- import { ParseResult as ParseResult$1 } from '@babel/parser';
5
5
  import { RootNode, SourceLocation } from '@vue/compiler-core';
6
6
  import { SFCTemplateBlock, SFCScriptBlock, SFCStyleBlock } from '@vue/compiler-sfc';
7
7
  import { Options } from 'prettier';
8
8
 
9
+ interface FileLockOptions {
10
+ /**
11
+ * 锁过期时间(毫秒)
12
+ * 默认: 10000 (10秒)
13
+ */
14
+ stale?: number;
15
+ /**
16
+ * 锁更新间隔(毫秒)
17
+ * 默认: stale / 2
18
+ */
19
+ update?: number;
20
+ /**
21
+ * 重试配置
22
+ * 可以是重试次数或 retry 选项对象
23
+ * 默认: 5
24
+ */
25
+ retries?: number | {
26
+ retries?: number;
27
+ factor?: number;
28
+ minTimeout?: number;
29
+ maxTimeout?: number;
30
+ randomize?: boolean;
31
+ };
32
+ /**
33
+ * 是否解析符号链接
34
+ * 默认: true
35
+ */
36
+ realpath?: boolean;
37
+ /**
38
+ * 自定义锁文件路径
39
+ * 默认: `${filePath}.lock`
40
+ */
41
+ lockfilePath?: string;
42
+ }
43
+
44
+ type CompilationUnit = SFCUnit | ScriptUnit | StyleUnit;
45
+ interface SFCUnit extends BaseUnit {
46
+ type: CacheKey.SFC;
47
+ output?: {
48
+ jsx: OutputItem;
49
+ css: Partial<OutputItem>;
50
+ };
51
+ }
52
+ interface ScriptUnit extends BaseUnit {
53
+ type: CacheKey.SCRIPT;
54
+ output?: {
55
+ script: OutputItem;
56
+ };
57
+ }
58
+ interface StyleUnit extends Omit<BaseUnit, 'hasRoute'> {
59
+ type: CacheKey.STYLE;
60
+ output?: {
61
+ style: OutputItem;
62
+ };
63
+ }
64
+ interface AssetUnit extends Omit<BaseUnit, 'fileId' | 'source' | 'hasRoute'> {
65
+ type: CacheKey.ASSET;
66
+ }
67
+ interface BaseUnit extends FileMeta {
68
+ file: string;
69
+ fileId: string;
70
+ source: string;
71
+ hasRoute?: boolean;
72
+ }
73
+ interface OutputItem {
74
+ file: string;
75
+ code: string;
76
+ }
77
+
78
+ type LoadedCache<T = CacheMeta> = {
79
+ key: CacheKey;
80
+ target: T[];
81
+ source: CacheList;
82
+ };
83
+ type CacheMeta = Vue2ReactCacheMeta | FileCacheMeta;
84
+ declare enum CacheKey {
85
+ SFC = "sfc",
86
+ SCRIPT = "script",
87
+ STYLE = "style",
88
+ ASSET = "copied"
89
+ }
90
+ interface CacheList {
91
+ [CacheKey.SFC]: Vue2ReactCacheMeta[];
92
+ [CacheKey.SCRIPT]: ScriptCacheMeta[];
93
+ [CacheKey.STYLE]: StyleCacheMeta[];
94
+ [CacheKey.ASSET]: FileCacheMeta[];
95
+ }
96
+ type Vue2ReactCacheMeta = Omit<SFCUnit, 'source'>;
97
+ type ScriptCacheMeta = Omit<ScriptUnit, 'source'>;
98
+ type StyleCacheMeta = Omit<StyleUnit, 'source'>;
99
+ interface FileCacheMeta extends FileMeta {
100
+ file: string;
101
+ }
102
+ interface FileMeta {
103
+ fileSize: number;
104
+ mtime: number;
105
+ hash?: string;
106
+ }
107
+ interface CacheCheckResult {
108
+ shouldCompile: boolean;
109
+ hash?: string;
110
+ }
111
+
112
+ type CompilationResult = SFCCompilationResult | ScriptCompilationResult | StyleCompilationResult;
113
+ interface SFCCompilationResult extends BaseCompilationResult {
114
+ fileInfo: {
115
+ jsx: {
116
+ file: string;
117
+ lang: string;
118
+ };
119
+ css: {
120
+ file?: string;
121
+ hash?: string;
122
+ code?: string;
123
+ };
124
+ };
125
+ }
126
+ interface ScriptCompilationResult extends BaseCompilationResult {
127
+ fileInfo: {
128
+ script: {
129
+ file: string;
130
+ lang: string;
131
+ };
132
+ };
133
+ }
134
+ interface StyleCompilationResult extends Omit<BaseCompilationResult, 'hasRoute' | keyof GeneratorResult> {
135
+ code: string;
136
+ fileInfo: {
137
+ style: {
138
+ file: string;
139
+ lang: string;
140
+ };
141
+ };
142
+ }
143
+ interface BaseCompilationResult extends GeneratorResult {
144
+ fileId: string;
145
+ hasRoute?: boolean;
146
+ }
147
+
9
148
  type LangType = 'js' | 'jsx' | 'ts' | 'tsx';
10
149
 
11
150
  type ReactiveTypes = 'ref' | 'reactive' | 'indirect' | 'none';
@@ -122,434 +261,24 @@ interface IPropsContext {
122
261
  slotTypes: t.TSType[];
123
262
  }
124
263
 
125
- interface ParseResult {
126
- template: BlockIR<SFCTemplateBlock, RootNode>;
127
- script: BlockIR<SFCScriptBlock, ParseResult$1>;
128
- style: BlockIR<SFCStyleBlock, undefined>;
129
- }
130
- type BlockIR<S, T> = {
131
- source?: S;
132
- ast: T;
133
- } | null;
134
- interface ParserOptions {
135
- plugins?: PluginRegister<ParseResult>;
136
- }
137
- /**
138
- * 解析 Vue 单文件组件(SFC)源码,生成结构化解析结果。
139
- *
140
- * 此函数是解析阶段的核心入口,
141
- * 负责将 Vue SFC 源码解析为包含模板、脚本、样式等各个块的结构化数据。
142
- *
143
- * @param source - Vue 单文件组件的源码字符串
144
- * @param ctx - 编译上下文对象
145
- * @param plugins - 可选的插件注册表,用于对解析结果进行自定义处理和增强
146
- * @returns 解析结果对象,包含模板、脚本、样式块的解析信息
147
- * @throws 不会直接抛出异常,错误信息会通过日志系统记录
148
- *
149
- * @remarks
150
- * - 函数内部使用 Vue 官方的 `@vue/compiler-sfc` 进行基础解析
151
- * - 解析结果会经过后处理阶段,包括脚本元数据扫描
152
- * - 插件按照注册顺序依次执行,可以对解析结果进行修改或增强
153
- * - 错误处理:语法错误和编译错误会通过日志系统记录,不会中断执行
154
- *
155
- * @example
156
- * ```typescript
157
- * // 基础用法:解析一个简单的 Vue 组件
158
- *
159
- * const vueSource = `
160
- * <template>
161
- * <div class="greeting">
162
- * Hello, {{ name }}!
163
- * </div>
164
- * </template>
165
- *
166
- * <script setup lang="ts">
167
- * import { ref } from 'vue'
168
- *
169
- * const name = ref('World')
170
- * const count = ref(0)
171
- *
172
- * function increment() {
173
- * count.value++
174
- * }
175
- * </script>
176
- *
177
- * <style scoped>
178
- * .greeting {
179
- * color: #42b983;
180
- * font-size: 1.5rem;
181
- * }
182
- * </style>
183
- * `;
184
- *
185
- * const ctx: ICompilationContext = {
186
- * filename: 'MyComponent.vue',
187
- * // 其他上下文配置...
188
- * };
189
- *
190
- * const result = parseSFC(vueSource, ctx);
191
- *
192
- * // 访问解析结果
193
- * console.log(result.template?.ast); // 模板的 AST 节点
194
- * console.log(result.script?.ast); // 脚本的 Babel 解析结果
195
- * console.log(result.style?.source); // 样式块原始信息
196
- *
197
- * // 使用可选插件配置,进行自定义处理
198
- * const options: ParserOptions = {
199
- * plugins: {
200
- * myPlugin: (result, ctx) => {
201
- * // 提取组件元数据
202
- * const componentName = extractComponentName(result);
203
- * // 添加自定义分析结果
204
- * result.metadata = {
205
- * name: componentName,
206
- * analyzedAt: new Date().toISOString()
207
- * };
208
- * }
209
- * }
210
- * };
211
- *
212
- * const resultWithPlugin = parseSFC(vueSource, compilationContext, options);
213
- * ```
214
- */
215
- declare function parseSFC(source: string, ctx: ICompilationContext, options?: ParserOptions): ParseResult;
216
-
217
- /**
218
- * 仅用于解析 script 文件,参数与 parseSFC 一致
219
- */
220
- declare function parseOnlyScript(source: string, ctx: ICompilationContext, options?: ParserOptions): ParseResult;
221
-
222
- /**
223
- * 解析 Vue 组件源码的统一入口函数。
224
- *
225
- * 根据输入类型自动选择解析器:
226
- * - `sfc`: 解析 Vue 单文件组件(包含 template/script/style)
227
- * - `script-*`: 仅解析脚本文件(如 .js、.ts)
228
- *
229
- * @param source - 源码字符串
230
- * @param ctx - 编译上下文,包含输入类型、文件名等信息
231
- * @param options - 可选的解析器配置,如插件
232
- * @returns 解析结果对象,包含模板、脚本、样式的结构化数据
233
- */
234
- declare function parse(source: string, ctx: ICompilationContext, options?: ParserOptions): ParseResult;
235
-
236
- interface ScriptBlockIR {
237
- /** Transformed full script AST (used for script-only input). */
238
- scriptAST?: ParseResult$1;
239
- imports: t.ImportDeclaration[];
240
- exports: t.ExportDeclaration[];
241
- tsTypes: t.TypeScript[];
242
- /** Executable statements extracted from script block. */
243
- statement: {
244
- /** Statements hoisted outside component function. */
245
- global: t.Node[];
246
- /** Statements kept inside component function. */
247
- local: ParseResult$1<t.File> | null;
248
- };
249
- }
250
-
251
- declare const enum NodeTypes {
252
- FRAGMENT = 0,
253
- ELEMENT = 1,
254
- TEXT = 2,
255
- COMMENT = 3,
256
- JSX_INTERPOLATION = 4
257
- }
258
- declare const enum PropTypes {
259
- ATTRIBUTE = 1,
260
- SLOT = 2,
261
- EVENT = 3,
262
- DYNAMIC_ATTRIBUTE = 4
263
- }
264
- interface BabelExp<T = Expression> {
265
- content: string;
266
- ast: T;
267
- }
268
-
269
- interface BaseSimpleNodeIR {
270
- type: NodeTypes;
271
- content: string;
272
- babelExp: Expression;
273
- }
274
- interface FragmentNodeIR {
275
- type: NodeTypes;
276
- children: TemplateChildNodeIR[];
264
+ type CompilerPlugins = PluginRegister<CompilationResult> & {
265
+ /**
266
+ * Register parser plugins
267
+ */
268
+ parser?: PluginRegister<ParseResult>;
269
+ /**
270
+ * Register transformer plugins
271
+ */
272
+ transformer?: PluginRegister<ReactIRDescriptor>;
273
+ /**
274
+ * Register codegen plugins
275
+ */
276
+ codegen?: PluginRegister<GeneratorResult>;
277
+ };
278
+ interface PluginRegister<T> {
279
+ [name: string]: (result: T, ctx: ICompilationContext) => void;
277
280
  }
278
281
 
279
- interface ElementNodeIR extends BaseElementNodeIR {
280
- type: NodeTypes;
281
- props: (PropsIR | SlotPropsIR)[];
282
- children: TemplateChildNodeIR[];
283
- meta: Partial<ElementNodeIRMeta>;
284
- conditionIsHandled: boolean;
285
- isBuiltIn?: boolean;
286
- isRoute?: boolean;
287
- }
288
- interface BaseElementNodeIR {
289
- tag: string;
290
- isComponent?: boolean;
291
- isSelfClosing?: boolean;
292
- ref?: string;
293
- loc?: SourceLocation;
294
- }
295
- interface ElementNodeIRMeta {
296
- condition: ConditionMeta;
297
- loop: LoopMeta;
298
- memo: MemoMeta;
299
- show: ShowMeta;
300
- }
301
- type ConditionMeta = {
302
- if?: boolean;
303
- elseIf?: boolean;
304
- else?: boolean;
305
- value: string;
306
- babelExp: BabelExp;
307
- next?: ElementNodeIR;
308
- isHandled: boolean;
309
- };
310
- type LoopMeta = {
311
- isLoop?: boolean;
312
- value: {
313
- source: string;
314
- value: string;
315
- key?: string;
316
- index?: string;
317
- };
318
- isHandled: boolean;
319
- };
320
- type MemoMeta = {
321
- isMemo?: boolean;
322
- value: string;
323
- babelExp: BabelExp<ArrayExpression>;
324
- isHandled: boolean;
325
- };
326
- type ShowMeta = {
327
- isShow?: boolean;
328
- value: string;
329
- babelExp: BabelExp;
330
- };
331
-
332
- interface PropsIR {
333
- type: PropTypes;
334
- rawName?: string;
335
- name: string;
336
- isStatic?: boolean;
337
- modifiers?: string[];
338
- value: PropIRValue;
339
- isKeyLessVBind?: boolean;
340
- babelExp: BabelExp<JSXIdentifier | Expression>;
341
- }
342
- type PropIRValue = {
343
- content: string;
344
- isStringLiteral?: boolean;
345
- merge?: string[];
346
- babelExp: BabelExp;
347
- };
348
-
349
- interface SlotPropsIR {
350
- type: PropTypes.SLOT;
351
- name: string;
352
- rawName: string;
353
- isStatic: boolean;
354
- isScoped: boolean;
355
- content?: TemplateChildNodeIR[];
356
- callback?: {
357
- arg: string;
358
- exp: TemplateChildNodeIR[];
359
- };
360
- }
361
-
362
- interface TemplateBlockIR {
363
- children: TemplateChildNodeIR[];
364
- }
365
- type TemplateChildNodeIR = ElementNodeIR | BaseSimpleNodeIR | FragmentNodeIR;
366
-
367
- interface ReactIRDescriptor {
368
- template: TemplateBlockIR;
369
- script: ScriptBlockIR;
370
- style?: string;
371
- }
372
- interface TransformerOptions {
373
- plugins?: PluginRegister<ReactIRDescriptor>;
374
- }
375
- /**
376
- * 将 Vue SFC 解析结果转换为 React 中间表示(IR)。
377
- *
378
- * 此函数是转换阶段的核心入口,负责将 Vue 组件的解析结果(AST)转换为
379
- * 适合生成 React 代码的中间表示形式。转换过程包括模板转换和脚本转换。
380
- *
381
- * @param ast - Vue SFC 的解析结果,来自 {@link parse} 函数的返回值
382
- * @param ctx - 编译上下文对象
383
- * @param plugins - 可选的插件注册表,用于对转换结果进行自定义处理和增强
384
- * @returns React 中间表示描述符,包含模板、脚本和样式的转换结果
385
- *
386
- * @remarks
387
- * - 模板转换:将 Vue 模板 AST 转换为 React 可用的模板 IR
388
- * - 脚本转换:将 Vue 脚本 AST 转换为 React 可用的脚本 IR
389
- * - 样式处理:提取样式块内容,保留原始 CSS 字符串
390
- * - 插件支持:支持通过插件对转换结果进行自定义处理
391
- *
392
- * @example
393
- * ```typescript
394
- * // 转换示例
395
- *
396
- * const ctx: ICompilationContext = {
397
- * filename: 'MyComponent.vue',
398
- * // 其他上下文配置...
399
- * };
400
- *
401
- * // 1. 首先解析 Vue SFC
402
- * const parseResult = parse(vueSource, ctx);
403
- *
404
- * // 2. 将解析结果转换为 React IR
405
- * const reactIR = transform(parseResult, ctx);
406
- *
407
- * // 访问转换结果
408
- * console.log(reactIR.template); // 模板转换结果
409
- * console.log(reactIR.script); // 脚本转换结果
410
- * console.log(reactIR.style); // 样式内容字符串
411
- *
412
- * // 使用可选插件配置,进行自定义处理
413
- * const options: TransformerOptions = {
414
- * plugins: {
415
- * extractMetadata: (result, ctx) => {
416
- * const componentName = extractComponentNameFromScript(result.script);
417
- * const propsCount = countProps(result.script);
418
- *
419
- * result.metadata = {
420
- * name: componentName,
421
- * propsCount,
422
- * hasStyle: !!result.style,
423
- * convertedAt: new Date().toISOString(),
424
- * };
425
- * },
426
- *
427
- * // 插2:样式预处理
428
- * preprocessStyles: (result, ctx) => {
429
- * if (result.style) {
430
- * // 对样式进行预处理,如添加前缀等
431
- * result.style = addVendorPrefixes(result.style);
432
- * result.style = minifyCSS(result.style);
433
- * }
434
- * },
435
- * }
436
- * }
437
- *
438
- * // 使用多个插件进行转换
439
- * const reactIRWithPlugins = transform(parseResult, ctx, plugins);
440
- *
441
- * // 插件处理后的结果包含自定义数据
442
- * console.log(reactIRWithPlugins.metadata);
443
- * ```
444
- */
445
- declare function transform(ast: ParseResult, ctx: ICompilationContext, options?: TransformerOptions): ReactIRDescriptor;
446
-
447
- interface GeneratorOptions extends GeneratorOptions$1 {
448
- plugins?: PluginRegister<GeneratorResult>;
449
- }
450
- interface GeneratorResult {
451
- ast: t.Program | ParseResult$1;
452
- code: string;
453
- source: string;
454
- }
455
- /**
456
- * 将 React 中间表示(IR)生成为可执行的 JSX/TSX 代码。
457
- *
458
- * 此函数是代码生成阶段的核心入口,负责将转换后的 React IR 转换为
459
- * 完整的 AST 并最终生成源代码。生成过程包括 JSX 构建和脚本构建。
460
- *
461
- * @param ir - React 中间表示描述符,来自 {@link transform} 函数的返回值
462
- * @param ctx - 编译上下文对象
463
- * @param options - 可选的生成选项,包括 Babel 生成器选项和插件
464
- * @returns 生成结果对象,包含 AST、生成的代码和原始源码引用
465
- *
466
- * @remarks
467
- * - JSX 构建:将模板 IR 转换为 JSX AST 节点
468
- * - 脚本构建:将脚本 IR 与 JSX 结合,构建完整的程序 AST
469
- * - 代码生成:使用 Babel 生成器将 AST 转换为源代码字符串
470
- * - 插件支持:支持通过插件对生成结果进行自定义处理
471
- *
472
- * @example
473
- * ```typescript
474
- * // 生成示例
475
- *
476
- * const ctx: ICompilationContext = {
477
- * filename: 'MyComponent.vue',
478
- * source: vueSource,
479
- * // 其他上下文配置...
480
- * };
481
- *
482
- * // 1. 解析 Vue SFC
483
- * const parseResult = parse(vueSource, ctx);
484
- *
485
- * // 2. 转换为 React IR
486
- * const reactIR = transform(parseResult, ctx);
487
- *
488
- * // 使用可选配置项进行自定义处理
489
- * const generatorOptions: GeneratorOptions = {
490
- * // 配置 jsesc 避免 Unicode 转义
491
- * jsescOption: {
492
- * minimal: true,
493
- * quotes: 'single'
494
- * },
495
- * minified: true,
496
- *
497
- * // 自定义插件
498
- * plugins: {
499
- * // 插件1:代码格式化
500
- * formatCode: (result, ctx) => {
501
- * // 使用 prettier 格式化生成的代码
502
- * result.code = prettier.format(result.code, {
503
- * parser: 'babel',
504
- * semi: true,
505
- * singleQuote: true,
506
- * });
507
- * },
508
- *
509
- * // 插件2:代码质量检查
510
- * lintGeneratedCode: (result, ctx) => {
511
- * const issues = eslint.verify(result.code, {
512
- * rules: {
513
- * 'react/react-in-jsx-scope': 'off',
514
- * 'no-unused-vars': 'warn',
515
- * },
516
- * });
517
- *
518
- * if (issues.length > 0) {
519
- * result.lintIssues = issues;
520
- * console.warn(`Found ${issues.length} lint issues in generated code`);
521
- * }
522
- * },
523
- * },
524
- * };
525
- *
526
- * // 3. 生成 React 组件
527
- * const generated = generateComponent(reactIR, ctx, generatorOptions);
528
- *
529
- * // 访问生成结果
530
- * console.log(generated.ast); // 完整的 Babel AST
531
- * console.log(generated.code); // 生成的 React 代码
532
- * console.log(generated.source); // 原始 Vue 源码
533
- * ```
534
- */
535
- declare function generateComponent(ir: ReactIRDescriptor, ctx: ICompilationContext, options?: GeneratorOptions): GeneratorResult;
536
-
537
- declare function generateOnlyScript(ir: ReactIRDescriptor, ctx: ICompilationContext, options?: GeneratorOptions): GeneratorResult;
538
-
539
- /**
540
- * 代码生成的统一入口函数。
541
- *
542
- * 根据输入类型自动选择生成器:
543
- * - `sfc`: 生成完整的 React 组件(包含 JSX 和脚本)
544
- * - `script-*`: 仅生成脚本代码(如 .js、.ts)
545
- *
546
- * @param ir - React 中间表示描述符,来自转换阶段的结果
547
- * @param ctx - 编译上下文,包含输入类型、源码等信息
548
- * @param options - 可选的生成器配置,如 Babel 生成选项和插件
549
- * @returns 生成结果对象,包含 AST、生成的代码和原始源码
550
- */
551
- declare function generate(ir: ReactIRDescriptor, ctx: ICompilationContext, options?: GeneratorOptions): GeneratorResult;
552
-
553
282
  interface CompilerOptions {
554
283
  /**
555
284
  * Manually specify the root directory.
@@ -570,47 +299,10 @@ interface CompilerOptions {
570
299
  * @default true
571
300
  */
572
301
  cache?: boolean;
573
- output?: {
574
- /**
575
- * Output the name of the root directory corresponding to the file's location.
576
- * @default '.vureact'
577
- */
578
- workspace?: string;
579
- /**
580
- * Output directory name, relative to `output.workspace`
581
- * @default 'dist'
582
- */
583
- outDir?: string;
584
- /**
585
- * Whether to automatically call Vite to initialize a standard
586
- * React project environment before compilation.
587
- * @default true
588
- */
589
- bootstrapVite?: boolean | {
590
- /**
591
- * @default 'react-ts'
592
- */
593
- template: 'react-ts' | 'react';
594
- };
595
- /**
596
- * Specify asset files that do not need to be copied.
597
- * They can be filenames or paths, using fuzzy matching.
598
- * @default
599
- * [
600
- * 'package.json',
601
- * 'package-lock.json',
602
- * 'pnpm-lock.yaml',
603
- * 'index.html',
604
- * 'tsconfig.',
605
- * 'vite.config.',
606
- * 'eslint.config.',
607
- * 'readme.',
608
- * 'vue.',
609
- * 'vureact.config.js',
610
- * ]
611
- */
612
- ignoreAssets?: string[];
613
- };
302
+ /**
303
+ * @see {@link OutputConfig}
304
+ */
305
+ output?: OutputConfig;
614
306
  /**
615
307
  * Excluded file/directory matching patterns (glob syntax supported).
616
308
  * @default
@@ -630,6 +322,7 @@ interface CompilerOptions {
630
322
  recursive?: boolean;
631
323
  /**
632
324
  * Options passed through to babel-generator.
325
+ * @see {@link GeneratorOptions}
633
326
  */
634
327
  generate?: GeneratorOptions$1;
635
328
  /**
@@ -642,10 +335,36 @@ interface CompilerOptions {
642
335
  * @default true
643
336
  */
644
337
  preprocessStyles?: boolean;
338
+ /**
339
+ * Specify the path to the Vue Router config file
340
+ * The file must default export createRouter
341
+ * Used to inject Router Provider in React's main.tsx or main.jsx
342
+ *
343
+ *
344
+ *
345
+ * @see {@link RouterConfig}
346
+ *
347
+ * @example
348
+ * ```js
349
+ * router: {
350
+ * configFile: 'src/router/index.ts'
351
+ * }
352
+ * ```
353
+ *
354
+ * Assumes the config file default exports createRouter:
355
+ *
356
+ * ```js
357
+ * // src/router/index.ts
358
+ * export default createRouter({ ... })
359
+ * ```
360
+ */
361
+ router?: RouterConfig;
645
362
  /**
646
363
  * Can be used to add plugins and customize the output results of
647
364
  * the parse/transform/codegen/compiled stages respectively.
648
365
  *
366
+ * @see {@link CompilerPlugins}
367
+ *
649
368
  * @example
650
369
  * ```ts
651
370
  * plugins: {
@@ -666,50 +385,16 @@ interface CompilerOptions {
666
385
  * }
667
386
  * ```
668
387
  */
669
- plugins?: PluginRegister<CompilationResult> & {
670
- /**
671
- * Register parser plugins
672
- */
673
- parser?: PluginRegister<ParseResult>;
674
- /**
675
- * Register transformer plugins
676
- */
677
- transformer?: PluginRegister<ReactIRDescriptor>;
678
- /**
679
- * Register codegen plugins
680
- */
681
- codegen?: PluginRegister<GeneratorResult>;
682
- };
683
- format?: {
684
- /**
685
- * @default false
686
- */
687
- enabled?: boolean;
688
- /**
689
- * @default 'prettier'
690
- */
691
- formatter?: 'prettier' | 'builtin';
692
- /**
693
- * Configure the formatting options for Prettier,
694
- * which takes effect only when the formatter is set to 'prettier'.
695
- */
696
- prettierOptions?: Options;
697
- };
388
+ plugins?: CompilerPlugins;
389
+ /**
390
+ * @see {@link FormatConfig}
391
+ */
392
+ format?: FormatConfig;
698
393
  /**
699
394
  * Log Control Options
395
+ * @see {@link LoggingConfig}
700
396
  */
701
- logging?: {
702
- /**
703
- * @default true
704
- */
705
- enabled?: boolean;
706
- /** Whether to output warning messages. */
707
- warnings?: boolean;
708
- /** Whether to output info messages. */
709
- info?: boolean;
710
- /** Whether to output error messages. */
711
- errors?: boolean;
712
- };
397
+ logging?: LoggingConfig;
713
398
  /**
714
399
  * Execute only after the first successful full compilation.
715
400
  */
@@ -722,107 +407,130 @@ interface CompilerOptions {
722
407
  */
723
408
  onChange?: (event: 'add' | 'change', unit: CompilationUnit) => Promise<void | undefined>;
724
409
  }
725
- interface PluginRegister<T> {
726
- [name: string]: (result: T, ctx: ICompilationContext) => void;
727
- }
728
- type CompilationResult = SFCCompilationResult | ScriptCompilationResult | StyleCompilationResult;
729
- interface SFCCompilationResult extends BaseCompilationResult {
730
- fileInfo: {
731
- jsx: {
732
- file: string;
733
- lang: string;
734
- };
735
- css: {
736
- file?: string;
737
- hash?: string;
738
- code?: string;
739
- };
410
+ interface OutputConfig {
411
+ /**
412
+ * Output the name of the root directory corresponding to the file's location.
413
+ * @default '.vureact'
414
+ */
415
+ workspace?: string;
416
+ /**
417
+ * Output directory name, relative to `output.workspace`
418
+ * @default 'dist'
419
+ */
420
+ outDir?: string;
421
+ /**
422
+ * Whether to automatically call Vite to initialize a standard
423
+ * React project environment before compilation.
424
+ * @default true
425
+ */
426
+ bootstrapVite?: boolean | {
427
+ /**
428
+ * Specify the React template type.
429
+ * @default 'react-ts'
430
+ */
431
+ template?: 'react-ts' | 'react';
432
+ /**
433
+ * Specify the Vite version for initial installation, which must start with '@'.
434
+ * @default '@latest'
435
+ *
436
+ * @example
437
+ *
438
+ * ```js
439
+ * { vite: '@7' }
440
+ * ```
441
+ */
442
+ vite?: string;
443
+ /**
444
+ * Specify the React version for initial installation.
445
+ * @default 'latest'
446
+ *
447
+ * @example
448
+ *
449
+ * ```js
450
+ * { react: '^19.2.0' }
451
+ * ```
452
+ */
453
+ react?: string;
740
454
  };
455
+ /**
456
+ * Specify asset files that do not need to be copied.
457
+ * They can be filenames or paths, using fuzzy matching.
458
+ * @default
459
+ * [
460
+ * 'package.json',
461
+ * 'package-lock.json',
462
+ * 'pnpm-lock.yaml',
463
+ * 'index.html',
464
+ * 'tsconfig.',
465
+ * 'vite.config.',
466
+ * 'eslint.config.',
467
+ * 'readme.',
468
+ * 'vue.',
469
+ * '.vue',
470
+ * 'vureact.config.js',
471
+ * 'vureact.config.ts',
472
+ * ]
473
+ */
474
+ ignoreAssets?: string[];
475
+ /**
476
+ * Customize the generated package.json file.
477
+ * This function receives the default package.json object
478
+ * and should return the modified version.
479
+ *
480
+ * @note This option only takes effect when `bootstrapVite` is enabled.
481
+ *
482
+ * @example
483
+ * ```js
484
+ * packageJson: (defaultPkg) => {
485
+ * // Modify and return the copy
486
+ * defaultPkg.dependencies['my-library'] = '^1.0.0';
487
+ * return defaultPkg;
488
+ * }
489
+ * ```
490
+ */
491
+ packageJson?: (defaultPkg: Record<string, any>) => Record<string, any>;
741
492
  }
742
- interface ScriptCompilationResult extends BaseCompilationResult {
743
- fileInfo: {
744
- script: {
745
- file: string;
746
- lang: string;
747
- };
748
- };
493
+ interface RouterConfig {
494
+ /**
495
+ * Path to the Vue Router config file.
496
+ * Must be the location where `createRouter` is **exported as default**.
497
+ */
498
+ configFile: string;
499
+ /**
500
+ * Automatically update the react app entry file to use the VuReact Router Provider.
501
+ *
502
+ * Note: Injection only occurs when `output.bootstrapVite` is enabled.
503
+ *
504
+ * @default true
505
+ */
506
+ autoUpdateEntry?: boolean;
749
507
  }
750
- interface StyleCompilationResult extends Omit<BaseCompilationResult, 'hasRoute' | keyof GeneratorResult> {
751
- code: string;
752
- fileInfo: {
753
- style: {
754
- file: string;
755
- lang: string;
756
- };
757
- };
508
+ interface FormatConfig {
509
+ /**
510
+ * @default false
511
+ */
512
+ enabled?: boolean;
513
+ /**
514
+ * @default 'prettier'
515
+ */
516
+ formatter?: 'prettier' | 'builtin';
517
+ /**
518
+ * Configure the formatting options for Prettier,
519
+ * which takes effect only when the formatter is set to 'prettier'.
520
+ */
521
+ prettierOptions?: Options;
758
522
  }
759
- interface BaseCompilationResult extends GeneratorResult {
760
- fileId: string;
761
- hasRoute?: boolean;
762
- }
763
- type CompilationUnit = SFCUnit | ScriptUnit | StyleUnit;
764
- interface SFCUnit extends BaseUnit {
765
- type: CacheKey.SFC;
766
- output?: {
767
- jsx: OutputItem;
768
- css: Partial<OutputItem>;
769
- };
770
- }
771
- interface ScriptUnit extends BaseUnit {
772
- type: CacheKey.SCRIPT;
773
- output?: {
774
- script: OutputItem;
775
- };
776
- }
777
- interface StyleUnit extends Omit<BaseUnit, 'hasRoute'> {
778
- type: CacheKey.STYLE;
779
- output?: {
780
- style: OutputItem;
781
- };
782
- }
783
- interface AssetUnit extends Omit<BaseUnit, 'fileId' | 'source' | 'hasRoute'> {
784
- type: CacheKey.ASSET;
785
- }
786
- interface BaseUnit extends FileMeta {
787
- file: string;
788
- fileId: string;
789
- source: string;
790
- hasRoute?: boolean;
791
- }
792
- interface OutputItem {
793
- file: string;
794
- code: string;
795
- }
796
- type LoadedCache<T = CacheMeta> = {
797
- key: CacheKey;
798
- target: T[];
799
- source: CacheList;
800
- };
801
- type CacheMeta = Vue2ReactCacheMeta | FileCacheMeta;
802
- declare enum CacheKey {
803
- SFC = "sfc",
804
- SCRIPT = "script",
805
- STYLE = "style",
806
- ASSET = "copied"
807
- }
808
- interface CacheList {
809
- [CacheKey.SFC]: Vue2ReactCacheMeta[];
810
- [CacheKey.SCRIPT]: FileCacheMeta[];
811
- [CacheKey.STYLE]: FileCacheMeta[];
812
- [CacheKey.ASSET]: FileCacheMeta[];
813
- }
814
- type Vue2ReactCacheMeta = Omit<SFCUnit, 'source'>;
815
- interface FileCacheMeta extends FileMeta {
816
- file: string;
817
- }
818
- interface FileMeta {
819
- fileSize: number;
820
- mtime: number;
821
- hash?: string;
822
- }
823
- interface CacheCheckResult {
824
- shouldCompile: boolean;
825
- hash?: string;
523
+ interface LoggingConfig {
524
+ /**
525
+ * @default true
526
+ */
527
+ enabled?: boolean;
528
+ /** Whether to output warning messages. */
529
+ warnings?: boolean;
530
+ /** Whether to output info messages. */
531
+ info?: boolean;
532
+ /** Whether to output error messages. */
533
+ errors?: boolean;
826
534
  }
827
535
 
828
536
  declare class Helper {
@@ -845,8 +553,9 @@ declare class Helper {
845
553
  isSingleFile(): boolean;
846
554
  /**
847
555
  * 获取输出文件的路径。如:'[root]/.vureact/dist/'
556
+ * @param addInput 会输出如:'[root]/.vureact/dist/[input]/'
848
557
  */
849
- getOuputPath(): string;
558
+ getOuputPath(addInput?: boolean): string;
850
559
  getOutDirName(): string;
851
560
  getWorkspaceDir(): string;
852
561
  /**
@@ -898,9 +607,16 @@ declare class Helper {
898
607
  */
899
608
  compareFileMeta(a: FileMeta, b: FileMeta): boolean;
900
609
  /**
901
- * 统一的写文件方法,包含自动创建目录
610
+ * 统一的写文件方法,包含自动创建目录(带文件互斥锁可选)
611
+ * @param filePath - 要写入的文件路径
612
+ * @param content - 要写入的内容
613
+ * @param options - 可选配置项
614
+ * @param options.lock - 是否启用文件锁(默认false)
902
615
  */
903
- writeFileWithDir(filePath: string, content: string): Promise<void>;
616
+ writeFileWithDir(filePath: string, content: string, options?: FileLockOptions & {
617
+ lock?: boolean;
618
+ }): Promise<void>;
619
+ rmFile(filePath: string): Promise<void>;
904
620
  /**
905
621
  * 加载指定文件的缓存内容
906
622
  * @param key 缓存键
@@ -925,7 +641,6 @@ declare class Helper {
925
641
  getFileMeta(filePath: string): Promise<FileMeta>;
926
642
  removeOutputFile(filePath: string, resolveOutputPath?: boolean): Promise<void>;
927
643
  updateCache(targetFile: string, newData: any, cache: LoadedCache): void;
928
- resolveViteCreateApp(): void;
929
644
  /**
930
645
  * 获取需要排除编译的文件
931
646
  */
@@ -940,460 +655,263 @@ declare class Helper {
940
655
  * 读取 package.json 文件内容,并处理成对象返回
941
656
  */
942
657
  resolvePackageFile(path: string): Promise<Record<string, any>>;
658
+ /**
659
+ * 获取目录到文件的相对路径
660
+ * @returns 结果路径不包含文件拓展名,并以诸如 ./ 开头
661
+ */
662
+ resolveRelativePath(from: string, to: string): string;
943
663
  }
944
664
 
945
665
  /**
946
- * 基础编译器类,提供 Vue 到 React 代码转换的核心编译功能。
947
- *
948
- * 此类继承自 {@link Helper},是 VuReact 编译器的核心实现,负责:
949
- * 1. 初始化编译上下文:为每次编译创建独立的上下文环境
950
- * 2. 执行完整的编译流程:解析(parse)→ 转换(transform)→ 生成(generate)
951
- * 3. 管理编译选项和配置:处理用户传入的编译器配置
952
- * 4. 插件系统集成:支持解析、转换、生成各阶段的插件扩展
953
- * 5. 错误处理和日志记录:通过日志系统记录编译过程中的信息
954
- *
955
- * 主要用途:
956
- * - 供 {@link FileCompiler} 继承使用,实现文件系统级别的批量编译
957
- * - 直接实例化用于单文件编译,适合集成到其他工具链中
958
- *
959
- * 核心特性:
960
- * - 三阶段编译流水线:严格遵循解析→转换→生成的编译模型
961
- * - 插件化架构:支持在编译各阶段插入自定义逻辑
962
- * - 上下文隔离:每次编译创建独立上下文,避免状态污染
963
- * - 资源感知:自动处理样式预处理和路由依赖检测
964
- * - 错误恢复:编译错误通过日志记录,不会中断整个流程
965
- *
966
- * 编译流程详解:
967
- * 1. 解析阶段:使用 {@link parse} 将 Vue 代码解析为抽象语法树(AST)
968
- * 2. 转换阶段:使用 {@link transform} 将 Vue AST 转换为 React 中间表示(IR)
969
- * 3. 生成阶段:使用 {@link generate} 将 React IR 生成为目标代码
970
- * 4. 插件执行:在各阶段执行注册的插件,支持自定义扩展
666
+ * 基础编译器类 - Vue 到 React 代码转换的核心实现
971
667
  *
972
- * @example
973
- * ```typescript
974
- * // 创建基础编译器实例
975
- * const compiler = new BaseCompiler({
976
- * cache: true,
977
- * preprocessStyles: true,
978
- * plugins: {
979
- * parser: {
980
- * myParserPlugin: (result, ctx) => {
981
- * // 解析阶段插件
982
- * }
983
- * },
984
- * transformer: {
985
- * myTransformerPlugin: (result, ctx) => {
986
- * // 转换阶段插件
987
- * }
988
- * }
989
- * }
990
- * });
991
- *
992
- * // 编译单个 Vue 文件
993
- * const result = compiler.compile(vueSourceCode, '/path/to/Component.vue');
994
- *
995
- * // 获取编译结果
996
- * console.log(result.fileInfo); // 包含 JSX 和 CSS 文件信息
997
- * console.log(result.code); // 生成的 React 代码
998
- * ```
999
- *
1000
- * @remarks
1001
- * - 上下文管理:使用 {@link CompilationAdapter.createContext} 创建编译上下文
1002
- * - 错误处理:编译过程中的错误通过 {@link logger} 记录,不会抛出异常
1003
- * - 配置继承:继承 {@link Helper} 类的文件路径处理、缓存管理、格式化等工具方法
1004
- * - 版本管理:自动读取 package.json 中的版本号,便于调试和日志记录
1005
- *
1006
- * @see {@link FileCompiler} 用于文件系统级别的批量编译和增量构建
1007
- * @see {@link Helper} 提供基础的文件路径处理、缓存管理和格式化工具
1008
- * @see {@link CompilerOptions} 查看完整的编译器配置选项
1009
- * @see {@link CompilationResult} 编译结果的数据结构
668
+ * 继承自 Helper,提供单文件编译功能,支持插件系统和三阶段编译流程。
669
+ * 主要用于文件系统批量编译和单文件编译场景。
1010
670
  */
1011
671
  declare class BaseCompiler extends Helper {
1012
672
  version: string;
1013
673
  options: CompilerOptions;
1014
674
  private createContext;
675
+ constructor(options?: CompilerOptions);
676
+ /** 编译 Vue 源代码为 React 代码 */
677
+ compile(source: string, filename: string): CompilationResult;
678
+ private prepareGenerateOptions;
679
+ private resolveMainResult;
680
+ private resolveStyleResult;
681
+ }
682
+
683
+ declare class CleanupManager {
684
+ private fileCompiler;
685
+ constructor(fileCompiler: FileCompiler);
1015
686
  /**
1016
- * 创建基础编译器实例
1017
- *
1018
- * @param options - 编译器配置选项,包括缓存设置、插件配置、样式预处理等,
1019
- * 所有选项都会传递给父类 {@link Helper} 进行初始化
1020
- *
1021
- * @see {@link CompilerOptions} 查看完整的配置选项说明
687
+ * Delete the build artifacts and cache corresponding to the specified path.
1022
688
  */
1023
- constructor(options?: CompilerOptions);
689
+ removeOutputPath(targetPath: string, type: CacheKey): Promise<void>;
1024
690
  /**
1025
- * 编译 Vue 源代码为 React 代码
1026
- *
1027
- * 该方法执行完整的编译流程,包括:
1028
- * 1. 上下文初始化:创建独立的编译上下文
1029
- * 2. 解析阶段:将 Vue 源代码解析为抽象语法树(AST)
1030
- * 3. 转换阶段:将 Vue AST 转换为 React 中间表示(IR)
1031
- * 4. 生成阶段:将 React IR 生成为目标代码
1032
- * 5. 插件执行:在各阶段执行注册的插件
1033
- * 6. 结果处理:整理编译结果并返回
1034
- *
1035
- * 编译过程使用 try-finally 确保上下文资源被正确清理,
1036
- * 即使编译过程中发生错误也不会导致资源泄漏。
1037
- *
1038
- * @param source - Vue 源代码字符串
1039
- * @param filename - 源文件名,用于生成文件ID和输出路径
1040
- * @returns {CompilationResult} 编译结果,包含生成的代码和文件信息
1041
- *
1042
- * @example
1043
- * ```typescript
1044
- * const vueCode = `
1045
- * <template>
1046
- * <div class="container">{{ message }}</div>
1047
- * </template>
1048
- *
1049
- * <script setup lang="ts">
1050
- * import { ref } from 'vue;
1051
- * const message = ref('Hello Vue')
1052
- * </script>
1053
- *
1054
- * <style scoped>
1055
- * .container {
1056
- * padding: 20px;
1057
- * }
1058
- * </style>
1059
- * `;
1060
- *
1061
- * const result = compiler.compile(vueCode, 'MyComponent.vue');
1062
- * console.log(result);
1063
- * ```
1064
- *
1065
- * @throws 不会直接抛出异常,错误通过日志系统记录
1066
- * @see {@link parse} 解析函数
1067
- * @see {@link transform} 转换函数
1068
- * @see {@link generate} 生成函数
1069
- * @see {@link executePlugins} 插件执行函数
691
+ * Delete the build artifacts or asset files and cache corresponding to the specified path.
1070
692
  */
1071
- compile(source: string, filename: string): CompilationResult;
693
+ cleanupOldOutput(key: CacheKey, filter: (m: CacheMeta) => boolean): Promise<void>;
694
+ }
695
+
696
+ declare class AssetManager {
697
+ private fileCompiler;
698
+ private cleanupManager;
699
+ pipelineFiles: string[];
700
+ private skippedCount;
701
+ constructor(fileCompiler: FileCompiler, cleanupManager: CleanupManager);
1072
702
  /**
1073
- * 初始化 Babel 代码生成选项
1074
- *
1075
- * 合并用户自定义的生成选项与默认选项,确保代码生成的一致性和正确性。
1076
- * 默认配置优化了代码的可读性和性能:
1077
- * 1. 最小化 Unicode 转义:只转义必要的字符,保持代码可读性
1078
- * 2. 统一引号风格:使用单引号,符合 JavaScript 社区常见约定
1079
- * 3. 代码压缩:启用最小化,移除不必要的空白字符
1080
- *
1081
- * 如果用户提供了自定义选项,会与默认选项进行深度合并,
1082
- * 用户选项的优先级高于默认选项。
1083
- *
1084
- * @private
1085
- * @param filename - 可选的文件名,用于源码映射配置
1086
- * @returns {GeneratorOptions} 合并后的 Babel 生成选项
1087
- *
1088
- * @remarks
1089
- * - 源码映射支持:如果启用了 sourceMaps 且提供了文件名,会自动设置 sourceFileName
1090
- * - 选项合并策略:用户选项会覆盖默认选项,实现灵活的配置
1091
- * - 性能优化:默认启用 minified 以减少生成代码的体积
1092
- * - 编码安全:使用 jsesc 确保特殊字符的正确转义
1093
- *
1094
- * @example
1095
- * ```typescript
1096
- * // 默认选项
1097
- * const defaultOptions = {
1098
- * jsescOption: { minimal: true, quotes: 'single' },
1099
- * minified: true
1100
- * };
1101
- *
1102
- * // 用户自定义选项
1103
- * const userOptions = {
1104
- * minified: false, // 覆盖默认值
1105
- * sourceMaps: true // 新增选项
1106
- * };
1107
- *
1108
- * // 合并结果
1109
- * const merged = {
1110
- * jsescOption: { minimal: true, quotes: 'single' },
1111
- * minified: false, // 用户值
1112
- * sourceMaps: true // 用户值
1113
- * };
1114
- * ```
1115
- *
1116
- * @see {@link GeneratorOptions} Babel 生成选项类型定义
703
+ * 运行资源文件处理管线
1117
704
  */
1118
- private prepareGenerateOptions;
705
+ runAssetPipeline(): Promise<number>;
1119
706
  /**
1120
- * 处理 SFC Script 文件的编译结果
1121
- *
1122
- * 根据编译上下文中的输入类型(SFC 或 Script),整理并返回相应的编译结果。
1123
- * 对于 SFC 文件,返回包含 JSX 和 CSS 信息的完整结果;
1124
- * 对于 Script 文件,返回仅包含脚本信息的结果。
1125
- *
1126
- * 关键处理逻辑:
1127
- * 1. 构建基础结果:包含文件ID、路由信息和生成的代码
1128
- * 2. 确定输出路径:根据源文件路径和语言类型生成输出文件路径
1129
- * 3. 区分文件类型:
1130
- * - SFC 文件:生成 .tsx 扩展名,包含样式信息
1131
- * - Script 文件:保持原扩展名,不包含样式信息
1132
- * 4. 样式处理:提取样式文件路径、作用域ID和样式代码
1133
- *
1134
- * @private
1135
- * @param ir - React 中间表示(IR)描述符,包含转换后的组件信息
1136
- * @param gen - 代码生成结果,包含生成的代码和源码映射
1137
- * @param ctxData - 编译上下文数据,包含文件信息和编译状态
1138
- * @returns {CompilationResult} 整理后的编译结果
1139
- *
1140
- * @remarks
1141
- * - 文件扩展名处理:Vue SFC 文件会转换为 .tsx 或 .jsx 文件
1142
- * - 样式分离:SFC 中的样式会被提取到独立的 CSS 文件中
1143
- * - 路由检测:自动检测组件是否使用路由,用于依赖注入
1144
- * - 作用域样式:为 Scoped CSS 生成唯一的作用域ID
1145
- *
1146
- * @see {@link SFCCompilationResult} SFC 编译结果类型
1147
- * @see {@link ScriptCompilationResult} Script 编译结果类型
707
+ * Process single asset file, compare with cache and decide whether to copy.
1148
708
  */
1149
- private resolveMainResult;
709
+ processAsset(filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<FileCacheMeta | undefined>;
1150
710
  /**
1151
- * 处理 Style 文件的编译结果
1152
- * @param data style 文件解析结果
1153
- * @param ctxData 上下文数据
711
+ * 更新缓存
1154
712
  */
1155
- private resolveStyleResult;
713
+ private updateCache;
714
+ /**
715
+ * 获取跳过的文件数量
716
+ */
717
+ getSkippedCount(): number;
718
+ /**
719
+ * 重置跳过的文件数量
720
+ */
721
+ resetSkippedCount(): void;
1156
722
  }
1157
723
 
1158
- /**
1159
- * 文件系统编译器,负责将 Vue 项目批量转换为 React 项目。
1160
- *
1161
- * 此类继承自 {@link BaseCompiler},提供完整的文件系统级别编译功能,包括:
1162
- * 1. 批量扫描和编译 Vue 单文件组件(SFC)
1163
- * 2. 处理 JavaScript/TypeScript 脚本文件
1164
- * 3. 资源文件(图片、样式、字体等)的智能拷贝
1165
- * 4. 增量编译和高效的缓存管理
1166
- * 5. 文件系统监控和自动清理
1167
- * 6. Vite 项目环境自动初始化
1168
- *
1169
- * 主要特性:
1170
- * - 增量编译:基于文件哈希、大小和修改时间进行变更检测,跳过未变更的文件
1171
- * - 智能缓存:维护编译缓存,支持清理过期文件,提高编译效率
1172
- * - 资源处理:自动识别并拷贝非编译文件,保持目录结构
1173
- * - 错误隔离:单个文件编译失败不影响其他文件处理
1174
- * - 并发处理:支持 Promise.all 并发处理多个文件,提升编译速度
1175
- * - 路由感知:自动检测并注入 Vue Router 到 React Router 的转换依赖
1176
- *
1177
- * 架构设计:
1178
- * 该类采用管理器模式,将不同职责分解到独立的子管理器中:
1179
- * - {@link PipelineManager}: 管理编译管线流程
1180
- * - {@link FileProcessor}: 处理单个文件的编译逻辑
1181
- * - {@link CacheManager}: 管理编译缓存
1182
- * - {@link AssetManager}: 处理资源文件拷贝
1183
- * - {@link CleanupManager}: 清理过期文件和缓存
1184
- * - {@link ViteBootstrapper}: 初始化 Vite React 项目环境
1185
- *
1186
- * @example
1187
- * ```typescript
1188
- * // 创建文件编译器实例
1189
- * const compiler = new FileCompiler({
1190
- * input: 'src',
1191
- * exclude: ['src/main.ts'],
1192
- * output: {
1193
- * workspace: '.vureact',
1194
- * outDir: 'react-app',
1195
- * bootstrapVite: true
1196
- * },
1197
- * cache: true,
1198
- * watch: false
1199
- * });
1200
- *
1201
- * // 执行完整编译
1202
- * await compiler.execute();
1203
- *
1204
- * // 处理单个 Vue 文件(适用于 watch 模式)
1205
- * await compiler.processSFC('/path/to/Component.vue');
1206
- *
1207
- * // 处理单个 Script 文件
1208
- * await compiler.processScript('/path/to/foo.ts');
1209
- *
1210
- * // 处理单个资源文件
1211
- * await compiler.processAsset('/path/to/image.png');
1212
- *
1213
- * // 清理指定路径的输出文件
1214
- * await compiler.removeOutputPath('/path/to/old-component.vue', CacheKey.SFC);
1215
- * ```
1216
- *
1217
- * @remarks
1218
- * - 编译流程:遵循 "环境初始化 → SFC编译 → Script编译 → 资源拷贝" 的顺序
1219
- * - 缓存机制:使用文件哈希、大小和修改时间三重验证确保准确性
1220
- * - 目录结构:保持输入目录结构到输出目录,便于项目迁移
1221
- * - 错误处理:编译错误会记录到日志系统,但不会中断整个编译流程
1222
- * - 性能优化:支持并发处理和增量编译,大幅提升大型项目编译速度
1223
- *
1224
- * @see {@link BaseCompiler} 提供核心的单文件编译功能
1225
- * @see {@link Helper} 提供基础的文件路径处理和工具方法
1226
- * @see {@link CompilerOptions} 完整的编译器配置选项
1227
- */
1228
- declare class FileCompiler extends BaseCompiler {
1229
- private skippedCount;
1230
- private spinner;
1231
- private viteBootstrapper;
1232
- private pipelineManager;
1233
- private fileProcessor;
1234
- private compilationUnitProcessor;
1235
- private cacheManager;
1236
- private assetManager;
1237
- private cleanupManager;
724
+ declare class CacheManager {
725
+ private fileCompiler;
726
+ private pendingUpdates;
727
+ constructor(fileCompiler: FileCompiler);
1238
728
  /**
1239
- * 创建文件系统编译器实例
1240
- *
1241
- * @param options - 编译器配置选项,继承自 BaseCompiler 的选项
1242
- * 包括输入路径、输出配置、缓存设置、排除模式等
1243
- * @see {@link CompilerOptions} 查看完整的选项说明
729
+ * 批量更新缓存记录
1244
730
  */
1245
- constructor(options?: CompilerOptions);
731
+ updateCacheIncrementally(unit: CompilationUnit, key: CacheKey): Promise<void>;
1246
732
  /**
1247
- * 初始化所有管理器实例
1248
- *
1249
- * 按照依赖关系顺序创建各个管理器:
1250
- * 1. 基础管理器(无依赖)
1251
- * 2. 依赖基础管理器的管理器
1252
- * 3. 依赖其他管理器的管理器
1253
- *
1254
- * @private
733
+ * 批量保存缓存
1255
734
  */
1256
- private initializeManagers;
735
+ flushCache(key: CacheKey): Promise<void>;
1257
736
  /**
1258
- * 执行完整的编译流程
1259
- *
1260
- * 该方法执行以下步骤:
1261
- * 1. 环境初始化:清理旧输出(如果禁用缓存)并初始化 Vite 项目环境
1262
- * 2. Vue 文件编译:批量处理所有 .vue 文件
1263
- * 3. Script 文件编译:批量处理所有 .js/.ts 文件
1264
- * 4. 资源文件拷贝:处理剩余的非编译文件
1265
- * 5. 统计输出:显示编译结果和性能统计
1266
- *
1267
- * @async
1268
- * @throws {Error} 当编译过程中发生致命错误时抛出
1269
- * @returns {Promise<void>}
737
+ * 更新缓存
1270
738
  */
1271
- execute(): Promise<void>;
739
+ private updateCache;
740
+ }
741
+
742
+ declare class CompilationUnitProcessor {
743
+ private fileCompiler;
744
+ constructor(fileCompiler: FileCompiler);
1272
745
  /**
1273
- * 运行管线并显示加载动画
1274
- *
1275
- * @private
1276
- * @param text - 加载动画显示的文本
1277
- * @param pipelineFn - 要执行的管线函数
1278
- * @returns 返回的处理的文件数
746
+ * 处理编译单元,落地成对应代码和文件
1279
747
  */
1280
- private runPipelineWithSpinner;
748
+ resolve(unit: CompilationUnit, key: CacheKey): Promise<CompilationUnit>;
749
+ private resolveResult;
1281
750
  /**
1282
- * 处理单个 Vue 单文件组件(SFC)
1283
- *
1284
- * 此方法主要用于 CLI watch 模式,当检测到文件变更时调用。
1285
- * 支持增量编译,如果文件未变更则跳过编译。
1286
- *
1287
- * @async
1288
- * @param filePath - Vue 文件的绝对路径
1289
- * @param existingCache - 可选的预加载缓存对象,用于增量编译
1290
- * @returns {Promise<SFCUnit | undefined>} 编译单元对象,如果跳过编译则返回 undefined
1291
- * @see {@link FileProcessor.processSFC}
751
+ * 将编译产物写入磁盘
752
+ */
753
+ saveCompiledFiles(unit: CompilationUnit, key: CacheKey): Promise<void>;
754
+ }
755
+
756
+ declare class FileProcessor {
757
+ private fileCompiler;
758
+ private compilationUnitProcessor;
759
+ private cacheManager;
760
+ private skippedCount;
761
+ constructor(fileCompiler: FileCompiler, compilationUnitProcessor: CompilationUnitProcessor, cacheManager: CacheManager);
762
+ /**
763
+ * Process a single Vue file (this method is called directly in CLI Watch mode)
1292
764
  */
1293
765
  processSFC(filePath: string, existingCache?: LoadedCache<Vue2ReactCacheMeta>): Promise<SFCUnit | undefined>;
1294
766
  /**
1295
- * 处理单个 JavaScript/TypeScript 脚本文件
1296
- *
1297
- * 此方法主要用于 CLI 的 watch 模式,当检测到文件变更时调用。
1298
- * 支持增量编译,如果文件未变更则跳过编译。
1299
- *
1300
- * @async
1301
- * @param filePath - 脚本文件的绝对路径
1302
- * @param existingCache - 可选的预加载缓存对象,用于增量编译
1303
- * @returns {Promise<ScriptUnit | undefined>} 编译单元对象,如果跳过编译则返回 undefined
1304
- * @see {@link FileProcessor.processScript}
767
+ * Process a single script file (this method is called directly in CLI Watch mode)
1305
768
  */
1306
769
  processScript(filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<ScriptUnit | undefined>;
1307
770
  /**
1308
- * 处理单个 CSS/LESS/SCSS 样式文件
1309
- *
1310
- * 此方法主要用于 CLI 的 watch 模式,当检测到文件变更时调用。
1311
- * 支持增量编译,如果文件未变更则跳过编译。
1312
- *
1313
- * @async
1314
- * @param filePath - style 文件的绝对路径
1315
- * @param existingCache - 可选的预加载缓存对象,用于增量编译
1316
- * @returns {Promise<ScriptUnit | undefined>} 编译单元对象,如果跳过编译则返回 undefined
1317
- * @see {@link FileProcessor.processStyle}
771
+ * Process a single style file (this method is called directly in CLI Watch mode)
1318
772
  */
1319
773
  processStyle(filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<StyleUnit | undefined>;
1320
774
  /**
1321
- * 处理单个文件(Vue Script)
1322
- *
1323
- * 通用的文件处理方法,根据 CacheKey 类型自动选择处理逻辑。
1324
- * 主要用于内部调用和统一的文件处理接口。
1325
- *
1326
- * @async
1327
- * @param key - 文件类型标识(SFC 或 SCRIPT)
1328
- * @param filePath - 文件的绝对路径
1329
- * @param existingCache - 可选的预加载缓存对象
1330
- * @returns {Promise<SFCUnit | ScriptUnit | undefined>} 编译单元对象
1331
- * @see {@link FileProcessor.processFile}
775
+ * Process a single vue file
1332
776
  */
1333
- processFile(key: CacheKey, filePath: string, existingCache?: LoadedCache): Promise<CompilationUnit | undefined>;
777
+ processFile(key: CacheKey.SFC, filePath: string, existingCache?: LoadedCache<Vue2ReactCacheMeta>): Promise<SFCUnit | undefined>;
1334
778
  /**
1335
- * 处理单个资源文件
1336
- *
1337
- * 比较文件与缓存,决定是否需要拷贝。
1338
- * 资源文件包括图片、字体、样式文件等非编译文件。
1339
- *
1340
- * @async
1341
- * @param filePath - 资源文件的绝对路径
1342
- * @param existingCache - 可选的预加载缓存对象
1343
- * @returns {Promise<FileCacheMeta>} 资源文件的缓存元数据
1344
- * @see {@link AssetManager.processAsset}
779
+ * Process a single script file
1345
780
  */
1346
- processAsset(filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<FileCacheMeta>;
781
+ processFile(key: CacheKey.SCRIPT, filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<ScriptUnit | undefined>;
1347
782
  /**
1348
- * 删除指定路径对应的输出文件和缓存
1349
- *
1350
- * 当源文件被删除或重命名时,需要清理对应的输出文件。
1351
- * 主要用于 watch 模式下的文件删除处理。
1352
- *
1353
- * @async
1354
- * @param targetPath - 源代码的路径(文件或文件夹)
1355
- * @param type - 清理类型,指定是 SFC、SCRIPT 还是 ASSET
1356
- * @returns {Promise<void>}
1357
- * @see {@link CleanupManager.removeOutputPath}
783
+ * Process a single style file
1358
784
  */
1359
- removeOutputPath(targetPath: string, type: CacheKey): Promise<void>;
785
+ processFile(key: CacheKey.STYLE, filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<StyleUnit | undefined>;
786
+ /**
787
+ * Process a single vue/script/style file
788
+ */
789
+ processFile(key: CacheKey, filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<CompilationUnit | undefined>;
790
+ /**
791
+ * 对 package.json 注入路由依赖项
792
+ */
793
+ private addRouterToPackageJson;
794
+ /**
795
+ * 注入路由提供器到 React 应用的入口文件(如 main.tsx)
796
+ */
797
+ private updateEntryWithRouterProvider;
1360
798
  /**
1361
799
  * 获取跳过的文件数量
1362
- *
1363
- * 在增量编译中,未变更的文件会被跳过。
1364
- * 此方法返回当前编译会话中跳过的文件数量。
1365
- *
1366
- * @returns {number} 跳过的文件数量
1367
800
  */
1368
801
  getSkippedCount(): number;
1369
802
  /**
1370
803
  * 重置跳过的文件数量
1371
- *
1372
- * 在每次新的编译会话开始时调用,重置计数器。
1373
804
  */
1374
805
  resetSkippedCount(): void;
806
+ }
807
+
808
+ declare class PipelineManager {
809
+ private fileCompiler;
810
+ private fileProcessor;
811
+ private skippedCount;
812
+ private cleanupManager;
813
+ constructor(fileCompiler: FileCompiler, fileProcessor: FileProcessor);
1375
814
  /**
1376
- * 显示编译统计信息
1377
- *
1378
- * 在编译完成后调用,显示以下信息:
1379
- * 1. 编译耗时
1380
- * 2. 输出目录
1381
- * 3. 跳过的文件数量
1382
- * 4. 处理的文件分类统计
1383
- *
1384
- * @private
1385
- * @param endTime - 格式化后的编译耗时字符串
1386
- * @param sfcCount - 处理的 Vue 文件数量
1387
- * @param scriptCount - 处理的脚本文件数量
1388
- * @param assetCount - 处理的资源文件数量
815
+ * 运行 SFC 编译管线
1389
816
  */
817
+ runSfcPipeline(): Promise<number>;
818
+ /**
819
+ * 运行 Script 编译管线
820
+ */
821
+ runScriptPipeline(): Promise<number>;
822
+ /**
823
+ * 运行 Style 编译管线
824
+ */
825
+ runStylePipeline(): Promise<number>;
826
+ /**
827
+ * 核心编译管线
828
+ */
829
+ private runCorePipeline;
830
+ /**
831
+ * 获取跳过的文件数量
832
+ */
833
+ getSkippedCount(): number;
834
+ /**
835
+ * 重置跳过的文件数量
836
+ */
837
+ resetSkippedCount(): void;
838
+ }
839
+
840
+ /**
841
+ * Vite 环境初始化管理器
842
+ */
843
+ declare class ViteBootstrapper {
844
+ private fileCompiler;
845
+ private options;
846
+ private spinner;
847
+ private defaultConfig;
848
+ constructor(fileCompiler: FileCompiler, options: CompilerOptions);
849
+ /**
850
+ * 检查是否需要初始化 Vite 环境
851
+ */
852
+ private isSingleFile;
853
+ /**
854
+ * 利用 Vite 官方脚手架创建标准 React 环境
855
+ */
856
+ bootstrapIfNeeded(): Promise<boolean | undefined>;
857
+ /**
858
+ * 执行 vite 创建命令
859
+ */
860
+ private resolveViteCreateApp;
861
+ /**
862
+ * 处理 React 包版本
863
+ * @param ver 版本号
864
+ */
865
+ private resolveReactVersion;
866
+ }
867
+
868
+ interface CompilerManager {
869
+ viteBootstrapper: ViteBootstrapper;
870
+ fileProcessor: FileProcessor;
871
+ pipelineManager: PipelineManager;
872
+ assetManager: AssetManager;
873
+ cacheManager: CacheManager;
874
+ cleanupManager: CleanupManager;
875
+ }
876
+
877
+ /**
878
+ * 文件系统编译器 - 将 Vue 项目批量转换为 React 项目
879
+ *
880
+ * 提供完整的文件系统级别编译功能,包括 SFC、脚本、样式编译,
881
+ * 资源拷贝、增量编译、缓存管理和 Vite 环境初始化。
882
+ */
883
+ declare class FileCompiler extends BaseCompiler {
884
+ manager: CompilerManager;
885
+ private spinner;
886
+ constructor(options?: CompilerOptions);
887
+ /** 执行完整的编译流程 */
888
+ execute(): Promise<void>;
889
+ /** 处理单个 Vue 单文件组件(SFC) */
890
+ processSFC(filePath: string, existingCache?: LoadedCache<Vue2ReactCacheMeta>): Promise<SFCUnit | undefined>;
891
+ /** 处理单个 JavaScript/TypeScript 脚本文件 */
892
+ processScript(filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<ScriptUnit | undefined>;
893
+ /** 处理单个 CSS/LESS/SCSS 样式文件 */
894
+ processStyle(filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<StyleUnit | undefined>;
895
+ /** 处理单个文件(Vue 或 Script) */
896
+ processFile(key: CacheKey, filePath: string, existingCache?: LoadedCache): Promise<CompilationUnit | undefined>;
897
+ /** 处理单个资源文件 */
898
+ processAsset(filePath: string, existingCache?: LoadedCache<FileCacheMeta>): Promise<FileCacheMeta | undefined>;
899
+ /** 批量保存缓存 */
900
+ flushCache(key: CacheKey): Promise<void>;
901
+ /** 删除指定路径对应的输出文件和缓存 */
902
+ removeOutputPath(targetPath: string, type: CacheKey): Promise<void>;
903
+ private runPipelineWithSpinner;
1390
904
  private showCompileStats;
905
+ private resetSkippedCount;
1391
906
  }
1392
907
 
1393
908
  /**
1394
- * @see {@link CompilerOptions}
909
+ * Type helper to make it easier to use vureact.config.js
910
+ * accepts a direct {@link UserConfig} object, or a function that returns it.
1395
911
  */
1396
- declare function defineConfig(options: CompilerOptions): CompilerOptions;
912
+ declare function defineConfig(config: CompilerOptions): CompilerOptions;
913
+ declare function defineConfig(config: UserConfigFnObject): CompilerOptions;
914
+ type UserConfigFnObject = () => CompilerOptions;
1397
915
 
1398
916
  /**
1399
917
  * Next Vue to React compiler, compiles Vue 3 syntax into runnable React 18+ code.
@@ -1406,4 +924,241 @@ declare function defineConfig(options: CompilerOptions): CompilerOptions;
1406
924
  declare class VuReact extends FileCompiler {
1407
925
  }
1408
926
 
1409
- export { type AssetUnit, type BaseCompilationResult, BaseCompiler, type BaseUnit, type CacheCheckResult, CacheKey, type CacheList, type CacheMeta, type CompilationResult, type CompilationUnit, type CompilerOptions, type FileCacheMeta, FileCompiler, type FileMeta, type GeneratorOptions, type GeneratorResult, Helper, type LoadedCache, type ParseResult, type ParserOptions, type PluginRegister, type ReactIRDescriptor, type SFCCompilationResult, type SFCUnit, type ScriptCompilationResult, type ScriptUnit, type StyleCompilationResult, type StyleUnit, type TransformerOptions, VuReact, type Vue2ReactCacheMeta, defineConfig, generate, generateComponent, generateOnlyScript, parse, parseOnlyScript, parseSFC, transform };
927
+ interface ParseResult {
928
+ template: BlockIR<SFCTemplateBlock, RootNode>;
929
+ script: BlockIR<SFCScriptBlock, ParseResult$1>;
930
+ style: BlockIR<SFCStyleBlock, undefined>;
931
+ }
932
+ type BlockIR<S, T> = {
933
+ source?: S;
934
+ ast: T;
935
+ } | null;
936
+ interface ParserOptions {
937
+ plugins?: PluginRegister<ParseResult>;
938
+ }
939
+ /**
940
+ * 解析 Vue 单文件组件(SFC)源码,生成结构化解析结果。
941
+ *
942
+ * 此函数是解析阶段的核心入口,
943
+ * 负责将 Vue SFC 源码解析为包含模板、脚本、样式等各个块的结构化数据。
944
+ *
945
+ * @param source - Vue 单文件组件的源码字符串
946
+ * @param ctx - 编译上下文对象
947
+ * @param plugins - 可选的插件注册表,用于对解析结果进行自定义处理和增强
948
+ *
949
+ * @returns 解析结果对象,包含模板、脚本、样式块的解析信息
950
+ * @throws 不会直接抛出异常,错误信息会通过日志系统记录
951
+ */
952
+ declare function parseSFC(source: string, ctx: ICompilationContext, options?: ParserOptions): ParseResult;
953
+
954
+ /**
955
+ * 仅用于解析 script 文件,参数与 parseSFC 一致
956
+ */
957
+ declare function parseOnlyScript(source: string, ctx: ICompilationContext, options?: ParserOptions): ParseResult;
958
+
959
+ /**
960
+ * 解析 Vue 组件源码的统一入口函数。
961
+ *
962
+ * 根据输入类型自动选择解析器:
963
+ * - `sfc`: 解析 Vue 单文件组件(包含 template/script/style)
964
+ * - `script-*`: 仅解析脚本文件(如 .js、.ts)
965
+ *
966
+ * @param source - 源码字符串
967
+ * @param ctx - 编译上下文,包含输入类型、文件名等信息
968
+ * @param options - 可选的解析器配置,如插件
969
+ * @returns 解析结果对象,包含模板、脚本、样式的结构化数据
970
+ */
971
+ declare function parse(source: string, ctx: ICompilationContext, options?: ParserOptions): ParseResult;
972
+
973
+ interface ScriptBlockIR {
974
+ /** Transformed full script AST (used for script-only input). */
975
+ scriptAST?: ParseResult$1;
976
+ imports: t.ImportDeclaration[];
977
+ exports: t.ExportDeclaration[];
978
+ tsTypes: t.TypeScript[];
979
+ /** Executable statements extracted from script block. */
980
+ statement: {
981
+ /** Statements hoisted outside component function. */
982
+ global: t.Node[];
983
+ /** Statements kept inside component function. */
984
+ local: ParseResult$1<t.File> | null;
985
+ };
986
+ }
987
+
988
+ declare const enum NodeTypes {
989
+ FRAGMENT = 0,
990
+ ELEMENT = 1,
991
+ TEXT = 2,
992
+ COMMENT = 3,
993
+ JSX_INTERPOLATION = 4
994
+ }
995
+ declare const enum PropTypes {
996
+ ATTRIBUTE = 1,
997
+ SLOT = 2,
998
+ EVENT = 3,
999
+ DYNAMIC_ATTRIBUTE = 4
1000
+ }
1001
+ interface BabelExp<T = Expression> {
1002
+ content: string;
1003
+ ast: T;
1004
+ }
1005
+
1006
+ interface BaseSimpleNodeIR {
1007
+ type: NodeTypes;
1008
+ content: string;
1009
+ babelExp: Expression;
1010
+ }
1011
+ interface FragmentNodeIR {
1012
+ type: NodeTypes;
1013
+ children: TemplateChildNodeIR[];
1014
+ }
1015
+
1016
+ interface ElementNodeIR extends BaseElementNodeIR {
1017
+ type: NodeTypes;
1018
+ props: (PropsIR | SlotPropsIR)[];
1019
+ children: TemplateChildNodeIR[];
1020
+ meta: Partial<ElementNodeIRMeta>;
1021
+ conditionIsHandled: boolean;
1022
+ isBuiltIn?: boolean;
1023
+ isRoute?: boolean;
1024
+ }
1025
+ interface BaseElementNodeIR {
1026
+ tag: string;
1027
+ isComponent?: boolean;
1028
+ isSelfClosing?: boolean;
1029
+ ref?: string;
1030
+ loc?: SourceLocation;
1031
+ }
1032
+ interface ElementNodeIRMeta {
1033
+ condition: ConditionMeta;
1034
+ loop: LoopMeta;
1035
+ memo: MemoMeta;
1036
+ show: ShowMeta;
1037
+ }
1038
+ type ConditionMeta = {
1039
+ if?: boolean;
1040
+ elseIf?: boolean;
1041
+ else?: boolean;
1042
+ value: string;
1043
+ babelExp: BabelExp;
1044
+ next?: ElementNodeIR;
1045
+ isHandled: boolean;
1046
+ };
1047
+ type LoopMeta = {
1048
+ isLoop?: boolean;
1049
+ value: {
1050
+ source: string;
1051
+ value: string;
1052
+ key?: string;
1053
+ index?: string;
1054
+ };
1055
+ isHandled: boolean;
1056
+ };
1057
+ type MemoMeta = {
1058
+ isMemo?: boolean;
1059
+ value: string;
1060
+ babelExp: BabelExp<ArrayExpression>;
1061
+ isHandled: boolean;
1062
+ };
1063
+ type ShowMeta = {
1064
+ isShow?: boolean;
1065
+ value: string;
1066
+ babelExp: BabelExp;
1067
+ };
1068
+
1069
+ interface PropsIR {
1070
+ type: PropTypes;
1071
+ rawName?: string;
1072
+ name: string;
1073
+ isStatic?: boolean;
1074
+ modifiers?: string[];
1075
+ value: PropIRValue;
1076
+ isKeyLessVBind?: boolean;
1077
+ babelExp: BabelExp<JSXIdentifier | Expression>;
1078
+ }
1079
+ type PropIRValue = {
1080
+ content: string;
1081
+ isStringLiteral?: boolean;
1082
+ merge?: string[];
1083
+ babelExp: BabelExp;
1084
+ };
1085
+
1086
+ interface SlotPropsIR {
1087
+ type: PropTypes.SLOT;
1088
+ name: string;
1089
+ rawName: string;
1090
+ isStatic: boolean;
1091
+ isScoped: boolean;
1092
+ content?: TemplateChildNodeIR[];
1093
+ callback?: {
1094
+ arg: string;
1095
+ exp: TemplateChildNodeIR[];
1096
+ };
1097
+ }
1098
+
1099
+ interface TemplateBlockIR {
1100
+ children: TemplateChildNodeIR[];
1101
+ }
1102
+ type TemplateChildNodeIR = ElementNodeIR | BaseSimpleNodeIR | FragmentNodeIR;
1103
+
1104
+ interface ReactIRDescriptor {
1105
+ template: TemplateBlockIR;
1106
+ script: ScriptBlockIR;
1107
+ style?: string;
1108
+ }
1109
+ interface TransformerOptions {
1110
+ plugins?: PluginRegister<ReactIRDescriptor>;
1111
+ }
1112
+ /**
1113
+ * 将 Vue SFC 解析结果转换为 React 中间表示(IR)。
1114
+ *
1115
+ * 此函数是转换阶段的核心入口,负责将 Vue 组件的解析结果(AST)转换为
1116
+ * 适合生成 React 代码的中间表示形式。转换过程包括模板转换和脚本转换。
1117
+ *
1118
+ * @param ast - Vue SFC 的解析结果,来自 {@link parse} 函数的返回值
1119
+ * @param ctx - 编译上下文对象
1120
+ * @param plugins - 可选的插件注册表,用于对转换结果进行自定义处理和增强
1121
+ *
1122
+ * @returns React 中间表示描述符,包含模板、脚本和样式的转换结果
1123
+ */
1124
+ declare function transform(ast: ParseResult, ctx: ICompilationContext, options?: TransformerOptions): ReactIRDescriptor;
1125
+
1126
+ interface GeneratorOptions extends GeneratorOptions$1 {
1127
+ plugins?: PluginRegister<GeneratorResult>;
1128
+ }
1129
+ interface GeneratorResult {
1130
+ ast: t.Program | ParseResult$1;
1131
+ code: string;
1132
+ source: string;
1133
+ }
1134
+ /**
1135
+ * 将 React 中间表示(IR)生成为可执行的 JSX/TSX 代码。
1136
+ *
1137
+ * 此函数是代码生成阶段的核心入口,负责将转换后的 React IR 转换为
1138
+ * 完整的 AST 并最终生成源代码。生成过程包括 JSX 构建和脚本构建。
1139
+ *
1140
+ * @param ir - React 中间表示描述符,来自 {@link transform} 函数的返回值
1141
+ * @param ctx - 编译上下文对象
1142
+ * @param options - 可选的生成选项,包括 Babel 生成器选项和插件
1143
+ *
1144
+ * @returns 生成结果对象,包含 AST、生成的代码和原始源码引用
1145
+ */
1146
+ declare function generateComponent(ir: ReactIRDescriptor, ctx: ICompilationContext, options?: GeneratorOptions): GeneratorResult;
1147
+
1148
+ declare function generateOnlyScript(ir: ReactIRDescriptor, ctx: ICompilationContext, options?: GeneratorOptions): GeneratorResult;
1149
+
1150
+ /**
1151
+ * 代码生成的统一入口函数。
1152
+ *
1153
+ * 根据输入类型自动选择生成器:
1154
+ * - `sfc`: 生成完整的 React 组件(包含 JSX 和脚本)
1155
+ * - `script-*`: 仅生成脚本代码(如 .js、.ts)
1156
+ *
1157
+ * @param ir - React 中间表示描述符,来自转换阶段的结果
1158
+ * @param ctx - 编译上下文,包含输入类型、源码等信息
1159
+ * @param options - 可选的生成器配置,如 Babel 生成选项和插件
1160
+ * @returns 生成结果对象,包含 AST、生成的代码和原始源码
1161
+ */
1162
+ declare function generate(ir: ReactIRDescriptor, ctx: ICompilationContext, options?: GeneratorOptions): GeneratorResult;
1163
+
1164
+ export { type AssetUnit, type BaseCompilationResult, BaseCompiler, type BaseUnit, type CacheCheckResult, CacheKey, type CacheList, type CacheMeta, type CompilationResult, type CompilationUnit, type CompilerOptions, type CompilerPlugins, type FileCacheMeta, FileCompiler, type FileMeta, type FormatConfig, type GeneratorOptions, type GeneratorResult, Helper, type LoadedCache, type LoggingConfig, type OutputConfig, type ParseResult, type ParserOptions, type PluginRegister, type ReactIRDescriptor, type RouterConfig, type SFCCompilationResult, type SFCUnit, type ScriptCacheMeta, type ScriptCompilationResult, type ScriptUnit, type StyleCacheMeta, type StyleCompilationResult, type StyleUnit, type TransformerOptions, type UserConfigFnObject, VuReact, type Vue2ReactCacheMeta, defineConfig, generate, generateComponent, generateOnlyScript, parse, parseOnlyScript, parseSFC, transform };