@meituan-nocode/vite-plugin-nocode-compiler 0.1.0-beta.9-z → 0.2.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 CHANGED
@@ -1,137 +1,127 @@
1
1
  # @meituan-nocode/vite-plugin-nocode-compiler
2
2
 
3
- Vite 插件,用于在构建过程中处理 Vue React 组件,添加 nocode 相关的标记。该插件支持自动检测项目框架类型(Vue/React),并应用相应的编译逻辑。
3
+ 一个专门为 Vite 开发的 nocode 编译插件,用于自动为 JSX/TSX 元素添加 nocode 所需的标识属性。该插件基于 `@meituan-nocode/nocode-compiler-core` 实现,提供了高性能的代码转换能力。
4
4
 
5
5
  ## 特性
6
6
 
7
- - 支持 Vue React 组件的处理
8
- - 自动检测项目框架类型
9
- - 兼容 Vite 4.x 5.x 版本
10
- - 可配置的日志输出
11
- - 支持 ESM 和 CommonJS 导入方式
7
+ - 🎯 **智能元素标记**: 自动为 JSX/TSX 元素添加 nocode 相关属性
8
+ - 🔍 **上下文感知**: 智能识别数组映射上下文,添加相应的数组和索引信息
9
+ - ⚡️ **高性能**: 基于 Babel AST 实现,支持增量编译
10
+ - 🛡️ **类型安全**: 使用 TypeScript 编写,提供完整的类型定义
11
+ - 📦 **零配置**: 开箱即用,支持自定义配置
12
12
 
13
13
  ## 安装
14
14
 
15
15
  ```bash
16
- # npm
17
16
  npm install @meituan-nocode/vite-plugin-nocode-compiler --save-dev
18
-
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
17
+ # 或
18
+ yarn add -D @meituan-nocode/vite-plugin-nocode-compiler
19
+ #
20
+ pnpm add -D @meituan-nocode/vite-plugin-nocode-compiler
24
21
  ```
25
22
 
26
- ## 使用方法
23
+ ## 使用
24
+
25
+ ### 基础配置
27
26
 
28
- ### ESM 导入 (推荐)
27
+ 在你的 `vite.config.ts` 中添加插件:
29
28
 
30
- ```js
31
- // vite.config.js / vite.config.ts
29
+ ```typescript
32
30
  import { defineConfig } from 'vite';
33
- import { nocodeCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
34
- // 或者使用默认导入
35
- // import nocodeCompiler from '@meituan-nocode/vite-plugin-nocode-compiler';
31
+ import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
36
32
 
37
33
  export default defineConfig({
38
- plugins: [
39
- nocodeCompiler({
40
- enableLogging: true,
41
- forceFramework: 'vue', // 或 'react'
42
- }),
43
- ],
44
- });
45
- ```
46
-
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
- ],
34
+ plugins: [componentCompiler()],
64
35
  });
65
36
  ```
66
37
 
67
- ### 兼容性导入 (如果上述方法不起作用)
38
+ ### 自定义配置
68
39
 
69
- ```js
70
- // vite.config.js / vite.config.ts
40
+ ```typescript
71
41
  import { defineConfig } from 'vite';
72
- import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler/compat';
42
+ import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
73
43
 
74
44
  export default defineConfig({
75
45
  plugins: [
76
46
  componentCompiler({
77
- enableLogging: true,
78
- forceFramework: 'vue', // 或 'react'
47
+ enableLogging: true, // 启用调试日志
48
+ rootPath: process.cwd(), // 自定义项目根路径
79
49
  }),
80
50
  ],
81
51
  });
82
52
  ```
83
53
 
84
- ## 配置选项
85
-
86
- | 选项 | 类型 | 默认值 | 描述 |
87
- | ---------------- | ------------------ | -------- | ------------------------------------------------------ |
88
- | `enableLogging` | `boolean` | `false` | 是否启用日志输出,开启后会输出详细的处理过程和调试信息 |
89
- | `forceFramework` | `'vue' \| 'react'` | 自动检测 | 强制指定框架类型,不指定时会自动检测 |
90
- | `jsxOptions` | `object` | `{}` | JSX 编译器选项,用于配置 React 组件的编译行为 |
91
- | `vueOptions` | `object` | `{}` | Vue 编译器选项,用于配置 Vue 组件的编译行为 |
92
-
93
- ## 框架检测
94
-
95
- 如果未指定 `forceFramework`,插件会按以下优先级自动检测项目使用的框架:
96
-
97
- 1. 检查 package.json 中的依赖(React 依赖优先于 Vue 依赖)
98
- 2. 检查 src 目录下是否有框架特定文件(JSX/TSX 文件优先于 Vue 文件)
99
- 3. 如果以上都没有检测到,默认返回 React
54
+ ## 工作原理
100
55
 
101
- ## 兼容性
56
+ 插件会在构建过程中拦截并处理 `.jsx`、`.tsx`、`.js` 和 `.ts` 文件,通过 AST 分析为符合条件的 JSX 元素添加以下属性:
102
57
 
103
- 本插件兼容 Vite 4.x 和 5.x 版本。插件会自动检测 Vite 版本并进行相应的适配,确保在不同版本的 Vite 环境中都能正常工作。
58
+ - `data-nocode-id`: 元素的唯一标识,格式为 `文件路径:行号:列号`
59
+ - `data-nocode-name`: 元素的标签名称
60
+ - `data-nocode-array`: 当元素在数组 map 中时,记录数组名称
61
+ - `data-nocode-index`: 当元素在数组 map 中时,记录当前索引
104
62
 
105
- ### Vite 版本支持
63
+ ### 示例
106
64
 
107
- - **Vite 4.x**: 完全支持 (>=4.0.0)
108
- - **Vite 5.x**: 完全支持 (包括最新的 5.4.x)
65
+ 输入代码:
109
66
 
110
- 如果在使用过程中遇到任何兼容性问题,请开启日志功能进行调试:
111
-
112
- ```js
113
- nocodeCompiler({
114
- enableLogging: true,
115
- });
67
+ ```jsx
68
+ function TodoList({ items }) {
69
+ return (
70
+ <div className='todo-list'>
71
+ {items.map((item, index) => (
72
+ <div key={item.id} className='todo-item'>
73
+ <span>{item.text}</span>
74
+ <button onClick={() => handleDelete(item.id)}>删除</button>
75
+ </div>
76
+ ))}
77
+ </div>
78
+ );
79
+ }
116
80
  ```
117
81
 
118
- ## 常见问题
119
-
120
- ### Q: 如何确认插件正在正常工作?
82
+ 编译后:
83
+
84
+ ```jsx
85
+ function TodoList({ items }) {
86
+ return (
87
+ <div data-nocode-id='TodoList.jsx:2:8' data-nocode-name='div' className='todo-list'>
88
+ {items.map((item, index) => (
89
+ <div data-nocode-id='TodoList.jsx:4:12' data-nocode-name='div' data-nocode-array='items' data-nocode-index='${index}' key={item.id} className='todo-item'>
90
+ <span data-nocode-id='TodoList.jsx:5:16' data-nocode-name='span' data-nocode-array='items' data-nocode-index='${index}'>
91
+ {item.text}
92
+ </span>
93
+ <button data-nocode-id='TodoList.jsx:6:16' data-nocode-name='button' data-nocode-array='items' data-nocode-index='${index}' onClick={() => handleDelete(item.id)}>
94
+ 删除
95
+ </button>
96
+ </div>
97
+ ))}
98
+ </div>
99
+ );
100
+ }
101
+ ```
121
102
 
122
- A: 开启 `enableLogging` 选项,构建时会输出详细的处理日志,包括检测到的框架类型和处理的文件。
103
+ ## 技术细节
123
104
 
124
- ### Q: 插件处理哪些文件类型?
105
+ - 使用 `@babel/parser` 进行代码解析
106
+ - 使用 `@babel/traverse` 进行 AST 遍历
107
+ - 使用 `magic-string` 进行高效的代码修改
108
+ - 支持 TypeScript、装饰器等高级语法特性
109
+ - 智能跳过 `node_modules` 中的文件
125
110
 
126
- A: 插件会处理以下文件类型:
111
+ ## 版本要求
127
112
 
128
- - Vue 框架: `.vue` 文件
129
- - React 框架: `.jsx`, `.tsx` 文件,以及包含 JSX 语法的 `.js`, `.ts` 文件
113
+ - Vite: ^5.4.0
114
+ - Node.js: ^20.0.0
130
115
 
131
- ### Q: 如何在 monorepo 项目中使用?
116
+ ## 开发
132
117
 
133
- A: 在各子项目的 `vite.config.js` 中分别配置插件即可。如果子项目共享配置,确保正确设置 `forceFramework` 选项。
118
+ ```bash
119
+ # 安装依赖
120
+ pnpm install
134
121
 
135
- ## 许可证
122
+ # 开发模式
123
+ pnpm run dev
136
124
 
137
- MIT
125
+ # 构建
126
+ pnpm run build
127
+ ```
package/dist/index.d.ts CHANGED
@@ -1,12 +1,9 @@
1
- import { Plugin } from 'vite';
2
- import { FrameworkType } from './framework-detector';
3
1
  export interface NocodeCompilerOptions {
4
2
  enableLogging?: boolean;
5
- forceFramework?: FrameworkType;
3
+ rootPath?: string;
6
4
  }
7
- /**
8
- * Vite插件:nocode-compiler
9
- * 用于在构建过程中处理Vue和React组件,添加nocode相关的标记
10
- * 兼容 Vite 4.x 5.x 版本
11
- */
12
- export declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
5
+ export declare function componentCompiler(options?: NocodeCompilerOptions): {
6
+ name: string;
7
+ enforce: "pre";
8
+ transform(code: string, id: string): Promise<string | null>;
9
+ };
package/dist/index.js CHANGED
@@ -1,128 +1,28 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.componentCompiler = componentCompiler;
7
- const vite_1 = require("vite");
8
4
  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
- */
25
5
  function componentCompiler(options = {}) {
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,
36
- });
37
- const vueCompiler = new nocode_compiler_core_1.VueCompiler({
38
- enableLogging: options.enableLogging || false,
39
- });
6
+ const { enableLogging = false, rootPath = process.cwd() } = options;
40
7
  return {
41
8
  name: 'vite-plugin-nocode-compiler',
42
9
  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
- },
50
10
  async transform(code, id) {
51
- // 跳过node_modules
11
+ // 检查文件是否在 node_modules
52
12
  if (id.includes('node_modules')) {
53
- return null;
54
- }
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
- }
13
+ return code;
99
14
  }
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
- }
15
+ // 检查文件扩展名
16
+ if (!/\.(jsx?|tsx?)$/.test(id)) {
17
+ return code;
124
18
  }
125
- return null;
19
+ // 使用 jsxcompiler 编译
20
+ const jsxCompiler = new nocode_compiler_core_1.JSXCompiler({
21
+ enableLogging,
22
+ rootPath,
23
+ });
24
+ const result = jsxCompiler.compile(code, id);
25
+ return result;
126
26
  },
127
27
  };
128
28
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@meituan-nocode/vite-plugin-nocode-compiler",
3
- "version": "0.1.0-beta.9-z",
4
- "description": "nocode compiler plugin",
3
+ "version": "0.2.0-beta.1",
4
+ "description": "Vite plugin for nocode compiler",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -13,15 +13,12 @@
13
13
  "build": "tsc",
14
14
  "prepublishOnly": "npm run build"
15
15
  },
16
- "peerDependencies": {
17
- "vite": ">=4.0.0"
18
- },
19
16
  "devDependencies": {
20
17
  "@types/node": "^20.0.0",
21
18
  "typescript": "^5.0.0",
22
19
  "vite": "^5.4.0"
23
20
  },
24
21
  "dependencies": {
25
- "@meituan-nocode/nocode-compiler-core": "0.1.0-beta.16-z"
22
+ "@meituan-nocode/nocode-compiler-core": "0.2.0"
26
23
  }
27
24
  }
@@ -1,13 +0,0 @@
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;
@@ -1,85 +0,0 @@
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
- }