@vureact/compiler-core 1.6.2 → 1.7.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.
- package/README.en.md +20 -235
- package/README.md +21 -236
- package/lib/{chunk-FLZ5AUPG.js → chunk-Q36XF5TP.js} +749 -473
- package/lib/{chunk-SRBJ5B5X.esm.js → chunk-Q7HZAZHD.esm.js} +625 -349
- package/lib/cli.esm.js +2 -2
- package/lib/cli.js +10 -10
- package/lib/compiler-core.esm.js +2 -2
- package/lib/compiler-core.js +3 -3
- package/package.json +2 -1
package/README.en.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# @vureact/compiler-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Write Vue, Ship Production-Ready React** — Build React 18+ apps with the Vue 3 mental model.
|
|
4
4
|
|
|
5
5
|
[](https://vureact.top/en/)
|
|
6
|
-
[](https://www.npmjs.com/package/@vureact/compiler-core)
|
|
7
|
+
[](https://www.npmjs.com/package/@vureact/compiler-core)
|
|
8
8
|
[](https://nodejs.org/)
|
|
9
9
|
[](https://opensource.org/licenses/MIT)
|
|
10
10
|
[](https://vuejs.org/)
|
|
@@ -12,107 +12,21 @@ A compiler that allows you to write React 18+ applications using Vue 3 syntax.
|
|
|
12
12
|
|
|
13
13
|
English | [简体中文](./README.md)
|
|
14
14
|
|
|
15
|
-
##
|
|
16
|
-
|
|
17
|
-
This section will guide you through creating, compiling, and running your first VuReact project; alternatively, you can check out the [playground](https://codesandbox.io/p/github/vureact-js/example-crm-admin-backend/master).
|
|
18
|
-
|
|
19
|
-
After completion, you will clearly understand three things:
|
|
20
|
-
|
|
21
|
-
1. Under what conventions input SFCs can be stably converted
|
|
22
|
-
2. What the compiled directory structure looks like
|
|
23
|
-
3. The semantic correspondence between the output TSX and the original SFC
|
|
24
|
-
4. The compiler automatically analyzes and appends dependencies, eliminating the need to manually manage React hooks dependencies
|
|
15
|
+
## Introduction
|
|
25
16
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
First, set up a minimal project (illustration):
|
|
29
|
-
|
|
30
|
-
```txt
|
|
31
|
-
my-app/
|
|
32
|
-
├─ src/
|
|
33
|
-
│ ├─ components/
|
|
34
|
-
│ │ └─ Counter.vue
|
|
35
|
-
│ ├─ main.ts
|
|
36
|
-
│ └─ index.css
|
|
37
|
-
├─ package.json
|
|
38
|
-
└─ vureact.config.js
|
|
39
|
-
```
|
|
17
|
+
`@vureact/compiler-core` is the core compilation package of VuReact, responsible for compiling Vue 3 SFC, script files, and style files into React 18+ JSX/TSX code ready for production.
|
|
40
18
|
|
|
41
|
-
##
|
|
42
|
-
|
|
43
|
-
Install the VuReact compiler in your Vue project:
|
|
19
|
+
## Installation
|
|
44
20
|
|
|
45
21
|
```bash
|
|
46
|
-
# Using npm
|
|
47
22
|
npm install -D @vureact/compiler-core
|
|
48
|
-
|
|
49
|
-
# Using yarn
|
|
50
|
-
yarn add -D @vureact/compiler-core
|
|
51
|
-
|
|
52
|
-
# Using pnpm
|
|
53
|
-
pnpm add -D @vureact/compiler-core
|
|
54
23
|
```
|
|
55
24
|
|
|
56
|
-
##
|
|
57
|
-
|
|
58
|
-
`src/components/Counter.vue`
|
|
59
|
-
|
|
60
|
-
```html
|
|
61
|
-
<template>
|
|
62
|
-
<section class="counter-card">
|
|
63
|
-
<h2>{{ props.title || title }}</h2>
|
|
64
|
-
<p>Count: {{ count }}</p>
|
|
65
|
-
<button @click="increment">+1</button>
|
|
66
|
-
<button @click="methods.decrease">-1</button>
|
|
67
|
-
</section>
|
|
68
|
-
</template>
|
|
69
|
-
|
|
70
|
-
<script setup lang="ts">
|
|
71
|
-
// @vr-name: Counter (Note: Tells the compiler what component name to generate)
|
|
72
|
-
import { computed, ref } from 'vue';
|
|
73
|
-
|
|
74
|
-
// You can also use macros to define component names
|
|
75
|
-
defineOptions({ name: 'Counter' });
|
|
76
|
-
|
|
77
|
-
// Define props
|
|
78
|
-
const props = defineProps<{ title?: string }>();
|
|
79
|
-
|
|
80
|
-
// Define emits
|
|
81
|
-
const emits = defineEmits<{
|
|
82
|
-
(e: 'change'): void;
|
|
83
|
-
(e: 'update', value: number): number;
|
|
84
|
-
}>();
|
|
85
|
-
|
|
86
|
-
const step = ref(1);
|
|
87
|
-
const count = ref(0);
|
|
88
|
-
const title = computed(() => `Counter x${step.value}`);
|
|
89
|
-
|
|
90
|
-
const increment = () => {
|
|
91
|
-
count.value += step.value;
|
|
92
|
-
emits('update', count.value);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const methods = {
|
|
96
|
-
decrease() {
|
|
97
|
-
count.value -= step.value;
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
</script>
|
|
101
|
-
|
|
102
|
-
<style lang="less" scoped>
|
|
103
|
-
@border-color: #ddd;
|
|
104
|
-
@border-radius: 8px;
|
|
105
|
-
@padding-base: 12px;
|
|
25
|
+
## Quick Start
|
|
106
26
|
|
|
107
|
-
|
|
108
|
-
border: 1px solid @border-color;
|
|
109
|
-
border-radius: @border-radius;
|
|
110
|
-
padding: @padding-base;
|
|
111
|
-
}
|
|
112
|
-
</style>
|
|
113
|
-
```
|
|
27
|
+
👉 **Full tutorial: [VuReact Website - Quick Start](https://vureact.top/en/guide/quick-start.html)**
|
|
114
28
|
|
|
115
|
-
|
|
29
|
+
### Configuration Example
|
|
116
30
|
|
|
117
31
|
`vureact.config.ts`
|
|
118
32
|
|
|
@@ -120,164 +34,35 @@ pnpm add -D @vureact/compiler-core
|
|
|
120
34
|
import { defineConfig } from '@vureact/compiler-core';
|
|
121
35
|
|
|
122
36
|
export default defineConfig({
|
|
123
|
-
input: 'src',
|
|
124
|
-
// Key: Exclude Vue entry files to avoid entry semantic conflicts
|
|
37
|
+
input: './src',
|
|
125
38
|
exclude: ['src/main.ts'],
|
|
126
39
|
output: {
|
|
127
40
|
workspace: '.vureact',
|
|
128
|
-
outDir: '
|
|
129
|
-
|
|
130
|
-
// Recommended to enable in actual use
|
|
131
|
-
bootstrapVite: false,
|
|
41
|
+
outDir: 'react-app',
|
|
42
|
+
bootstrapVite: true,
|
|
132
43
|
},
|
|
133
44
|
});
|
|
134
45
|
```
|
|
135
46
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
### Method 1: Use the npx command
|
|
139
|
-
|
|
140
|
-
Run in the root directory:
|
|
141
|
-
|
|
142
|
-
```bash
|
|
143
|
-
npx vureact build
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### Method 2: Use npm scripts
|
|
147
|
-
|
|
148
|
-
Add script commands to `package.json`:
|
|
149
|
-
|
|
150
|
-
```json
|
|
151
|
-
"scripts": {
|
|
152
|
-
"watch": "vureact watch",
|
|
153
|
-
"build": "vureact build"
|
|
154
|
-
}
|
|
155
|
-
```
|
|
47
|
+
### Commands
|
|
156
48
|
|
|
157
49
|
```bash
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
## Step 5: View the Output Directory Tree
|
|
162
|
-
|
|
163
|
-
Compiled directory (illustration):
|
|
164
|
-
|
|
165
|
-
```txt
|
|
166
|
-
my-app/
|
|
167
|
-
├─ .vureact/
|
|
168
|
-
│ ├─ cache/
|
|
169
|
-
│ │ └─ _metadata.json
|
|
170
|
-
│ └─ dist/
|
|
171
|
-
│ └─ src/
|
|
172
|
-
│ └─ components/
|
|
173
|
-
│ ├─ Counter.tsx
|
|
174
|
-
│ └─ Counter-<hash>.css
|
|
175
|
-
├─ src/
|
|
176
|
-
│ └─ ...
|
|
177
|
-
└─ vureact.config.js
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
## Step 6: Compare the Generated Results
|
|
181
|
-
|
|
182
|
-
Below is a typical formatted output (slightly simplified for illustration; the actual hash and property names are subject to local output):
|
|
183
|
-
|
|
184
|
-
```tsx
|
|
185
|
-
import { memo, useCallback, useMemo } from 'react';
|
|
186
|
-
import { useComputed, useVRef } from '@vureact/runtime-core';
|
|
187
|
-
import './Counter-a1b2c3.css';
|
|
188
|
-
|
|
189
|
-
// Derived from defineProps and defineEmits
|
|
190
|
-
type ICounterType = {
|
|
191
|
-
title?: string;
|
|
192
|
-
onChange: () => void;
|
|
193
|
-
onUpdate: (value: number) => number;
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
// Component wrapped with memo
|
|
197
|
-
const Counter = memo((props: ICounterType) => {
|
|
198
|
-
// ref/computed converted to equivalent adaptation APIs
|
|
199
|
-
const step = useVRef(1);
|
|
200
|
-
const count = useVRef(0);
|
|
201
|
-
const title = useComputed(() => `Counter x${step.value}`);
|
|
202
|
-
|
|
203
|
-
// Automatically analyze dependencies of top-level arrow functions and append useCallback optimization
|
|
204
|
-
const increment = useCallback(() => {
|
|
205
|
-
count.value += step.value;
|
|
206
|
-
props.onUpdate?.(count.value); // emits conversion
|
|
207
|
-
}, [count.value, step.value, props.onUpdate]);
|
|
208
|
-
|
|
209
|
-
// Automatically analyze dependencies in top-level objects and append useMemo optimization
|
|
210
|
-
const methods = useMemo(
|
|
211
|
-
() => ({
|
|
212
|
-
decrease() {
|
|
213
|
-
count.value -= step.value;
|
|
214
|
-
},
|
|
215
|
-
}),
|
|
216
|
-
[count.value, step.value],
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
return (
|
|
220
|
-
<>
|
|
221
|
-
<section className="counter-card" data-css-a1b2c3>
|
|
222
|
-
<h2 data-css-a1b2c3>{props.title || title.value}</h2>
|
|
223
|
-
<p data-css-a1b2c3>Count: {count.value}</p>
|
|
224
|
-
<button onClick={increment} data-css-a1b2c3>
|
|
225
|
-
+1
|
|
226
|
-
</button>
|
|
227
|
-
<button onClick={methods.decrease} data-css-a1b2c3>
|
|
228
|
-
-1
|
|
229
|
-
</button>
|
|
230
|
-
</section>
|
|
231
|
-
</>
|
|
232
|
-
);
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
export default Counter;
|
|
50
|
+
npx vureact build # Build project
|
|
51
|
+
npx vureact watch # Watch mode
|
|
236
52
|
```
|
|
237
53
|
|
|
238
|
-
|
|
54
|
+
## Ecosystem
|
|
239
55
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
border: 1px solid #ddd;
|
|
243
|
-
border-radius: 8px;
|
|
244
|
-
padding: 12px;
|
|
245
|
-
}
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
## Key Observations
|
|
56
|
+
- **[VuReact Runtime Core](https://runtime.vureact.top/en)**: React-compatible implementations of Vue core APIs
|
|
57
|
+
- **[VuReact Router](https://router.vureact.top/en)**: Vue Router 4.x → React Router DOM 7.9+ conversion
|
|
249
58
|
|
|
250
|
-
|
|
251
|
-
2. `defineProps` and `defineEmits` are converted to TS component types
|
|
252
|
-
3. Non-pure UI display components are wrapped with `memo` by default
|
|
253
|
-
4. `ref` / `computed` are converted to runtime adaptation APIs (`useVRef` / `useComputed`)
|
|
254
|
-
5. Template event callbacks generate React-semantic `onClick`
|
|
255
|
-
6. Top-level arrow functions have their dependencies automatically analyzed and `useCallback` is injected where applicable
|
|
256
|
-
7. Top-level variable declarations have their dependencies automatically analyzed and `useMemo` is injected where applicable
|
|
257
|
-
8. The `.value` suffix is added to original `ref` state values in JSX
|
|
258
|
-
9. Less styles are compiled to CSS code
|
|
259
|
-
10. Scoped styles generate hashed CSS files and add scoped attributes to elements
|
|
59
|
+
If necessary, you can choose [☣️ Mixed Coding](https://vureact.top/en/guide/mind-control-readme.html) to directly use the React ecosystem.
|
|
260
60
|
|
|
261
61
|
## FAQ
|
|
262
62
|
|
|
263
|
-
|
|
264
|
-
- Calling APIs that are converted to Hooks outside the top level
|
|
265
|
-
- Unanalyzable expressions appearing in templates (triggering warnings)
|
|
266
|
-
- Disabling style preprocessing while using `scoped`, leading to scoping failure
|
|
267
|
-
|
|
268
|
-
For more details, please refer to the [FAQ](https://vureact.top/guide/en/faq.html) of the official documentation.
|
|
269
|
-
|
|
270
|
-
## Ecosystem Integration
|
|
271
|
-
|
|
272
|
-
- **[VuReact Runtime Core](https://runtime.vureact.top/en)**: Provides React versions of Vue's common built-in components, core Composition API, etc.
|
|
273
|
-
- **[VuReact Router](https://router.vureact.top/en)**: Supports conversion from Vue Router 4.x to React Router DOM 7.9+.
|
|
274
|
-
|
|
275
|
-
If necessary, you can choose [☣️ Mixed Coding](https://vureact.top/guide/mind-control-readme.html) to directly use the React ecosystem.
|
|
63
|
+
👉 [FAQ](https://vureact.top/en/guide/faq.html)
|
|
276
64
|
|
|
277
65
|
## 🔗 Links
|
|
278
66
|
|
|
279
67
|
- GitHub: <https://github.com/vureact-js/core>
|
|
280
68
|
- Documentation: <https://vureact.top/en>
|
|
281
|
-
- Playground:
|
|
282
|
-
- CRM Admin Dashboard (Standard): <https://codesandbox.io/p/github/vureact-js/example-crm-admin-backend/master>
|
|
283
|
-
- Customer Support Hub (Mixed): <https://codesandbox.io/p/github/vureact-js/example-customer-support-hub/master?import=true>
|
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# @vureact/compiler-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**写 Vue,交付生产级 React 代码** —— 让你在 Vue 3 心智下,编译出 React 18+ 应用的编译工具。
|
|
4
4
|
|
|
5
5
|
[](https://vureact.top/)
|
|
6
|
-
[](https://www.npmjs.com/package/@vureact/compiler-core)
|
|
7
|
+
[](https://www.npmjs.com/package/@vureact/compiler-core)
|
|
8
8
|
[](https://nodejs.org/)
|
|
9
9
|
[](https://opensource.org/licenses/MIT)
|
|
10
10
|
[](https://vuejs.org/)
|
|
@@ -12,107 +12,21 @@
|
|
|
12
12
|
|
|
13
13
|
简体中文 | [English](./README.en.md)
|
|
14
14
|
|
|
15
|
-
##
|
|
16
|
-
|
|
17
|
-
本节将引导你完成第一个 VuReact 项目的创建、编译和运行;或者选择查看 [CodeSandbox 在线案例](https://codesandbox.io/p/github/vureact-js/example-crm-admin-backend/master)。
|
|
18
|
-
|
|
19
|
-
完成后你会明确三件事:
|
|
20
|
-
|
|
21
|
-
1. 输入 SFC 在什么约定下可稳定转换
|
|
22
|
-
2. 编译后目录会长什么样
|
|
23
|
-
3. 输出 TSX 与原始 SFC 的语义对应关系
|
|
24
|
-
4. 编译器会自动分析并追加依赖,无需手动管理 React hooks 依赖项
|
|
25
|
-
|
|
26
|
-
## Step 0:准备目录
|
|
27
|
-
|
|
28
|
-
先准备一个最小工程(示意):
|
|
29
|
-
|
|
30
|
-
```txt
|
|
31
|
-
my-app/
|
|
32
|
-
├─ src/
|
|
33
|
-
│ ├─ components/
|
|
34
|
-
│ │ └─ Counter.vue
|
|
35
|
-
│ ├─ main.ts
|
|
36
|
-
│ └─ index.css
|
|
37
|
-
├─ package.json
|
|
38
|
-
└─ vureact.config.js
|
|
39
|
-
```
|
|
15
|
+
## 简介
|
|
40
16
|
|
|
41
|
-
|
|
17
|
+
`@vureact/compiler-core` 是 VuReact 的核心编译包,负责将 Vue 3 单文件组件、脚本文件和样式文件**三位一体**编译为可直接用于生产环境的 React 18+ JSX/TSX 代码。
|
|
42
18
|
|
|
43
|
-
|
|
19
|
+
## 安装
|
|
44
20
|
|
|
45
21
|
```bash
|
|
46
|
-
# 使用 npm
|
|
47
22
|
npm install -D @vureact/compiler-core
|
|
48
|
-
|
|
49
|
-
# 使用 yarn
|
|
50
|
-
yarn add -D @vureact/compiler-core
|
|
51
|
-
|
|
52
|
-
# 使用 pnpm
|
|
53
|
-
pnpm add -D @vureact/compiler-core
|
|
54
23
|
```
|
|
55
24
|
|
|
56
|
-
##
|
|
57
|
-
|
|
58
|
-
`src/components/Counter.vue`
|
|
59
|
-
|
|
60
|
-
```html
|
|
61
|
-
<template>
|
|
62
|
-
<section class="counter-card">
|
|
63
|
-
<h2>{{ props.title || title }}</h2>
|
|
64
|
-
<p>Count: {{ count }}</p>
|
|
65
|
-
<button @click="increment">+1</button>
|
|
66
|
-
<button @click="methods.decrease">-1</button>
|
|
67
|
-
</section>
|
|
68
|
-
</template>
|
|
69
|
-
|
|
70
|
-
<script setup lang="ts">
|
|
71
|
-
// @vr-name: Counter (注:用于告诉编译器,该生成什么组件名)
|
|
72
|
-
import { computed, ref } from 'vue';
|
|
73
|
-
|
|
74
|
-
// 也可以使用宏定义组件名
|
|
75
|
-
defineOptions({ name: 'Counter' });
|
|
76
|
-
|
|
77
|
-
// 定义 props
|
|
78
|
-
const props = defineProps<{ title?: string }>();
|
|
79
|
-
|
|
80
|
-
// 定义 emits
|
|
81
|
-
const emits = defineEmits<{
|
|
82
|
-
(e: 'change'): void;
|
|
83
|
-
(e: 'update', value: number): number;
|
|
84
|
-
}>();
|
|
85
|
-
|
|
86
|
-
const step = ref(1);
|
|
87
|
-
const count = ref(0);
|
|
88
|
-
const title = computed(() => `Counter x${step.value}`);
|
|
89
|
-
|
|
90
|
-
const increment = () => {
|
|
91
|
-
count.value += step.value;
|
|
92
|
-
emits('update', count.value);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const methods = {
|
|
96
|
-
decrease() {
|
|
97
|
-
count.value -= step.value;
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
</script>
|
|
101
|
-
|
|
102
|
-
<style lang="less" scoped>
|
|
103
|
-
@border-color: #ddd;
|
|
104
|
-
@border-radius: 8px;
|
|
105
|
-
@padding-base: 12px;
|
|
25
|
+
## 快速开始
|
|
106
26
|
|
|
107
|
-
|
|
108
|
-
border: 1px solid @border-color;
|
|
109
|
-
border-radius: @border-radius;
|
|
110
|
-
padding: @padding-base;
|
|
111
|
-
}
|
|
112
|
-
</style>
|
|
113
|
-
```
|
|
27
|
+
👉 **完整教程请访问:[VuReact - 快速开始](https://vureact.top/guide/quick-start.html)**
|
|
114
28
|
|
|
115
|
-
|
|
29
|
+
### 配置示例
|
|
116
30
|
|
|
117
31
|
`vureact.config.ts`
|
|
118
32
|
|
|
@@ -120,165 +34,36 @@ pnpm add -D @vureact/compiler-core
|
|
|
120
34
|
import { defineConfig } from '@vureact/compiler-core';
|
|
121
35
|
|
|
122
36
|
export default defineConfig({
|
|
123
|
-
input: 'src',
|
|
124
|
-
// 关键:排除 Vue 入口文件,避免入口语义冲突
|
|
37
|
+
input: './src',
|
|
125
38
|
exclude: ['src/main.ts'],
|
|
126
39
|
output: {
|
|
127
40
|
workspace: '.vureact',
|
|
128
|
-
outDir: '
|
|
129
|
-
|
|
130
|
-
// 实际使用时建议开启
|
|
131
|
-
bootstrapVite: false,
|
|
41
|
+
outDir: 'react-app',
|
|
42
|
+
bootstrapVite: true,
|
|
132
43
|
},
|
|
133
44
|
});
|
|
134
45
|
```
|
|
135
46
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
### 方式一:使用 npx 命令
|
|
139
|
-
|
|
140
|
-
在根目录下运行:
|
|
141
|
-
|
|
142
|
-
```bash
|
|
143
|
-
npx vureact build
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### 方式二:使用 npm scripts
|
|
147
|
-
|
|
148
|
-
在 `package.json` 里添加脚本命令:
|
|
149
|
-
|
|
150
|
-
```json
|
|
151
|
-
"scripts": {
|
|
152
|
-
"watch": "vureact watch",
|
|
153
|
-
"build": "vureact build"
|
|
154
|
-
}
|
|
155
|
-
```
|
|
47
|
+
### 编译命令
|
|
156
48
|
|
|
157
49
|
```bash
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
## Step 5:查看输出目录树
|
|
162
|
-
|
|
163
|
-
编译后目录(示意):
|
|
164
|
-
|
|
165
|
-
```txt
|
|
166
|
-
my-app/
|
|
167
|
-
├─ .vureact/
|
|
168
|
-
│ ├─ cache/
|
|
169
|
-
│ │ └─ _metadata.json
|
|
170
|
-
│ └─ dist/
|
|
171
|
-
│ └─ src/
|
|
172
|
-
│ └─ components/
|
|
173
|
-
│ ├─ Counter.tsx
|
|
174
|
-
│ └─ counter-<hash>.css
|
|
175
|
-
├─ src/
|
|
176
|
-
│ └─ ...
|
|
177
|
-
└─ vureact.config.js
|
|
50
|
+
npx vureact build # 编译项目
|
|
51
|
+
npx vureact watch # 监听模式
|
|
178
52
|
```
|
|
179
53
|
|
|
180
|
-
## Step 6:对照生成结果
|
|
181
|
-
|
|
182
|
-
下面是一个格式化后的典型输出(为说明做了轻微简化,实际哈希与属性名以本地产物为准):
|
|
183
|
-
|
|
184
|
-
```tsx
|
|
185
|
-
import { memo, useCallback, useMemo } from 'react';
|
|
186
|
-
import { useComputed, useVRef } from '@vureact/runtime-core';
|
|
187
|
-
import './counter-a1b2c3.css';
|
|
188
|
-
|
|
189
|
-
// 根据 defineProps 和 defineEmits 推导
|
|
190
|
-
type ICounterType = {
|
|
191
|
-
title?: string;
|
|
192
|
-
onChange: () => void;
|
|
193
|
-
onUpdate: (value: number) => number;
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
// memo 包裹组件
|
|
197
|
-
const Counter = memo((props: ICounterType) => {
|
|
198
|
-
// ref/computed 转换成了对等的适配 API
|
|
199
|
-
const step = useVRef(1);
|
|
200
|
-
const count = useVRef(0);
|
|
201
|
-
const title = useComputed(() => `Counter x${step.value}`);
|
|
202
|
-
|
|
203
|
-
// 自动分析顶层箭头函数依赖,并追加 useCallback 优化
|
|
204
|
-
const increment = useCallback(() => {
|
|
205
|
-
count.value += step.value;
|
|
206
|
-
props.onUpdate?.(count.value); // emits 转换
|
|
207
|
-
}, [count.value, step.value, props.onUpdate]);
|
|
208
|
-
|
|
209
|
-
// 自动分析顶层对象中的依赖,并追加 useMemo 优化
|
|
210
|
-
const methods = useMemo(
|
|
211
|
-
() => ({
|
|
212
|
-
decrease() {
|
|
213
|
-
count.value -= step.value;
|
|
214
|
-
},
|
|
215
|
-
}),
|
|
216
|
-
[count.value, step.value],
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
return (
|
|
220
|
-
<>
|
|
221
|
-
<section className="counter-card" data-css-a1b2c3>
|
|
222
|
-
<h2 data-css-a1b2c3>{props.title || title.value}</h2>
|
|
223
|
-
<p data-css-a1b2c3>Count: {count.value}</p>
|
|
224
|
-
<button onClick={increment} data-css-a1b2c3>
|
|
225
|
-
+1
|
|
226
|
-
</button>
|
|
227
|
-
<button onClick={methods.decrease} data-css-a1b2c3>
|
|
228
|
-
-1
|
|
229
|
-
</button>
|
|
230
|
-
</section>
|
|
231
|
-
</>
|
|
232
|
-
);
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
export default Counter;
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
CSS 文件内容:
|
|
239
|
-
|
|
240
|
-
```css
|
|
241
|
-
.counter-card[data-css-a1b2c3] {
|
|
242
|
-
border: 1px solid #ddd;
|
|
243
|
-
border-radius: 8px;
|
|
244
|
-
padding: 12px;
|
|
245
|
-
}
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
## 关键观察点
|
|
249
|
-
|
|
250
|
-
1. `// @vr-name: Counter` 这段特殊注释定义了组件名
|
|
251
|
-
2. `defineProps` 和 `defineEmits` 被转换成了 TS 组件类型
|
|
252
|
-
3. 非纯 UI 展示组件,默认会走 `memo` 包装
|
|
253
|
-
4. `ref` / `computed` 被转换为 runtime 适配 API(`useVRef` / `useComputed`)
|
|
254
|
-
5. 模板事件回调会生成符合 React 语义的 `onClick`
|
|
255
|
-
6. 顶层箭头函数自动分析依赖,尝试注入 `useCallback`
|
|
256
|
-
7. 顶层变量声明自动分析依赖,尝试注入 `useMemo`
|
|
257
|
-
8. 对 JSX 中的原 `ref` 状态值补上 `.value`
|
|
258
|
-
9. `less` 样式被编译为 css 代码
|
|
259
|
-
10. `scoped` 样式会生成带哈希的 css 文件,并在元素上标注作用域属性
|
|
260
|
-
|
|
261
|
-
## 常见问题
|
|
262
|
-
|
|
263
|
-
- 没排除 Vue 入口文件,如 `src/main.ts`
|
|
264
|
-
- 在非顶层调用会被转换为 Hook 的 API
|
|
265
|
-
- 模板里出现不可分析表达式并被告警
|
|
266
|
-
- 关闭样式预处理且使用 `scoped`,导致作用域失效
|
|
267
|
-
|
|
268
|
-
更多请查阅官方文档的 [FAQ 章节](https://vureact.top/guide/faq.html)。
|
|
269
|
-
|
|
270
54
|
## 生态集成
|
|
271
55
|
|
|
272
56
|
- **[VuReact Runtime Core](https://runtime.vureact.top/)**:提供 React 版的 Vue 常用内置组件、核心 Composition API 等
|
|
273
|
-
- **[VuReact Router](https://router.vureact.top/)**:支持 Vue Router 4.x
|
|
57
|
+
- **[VuReact Router](https://router.vureact.top/)**:支持 Vue Router 4.x → React Router DOM 7.9+ 转换
|
|
274
58
|
|
|
275
59
|
如果确实需要,你可以选择 [☣️混合编写](https://vureact.top/guide/mind-control-readme.html),以此直接使用 React 生态。
|
|
276
60
|
|
|
61
|
+
## 常见问题
|
|
62
|
+
|
|
63
|
+
👉 请查阅[官网 FAQ 章节](https://vureact.top/guide/faq.html)
|
|
64
|
+
|
|
277
65
|
## 🔗 链接
|
|
278
66
|
|
|
279
67
|
- GitHub:<https://github.com/vureact-js/core>
|
|
280
68
|
- Gitee:<https://gitee.com/vureact-js/core>
|
|
281
|
-
-
|
|
282
|
-
- CodeSandbox 在线案例:
|
|
283
|
-
- 客户关系管理后台(标准):<https://codesandbox.io/p/github/vureact-js/example-crm-admin-backend/master>
|
|
284
|
-
- 客户支持协同后台(混写):<https://codesandbox.io/p/github/vureact-js/example-customer-support-hub/master?import=true>
|
|
69
|
+
- 文档:<https://vureact.top>
|