@actview/plugin-babel 1.0.4 → 1.0.6
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.md +64 -64
- package/dist/index.js +10 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
# @actview/plugin-babel
|
|
2
|
-
|
|
3
|
-
> **迁移记录**:本包原为 `@actview/babel-plugin-actview`(目录 `plugins/babel-plugin-actview`),现更名为 `@actview/plugin-babel`(目录 `plugins/babel`),与 `@actview/plugin-vite` / `@actview/plugin-scoped` 统一为 `@actview/plugin-*` 命名。旧包名已弃用,请使用新包名安装与引用。
|
|
4
|
-
|
|
5
|
-
**ActView 编译核心(Babel 插件)** —— 把 JSX 组件函数自动转换为 `defineComponent`,独立于 Vite 宿主。
|
|
6
|
-
|
|
7
|
-
Babel 插件 `defineComponentPlugin` 是 ActView 编译链的**核心**:将大写开头的函数 / 箭头 / 默认导出组件包装为 `defineComponent` 产物(`{ __setup }`),并完成具名插槽提取、props 白名单、早退 return 包装等转换。`@actview/plugin-vite` 与 `@actview/plugin-scoped` 都基于它。
|
|
8
|
-
|
|
9
|
-
同时本包导出**共享 Babel 宿主壳**(`createBabelTransform` / `createBabelItem` / `transformWithBabel`),供各 Vite 插件复用统一的 `transformSync` 调用参数。
|
|
10
|
-
|
|
11
|
-
## 核心功能
|
|
12
|
-
|
|
13
|
-
- **组件自动转换**:函数 / 箭头 / 默认导出组件 → `defineComponent`(自动注入 `import { defineComponent } from '@actview/core'`)
|
|
14
|
-
- **具名插槽**:`<template slot>` → `slots` prop(编译期提取)
|
|
15
|
-
- **props 白名单**:从 `defineComponent({ props })` 声明提取 `__props`
|
|
16
|
-
- **早退 return**:组件渲染函数中的 `if / 三元 / &&` 早退分支包装(`isRenderExpr`)
|
|
17
|
-
- **共享宿主壳**:`createBabelTransform` 等,统一 `parserOpts` / `retainLines` / `sourceMaps` / `babelrc:false` / `configFile:false`
|
|
18
|
-
|
|
19
|
-
## 安装
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
pnpm add -D @actview/plugin-babel
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## 快速开始
|
|
26
|
-
|
|
27
|
-
```ts
|
|
28
|
-
import { defineComponentPlugin } from '@actview/plugin-babel'
|
|
29
|
-
import * as babel from '@babel/core'
|
|
30
|
-
|
|
31
|
-
const result = babel.transformSync(code, {
|
|
32
|
-
filename: 'App.tsx',
|
|
33
|
-
plugins: [[defineComponentPlugin, {}]],
|
|
34
|
-
parserOpts: { plugins: ['jsx', 'typescript'] },
|
|
35
|
-
})
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
> 大多数场景不需要直接使用 —— 接入 `@actview/plugin-vite`(Vite 项目)即可自动完成转换。
|
|
39
|
-
|
|
40
|
-
## API
|
|
41
|
-
|
|
42
|
-
| 导出 | 说明 |
|
|
43
|
-
|---|---|
|
|
44
|
-
| `defineComponentPlugin`(默认导出同) | Babel 插件工厂:组件 → `defineComponent` 转换 |
|
|
45
|
-
| `createBabelTransform(plugin)` | 宿主壳工厂:模块级创建一次 ConfigItem,返回 `(code, filename) => { code, map } \| null` |
|
|
46
|
-
| `createBabelItem(plugin)` | 把插件工厂预编译为 ConfigItem(Babel 8 同步) |
|
|
47
|
-
| `transformWithBabel(code, filename, pluginItem)` | 统一参数的 `transformSync` |
|
|
48
|
-
| 类型:`BabelPlugin` / `BabelHostResult` | 宿主壳类型 |
|
|
49
|
-
|
|
50
|
-
## 依赖关系
|
|
51
|
-
|
|
52
|
-
- `@babel/core`(^8)
|
|
53
|
-
- 被依赖方:`@actview/plugin-vite`、`@actview/plugin-scoped`(复用宿主壳)
|
|
54
|
-
|
|
55
|
-
## 开发
|
|
56
|
-
|
|
57
|
-
```bash
|
|
58
|
-
pnpm build # tsup 打包 dist
|
|
59
|
-
pnpm test # vitest(test/plugin.test.ts:组件转换/插槽/props/早退等 30 用例)
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
## License
|
|
63
|
-
|
|
64
|
-
MIT
|
|
1
|
+
# @actview/plugin-babel
|
|
2
|
+
|
|
3
|
+
> **迁移记录**:本包原为 `@actview/babel-plugin-actview`(目录 `plugins/babel-plugin-actview`),现更名为 `@actview/plugin-babel`(目录 `plugins/babel`),与 `@actview/plugin-vite` / `@actview/plugin-scoped` 统一为 `@actview/plugin-*` 命名。旧包名已弃用,请使用新包名安装与引用。
|
|
4
|
+
|
|
5
|
+
**ActView 编译核心(Babel 插件)** —— 把 JSX 组件函数自动转换为 `defineComponent`,独立于 Vite 宿主。
|
|
6
|
+
|
|
7
|
+
Babel 插件 `defineComponentPlugin` 是 ActView 编译链的**核心**:将大写开头的函数 / 箭头 / 默认导出组件包装为 `defineComponent` 产物(`{ __setup }`),并完成具名插槽提取、props 白名单、早退 return 包装等转换。`@actview/plugin-vite` 与 `@actview/plugin-scoped` 都基于它。
|
|
8
|
+
|
|
9
|
+
同时本包导出**共享 Babel 宿主壳**(`createBabelTransform` / `createBabelItem` / `transformWithBabel`),供各 Vite 插件复用统一的 `transformSync` 调用参数。
|
|
10
|
+
|
|
11
|
+
## 核心功能
|
|
12
|
+
|
|
13
|
+
- **组件自动转换**:函数 / 箭头 / 默认导出组件 → `defineComponent`(自动注入 `import { defineComponent } from '@actview/core'`)
|
|
14
|
+
- **具名插槽**:`<template slot>` → `slots` prop(编译期提取)
|
|
15
|
+
- **props 白名单**:从 `defineComponent({ props })` 声明提取 `__props`
|
|
16
|
+
- **早退 return**:组件渲染函数中的 `if / 三元 / &&` 早退分支包装(`isRenderExpr`)
|
|
17
|
+
- **共享宿主壳**:`createBabelTransform` 等,统一 `parserOpts` / `retainLines` / `sourceMaps` / `babelrc:false` / `configFile:false`
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm add -D @actview/plugin-babel
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 快速开始
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { defineComponentPlugin } from '@actview/plugin-babel'
|
|
29
|
+
import * as babel from '@babel/core'
|
|
30
|
+
|
|
31
|
+
const result = babel.transformSync(code, {
|
|
32
|
+
filename: 'App.tsx',
|
|
33
|
+
plugins: [[defineComponentPlugin, {}]],
|
|
34
|
+
parserOpts: { plugins: ['jsx', 'typescript'] },
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
> 大多数场景不需要直接使用 —— 接入 `@actview/plugin-vite`(Vite 项目)即可自动完成转换。
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
| 导出 | 说明 |
|
|
43
|
+
|---|---|
|
|
44
|
+
| `defineComponentPlugin`(默认导出同) | Babel 插件工厂:组件 → `defineComponent` 转换 |
|
|
45
|
+
| `createBabelTransform(plugin)` | 宿主壳工厂:模块级创建一次 ConfigItem,返回 `(code, filename) => { code, map } \| null` |
|
|
46
|
+
| `createBabelItem(plugin)` | 把插件工厂预编译为 ConfigItem(Babel 8 同步) |
|
|
47
|
+
| `transformWithBabel(code, filename, pluginItem)` | 统一参数的 `transformSync` |
|
|
48
|
+
| 类型:`BabelPlugin` / `BabelHostResult` | 宿主壳类型 |
|
|
49
|
+
|
|
50
|
+
## 依赖关系
|
|
51
|
+
|
|
52
|
+
- `@babel/core`(^8)
|
|
53
|
+
- 被依赖方:`@actview/plugin-vite`、`@actview/plugin-scoped`(复用宿主壳)
|
|
54
|
+
|
|
55
|
+
## 开发
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pnpm build # tsup 打包 dist
|
|
59
|
+
pnpm test # vitest(test/plugin.test.ts:组件转换/插槽/props/早退等 30 用例)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,11 @@ import { types as t } from "@babel/core";
|
|
|
3
3
|
import generate from "@babel/generator";
|
|
4
4
|
function extractNamedSlots(el) {
|
|
5
5
|
const nameNode = el.openingElement.name;
|
|
6
|
-
if (
|
|
6
|
+
if (t.isJSXIdentifier(nameNode)) {
|
|
7
|
+
if (!/^[A-Z]/.test(nameNode.name)) return;
|
|
8
|
+
} else if (!t.isJSXMemberExpression(nameNode)) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
7
11
|
const children = el.children;
|
|
8
12
|
const slotProps = [];
|
|
9
13
|
const remaining = [];
|
|
@@ -127,6 +131,7 @@ function defineComponentPlugin() {
|
|
|
127
131
|
const name = node.id.name;
|
|
128
132
|
if (!/^[A-Z]/.test(name)) return;
|
|
129
133
|
const fn = t.functionExpression(null, node.params, node.body, false, false);
|
|
134
|
+
if (node.typeParameters) fn.typeParameters = node.typeParameters;
|
|
130
135
|
const wrapped = wrapComponentFn(fn, name);
|
|
131
136
|
if (!wrapped) return;
|
|
132
137
|
const call = wrapped;
|
|
@@ -175,6 +180,10 @@ function defineComponentPlugin() {
|
|
|
175
180
|
t.isFunctionDeclaration(decl);
|
|
176
181
|
if (!isFn) return;
|
|
177
182
|
const fn = t.isFunctionDeclaration(decl) ? t.functionExpression(null, decl.params, decl.body, false, false) : decl;
|
|
183
|
+
if (t.isFunctionDeclaration(decl) && decl.typeParameters) {
|
|
184
|
+
;
|
|
185
|
+
fn.typeParameters = decl.typeParameters;
|
|
186
|
+
}
|
|
178
187
|
const wrapped = wrapComponentFn(fn);
|
|
179
188
|
if (!wrapped) return;
|
|
180
189
|
const call = wrapped;
|