@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.
@@ -1,211 +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.JSXCompiler = void 0;
7
- const parser_1 = require("@babel/parser");
8
- const traverse_1 = __importDefault(require("@babel/traverse"));
9
- const magic_string_1 = __importDefault(require("magic-string"));
10
- const path_1 = __importDefault(require("path"));
11
- const utils_1 = require("../utils/utils");
12
- /**
13
- * JSX/TSX 编译器类
14
- * 负责处理 JSX/TSX 文件的编译逻辑
15
- *
16
- * 支持的文件类型:
17
- * - .jsx: React JSX 文件
18
- * - .tsx: React TypeScript JSX 文件
19
- *
20
- * 注意:
21
- * - TypeScript 类型信息在编译时会被擦除
22
- * - 编译器关注的是 JSX 结构而不是类型信息
23
- */
24
- class JSXCompiler {
25
- static supportedExtensions = ['.jsx', '.tsx'];
26
- static extensionName = 'jsx';
27
- options;
28
- logger;
29
- cwd;
30
- constructor(options) {
31
- this.options = options;
32
- this.logger = new utils_1.Logger(options.enableLogging);
33
- this.cwd = process.cwd();
34
- }
35
- /**
36
- * 编译 JSX 代码
37
- */
38
- async compile(code, id) {
39
- this.logger.log('\nProcessing file:', id);
40
- if (!(0, utils_1.shouldProcessFile)(id, this.options.validExtensions)) {
41
- this.logger.log('Skipping file (not .jsx or in node_modules):', id);
42
- return null;
43
- }
44
- const relativePath = path_1.default.relative(this.cwd, id);
45
- this.logger.log('Relative path:', relativePath);
46
- try {
47
- const parserOptions = {
48
- sourceType: 'module',
49
- plugins: ['jsx', 'typescript'],
50
- };
51
- const ast = (0, parser_1.parse)(code, parserOptions);
52
- const magicString = new magic_string_1.default(code);
53
- let changedElementsCount = 0;
54
- let arrayContext = null;
55
- // 使用 @babel/traverse 替代 estree-walker
56
- (0, traverse_1.default)(ast, {
57
- enter: path => {
58
- const node = path.node;
59
- // 检测数组的 map 调用
60
- if (node.type === 'CallExpression') {
61
- const callExpr = node;
62
- if (callExpr.callee.type === 'MemberExpression') {
63
- const memberExpr = callExpr.callee;
64
- if (memberExpr.property.type === 'Identifier' && memberExpr.property.name === 'map') {
65
- this.logger.log('Found array map:', node);
66
- let arrayName = null;
67
- // 获取数组名称
68
- if (memberExpr.object.type === 'Identifier') {
69
- arrayName = memberExpr.object.name;
70
- }
71
- else if (memberExpr.object.type === 'MemberExpression') {
72
- const innerMember = memberExpr.object;
73
- if (innerMember.object.type === 'Identifier' && innerMember.property.type === 'Identifier') {
74
- arrayName = `${innerMember.object.name}.${innerMember.property.name}`;
75
- }
76
- }
77
- // 获取参数名(通常是 index)
78
- let paramName = 'index';
79
- if (callExpr.arguments.length > 0) {
80
- const callback = callExpr.arguments[0];
81
- if (callback.type === 'ArrowFunctionExpression' || callback.type === 'FunctionExpression') {
82
- const func = callback;
83
- if (func.params.length > 1 && func.params[1].type === 'Identifier') {
84
- paramName = func.params[1].name;
85
- }
86
- }
87
- }
88
- if (arrayName) {
89
- arrayContext = { arrayName, paramName };
90
- this.logger.log('Found array map:', { arrayName, paramName });
91
- }
92
- }
93
- }
94
- }
95
- // 处理 JSX 开标签
96
- if (node.type === 'JSXOpeningElement') {
97
- const jsxOpeningElement = node;
98
- let elementName;
99
- // 获取元素名称
100
- if (jsxOpeningElement.name.type === 'JSXIdentifier') {
101
- elementName = jsxOpeningElement.name.name;
102
- }
103
- else if (jsxOpeningElement.name.type === 'JSXMemberExpression') {
104
- const memberExpr = jsxOpeningElement.name;
105
- if (memberExpr.object.type === 'JSXIdentifier' && memberExpr.property.type === 'JSXIdentifier') {
106
- elementName = `${memberExpr.object.name}.${memberExpr.property.name}`;
107
- }
108
- else {
109
- return;
110
- }
111
- }
112
- else {
113
- return;
114
- }
115
- // 跳过 Fragment
116
- if (elementName === 'Fragment' || elementName === 'React.Fragment') {
117
- return;
118
- }
119
- const shouldTag = (0, utils_1.shouldTagElement)(elementName, this.options.threeFiberElems, this.options.dreiElems);
120
- this.logger.log('Found JSX element:', {
121
- elementName,
122
- shouldTag,
123
- hasArrayContext: !!arrayContext,
124
- });
125
- if (shouldTag) {
126
- const line = node.loc?.start?.line ?? 0;
127
- const col = node.loc?.start?.column ?? 0;
128
- // 收集元素内容
129
- const content = {};
130
- // 收集属性
131
- jsxOpeningElement.attributes.forEach(attr => {
132
- if (attr.type === 'JSXAttribute') {
133
- const attrName = attr.name.name;
134
- if (attrName === 'placeholder' || attrName === 'className') {
135
- if (attr.value?.type === 'StringLiteral') {
136
- content[attrName] = attr.value.value;
137
- }
138
- else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type === 'StringLiteral') {
139
- content[attrName] = attr.value.expression.value;
140
- }
141
- }
142
- }
143
- });
144
- // 收集文本内容
145
- const parentPath = path.parentPath;
146
- if (parentPath && parentPath.node.type === 'JSXElement') {
147
- const textContent = parentPath.node.children
148
- .map((child) => {
149
- if (child.type === 'JSXText') {
150
- return child.value.trim();
151
- }
152
- else if (child.type === 'JSXExpressionContainer' && child.expression.type === 'StringLiteral') {
153
- return child.expression.value;
154
- }
155
- return '';
156
- })
157
- .filter(Boolean)
158
- .join(' ')
159
- .trim();
160
- if (textContent) {
161
- content.text = textContent;
162
- }
163
- }
164
- const attributes = (0, utils_1.generateDataAttributes)(relativePath, line, col, elementName, content, arrayContext);
165
- this.logger.log('Adding attributes:', attributes);
166
- // 使用 path 获取准确的节点位置信息
167
- const nameEndPosition = jsxOpeningElement.name.end;
168
- if (nameEndPosition !== null) {
169
- magicString.appendLeft(nameEndPosition, attributes);
170
- changedElementsCount++;
171
- }
172
- }
173
- }
174
- },
175
- exit: path => {
176
- const node = path.node;
177
- // 离开 map 调用时清除上下文
178
- if (node.type === 'CallExpression') {
179
- const callExpr = node;
180
- if (callExpr.callee.type === 'MemberExpression') {
181
- const memberExpr = callExpr.callee;
182
- if (memberExpr.property.type === 'Identifier' && memberExpr.property.name === 'map') {
183
- if (arrayContext) {
184
- this.logger.log('Leaving array map context:', arrayContext.arrayName);
185
- }
186
- arrayContext = null;
187
- }
188
- }
189
- }
190
- },
191
- });
192
- this.logger.log('File processing complete:', {
193
- file: relativePath,
194
- elementsChanged: changedElementsCount,
195
- });
196
- if (changedElementsCount > 0) {
197
- this.logger.log('Modified code preview:', magicString.toString().slice(0, 500) + '...');
198
- }
199
- return {
200
- code: magicString.toString(),
201
- map: magicString.generateMap({ hires: true }),
202
- };
203
- }
204
- catch (error) {
205
- this.logger.error('Error processing file:', relativePath);
206
- this.logger.error(error);
207
- return null;
208
- }
209
- }
210
- }
211
- exports.JSXCompiler = JSXCompiler;
@@ -1,26 +0,0 @@
1
- import type { BaseCompiler } from './base-compiler';
2
- export interface VueCompilerOptions {
3
- validExtensions: Set<string>;
4
- threeFiberElems: string[];
5
- dreiElems: string[];
6
- enableLogging?: boolean;
7
- }
8
- /**
9
- * Vue 编译器类
10
- * 负责处理 Vue 文件的编译逻辑
11
- */
12
- export declare class VueCompiler implements BaseCompiler {
13
- static readonly supportedExtensions: string[];
14
- static readonly extensionName = "vue";
15
- private options;
16
- private logger;
17
- private cwd;
18
- constructor(options: VueCompilerOptions);
19
- /**
20
- * 编译 Vue 代码
21
- */
22
- compile(code: string, id: string): Promise<{
23
- code: string;
24
- map: any;
25
- } | null>;
26
- }
@@ -1,129 +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.VueCompiler = void 0;
7
- const compiler_sfc_1 = require("@vue/compiler-sfc");
8
- const magic_string_1 = __importDefault(require("magic-string"));
9
- const path_1 = __importDefault(require("path"));
10
- const utils_1 = require("../utils/utils");
11
- /**
12
- * Vue 编译器类
13
- * 负责处理 Vue 文件的编译逻辑
14
- */
15
- class VueCompiler {
16
- static supportedExtensions = ['.vue'];
17
- static extensionName = 'vue';
18
- options;
19
- logger;
20
- cwd;
21
- constructor(options) {
22
- this.options = options;
23
- this.logger = new utils_1.Logger(options.enableLogging);
24
- this.cwd = process.cwd();
25
- }
26
- /**
27
- * 编译 Vue 代码
28
- */
29
- async compile(code, id) {
30
- this.logger.log('\nProcessing file:', id);
31
- if (!(0, utils_1.shouldProcessFile)(id, this.options.validExtensions)) {
32
- this.logger.log('Skipping file (not .vue or in node_modules):', id);
33
- return null;
34
- }
35
- const relativePath = path_1.default.relative(this.cwd, id);
36
- this.logger.log('Relative path:', relativePath);
37
- try {
38
- // 解析Vue SFC
39
- const { descriptor } = (0, compiler_sfc_1.parse)(code);
40
- if (!descriptor.template) {
41
- return null;
42
- }
43
- const magicString = new magic_string_1.default(code);
44
- let changedElementsCount = 0;
45
- // 获取template的AST
46
- const templateAst = descriptor.template.ast;
47
- if (!templateAst) {
48
- return null;
49
- }
50
- // 递归处理AST节点
51
- const processNode = (node, arrayContext = null) => {
52
- if (node.type !== 1) {
53
- // 1 表示元素节点
54
- return;
55
- }
56
- const elementName = node.tag;
57
- // 跳过内置组件
58
- if (elementName === 'template' || elementName === 'slot') {
59
- return;
60
- }
61
- const shouldTag = (0, utils_1.shouldTagElement)(elementName, this.options.threeFiberElems, this.options.dreiElems);
62
- if (shouldTag) {
63
- const line = node.loc.start.line;
64
- const col = node.loc.start.column;
65
- // 收集元素内容
66
- const content = {};
67
- // 收集属性
68
- if (node.props) {
69
- node.props.forEach((prop) => {
70
- if (prop.type === 6) {
71
- // 6 表示属性
72
- if (prop.name === 'placeholder' || prop.name === 'class') {
73
- content[prop.name === 'class' ? 'className' : prop.name] = prop.value?.content;
74
- }
75
- }
76
- });
77
- }
78
- // 收集文本内容
79
- if (node.children) {
80
- const textContent = node.children
81
- .filter((child) => child.type === 2) // 2 表示文本节点
82
- .map((child) => child.content.trim())
83
- .filter(Boolean)
84
- .join(' ');
85
- if (textContent) {
86
- content.text = textContent;
87
- }
88
- }
89
- const attributes = (0, utils_1.generateDataAttributes)(relativePath, line, col, elementName, content, arrayContext);
90
- // 在标签名后插入属性
91
- const insertPos = node.loc.start.offset + elementName.length + 1;
92
- magicString.appendLeft(insertPos, attributes);
93
- changedElementsCount++;
94
- }
95
- // 处理v-for上下文
96
- let vForContext = null;
97
- const vForProp = node.props?.find((p) => p.type === 7 && p.name === 'for'); // 7 表示指令
98
- if (vForProp) {
99
- const match = vForProp.exp?.content.match(/\((.*?),\s*(.*?)\)\s+in\s+(.*)/);
100
- if (match) {
101
- vForContext = {
102
- arrayName: match[3],
103
- paramName: match[2],
104
- };
105
- }
106
- }
107
- // 递归处理子节点
108
- if (node.children) {
109
- node.children.forEach((child) => processNode(child, vForContext || arrayContext));
110
- }
111
- };
112
- processNode(templateAst);
113
- this.logger.log('File processing complete:', {
114
- file: relativePath,
115
- elementsChanged: changedElementsCount,
116
- });
117
- return {
118
- code: magicString.toString(),
119
- map: magicString.generateMap({ hires: true }),
120
- };
121
- }
122
- catch (error) {
123
- this.logger.error('Error processing file:', relativePath);
124
- this.logger.error(error);
125
- return null;
126
- }
127
- }
128
- }
129
- exports.VueCompiler = VueCompiler;
@@ -1,9 +0,0 @@
1
- export interface ArrayContext {
2
- arrayName: string;
3
- paramName: string;
4
- }
5
- export interface CompilerStats {
6
- totalFiles: number;
7
- processedFiles: number;
8
- totalElements: number;
9
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,23 +0,0 @@
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
- };
@@ -1,287 +0,0 @@
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
- };