@meituan-nocode/vite-plugin-nocode-compiler 0.1.0-beta.7 → 0.1.0-beta.9-z
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 +87 -75
- package/dist/framework-detector.d.ts +13 -0
- package/dist/framework-detector.js +85 -0
- package/dist/index.d.ts +10 -17
- package/dist/index.js +112 -38
- package/package.json +3 -3
- package/dist/nocode-compiler-core/src/compilers/base-compiler.d.ts +0 -51
- package/dist/nocode-compiler-core/src/compilers/base-compiler.js +0 -44
- package/dist/nocode-compiler-core/src/compilers/jsx-compiler.d.ts +0 -34
- package/dist/nocode-compiler-core/src/compilers/jsx-compiler.js +0 -211
- package/dist/nocode-compiler-core/src/compilers/vue-compiler.d.ts +0 -26
- package/dist/nocode-compiler-core/src/compilers/vue-compiler.js +0 -129
- package/dist/nocode-compiler-core/src/types.d.ts +0 -9
- package/dist/nocode-compiler-core/src/types.js +0 -2
- package/dist/nocode-compiler-core/src/utils/constants.d.ts +0 -23
- package/dist/nocode-compiler-core/src/utils/constants.js +0 -287
- package/dist/nocode-compiler-core/src/utils/utils.d.ts +0 -39
- package/dist/nocode-compiler-core/src/utils/utils.js +0 -77
- package/dist/vite-plugin-nocode-compiler/src/index.d.ts +0 -42
- package/dist/vite-plugin-nocode-compiler/src/index.js +0 -69
- package/dist/vite-plugin-nocode-compiler/src/types.d.ts +0 -8
- package/dist/vite-plugin-nocode-compiler/src/types.js +0 -2
package/README.md
CHANGED
|
@@ -1,125 +1,137 @@
|
|
|
1
1
|
# @meituan-nocode/vite-plugin-nocode-compiler
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vite 插件,用于在构建过程中处理 Vue 和 React 组件,添加 nocode 相关的标记。该插件支持自动检测项目框架类型(Vue/React),并应用相应的编译逻辑。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 特性
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
7
|
+
- ✅ 支持 Vue 和 React 组件的处理
|
|
8
|
+
- ✅ 自动检测项目框架类型
|
|
9
|
+
- ✅ 兼容 Vite 4.x 和 5.x 版本
|
|
10
|
+
- ✅ 可配置的日志输出
|
|
11
|
+
- ✅ 支持 ESM 和 CommonJS 导入方式
|
|
12
12
|
|
|
13
13
|
## 安装
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
+
# npm
|
|
16
17
|
npm install @meituan-nocode/vite-plugin-nocode-compiler --save-dev
|
|
17
|
-
```
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
# yarn
|
|
20
|
+
yarn add @meituan-nocode/vite-plugin-nocode-compiler --dev
|
|
21
|
+
|
|
22
|
+
# pnpm
|
|
23
|
+
pnpm add @meituan-nocode/vite-plugin-nocode-compiler -D
|
|
24
|
+
```
|
|
20
25
|
|
|
21
|
-
|
|
26
|
+
## 使用方法
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
### ESM 导入 (推荐)
|
|
24
29
|
|
|
25
|
-
```
|
|
30
|
+
```js
|
|
31
|
+
// vite.config.js / vite.config.ts
|
|
26
32
|
import { defineConfig } from 'vite';
|
|
27
|
-
import {
|
|
33
|
+
import { nocodeCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
|
|
34
|
+
// 或者使用默认导入
|
|
35
|
+
// import nocodeCompiler from '@meituan-nocode/vite-plugin-nocode-compiler';
|
|
28
36
|
|
|
29
37
|
export default defineConfig({
|
|
30
38
|
plugins: [
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
nocodeCompiler({
|
|
40
|
+
enableLogging: true,
|
|
41
|
+
forceFramework: 'vue', // 或 'react'
|
|
42
|
+
}),
|
|
33
43
|
],
|
|
34
44
|
});
|
|
35
45
|
```
|
|
36
46
|
|
|
37
|
-
###
|
|
47
|
+
### CommonJS 导入
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
// vite.config.js
|
|
51
|
+
const { defineConfig } = require('vite');
|
|
52
|
+
const { nocodeCompiler } = require('@meituan-nocode/vite-plugin-nocode-compiler');
|
|
53
|
+
// 或者使用默认导入并解构
|
|
54
|
+
// const pkg = require('@meituan-nocode/vite-plugin-nocode-compiler');
|
|
55
|
+
// const { nocodeCompiler } = pkg;
|
|
56
|
+
|
|
57
|
+
module.exports = defineConfig({
|
|
58
|
+
plugins: [
|
|
59
|
+
nocodeCompiler({
|
|
60
|
+
enableLogging: true,
|
|
61
|
+
forceFramework: 'vue', // 或 'react'
|
|
62
|
+
}),
|
|
63
|
+
],
|
|
64
|
+
});
|
|
65
|
+
```
|
|
38
66
|
|
|
39
|
-
|
|
67
|
+
### 兼容性导入 (如果上述方法不起作用)
|
|
40
68
|
|
|
41
|
-
```
|
|
69
|
+
```js
|
|
70
|
+
// vite.config.js / vite.config.ts
|
|
42
71
|
import { defineConfig } from 'vite';
|
|
43
|
-
import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
|
|
72
|
+
import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler/compat';
|
|
44
73
|
|
|
45
74
|
export default defineConfig({
|
|
46
75
|
plugins: [
|
|
47
76
|
componentCompiler({
|
|
48
|
-
enableLogging: true,
|
|
77
|
+
enableLogging: true,
|
|
78
|
+
forceFramework: 'vue', // 或 'react'
|
|
49
79
|
}),
|
|
50
|
-
// 其他插件...
|
|
51
80
|
],
|
|
52
81
|
});
|
|
53
82
|
```
|
|
54
83
|
|
|
55
|
-
##
|
|
84
|
+
## 配置选项
|
|
56
85
|
|
|
57
|
-
|
|
86
|
+
| 选项 | 类型 | 默认值 | 描述 |
|
|
87
|
+
| ---------------- | ------------------ | -------- | ------------------------------------------------------ |
|
|
88
|
+
| `enableLogging` | `boolean` | `false` | 是否启用日志输出,开启后会输出详细的处理过程和调试信息 |
|
|
89
|
+
| `forceFramework` | `'vue' \| 'react'` | 自动检测 | 强制指定框架类型,不指定时会自动检测 |
|
|
90
|
+
| `jsxOptions` | `object` | `{}` | JSX 编译器选项,用于配置 React 组件的编译行为 |
|
|
91
|
+
| `vueOptions` | `object` | `{}` | Vue 编译器选项,用于配置 Vue 组件的编译行为 |
|
|
58
92
|
|
|
59
|
-
|
|
60
|
-
- `data-nocode-name`: 元素名称
|
|
61
|
-
- `data-nocode-array`: 当元素在数组映射中时,包含数组名称
|
|
62
|
-
- `data-nocode-content`: 元素content内容
|
|
93
|
+
## 框架检测
|
|
63
94
|
|
|
64
|
-
|
|
95
|
+
如果未指定 `forceFramework`,插件会按以下优先级自动检测项目使用的框架:
|
|
65
96
|
|
|
66
|
-
|
|
97
|
+
1. 检查 package.json 中的依赖(React 依赖优先于 Vue 依赖)
|
|
98
|
+
2. 检查 src 目录下是否有框架特定文件(JSX/TSX 文件优先于 Vue 文件)
|
|
99
|
+
3. 如果以上都没有检测到,默认返回 React
|
|
67
100
|
|
|
68
|
-
|
|
69
|
-
function App() {
|
|
70
|
-
return (
|
|
71
|
-
<div>
|
|
72
|
-
<button>Click me</button>
|
|
73
|
-
{items.map((item, index) => (
|
|
74
|
-
<span key={item.id}>{item.name}</span>
|
|
75
|
-
))}
|
|
76
|
-
</div>
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
```
|
|
101
|
+
## 兼容性
|
|
80
102
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
<button data-nocode-id='App.jsx:4:6' data-nocode-name='button'>
|
|
88
|
-
Click me
|
|
89
|
-
</button>
|
|
90
|
-
{items.map((item, index) => (
|
|
91
|
-
<span data-nocode-id='App.jsx:6:8' data-nocode-name='span' data-nocode-array='items' data-nocode-index='${index}' key={item.id}>
|
|
92
|
-
{item.name}
|
|
93
|
-
</span>
|
|
94
|
-
))}
|
|
95
|
-
</div>
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
```
|
|
103
|
+
本插件兼容 Vite 4.x 和 5.x 版本。插件会自动检测 Vite 版本并进行相应的适配,确保在不同版本的 Vite 环境中都能正常工作。
|
|
104
|
+
|
|
105
|
+
### Vite 版本支持
|
|
106
|
+
|
|
107
|
+
- **Vite 4.x**: 完全支持 (>=4.0.0)
|
|
108
|
+
- **Vite 5.x**: 完全支持 (包括最新的 5.4.x)
|
|
99
109
|
|
|
100
|
-
|
|
110
|
+
如果在使用过程中遇到任何兼容性问题,请开启日志功能进行调试:
|
|
101
111
|
|
|
102
|
-
|
|
112
|
+
```js
|
|
113
|
+
nocodeCompiler({
|
|
114
|
+
enableLogging: true,
|
|
115
|
+
});
|
|
116
|
+
```
|
|
103
117
|
|
|
104
|
-
|
|
105
|
-
- **Vue**: ✅ 已实现
|
|
118
|
+
## 常见问题
|
|
106
119
|
|
|
107
|
-
|
|
120
|
+
### Q: 如何确认插件正在正常工作?
|
|
108
121
|
|
|
109
|
-
|
|
122
|
+
A: 开启 `enableLogging` 选项,构建时会输出详细的处理日志,包括检测到的框架类型和处理的文件。
|
|
110
123
|
|
|
111
|
-
|
|
112
|
-
# 构建
|
|
113
|
-
npm run build
|
|
124
|
+
### Q: 插件处理哪些文件类型?
|
|
114
125
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
126
|
+
A: 插件会处理以下文件类型:
|
|
127
|
+
|
|
128
|
+
- Vue 框架: `.vue` 文件
|
|
129
|
+
- React 框架: `.jsx`, `.tsx` 文件,以及包含 JSX 语法的 `.js`, `.ts` 文件
|
|
118
130
|
|
|
119
|
-
|
|
131
|
+
### Q: 如何在 monorepo 项目中使用?
|
|
120
132
|
|
|
121
|
-
|
|
133
|
+
A: 在各子项目的 `vite.config.js` 中分别配置插件即可。如果子项目共享配置,确保正确设置 `forceFramework` 选项。
|
|
122
134
|
|
|
123
|
-
##
|
|
135
|
+
## 许可证
|
|
124
136
|
|
|
125
|
-
|
|
137
|
+
MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type FrameworkType = 'vue' | 'react';
|
|
2
|
+
/**
|
|
3
|
+
* 框架检测器
|
|
4
|
+
* 按优先级检测项目使用的前端框架
|
|
5
|
+
* 1. 首先检查是否强制指定了框架
|
|
6
|
+
* 2. 然后检查package.json中的依赖(React依赖优先于Vue依赖)
|
|
7
|
+
* 3. 接着检查src目录下是否有框架特定文件(JSX/TSX文件优先于Vue文件)
|
|
8
|
+
* 4. 如果以上都没有检测到,默认返回React
|
|
9
|
+
*/
|
|
10
|
+
export declare function detectFramework(options?: {
|
|
11
|
+
forceFramework?: FrameworkType;
|
|
12
|
+
enableLogging?: boolean;
|
|
13
|
+
}): FrameworkType;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.detectFramework = detectFramework;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
/**
|
|
10
|
+
* 框架检测器
|
|
11
|
+
* 按优先级检测项目使用的前端框架
|
|
12
|
+
* 1. 首先检查是否强制指定了框架
|
|
13
|
+
* 2. 然后检查package.json中的依赖(React依赖优先于Vue依赖)
|
|
14
|
+
* 3. 接着检查src目录下是否有框架特定文件(JSX/TSX文件优先于Vue文件)
|
|
15
|
+
* 4. 如果以上都没有检测到,默认返回React
|
|
16
|
+
*/
|
|
17
|
+
function detectFramework(options) {
|
|
18
|
+
const log = (message, ...args) => {
|
|
19
|
+
if (options?.enableLogging) {
|
|
20
|
+
console.log(`[vite-nocode-compiler] ${message}`, ...args);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
// 1. 如果强制指定了框架,直接返回
|
|
24
|
+
if (options?.forceFramework) {
|
|
25
|
+
log('Using forced framework:', options.forceFramework);
|
|
26
|
+
return options.forceFramework;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
// 2. 检查package.json
|
|
30
|
+
const packagePath = path_1.default.resolve(process.cwd(), 'package.json');
|
|
31
|
+
log('Checking package.json:', packagePath);
|
|
32
|
+
if (fs_1.default.existsSync(packagePath)) {
|
|
33
|
+
try {
|
|
34
|
+
const pkg = JSON.parse(fs_1.default.readFileSync(packagePath, 'utf8'));
|
|
35
|
+
const deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
|
|
36
|
+
log('Package dependencies:', deps);
|
|
37
|
+
// React相关依赖(优先检查React,因为Vue项目可能也会依赖React组件)
|
|
38
|
+
if (deps.react || deps['react-dom']) {
|
|
39
|
+
log('React dependency detected');
|
|
40
|
+
return 'react';
|
|
41
|
+
}
|
|
42
|
+
// Vue相关依赖
|
|
43
|
+
if (deps.vue || deps['@vue/cli-service'] || deps.nuxt || deps['@vitejs/plugin-vue']) {
|
|
44
|
+
log('Vue dependency detected');
|
|
45
|
+
return 'vue';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
log('Failed to parse package.json:', e);
|
|
50
|
+
// 解析package.json失败,继续下一步检测
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// 3. 快速检查src目录下是否有框架特定文件(不递归)
|
|
54
|
+
const srcDir = path_1.default.resolve(process.cwd(), 'src');
|
|
55
|
+
log('Checking src directory:', srcDir);
|
|
56
|
+
if (fs_1.default.existsSync(srcDir)) {
|
|
57
|
+
try {
|
|
58
|
+
const files = fs_1.default.readdirSync(srcDir);
|
|
59
|
+
log('Files in src directory:', files);
|
|
60
|
+
// 检查是否有.jsx/.tsx文件(优先检查React文件)
|
|
61
|
+
if (files.some(file => file.endsWith('.jsx') || file.endsWith('.tsx'))) {
|
|
62
|
+
log('JSX/TSX file detected');
|
|
63
|
+
return 'react';
|
|
64
|
+
}
|
|
65
|
+
// 检查是否有.vue文件
|
|
66
|
+
if (files.some(file => file.endsWith('.vue'))) {
|
|
67
|
+
log('Vue file detected');
|
|
68
|
+
return 'vue';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
log('Failed to read src directory:', e);
|
|
73
|
+
// 读取目录失败,继续使用默认值
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// 4. 默认返回React
|
|
77
|
+
log('No framework detected, using default: react');
|
|
78
|
+
return 'react';
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
log('Error in detectFramework:', e);
|
|
82
|
+
// 出现任何错误,默认使用React
|
|
83
|
+
return 'react';
|
|
84
|
+
}
|
|
85
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @param options 编译器选项
|
|
5
|
-
* @returns Vite 插件
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* 创建 nocode 编译器插件,默认处理所有 jsx、tsx 和 vue 文件
|
|
9
|
-
* @param options 编译器选项
|
|
10
|
-
* @returns Vite 插件
|
|
11
|
-
*/
|
|
12
|
-
export interface CompilerPluginOptions {
|
|
13
|
-
/**
|
|
14
|
-
* 是否启用日志
|
|
15
|
-
* @default false
|
|
16
|
-
*/
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import { FrameworkType } from './framework-detector';
|
|
3
|
+
export interface NocodeCompilerOptions {
|
|
17
4
|
enableLogging?: boolean;
|
|
5
|
+
forceFramework?: FrameworkType;
|
|
18
6
|
}
|
|
19
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Vite插件:nocode-compiler
|
|
9
|
+
* 用于在构建过程中处理Vue和React组件,添加nocode相关的标记
|
|
10
|
+
* 兼容 Vite 4.x 和 5.x 版本
|
|
11
|
+
*/
|
|
12
|
+
export declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
|
package/dist/index.js
CHANGED
|
@@ -1,54 +1,128 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.componentCompiler = componentCompiler;
|
|
4
|
-
const
|
|
7
|
+
const vite_1 = require("vite");
|
|
8
|
+
const nocode_compiler_core_1 = require("@meituan-nocode/nocode-compiler-core");
|
|
9
|
+
const framework_detector_1 = require("./framework-detector");
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
/**
|
|
12
|
+
* 检测 Vite 版本
|
|
13
|
+
* @returns 返回主版本号 (4, 5 等)
|
|
14
|
+
*/
|
|
15
|
+
function getViteMajorVersion() {
|
|
16
|
+
// 从 viteVersion 中提取主版本号
|
|
17
|
+
const majorVersionMatch = vite_1.version.match(/^(\d+)\./);
|
|
18
|
+
return majorVersionMatch ? parseInt(majorVersionMatch[1], 10) : 0;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Vite插件:nocode-compiler
|
|
22
|
+
* 用于在构建过程中处理Vue和React组件,添加nocode相关的标记
|
|
23
|
+
* 兼容 Vite 4.x 和 5.x 版本
|
|
24
|
+
*/
|
|
5
25
|
function componentCompiler(options = {}) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
// 注册 JSX/TSX 编译器
|
|
17
|
-
registry.register({
|
|
18
|
-
extensionName: 'jsx',
|
|
19
|
-
supportedExtensions: ['.jsx', '.tsx'],
|
|
20
|
-
CompilerClass: nocode_compiler_core_1.JSXCompiler,
|
|
26
|
+
// 检测框架类型
|
|
27
|
+
let framework = null;
|
|
28
|
+
// 检测 Vite 版本
|
|
29
|
+
const viteMajorVersion = getViteMajorVersion();
|
|
30
|
+
if (options.enableLogging) {
|
|
31
|
+
console.log(`[vite-nocode-compiler] Detected Vite version: ${vite_1.version} (major: ${viteMajorVersion})`);
|
|
32
|
+
}
|
|
33
|
+
// 创建编译器实例
|
|
34
|
+
const jsxCompiler = new nocode_compiler_core_1.JSXCompiler({
|
|
35
|
+
enableLogging: options.enableLogging || false,
|
|
21
36
|
});
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
extensionName: 'vue',
|
|
25
|
-
supportedExtensions: ['.vue'],
|
|
26
|
-
CompilerClass: nocode_compiler_core_1.VueCompiler,
|
|
37
|
+
const vueCompiler = new nocode_compiler_core_1.VueCompiler({
|
|
38
|
+
enableLogging: options.enableLogging || false,
|
|
27
39
|
});
|
|
28
|
-
// 创建统计管理器
|
|
29
|
-
const statsManager = new nocode_compiler_core_1.StatsManager();
|
|
30
40
|
return {
|
|
31
41
|
name: 'vite-plugin-nocode-compiler',
|
|
32
42
|
enforce: 'pre',
|
|
43
|
+
configResolved() {
|
|
44
|
+
// 在配置解析后检测框架
|
|
45
|
+
framework = options.forceFramework || (0, framework_detector_1.detectFramework)(options);
|
|
46
|
+
if (options.enableLogging) {
|
|
47
|
+
console.log(`[vite-nocode-compiler] Detected framework: ${framework}`);
|
|
48
|
+
}
|
|
49
|
+
},
|
|
33
50
|
async transform(code, id) {
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
validExtensions: baseConfig.validExtensions,
|
|
37
|
-
threeFiberElems: baseConfig.threeFiberElems,
|
|
38
|
-
dreiElems: baseConfig.dreiElems,
|
|
39
|
-
enableLogging: baseConfig.enableLogging,
|
|
40
|
-
});
|
|
41
|
-
if (!compiler) {
|
|
51
|
+
// 跳过node_modules
|
|
52
|
+
if (id.includes('node_modules')) {
|
|
42
53
|
return null;
|
|
43
54
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
const fileExt = path_1.default.extname(id);
|
|
56
|
+
// 处理Vue文件
|
|
57
|
+
if (fileExt === '.vue') {
|
|
58
|
+
if (framework === 'vue' || options.forceFramework === 'vue') {
|
|
59
|
+
if (options.enableLogging) {
|
|
60
|
+
console.log(`[vite-nocode-compiler] Processing Vue file: ${id}`);
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const result = vueCompiler.compile(code, id);
|
|
64
|
+
if (typeof result === 'string') {
|
|
65
|
+
return {
|
|
66
|
+
code: result,
|
|
67
|
+
map: null,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error(`[vite-nocode-compiler] Error processing Vue file: ${id}`, error);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// 处理JSX/TSX文件
|
|
79
|
+
if (['.jsx', '.tsx'].includes(fileExt)) {
|
|
80
|
+
if (framework === 'react' || options.forceFramework === 'react') {
|
|
81
|
+
if (options.enableLogging) {
|
|
82
|
+
console.log(`[vite-nocode-compiler] Processing JSX/TSX file: ${id}`);
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const result = jsxCompiler.compile(code, id);
|
|
86
|
+
if (typeof result === 'string') {
|
|
87
|
+
return {
|
|
88
|
+
code: result,
|
|
89
|
+
map: null,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
console.error(`[vite-nocode-compiler] Error processing JSX/TSX file: ${id}`, error);
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// 处理JS/TS文件(可能包含JSX)
|
|
101
|
+
if (['.js', '.ts'].includes(fileExt)) {
|
|
102
|
+
if (framework === 'react' || options.forceFramework === 'react') {
|
|
103
|
+
// 简单检查是否包含JSX语法
|
|
104
|
+
if (code.includes('React.createElement') || code.includes('jsx') || (code.includes('<') && code.includes('/>'))) {
|
|
105
|
+
if (options.enableLogging) {
|
|
106
|
+
console.log(`[vite-nocode-compiler] Processing JS/TS file with potential JSX: ${id}`);
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
const result = jsxCompiler.compile(code, id);
|
|
110
|
+
if (typeof result === 'string') {
|
|
111
|
+
return {
|
|
112
|
+
code: result,
|
|
113
|
+
map: null,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
console.error(`[vite-nocode-compiler] Error processing JS/TS file: ${id}`, error);
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
50
124
|
}
|
|
51
|
-
return
|
|
125
|
+
return null;
|
|
52
126
|
},
|
|
53
127
|
};
|
|
54
128
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meituan-nocode/vite-plugin-nocode-compiler",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.9-z",
|
|
4
4
|
"description": "nocode compiler plugin",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"prepublishOnly": "npm run build"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"vite": "
|
|
17
|
+
"vite": ">=4.0.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@types/node": "^20.0.0",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"vite": "^5.4.0"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@
|
|
25
|
+
"@meituan-nocode/nocode-compiler-core": "0.1.0-beta.16-z"
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 基础编译器接口
|
|
3
|
-
* 定义所有编译器(JSX、Vue等)的通用接口
|
|
4
|
-
*/
|
|
5
|
-
export interface BaseCompiler {
|
|
6
|
-
/**
|
|
7
|
-
* 编译代码
|
|
8
|
-
* @param code 源代码
|
|
9
|
-
* @param id 文件路径
|
|
10
|
-
* @returns 编译结果或null(如果不需要处理)
|
|
11
|
-
*/
|
|
12
|
-
compile(code: string, id: string): Promise<{
|
|
13
|
-
code: string;
|
|
14
|
-
map: any;
|
|
15
|
-
} | null>;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* 编译器类构造函数类型
|
|
19
|
-
*/
|
|
20
|
-
export type CompilerConstructor = new (options: any) => BaseCompiler;
|
|
21
|
-
/**
|
|
22
|
-
* 编译器信息接口
|
|
23
|
-
*/
|
|
24
|
-
export interface CompilerInfo {
|
|
25
|
-
extensionName: string;
|
|
26
|
-
supportedExtensions: string[];
|
|
27
|
-
CompilerClass: CompilerConstructor;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* 编译器注册表
|
|
31
|
-
* 管理不同类型的编译器
|
|
32
|
-
*/
|
|
33
|
-
export declare class CompilerRegistry {
|
|
34
|
-
private compilers;
|
|
35
|
-
/**
|
|
36
|
-
* 注册编译器
|
|
37
|
-
*/
|
|
38
|
-
register(info: CompilerInfo): void;
|
|
39
|
-
/**
|
|
40
|
-
* 获取编译器信息
|
|
41
|
-
*/
|
|
42
|
-
getCompilerInfo(extensionName: string): CompilerInfo | undefined;
|
|
43
|
-
/**
|
|
44
|
-
* 获取所有支持的文件扩展名
|
|
45
|
-
*/
|
|
46
|
-
getAllSupportedExtensions(): string[];
|
|
47
|
-
/**
|
|
48
|
-
* 根据文件扩展名获取合适的编译器
|
|
49
|
-
*/
|
|
50
|
-
getCompilerForFile(filePath: string, options: any): BaseCompiler | null;
|
|
51
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CompilerRegistry = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* 编译器注册表
|
|
6
|
-
* 管理不同类型的编译器
|
|
7
|
-
*/
|
|
8
|
-
class CompilerRegistry {
|
|
9
|
-
compilers = new Map();
|
|
10
|
-
/**
|
|
11
|
-
* 注册编译器
|
|
12
|
-
*/
|
|
13
|
-
register(info) {
|
|
14
|
-
this.compilers.set(info.extensionName, info);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* 获取编译器信息
|
|
18
|
-
*/
|
|
19
|
-
getCompilerInfo(extensionName) {
|
|
20
|
-
return this.compilers.get(extensionName);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* 获取所有支持的文件扩展名
|
|
24
|
-
*/
|
|
25
|
-
getAllSupportedExtensions() {
|
|
26
|
-
const extensions = [];
|
|
27
|
-
for (const info of this.compilers.values()) {
|
|
28
|
-
extensions.push(...info.supportedExtensions);
|
|
29
|
-
}
|
|
30
|
-
return [...new Set(extensions)];
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* 根据文件扩展名获取合适的编译器
|
|
34
|
-
*/
|
|
35
|
-
getCompilerForFile(filePath, options) {
|
|
36
|
-
for (const info of this.compilers.values()) {
|
|
37
|
-
if (info.supportedExtensions.some(ext => filePath.endsWith(ext))) {
|
|
38
|
-
return new info.CompilerClass(options);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.CompilerRegistry = CompilerRegistry;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { BaseCompiler } from './base-compiler';
|
|
2
|
-
export interface JSXCompilerOptions {
|
|
3
|
-
validExtensions: Set<string>;
|
|
4
|
-
threeFiberElems: string[];
|
|
5
|
-
dreiElems: string[];
|
|
6
|
-
enableLogging?: boolean;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* JSX/TSX 编译器类
|
|
10
|
-
* 负责处理 JSX/TSX 文件的编译逻辑
|
|
11
|
-
*
|
|
12
|
-
* 支持的文件类型:
|
|
13
|
-
* - .jsx: React JSX 文件
|
|
14
|
-
* - .tsx: React TypeScript JSX 文件
|
|
15
|
-
*
|
|
16
|
-
* 注意:
|
|
17
|
-
* - TypeScript 类型信息在编译时会被擦除
|
|
18
|
-
* - 编译器关注的是 JSX 结构而不是类型信息
|
|
19
|
-
*/
|
|
20
|
-
export declare class JSXCompiler implements BaseCompiler {
|
|
21
|
-
static readonly supportedExtensions: string[];
|
|
22
|
-
static readonly extensionName = "jsx";
|
|
23
|
-
private options;
|
|
24
|
-
private logger;
|
|
25
|
-
private cwd;
|
|
26
|
-
constructor(options: JSXCompilerOptions);
|
|
27
|
-
/**
|
|
28
|
-
* 编译 JSX 代码
|
|
29
|
-
*/
|
|
30
|
-
compile(code: string, id: string): Promise<{
|
|
31
|
-
code: string;
|
|
32
|
-
map: any;
|
|
33
|
-
} | null>;
|
|
34
|
-
}
|