@meituan-nocode/vite-plugin-nocode-compiler 0.1.0-beta.8 → 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 CHANGED
@@ -1,124 +1,137 @@
1
1
  # @meituan-nocode/vite-plugin-nocode-compiler
2
2
 
3
- 一个用于 Vite nocode 编译插件,为 JSX 元素自动添加 nocode 所需的标识属性。
3
+ Vite 插件,用于在构建过程中处理 Vue React 组件,添加 nocode 相关的标记。该插件支持自动检测项目框架类型(Vue/React),并应用相应的编译逻辑。
4
4
 
5
- ## 功能
5
+ ## 特性
6
6
 
7
- - 🚀 **多框架支持**: 支持 JSX、TSXVue 文件
8
- - 📦 **自动元素标记**: 为非 Three.js Fiber 和非 Drei 的元素自动添加 nocode 标识
9
- - 🔄 **数组上下文检测**: 智能识别数组映射,添加索引信息
10
- - 🎯 **简单配置**: 开箱即用,仅需最少配置
11
- - ⚡️ **高性能**: 优化的编译过程,快速处理
7
+ - 支持 VueReact 组件的处理
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
- 在你的 `vite.config.js` `vite.config.ts` 中:
28
+ ### ESM 导入 (推荐)
24
29
 
25
- ```javascript
30
+ ```js
31
+ // vite.config.js / vite.config.ts
26
32
  import { defineConfig } from 'vite';
27
- import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
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
- componentCompiler(), // 使用默认配置
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
- ```javascript
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
- 插件会扫描 `.jsx`、`.tsx` `.vue` 文件中的元素,对于不在 Three.js Fiber 元素列表和 Drei 元素列表中的元素,自动添加以下属性:
86
+ | 选项 | 类型 | 默认值 | 描述 |
87
+ | ---------------- | ------------------ | -------- | ------------------------------------------------------ |
88
+ | `enableLogging` | `boolean` | `false` | 是否启用日志输出,开启后会输出详细的处理过程和调试信息 |
89
+ | `forceFramework` | `'vue' \| 'react'` | 自动检测 | 强制指定框架类型,不指定时会自动检测 |
90
+ | `jsxOptions` | `object` | `{}` | JSX 编译器选项,用于配置 React 组件的编译行为 |
91
+ | `vueOptions` | `object` | `{}` | Vue 编译器选项,用于配置 Vue 组件的编译行为 |
58
92
 
59
- - `data-nocode-id`: 格式为 `文件路径:行号:列号`
60
- - `data-nocode-tag-name`: 元素名称
61
- - `data-nocode-array`: 当元素在数组映射中时,包含数组名称
93
+ ## 框架检测
62
94
 
63
- ## 示例
95
+ 如果未指定 `forceFramework`,插件会按以下优先级自动检测项目使用的框架:
64
96
 
65
- 输入:
97
+ 1. 检查 package.json 中的依赖(React 依赖优先于 Vue 依赖)
98
+ 2. 检查 src 目录下是否有框架特定文件(JSX/TSX 文件优先于 Vue 文件)
99
+ 3. 如果以上都没有检测到,默认返回 React
66
100
 
67
- ```jsx
68
- function App() {
69
- return (
70
- <div>
71
- <button>Click me</button>
72
- {items.map((item, index) => (
73
- <span key={item.id}>{item.name}</span>
74
- ))}
75
- </div>
76
- );
77
- }
78
- ```
101
+ ## 兼容性
79
102
 
80
- 输出:
81
-
82
- ```jsx
83
- function App() {
84
- return (
85
- <div data-nocode-id='App.jsx:3:4' data-nocode-name='div'>
86
- <button data-nocode-id='App.jsx:4:6' data-nocode-name='button'>
87
- Click me
88
- </button>
89
- {items.map((item, index) => (
90
- <span data-nocode-id='App.jsx:6:8' data-nocode-name='span' data-nocode-array='items' data-nocode-index='${index}' key={item.id}>
91
- {item.name}
92
- </span>
93
- ))}
94
- </div>
95
- );
96
- }
97
- ```
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)
98
109
 
99
- ## 架构
110
+ 如果在使用过程中遇到任何兼容性问题,请开启日志功能进行调试:
100
111
 
101
- 该插件采用模块化设计,支持多种前端框架:
112
+ ```js
113
+ nocodeCompiler({
114
+ enableLogging: true,
115
+ });
116
+ ```
102
117
 
103
- - **JSX/TSX**: ✅ 已实现
104
- - **Vue**: ✅ 已实现
118
+ ## 常见问题
105
119
 
106
- 详细架构说明请参考 [ARCHITECTURE.md](./ARCHITECTURE.md)
120
+ ### Q: 如何确认插件正在正常工作?
107
121
 
108
- ## 开发
122
+ A: 开启 `enableLogging` 选项,构建时会输出详细的处理日志,包括检测到的框架类型和处理的文件。
109
123
 
110
- ```bash
111
- # 构建
112
- npm run build
124
+ ### Q: 插件处理哪些文件类型?
113
125
 
114
- # 开发模式(监听文件变化)
115
- npm run dev
116
- ```
126
+ A: 插件会处理以下文件类型:
127
+
128
+ - Vue 框架: `.vue` 文件
129
+ - React 框架: `.jsx`, `.tsx` 文件,以及包含 JSX 语法的 `.js`, `.ts` 文件
117
130
 
118
- ## 扩展支持
131
+ ### Q: 如何在 monorepo 项目中使用?
119
132
 
120
- 如需添加新框架支持,请参考架构文档中的扩展指南。
133
+ A: 在各子项目的 `vite.config.js` 中分别配置插件即可。如果子项目共享配置,确保正确设置 `forceFramework` 选项。
121
134
 
122
- ## License
135
+ ## 许可证
123
136
 
124
- ISC
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,5 +1,12 @@
1
- import type { Plugin } from 'vite';
1
+ import { Plugin } from 'vite';
2
+ import { FrameworkType } from './framework-detector';
2
3
  export interface NocodeCompilerOptions {
3
4
  enableLogging?: boolean;
5
+ forceFramework?: FrameworkType;
4
6
  }
7
+ /**
8
+ * Vite插件:nocode-compiler
9
+ * 用于在构建过程中处理Vue和React组件,添加nocode相关的标记
10
+ * 兼容 Vite 4.x 和 5.x 版本
11
+ */
5
12
  export declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
package/dist/index.js CHANGED
@@ -1,27 +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;
7
+ const vite_1 = require("vite");
4
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
- const { enableLogging = false } = 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
+ });
7
40
  return {
8
41
  name: 'vite-plugin-nocode-compiler',
9
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
+ },
10
50
  async transform(code, id) {
11
- // 检查文件是否在 node_modules
51
+ // 跳过node_modules
12
52
  if (id.includes('node_modules')) {
13
53
  return null;
14
54
  }
15
- // 检查文件扩展名
16
- if (!/\.(jsx?|tsx?)$/.test(id)) {
17
- return null;
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
+ }
18
124
  }
19
- // 使用 jsxcompiler 编译
20
- const jsxCompiler = new nocode_compiler_core_1.JSXCompiler({
21
- enableLogging: enableLogging || false,
22
- });
23
- const result = jsxCompiler.compile(code, id);
24
- return result;
125
+ return null;
25
126
  },
26
127
  };
27
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.8",
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": "^5.4.0"
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
- "@meituan-nocode/nocode-compiler-core": "0.1.0-beta.14"
25
+ "@meituan-nocode/nocode-compiler-core": "0.1.0-beta.16-z"
26
26
  }
27
27
  }