@meituan-nocode/vite-plugin-nocode-compiler 0.1.0-beta.1
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 +129 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +63 -0
- package/dist/modules/base-compiler.d.ts +51 -0
- package/dist/modules/base-compiler.js +44 -0
- package/dist/modules/jsx-compiler.d.ts +26 -0
- package/dist/modules/jsx-compiler.js +158 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.js +2 -0
- package/dist/utils/constants.d.ts +23 -0
- package/dist/utils/constants.js +287 -0
- package/dist/utils/utils.d.ts +33 -0
- package/dist/utils/utils.js +79 -0
- package/package.json +30 -0
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# @meituan-nocode/vite-plugin-nocode-compiler
|
|
2
|
+
|
|
3
|
+
一个用于 Vite 的 nocode 编译插件,为 JSX 元素自动添加 nocode 所需的标识属性。
|
|
4
|
+
|
|
5
|
+
## 功能
|
|
6
|
+
|
|
7
|
+
- 🚀 **多框架支持**: 模块化设计,当前支持 JSX,未来将支持 Vue
|
|
8
|
+
- 📦 **自动元素标记**: 为非 Three.js Fiber 和非 Drei 的元素自动添加 nocode 标识
|
|
9
|
+
- 🔄 **数组上下文检测**: 智能识别数组映射,添加索引信息
|
|
10
|
+
- 🛠️ **可配置**: 支持自定义配置选项
|
|
11
|
+
- 📝 **详细日志**: 提供完整的调试信息
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @meituan-nocode/vite-plugin-nocode-compiler --save-dev
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 使用
|
|
20
|
+
|
|
21
|
+
### 基础使用
|
|
22
|
+
|
|
23
|
+
在你的 `vite.config.js` 或 `vite.config.ts` 中:
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import { defineConfig } from 'vite';
|
|
27
|
+
import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
|
|
28
|
+
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
plugins: [
|
|
31
|
+
componentCompiler(),
|
|
32
|
+
// 其他插件...
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 自定义配置
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
import { defineConfig } from 'vite';
|
|
41
|
+
import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
|
|
42
|
+
|
|
43
|
+
export default defineConfig({
|
|
44
|
+
plugins: [
|
|
45
|
+
componentCompiler({
|
|
46
|
+
validExtensions: new Set(['.jsx', '.tsx']), // 支持的文件扩展名
|
|
47
|
+
enableLogging: true, // 启用调试日志
|
|
48
|
+
threeFiberElems: [
|
|
49
|
+
/* 自定义列表 */
|
|
50
|
+
], // 自定义 Three.js 元素
|
|
51
|
+
dreiElems: [
|
|
52
|
+
/* 自定义列表 */
|
|
53
|
+
], // 自定义 Drei 元素
|
|
54
|
+
}),
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 工作原理
|
|
60
|
+
|
|
61
|
+
插件会扫描 `.jsx` 文件中的 JSX 元素,对于不在 Three.js Fiber 元素列表和 Drei 元素列表中的元素,自动添加以下属性:
|
|
62
|
+
|
|
63
|
+
- `data-nocode-id`: 格式为 `文件路径:行号:列号`
|
|
64
|
+
- `data-nocode-name`: 元素名称
|
|
65
|
+
- `data-nocode-array`: 当元素在数组映射中时,包含数组名称
|
|
66
|
+
- `data-nocode-index`: 当元素在数组映射中时,包含索引变量
|
|
67
|
+
|
|
68
|
+
## 示例
|
|
69
|
+
|
|
70
|
+
输入:
|
|
71
|
+
|
|
72
|
+
```jsx
|
|
73
|
+
function App() {
|
|
74
|
+
return (
|
|
75
|
+
<div>
|
|
76
|
+
<button>Click me</button>
|
|
77
|
+
{items.map((item, index) => (
|
|
78
|
+
<span key={item.id}>{item.name}</span>
|
|
79
|
+
))}
|
|
80
|
+
</div>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
输出:
|
|
86
|
+
|
|
87
|
+
```jsx
|
|
88
|
+
function App() {
|
|
89
|
+
return (
|
|
90
|
+
<div data-nocode-id='App.jsx:3:4' data-nocode-name='div'>
|
|
91
|
+
<button data-nocode-id='App.jsx:4:6' data-nocode-name='button'>
|
|
92
|
+
Click me
|
|
93
|
+
</button>
|
|
94
|
+
{items.map((item, index) => (
|
|
95
|
+
<span data-nocode-id='App.jsx:6:8' data-nocode-name='span' data-nocode-array='items' data-nocode-index='${index}' key={item.id}>
|
|
96
|
+
{item.name}
|
|
97
|
+
</span>
|
|
98
|
+
))}
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## 架构
|
|
105
|
+
|
|
106
|
+
该插件采用模块化设计,支持多种前端框架:
|
|
107
|
+
|
|
108
|
+
- **JSX/TSX**: ✅ 已实现
|
|
109
|
+
- **Vue**: 🚧 接口已预留,等待实现
|
|
110
|
+
|
|
111
|
+
详细架构说明请参考 [ARCHITECTURE.md](./ARCHITECTURE.md)
|
|
112
|
+
|
|
113
|
+
## 开发
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
# 构建
|
|
117
|
+
npm run build
|
|
118
|
+
|
|
119
|
+
# 开发模式(监听文件变化)
|
|
120
|
+
npm run dev
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## 扩展支持
|
|
124
|
+
|
|
125
|
+
如需添加新框架支持,请参考架构文档中的扩展指南。
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
ISC
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.componentCompiler = componentCompiler;
|
|
4
|
+
const constants_1 = require("./utils/constants");
|
|
5
|
+
const base_compiler_1 = require("./modules/base-compiler");
|
|
6
|
+
const jsx_compiler_1 = require("./modules/jsx-compiler");
|
|
7
|
+
const utils_1 = require("./utils/utils");
|
|
8
|
+
/**
|
|
9
|
+
* 创建 nocode 编译器插件
|
|
10
|
+
* @param options 编译器选项
|
|
11
|
+
* @returns Vite 插件
|
|
12
|
+
*/
|
|
13
|
+
function componentCompiler(options = {}) {
|
|
14
|
+
// 合并默认配置
|
|
15
|
+
const config = {
|
|
16
|
+
...constants_1.DEFAULT_CONFIG,
|
|
17
|
+
...options,
|
|
18
|
+
validExtensions: new Set([...Array.from(constants_1.DEFAULT_CONFIG.validExtensions), ...(options.validExtensions ? Array.from(options.validExtensions) : [])]),
|
|
19
|
+
threeFiberElems: options.threeFiberElems || constants_1.DEFAULT_CONFIG.threeFiberElems,
|
|
20
|
+
dreiElems: options.dreiElems || constants_1.DEFAULT_CONFIG.dreiElems,
|
|
21
|
+
};
|
|
22
|
+
// 创建编译器注册表
|
|
23
|
+
const registry = new base_compiler_1.CompilerRegistry();
|
|
24
|
+
// 注册 JSX 编译器
|
|
25
|
+
registry.register({
|
|
26
|
+
name: 'jsx',
|
|
27
|
+
supportedExtensions: jsx_compiler_1.JSXCompiler.supportedExtensions,
|
|
28
|
+
CompilerClass: jsx_compiler_1.JSXCompiler,
|
|
29
|
+
});
|
|
30
|
+
// 未来可以在这里注册 Vue 编译器
|
|
31
|
+
// registry.register({
|
|
32
|
+
// name: 'vue',
|
|
33
|
+
// supportedExtensions: VueCompiler.supportedExtensions,
|
|
34
|
+
// CompilerClass: VueCompiler,
|
|
35
|
+
// });
|
|
36
|
+
// 创建统计管理器
|
|
37
|
+
const statsManager = new utils_1.StatsManager();
|
|
38
|
+
return {
|
|
39
|
+
name: 'vite-plugin-nocode-compiler',
|
|
40
|
+
enforce: 'pre',
|
|
41
|
+
async transform(code, id) {
|
|
42
|
+
// 获取适合的编译器
|
|
43
|
+
const compiler = registry.getCompilerForFile(id, {
|
|
44
|
+
validExtensions: config.validExtensions,
|
|
45
|
+
threeFiberElems: config.threeFiberElems,
|
|
46
|
+
dreiElems: config.dreiElems,
|
|
47
|
+
enableLogging: config.enableLogging,
|
|
48
|
+
});
|
|
49
|
+
if (!compiler) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
// 统计文件处理
|
|
53
|
+
statsManager.incrementTotalFiles();
|
|
54
|
+
// 编译代码
|
|
55
|
+
const result = await compiler.compile(code, id);
|
|
56
|
+
if (result) {
|
|
57
|
+
statsManager.incrementProcessedFiles();
|
|
58
|
+
// 这里可以添加元素计数,如果编译器返回相关信息的话
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
name: 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(name: string): CompilerInfo | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* 获取所有支持的文件扩展名
|
|
45
|
+
*/
|
|
46
|
+
getAllSupportedExtensions(): string[];
|
|
47
|
+
/**
|
|
48
|
+
* 根据文件扩展名获取合适的编译器
|
|
49
|
+
*/
|
|
50
|
+
getCompilerForFile(filePath: string, options: any): BaseCompiler | null;
|
|
51
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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.name, info);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 获取编译器信息
|
|
18
|
+
*/
|
|
19
|
+
getCompilerInfo(name) {
|
|
20
|
+
return this.compilers.get(name);
|
|
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;
|
|
@@ -0,0 +1,26 @@
|
|
|
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 编译器类
|
|
10
|
+
* 负责处理 JSX 文件的编译逻辑
|
|
11
|
+
*/
|
|
12
|
+
export declare class JSXCompiler implements BaseCompiler {
|
|
13
|
+
static readonly supportedExtensions: string[];
|
|
14
|
+
static readonly name = "jsx";
|
|
15
|
+
private options;
|
|
16
|
+
private logger;
|
|
17
|
+
private cwd;
|
|
18
|
+
constructor(options: JSXCompilerOptions);
|
|
19
|
+
/**
|
|
20
|
+
* 编译 JSX 代码
|
|
21
|
+
*/
|
|
22
|
+
compile(code: string, id: string): Promise<{
|
|
23
|
+
code: string;
|
|
24
|
+
map: any;
|
|
25
|
+
} | null>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.JSXCompiler = void 0;
|
|
40
|
+
const parser_1 = require("@babel/parser");
|
|
41
|
+
const magic_string_1 = __importDefault(require("magic-string"));
|
|
42
|
+
const path_1 = __importDefault(require("path"));
|
|
43
|
+
const utils_1 = require("../utils/utils");
|
|
44
|
+
/**
|
|
45
|
+
* JSX 编译器类
|
|
46
|
+
* 负责处理 JSX 文件的编译逻辑
|
|
47
|
+
*/
|
|
48
|
+
class JSXCompiler {
|
|
49
|
+
static supportedExtensions = ['.jsx'];
|
|
50
|
+
static name = 'jsx';
|
|
51
|
+
options;
|
|
52
|
+
logger;
|
|
53
|
+
cwd;
|
|
54
|
+
constructor(options) {
|
|
55
|
+
this.options = options;
|
|
56
|
+
this.logger = new utils_1.Logger(options.enableLogging);
|
|
57
|
+
this.cwd = process.cwd();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 编译 JSX 代码
|
|
61
|
+
*/
|
|
62
|
+
async compile(code, id) {
|
|
63
|
+
this.logger.log('\nProcessing file:', id);
|
|
64
|
+
if (!(0, utils_1.shouldProcessFile)(id, this.options.validExtensions)) {
|
|
65
|
+
this.logger.log('Skipping file (not .jsx or in node_modules):', id);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const relativePath = path_1.default.relative(this.cwd, id);
|
|
69
|
+
this.logger.log('Relative path:', relativePath);
|
|
70
|
+
try {
|
|
71
|
+
const parserOptions = {
|
|
72
|
+
sourceType: 'module',
|
|
73
|
+
plugins: ['jsx'],
|
|
74
|
+
};
|
|
75
|
+
const ast = (0, parser_1.parse)(code, parserOptions);
|
|
76
|
+
const magicString = new magic_string_1.default(code);
|
|
77
|
+
let changedElementsCount = 0;
|
|
78
|
+
let currentElement = null;
|
|
79
|
+
let arrayContext = null;
|
|
80
|
+
const estreeWalker = await Promise.resolve().then(() => __importStar(require('estree-walker')));
|
|
81
|
+
const walk = estreeWalker.walk || estreeWalker.default?.walk || estreeWalker.default;
|
|
82
|
+
walk(ast, {
|
|
83
|
+
enter: (node) => {
|
|
84
|
+
// 检测数组的 map 调用
|
|
85
|
+
if (node.type === 'CallExpression' && node.callee?.type === 'MemberExpression' && node.callee.property?.name === 'map') {
|
|
86
|
+
this.logger.log('Found array map:', node);
|
|
87
|
+
const callee = node.callee;
|
|
88
|
+
const arrayName = callee.object.name || (callee.object.type === 'MemberExpression' ? `${callee.object.object.name}.${callee.object.property.name}` : null);
|
|
89
|
+
if (arrayName) {
|
|
90
|
+
const paramName = node.arguments[0].params?.[1]?.name || 'index';
|
|
91
|
+
arrayContext = { arrayName, paramName };
|
|
92
|
+
this.logger.log('Found array map:', { arrayName, paramName });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (node.type === 'JSXElement') {
|
|
96
|
+
currentElement = node;
|
|
97
|
+
}
|
|
98
|
+
if (node.type === 'JSXOpeningElement') {
|
|
99
|
+
const jsxNode = node;
|
|
100
|
+
let elementName;
|
|
101
|
+
if (jsxNode.name.type === 'JSXIdentifier') {
|
|
102
|
+
elementName = jsxNode.name.name;
|
|
103
|
+
}
|
|
104
|
+
else if (jsxNode.name.type === 'JSXMemberExpression') {
|
|
105
|
+
const memberExpr = jsxNode.name;
|
|
106
|
+
elementName = `${memberExpr.object.name}.${memberExpr.property.name}`;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (elementName === 'Fragment' || elementName === 'React.Fragment') {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const shouldTag = (0, utils_1.shouldTagElement)(elementName, this.options.threeFiberElems, this.options.dreiElems);
|
|
115
|
+
this.logger.log('Found JSX element:', {
|
|
116
|
+
elementName,
|
|
117
|
+
shouldTag,
|
|
118
|
+
hasArrayContext: !!arrayContext,
|
|
119
|
+
});
|
|
120
|
+
if (shouldTag) {
|
|
121
|
+
const line = jsxNode.loc?.start?.line ?? 0;
|
|
122
|
+
const col = jsxNode.loc?.start?.column ?? 0;
|
|
123
|
+
const attributes = (0, utils_1.generateDataAttributes)(relativePath, line, col, elementName, arrayContext);
|
|
124
|
+
this.logger.log('Adding attributes:', attributes);
|
|
125
|
+
magicString.appendLeft(jsxNode.name.end ?? 0, attributes);
|
|
126
|
+
changedElementsCount++;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
leave: (node) => {
|
|
131
|
+
if (node.type === 'CallExpression' && node.callee?.type === 'MemberExpression' && node.callee.property?.name === 'map') {
|
|
132
|
+
if (arrayContext) {
|
|
133
|
+
this.logger.log('Leaving array map context:', arrayContext.arrayName);
|
|
134
|
+
}
|
|
135
|
+
arrayContext = null;
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
this.logger.log('File processing complete:', {
|
|
140
|
+
file: relativePath,
|
|
141
|
+
elementsChanged: changedElementsCount,
|
|
142
|
+
});
|
|
143
|
+
if (changedElementsCount > 0) {
|
|
144
|
+
this.logger.log('Modified code preview:', magicString.toString().slice(0, 500) + '...');
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
code: magicString.toString(),
|
|
148
|
+
map: magicString.generateMap({ hires: true }),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
this.logger.error('Error processing file:', relativePath);
|
|
153
|
+
this.logger.error(error);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
exports.JSXCompiler = JSXCompiler;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
export interface ArrayContext {
|
|
3
|
+
arrayName: string;
|
|
4
|
+
paramName: string;
|
|
5
|
+
}
|
|
6
|
+
export interface CompilerStats {
|
|
7
|
+
totalFiles: number;
|
|
8
|
+
processedFiles: number;
|
|
9
|
+
totalElements: number;
|
|
10
|
+
}
|
|
11
|
+
export interface CompilerOptions {
|
|
12
|
+
validExtensions?: Set<string>;
|
|
13
|
+
threeFiberElems?: string[];
|
|
14
|
+
dreiElems?: string[];
|
|
15
|
+
enableLogging?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export type NocodeCompilerPlugin = (options?: Partial<CompilerOptions>) => Plugin;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three.js Fiber 元素列表
|
|
3
|
+
* 这些元素不需要添加 nocode 标识
|
|
4
|
+
*/
|
|
5
|
+
export declare const threeFiberElems: string[];
|
|
6
|
+
/**
|
|
7
|
+
* Drei 元素列表
|
|
8
|
+
* 这些元素不需要添加 nocode 标识
|
|
9
|
+
*/
|
|
10
|
+
export declare const dreiElems: string[];
|
|
11
|
+
/**
|
|
12
|
+
* 支持的文件扩展名
|
|
13
|
+
*/
|
|
14
|
+
export declare const DEFAULT_VALID_EXTENSIONS: Set<string>;
|
|
15
|
+
/**
|
|
16
|
+
* 默认配置
|
|
17
|
+
*/
|
|
18
|
+
export declare const DEFAULT_CONFIG: {
|
|
19
|
+
validExtensions: Set<string>;
|
|
20
|
+
threeFiberElems: string[];
|
|
21
|
+
dreiElems: string[];
|
|
22
|
+
enableLogging: boolean;
|
|
23
|
+
};
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_CONFIG = exports.DEFAULT_VALID_EXTENSIONS = exports.dreiElems = exports.threeFiberElems = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Three.js Fiber 元素列表
|
|
6
|
+
* 这些元素不需要添加 nocode 标识
|
|
7
|
+
*/
|
|
8
|
+
exports.threeFiberElems = [
|
|
9
|
+
'object3D',
|
|
10
|
+
'audioListener',
|
|
11
|
+
'positionalAudio',
|
|
12
|
+
'mesh',
|
|
13
|
+
'batchedMesh',
|
|
14
|
+
'instancedMesh',
|
|
15
|
+
'scene',
|
|
16
|
+
'sprite',
|
|
17
|
+
'lOD',
|
|
18
|
+
'skinnedMesh',
|
|
19
|
+
'skeleton',
|
|
20
|
+
'bone',
|
|
21
|
+
'lineSegments',
|
|
22
|
+
'lineLoop',
|
|
23
|
+
'points',
|
|
24
|
+
'group',
|
|
25
|
+
'camera',
|
|
26
|
+
'perspectiveCamera',
|
|
27
|
+
'orthographicCamera',
|
|
28
|
+
'cubeCamera',
|
|
29
|
+
'arrayCamera',
|
|
30
|
+
'instancedBufferGeometry',
|
|
31
|
+
'bufferGeometry',
|
|
32
|
+
'boxBufferGeometry',
|
|
33
|
+
'circleBufferGeometry',
|
|
34
|
+
'coneBufferGeometry',
|
|
35
|
+
'cylinderBufferGeometry',
|
|
36
|
+
'dodecahedronBufferGeometry',
|
|
37
|
+
'extrudeBufferGeometry',
|
|
38
|
+
'icosahedronBufferGeometry',
|
|
39
|
+
'latheBufferGeometry',
|
|
40
|
+
'octahedronBufferGeometry',
|
|
41
|
+
'planeBufferGeometry',
|
|
42
|
+
'polyhedronBufferGeometry',
|
|
43
|
+
'ringBufferGeometry',
|
|
44
|
+
'shapeBufferGeometry',
|
|
45
|
+
'sphereBufferGeometry',
|
|
46
|
+
'tetrahedronBufferGeometry',
|
|
47
|
+
'torusBufferGeometry',
|
|
48
|
+
'torusKnotBufferGeometry',
|
|
49
|
+
'tubeBufferGeometry',
|
|
50
|
+
'wireframeGeometry',
|
|
51
|
+
'tetrahedronGeometry',
|
|
52
|
+
'octahedronGeometry',
|
|
53
|
+
'icosahedronGeometry',
|
|
54
|
+
'dodecahedronGeometry',
|
|
55
|
+
'polyhedronGeometry',
|
|
56
|
+
'tubeGeometry',
|
|
57
|
+
'torusKnotGeometry',
|
|
58
|
+
'torusGeometry',
|
|
59
|
+
'sphereGeometry',
|
|
60
|
+
'ringGeometry',
|
|
61
|
+
'planeGeometry',
|
|
62
|
+
'latheGeometry',
|
|
63
|
+
'shapeGeometry',
|
|
64
|
+
'extrudeGeometry',
|
|
65
|
+
'edgesGeometry',
|
|
66
|
+
'coneGeometry',
|
|
67
|
+
'cylinderGeometry',
|
|
68
|
+
'circleGeometry',
|
|
69
|
+
'boxGeometry',
|
|
70
|
+
'capsuleGeometry',
|
|
71
|
+
'material',
|
|
72
|
+
'shadowMaterial',
|
|
73
|
+
'spriteMaterial',
|
|
74
|
+
'rawShaderMaterial',
|
|
75
|
+
'shaderMaterial',
|
|
76
|
+
'pointsMaterial',
|
|
77
|
+
'meshPhysicalMaterial',
|
|
78
|
+
'meshStandardMaterial',
|
|
79
|
+
'meshPhongMaterial',
|
|
80
|
+
'meshToonMaterial',
|
|
81
|
+
'meshNormalMaterial',
|
|
82
|
+
'meshLambertMaterial',
|
|
83
|
+
'meshDepthMaterial',
|
|
84
|
+
'meshDistanceMaterial',
|
|
85
|
+
'meshBasicMaterial',
|
|
86
|
+
'meshMatcapMaterial',
|
|
87
|
+
'lineDashedMaterial',
|
|
88
|
+
'lineBasicMaterial',
|
|
89
|
+
'primitive',
|
|
90
|
+
'light',
|
|
91
|
+
'spotLightShadow',
|
|
92
|
+
'spotLight',
|
|
93
|
+
'pointLight',
|
|
94
|
+
'rectAreaLight',
|
|
95
|
+
'hemisphereLight',
|
|
96
|
+
'directionalLightShadow',
|
|
97
|
+
'directionalLight',
|
|
98
|
+
'ambientLight',
|
|
99
|
+
'lightShadow',
|
|
100
|
+
'ambientLightProbe',
|
|
101
|
+
'hemisphereLightProbe',
|
|
102
|
+
'lightProbe',
|
|
103
|
+
'spotLightHelper',
|
|
104
|
+
'skeletonHelper',
|
|
105
|
+
'pointLightHelper',
|
|
106
|
+
'hemisphereLightHelper',
|
|
107
|
+
'gridHelper',
|
|
108
|
+
'polarGridHelper',
|
|
109
|
+
'directionalLightHelper',
|
|
110
|
+
'cameraHelper',
|
|
111
|
+
'boxHelper',
|
|
112
|
+
'box3Helper',
|
|
113
|
+
'planeHelper',
|
|
114
|
+
'arrowHelper',
|
|
115
|
+
'axesHelper',
|
|
116
|
+
'texture',
|
|
117
|
+
'videoTexture',
|
|
118
|
+
'dataTexture',
|
|
119
|
+
'dataTexture3D',
|
|
120
|
+
'compressedTexture',
|
|
121
|
+
'cubeTexture',
|
|
122
|
+
'canvasTexture',
|
|
123
|
+
'depthTexture',
|
|
124
|
+
'raycaster',
|
|
125
|
+
'vector2',
|
|
126
|
+
'vector3',
|
|
127
|
+
'vector4',
|
|
128
|
+
'euler',
|
|
129
|
+
'matrix3',
|
|
130
|
+
'matrix4',
|
|
131
|
+
'quaternion',
|
|
132
|
+
'bufferAttribute',
|
|
133
|
+
'float16BufferAttribute',
|
|
134
|
+
'float32BufferAttribute',
|
|
135
|
+
'float64BufferAttribute',
|
|
136
|
+
'int8BufferAttribute',
|
|
137
|
+
'int16BufferAttribute',
|
|
138
|
+
'int32BufferAttribute',
|
|
139
|
+
'uint8BufferAttribute',
|
|
140
|
+
'uint16BufferAttribute',
|
|
141
|
+
'uint32BufferAttribute',
|
|
142
|
+
'instancedBufferAttribute',
|
|
143
|
+
'color',
|
|
144
|
+
'fog',
|
|
145
|
+
'fogExp2',
|
|
146
|
+
'shape',
|
|
147
|
+
'colorShiftMaterial',
|
|
148
|
+
];
|
|
149
|
+
/**
|
|
150
|
+
* Drei 元素列表
|
|
151
|
+
* 这些元素不需要添加 nocode 标识
|
|
152
|
+
*/
|
|
153
|
+
exports.dreiElems = [
|
|
154
|
+
'AsciiRenderer',
|
|
155
|
+
'Billboard',
|
|
156
|
+
'Clone',
|
|
157
|
+
'ComputedAttribute',
|
|
158
|
+
'Decal',
|
|
159
|
+
'Edges',
|
|
160
|
+
'Effects',
|
|
161
|
+
'GradientTexture',
|
|
162
|
+
'Image',
|
|
163
|
+
'MarchingCubes',
|
|
164
|
+
'Outlines',
|
|
165
|
+
'PositionalAudio',
|
|
166
|
+
'Sampler',
|
|
167
|
+
'ScreenSizer',
|
|
168
|
+
'ScreenSpace',
|
|
169
|
+
'Splat',
|
|
170
|
+
'Svg',
|
|
171
|
+
'Text',
|
|
172
|
+
'Text3D',
|
|
173
|
+
'Trail',
|
|
174
|
+
'CubeCamera',
|
|
175
|
+
'OrthographicCamera',
|
|
176
|
+
'PerspectiveCamera',
|
|
177
|
+
'CameraControls',
|
|
178
|
+
'FaceControls',
|
|
179
|
+
'KeyboardControls',
|
|
180
|
+
'MotionPathControls',
|
|
181
|
+
'PresentationControls',
|
|
182
|
+
'ScrollControls',
|
|
183
|
+
'DragControls',
|
|
184
|
+
'GizmoHelper',
|
|
185
|
+
'Grid',
|
|
186
|
+
'Helper',
|
|
187
|
+
'PivotControls',
|
|
188
|
+
'TransformControls',
|
|
189
|
+
'CubeTexture',
|
|
190
|
+
'Fbx',
|
|
191
|
+
'Gltf',
|
|
192
|
+
'Ktx2',
|
|
193
|
+
'Loader',
|
|
194
|
+
'Progress',
|
|
195
|
+
'ScreenVideoTexture',
|
|
196
|
+
'Texture',
|
|
197
|
+
'TrailTexture',
|
|
198
|
+
'VideoTexture',
|
|
199
|
+
'WebcamVideoTexture',
|
|
200
|
+
'CycleRaycast',
|
|
201
|
+
'DetectGPU',
|
|
202
|
+
'Example',
|
|
203
|
+
'FaceLandmarker',
|
|
204
|
+
'Fbo',
|
|
205
|
+
'Html',
|
|
206
|
+
'Select',
|
|
207
|
+
'SpriteAnimator',
|
|
208
|
+
'StatsGl',
|
|
209
|
+
'Stats',
|
|
210
|
+
'Trail',
|
|
211
|
+
'Wireframe',
|
|
212
|
+
'CurveModifier',
|
|
213
|
+
'AdaptiveDpr',
|
|
214
|
+
'AdaptiveEvents',
|
|
215
|
+
'BakeShadows',
|
|
216
|
+
'Bvh',
|
|
217
|
+
'Detailed',
|
|
218
|
+
'Instances',
|
|
219
|
+
'Merged',
|
|
220
|
+
'meshBounds',
|
|
221
|
+
'PerformanceMonitor',
|
|
222
|
+
'Points',
|
|
223
|
+
'Preload',
|
|
224
|
+
'Segments',
|
|
225
|
+
'Fisheye',
|
|
226
|
+
'Hud',
|
|
227
|
+
'Mask',
|
|
228
|
+
'MeshPortalMaterial',
|
|
229
|
+
'RenderCubeTexture',
|
|
230
|
+
'RenderTexture',
|
|
231
|
+
'View',
|
|
232
|
+
'MeshDiscardMaterial',
|
|
233
|
+
'MeshDistortMaterial',
|
|
234
|
+
'MeshReflectorMaterial',
|
|
235
|
+
'MeshRefractionMaterial',
|
|
236
|
+
'MeshTransmissionMaterial',
|
|
237
|
+
'MeshWobbleMaterial',
|
|
238
|
+
'PointMaterial',
|
|
239
|
+
'shaderMaterial',
|
|
240
|
+
'SoftShadows',
|
|
241
|
+
'CatmullRomLine',
|
|
242
|
+
'CubicBezierLine',
|
|
243
|
+
'Facemesh',
|
|
244
|
+
'Line',
|
|
245
|
+
'Mesh',
|
|
246
|
+
'QuadraticBezierLine',
|
|
247
|
+
'RoundedBox',
|
|
248
|
+
'ScreenQuad',
|
|
249
|
+
'AccumulativeShadows',
|
|
250
|
+
'Backdrop',
|
|
251
|
+
'BBAnchor',
|
|
252
|
+
'Bounds',
|
|
253
|
+
'CameraShake',
|
|
254
|
+
'Caustics',
|
|
255
|
+
'Center',
|
|
256
|
+
'Cloud',
|
|
257
|
+
'ContactShadows',
|
|
258
|
+
'Environment',
|
|
259
|
+
'Float',
|
|
260
|
+
'Lightformer',
|
|
261
|
+
'MatcapTexture',
|
|
262
|
+
'NormalTexture',
|
|
263
|
+
'RandomizedLight',
|
|
264
|
+
'Resize',
|
|
265
|
+
'ShadowAlpha',
|
|
266
|
+
'Shadow',
|
|
267
|
+
'Sky',
|
|
268
|
+
'Sparkles',
|
|
269
|
+
'SpotLightShadow',
|
|
270
|
+
'SpotLight',
|
|
271
|
+
'Stage',
|
|
272
|
+
'Stars',
|
|
273
|
+
'OrbitControls',
|
|
274
|
+
];
|
|
275
|
+
/**
|
|
276
|
+
* 支持的文件扩展名
|
|
277
|
+
*/
|
|
278
|
+
exports.DEFAULT_VALID_EXTENSIONS = new Set(['.jsx']);
|
|
279
|
+
/**
|
|
280
|
+
* 默认配置
|
|
281
|
+
*/
|
|
282
|
+
exports.DEFAULT_CONFIG = {
|
|
283
|
+
validExtensions: exports.DEFAULT_VALID_EXTENSIONS,
|
|
284
|
+
threeFiberElems: exports.threeFiberElems,
|
|
285
|
+
dreiElems: exports.dreiElems,
|
|
286
|
+
enableLogging: true,
|
|
287
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ArrayContext, CompilerStats } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* 判断是否需要给元素打标
|
|
4
|
+
*/
|
|
5
|
+
export declare function shouldTagElement(elementName: string, threeFiberElems: string[], dreiElems: string[]): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* 生成数据属性字符串
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateDataAttributes(relativePath: string, line: number, col: number, elementName: string, arrayContext?: ArrayContext | null): string;
|
|
10
|
+
/**
|
|
11
|
+
* 日志记录工具
|
|
12
|
+
*/
|
|
13
|
+
export declare class Logger {
|
|
14
|
+
private enableLogging;
|
|
15
|
+
constructor(enableLogging?: boolean);
|
|
16
|
+
log(message: string, ...args: any[]): void;
|
|
17
|
+
error(message: string, ...args: any[]): void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 统计信息管理
|
|
21
|
+
*/
|
|
22
|
+
export declare class StatsManager {
|
|
23
|
+
private stats;
|
|
24
|
+
constructor();
|
|
25
|
+
incrementTotalFiles(): void;
|
|
26
|
+
incrementProcessedFiles(): void;
|
|
27
|
+
addElements(count: number): void;
|
|
28
|
+
getStats(): CompilerStats;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 检查文件是否应该被处理
|
|
32
|
+
*/
|
|
33
|
+
export declare function shouldProcessFile(id: string, validExtensions: Set<string>): boolean;
|
|
@@ -0,0 +1,79 @@
|
|
|
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.StatsManager = exports.Logger = void 0;
|
|
7
|
+
exports.shouldTagElement = shouldTagElement;
|
|
8
|
+
exports.generateDataAttributes = generateDataAttributes;
|
|
9
|
+
exports.shouldProcessFile = shouldProcessFile;
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
/**
|
|
12
|
+
* 判断是否需要给元素打标
|
|
13
|
+
*/
|
|
14
|
+
function shouldTagElement(elementName, threeFiberElems, dreiElems) {
|
|
15
|
+
return !threeFiberElems.includes(elementName) && !dreiElems.includes(elementName);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 生成数据属性字符串
|
|
19
|
+
*/
|
|
20
|
+
function generateDataAttributes(relativePath, line, col, elementName, arrayContext) {
|
|
21
|
+
const dataComponentId = `${relativePath}:${line}:${col}`;
|
|
22
|
+
let arrayAttrs = '';
|
|
23
|
+
if (arrayContext) {
|
|
24
|
+
arrayAttrs = ` data-nocode-array="${arrayContext.arrayName}" data-nocode-index="\${${arrayContext.paramName}}"`;
|
|
25
|
+
}
|
|
26
|
+
return ` data-nocode-id="${dataComponentId}" data-nocode-name="${elementName}"${arrayAttrs}`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 日志记录工具
|
|
30
|
+
*/
|
|
31
|
+
class Logger {
|
|
32
|
+
enableLogging;
|
|
33
|
+
constructor(enableLogging = true) {
|
|
34
|
+
this.enableLogging = enableLogging;
|
|
35
|
+
}
|
|
36
|
+
log(message, ...args) {
|
|
37
|
+
if (this.enableLogging) {
|
|
38
|
+
console.log(`[nocode-compiler] ${message}`, ...args);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
error(message, ...args) {
|
|
42
|
+
if (this.enableLogging) {
|
|
43
|
+
console.error(`[nocode-compiler] ${message}`, ...args);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.Logger = Logger;
|
|
48
|
+
/**
|
|
49
|
+
* 统计信息管理
|
|
50
|
+
*/
|
|
51
|
+
class StatsManager {
|
|
52
|
+
stats;
|
|
53
|
+
constructor() {
|
|
54
|
+
this.stats = {
|
|
55
|
+
totalFiles: 0,
|
|
56
|
+
processedFiles: 0,
|
|
57
|
+
totalElements: 0,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
incrementTotalFiles() {
|
|
61
|
+
this.stats.totalFiles++;
|
|
62
|
+
}
|
|
63
|
+
incrementProcessedFiles() {
|
|
64
|
+
this.stats.processedFiles++;
|
|
65
|
+
}
|
|
66
|
+
addElements(count) {
|
|
67
|
+
this.stats.totalElements += count;
|
|
68
|
+
}
|
|
69
|
+
getStats() {
|
|
70
|
+
return { ...this.stats };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.StatsManager = StatsManager;
|
|
74
|
+
/**
|
|
75
|
+
* 检查文件是否应该被处理
|
|
76
|
+
*/
|
|
77
|
+
function shouldProcessFile(id, validExtensions) {
|
|
78
|
+
return validExtensions.has(path_1.default.extname(id)) && !id.includes('node_modules');
|
|
79
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meituan-nocode/vite-plugin-nocode-compiler",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "nocode编译插件",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"dev": "tsc --watch",
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"prepublishOnly": "npm run build"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"vite": "^5.4.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^20.0.0",
|
|
21
|
+
"typescript": "^5.0.0",
|
|
22
|
+
"vite": "^5.4.0"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@babel/parser": "^7.23.0",
|
|
26
|
+
"@babel/types": "^7.23.0",
|
|
27
|
+
"magic-string": "^0.30.0",
|
|
28
|
+
"estree-walker": "^3.0.0"
|
|
29
|
+
}
|
|
30
|
+
}
|