@wsxjs/wsx-vite-plugin 0.0.7 → 0.0.9
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/dist/index.d.mts +6 -26
- package/dist/index.d.ts +6 -26
- package/dist/index.js +585 -46
- package/dist/index.mjs +575 -46
- package/package.json +14 -6
- package/src/babel-plugin-wsx-focus.ts +219 -0
- package/src/babel-plugin-wsx-state.ts +394 -0
- package/src/babel-plugin-wsx-style.ts +149 -0
- package/src/index.ts +2 -1
- package/src/vite-plugin-wsx-babel.ts +182 -0
- package/src/vite-plugin-wsx.ts +0 -166
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,104 +17,641 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
21
31
|
var index_exports = {};
|
|
22
32
|
__export(index_exports, {
|
|
23
|
-
wsx: () =>
|
|
33
|
+
wsx: () => vitePluginWSXWithBabel
|
|
24
34
|
});
|
|
25
35
|
module.exports = __toCommonJS(index_exports);
|
|
26
36
|
|
|
27
|
-
// src/vite-plugin-wsx.ts
|
|
37
|
+
// src/vite-plugin-wsx-babel.ts
|
|
28
38
|
var import_esbuild = require("esbuild");
|
|
39
|
+
var import_core = require("@babel/core");
|
|
40
|
+
var import_fs = require("fs");
|
|
41
|
+
var import_path = require("path");
|
|
42
|
+
|
|
43
|
+
// src/babel-plugin-wsx-state.ts
|
|
44
|
+
var tModule = __toESM(require("@babel/types"));
|
|
45
|
+
function babelPluginWSXState() {
|
|
46
|
+
const t = tModule;
|
|
47
|
+
return {
|
|
48
|
+
name: "babel-plugin-wsx-state",
|
|
49
|
+
visitor: {
|
|
50
|
+
ClassDeclaration(path) {
|
|
51
|
+
const classBody = path.node.body;
|
|
52
|
+
const stateProperties = [];
|
|
53
|
+
console.info(
|
|
54
|
+
`[Babel Plugin WSX State] Processing class ${path.node.id?.name || "anonymous"}, members: ${classBody.body.length}`
|
|
55
|
+
);
|
|
56
|
+
for (const member of classBody.body) {
|
|
57
|
+
console.info(
|
|
58
|
+
` - Member type: ${member.type}, key: ${member.type === "ClassProperty" || member.type === "ClassPrivateProperty" ? member.key?.name : "N/A"}`
|
|
59
|
+
);
|
|
60
|
+
if ((member.type === "ClassProperty" || member.type === "ClassPrivateProperty") && member.key.type === "Identifier") {
|
|
61
|
+
console.info(
|
|
62
|
+
` - Property: ${member.key.name}, decorators: ${member.decorators?.length || 0}, hasValue: ${!!member.value}`
|
|
63
|
+
);
|
|
64
|
+
if (member.decorators && member.decorators.length > 0) {
|
|
65
|
+
member.decorators.forEach((decorator) => {
|
|
66
|
+
if (decorator.expression.type === "Identifier") {
|
|
67
|
+
console.info(` Decorator: ${decorator.expression.name}`);
|
|
68
|
+
} else if (decorator.expression.type === "CallExpression" && decorator.expression.callee.type === "Identifier") {
|
|
69
|
+
console.debug(
|
|
70
|
+
` Decorator: ${decorator.expression.callee.name}()`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
const hasStateDecorator = member.decorators?.some(
|
|
76
|
+
(decorator) => {
|
|
77
|
+
if (decorator.expression.type === "Identifier" && decorator.expression.name === "state") {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
if (decorator.expression.type === "CallExpression" && decorator.expression.callee.type === "Identifier" && decorator.expression.callee.name === "state") {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
if (hasStateDecorator && member.value) {
|
|
87
|
+
const key = member.key.name;
|
|
88
|
+
const initialValue = member.value;
|
|
89
|
+
const isObject = initialValue.type === "ObjectExpression" || initialValue.type === "ArrayExpression";
|
|
90
|
+
const isArray = initialValue.type === "ArrayExpression";
|
|
91
|
+
stateProperties.push({
|
|
92
|
+
key,
|
|
93
|
+
initialValue,
|
|
94
|
+
isObject,
|
|
95
|
+
isArray
|
|
96
|
+
// Add isArray flag
|
|
97
|
+
});
|
|
98
|
+
if (member.decorators) {
|
|
99
|
+
member.decorators = member.decorators.filter(
|
|
100
|
+
(decorator) => {
|
|
101
|
+
if (decorator.expression.type === "Identifier" && decorator.expression.name === "state") {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (decorator.expression.type === "CallExpression" && decorator.expression.callee.type === "Identifier" && decorator.expression.callee.name === "state") {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
member.value = void 0;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (stateProperties.length === 0) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
let constructor = classBody.body.find(
|
|
119
|
+
(member) => member.type === "ClassMethod" && member.kind === "constructor"
|
|
120
|
+
);
|
|
121
|
+
if (!constructor) {
|
|
122
|
+
constructor = t.classMethod(
|
|
123
|
+
"constructor",
|
|
124
|
+
t.identifier("constructor"),
|
|
125
|
+
[],
|
|
126
|
+
t.blockStatement([])
|
|
127
|
+
);
|
|
128
|
+
classBody.body.unshift(constructor);
|
|
129
|
+
}
|
|
130
|
+
const statements = [];
|
|
131
|
+
const hasSuper = constructor.body.body.some(
|
|
132
|
+
(stmt) => stmt.type === "ExpressionStatement" && stmt.expression.type === "CallExpression" && stmt.expression.callee.type === "Super"
|
|
133
|
+
);
|
|
134
|
+
if (!hasSuper) {
|
|
135
|
+
statements.push(t.expressionStatement(t.callExpression(t.super(), [])));
|
|
136
|
+
}
|
|
137
|
+
for (const { key, initialValue, isObject } of stateProperties) {
|
|
138
|
+
if (isObject) {
|
|
139
|
+
const reactiveVarId = t.identifier(`_${key}Reactive`);
|
|
140
|
+
statements.push(
|
|
141
|
+
t.variableDeclaration("let", [
|
|
142
|
+
t.variableDeclarator(
|
|
143
|
+
reactiveVarId,
|
|
144
|
+
t.callExpression(
|
|
145
|
+
t.memberExpression(
|
|
146
|
+
t.thisExpression(),
|
|
147
|
+
t.identifier("reactive")
|
|
148
|
+
),
|
|
149
|
+
[initialValue]
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
])
|
|
153
|
+
);
|
|
154
|
+
statements.push(
|
|
155
|
+
t.expressionStatement(
|
|
156
|
+
t.callExpression(
|
|
157
|
+
t.memberExpression(
|
|
158
|
+
t.identifier("Object"),
|
|
159
|
+
t.identifier("defineProperty")
|
|
160
|
+
),
|
|
161
|
+
[
|
|
162
|
+
t.thisExpression(),
|
|
163
|
+
t.stringLiteral(key),
|
|
164
|
+
t.objectExpression([
|
|
165
|
+
t.objectProperty(
|
|
166
|
+
t.identifier("get"),
|
|
167
|
+
t.arrowFunctionExpression([], reactiveVarId)
|
|
168
|
+
),
|
|
169
|
+
t.objectProperty(
|
|
170
|
+
t.identifier("set"),
|
|
171
|
+
t.arrowFunctionExpression(
|
|
172
|
+
[t.identifier("newValue")],
|
|
173
|
+
t.blockStatement([
|
|
174
|
+
t.expressionStatement(
|
|
175
|
+
t.assignmentExpression(
|
|
176
|
+
"=",
|
|
177
|
+
reactiveVarId,
|
|
178
|
+
t.conditionalExpression(
|
|
179
|
+
// Check if newValue is an object or array
|
|
180
|
+
t.logicalExpression(
|
|
181
|
+
"&&",
|
|
182
|
+
t.binaryExpression(
|
|
183
|
+
"!==",
|
|
184
|
+
t.identifier(
|
|
185
|
+
"newValue"
|
|
186
|
+
),
|
|
187
|
+
t.nullLiteral()
|
|
188
|
+
),
|
|
189
|
+
t.logicalExpression(
|
|
190
|
+
"&&",
|
|
191
|
+
t.binaryExpression(
|
|
192
|
+
"!==",
|
|
193
|
+
t.unaryExpression(
|
|
194
|
+
"typeof",
|
|
195
|
+
t.identifier(
|
|
196
|
+
"newValue"
|
|
197
|
+
)
|
|
198
|
+
),
|
|
199
|
+
t.stringLiteral(
|
|
200
|
+
"undefined"
|
|
201
|
+
)
|
|
202
|
+
),
|
|
203
|
+
t.logicalExpression(
|
|
204
|
+
"||",
|
|
205
|
+
t.callExpression(
|
|
206
|
+
t.memberExpression(
|
|
207
|
+
t.identifier(
|
|
208
|
+
"Array"
|
|
209
|
+
),
|
|
210
|
+
t.identifier(
|
|
211
|
+
"isArray"
|
|
212
|
+
)
|
|
213
|
+
),
|
|
214
|
+
[
|
|
215
|
+
t.identifier(
|
|
216
|
+
"newValue"
|
|
217
|
+
)
|
|
218
|
+
]
|
|
219
|
+
),
|
|
220
|
+
t.binaryExpression(
|
|
221
|
+
"===",
|
|
222
|
+
t.unaryExpression(
|
|
223
|
+
"typeof",
|
|
224
|
+
t.identifier(
|
|
225
|
+
"newValue"
|
|
226
|
+
)
|
|
227
|
+
),
|
|
228
|
+
t.stringLiteral(
|
|
229
|
+
"object"
|
|
230
|
+
)
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
)
|
|
234
|
+
),
|
|
235
|
+
// If object/array, wrap in reactive
|
|
236
|
+
t.callExpression(
|
|
237
|
+
t.memberExpression(
|
|
238
|
+
t.thisExpression(),
|
|
239
|
+
t.identifier("reactive")
|
|
240
|
+
),
|
|
241
|
+
[t.identifier("newValue")]
|
|
242
|
+
),
|
|
243
|
+
// Otherwise, just assign (for primitives)
|
|
244
|
+
t.identifier("newValue")
|
|
245
|
+
)
|
|
246
|
+
)
|
|
247
|
+
),
|
|
248
|
+
// Trigger rerender when value is replaced
|
|
249
|
+
t.expressionStatement(
|
|
250
|
+
t.callExpression(
|
|
251
|
+
t.memberExpression(
|
|
252
|
+
t.thisExpression(),
|
|
253
|
+
t.identifier("scheduleRerender")
|
|
254
|
+
),
|
|
255
|
+
[]
|
|
256
|
+
)
|
|
257
|
+
)
|
|
258
|
+
])
|
|
259
|
+
)
|
|
260
|
+
),
|
|
261
|
+
t.objectProperty(
|
|
262
|
+
t.identifier("enumerable"),
|
|
263
|
+
t.booleanLiteral(true)
|
|
264
|
+
),
|
|
265
|
+
t.objectProperty(
|
|
266
|
+
t.identifier("configurable"),
|
|
267
|
+
t.booleanLiteral(true)
|
|
268
|
+
)
|
|
269
|
+
])
|
|
270
|
+
]
|
|
271
|
+
)
|
|
272
|
+
)
|
|
273
|
+
);
|
|
274
|
+
} else {
|
|
275
|
+
const getterId = t.identifier(`_get${key}`);
|
|
276
|
+
const setterId = t.identifier(`_set${key}`);
|
|
277
|
+
statements.push(
|
|
278
|
+
t.variableDeclaration("const", [
|
|
279
|
+
t.variableDeclarator(
|
|
280
|
+
t.arrayPattern([getterId, setterId]),
|
|
281
|
+
t.callExpression(
|
|
282
|
+
t.memberExpression(
|
|
283
|
+
t.thisExpression(),
|
|
284
|
+
t.identifier("useState")
|
|
285
|
+
),
|
|
286
|
+
[t.stringLiteral(key), initialValue]
|
|
287
|
+
)
|
|
288
|
+
)
|
|
289
|
+
])
|
|
290
|
+
);
|
|
291
|
+
statements.push(
|
|
292
|
+
t.expressionStatement(
|
|
293
|
+
t.callExpression(
|
|
294
|
+
t.memberExpression(
|
|
295
|
+
t.identifier("Object"),
|
|
296
|
+
t.identifier("defineProperty")
|
|
297
|
+
),
|
|
298
|
+
[
|
|
299
|
+
t.thisExpression(),
|
|
300
|
+
t.stringLiteral(key),
|
|
301
|
+
t.objectExpression([
|
|
302
|
+
t.objectProperty(t.identifier("get"), getterId),
|
|
303
|
+
t.objectProperty(t.identifier("set"), setterId),
|
|
304
|
+
t.objectProperty(
|
|
305
|
+
t.identifier("enumerable"),
|
|
306
|
+
t.booleanLiteral(true)
|
|
307
|
+
),
|
|
308
|
+
t.objectProperty(
|
|
309
|
+
t.identifier("configurable"),
|
|
310
|
+
t.booleanLiteral(true)
|
|
311
|
+
)
|
|
312
|
+
])
|
|
313
|
+
]
|
|
314
|
+
)
|
|
315
|
+
)
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
constructor.body.body.push(...statements);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/babel-plugin-wsx-style.ts
|
|
326
|
+
var tModule2 = __toESM(require("@babel/types"));
|
|
327
|
+
function hasStylesImport(program) {
|
|
328
|
+
for (const node of program.body) {
|
|
329
|
+
if (node.type === "ImportDeclaration") {
|
|
330
|
+
const source = node.source.value;
|
|
331
|
+
if (typeof source === "string" && (source.endsWith(".css?inline") || source.endsWith(".css"))) {
|
|
332
|
+
const defaultSpecifier = node.specifiers.find(
|
|
333
|
+
(spec) => spec.type === "ImportDefaultSpecifier"
|
|
334
|
+
);
|
|
335
|
+
if (defaultSpecifier) {
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
function hasAutoStylesProperty(classBody) {
|
|
344
|
+
for (const member of classBody.body) {
|
|
345
|
+
if ((member.type === "ClassProperty" || member.type === "ClassPrivateProperty") && member.key.type === "Identifier" && member.key.name === "_autoStyles") {
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
function babelPluginWSXStyle() {
|
|
352
|
+
const t = tModule2;
|
|
353
|
+
return {
|
|
354
|
+
name: "babel-plugin-wsx-style",
|
|
355
|
+
visitor: {
|
|
356
|
+
Program(path, state) {
|
|
357
|
+
const { cssFileExists, cssFilePath, componentName } = state.opts;
|
|
358
|
+
if (!cssFileExists) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (hasStylesImport(path.node)) {
|
|
362
|
+
console.info(
|
|
363
|
+
`[Babel Plugin WSX Style] Skipping ${componentName}: styles already manually imported`
|
|
364
|
+
);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
console.info(
|
|
368
|
+
`[Babel Plugin WSX Style] Injecting CSS import for ${componentName}: ${cssFilePath}`
|
|
369
|
+
);
|
|
370
|
+
const importStatement = t.importDeclaration(
|
|
371
|
+
[t.importDefaultSpecifier(t.identifier("styles"))],
|
|
372
|
+
t.stringLiteral(cssFilePath)
|
|
373
|
+
);
|
|
374
|
+
let insertIndex = 0;
|
|
375
|
+
for (let i = 0; i < path.node.body.length; i++) {
|
|
376
|
+
const node = path.node.body[i];
|
|
377
|
+
if (node.type === "ImportDeclaration") {
|
|
378
|
+
insertIndex = i + 1;
|
|
379
|
+
} else {
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
path.node.body.splice(insertIndex, 0, importStatement);
|
|
384
|
+
},
|
|
385
|
+
ClassDeclaration(path, state) {
|
|
386
|
+
const { cssFileExists } = state.opts;
|
|
387
|
+
if (!cssFileExists) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const classBody = path.node.body;
|
|
391
|
+
if (hasAutoStylesProperty(classBody)) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const autoStylesProperty = t.classProperty(
|
|
395
|
+
t.identifier("_autoStyles"),
|
|
396
|
+
t.identifier("styles"),
|
|
397
|
+
null,
|
|
398
|
+
// typeAnnotation
|
|
399
|
+
[],
|
|
400
|
+
// decorators
|
|
401
|
+
false,
|
|
402
|
+
// computed
|
|
403
|
+
false
|
|
404
|
+
// static
|
|
405
|
+
);
|
|
406
|
+
classBody.body.unshift(autoStylesProperty);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// src/babel-plugin-wsx-focus.ts
|
|
413
|
+
var tModule3 = __toESM(require("@babel/types"));
|
|
414
|
+
var FOCUSABLE_ELEMENTS = /* @__PURE__ */ new Set([
|
|
415
|
+
"input",
|
|
416
|
+
"textarea",
|
|
417
|
+
"select",
|
|
418
|
+
"button"
|
|
419
|
+
// Also focusable
|
|
420
|
+
]);
|
|
421
|
+
function isFocusableElement(tagName, hasContentEditable) {
|
|
422
|
+
const lowerTag = tagName.toLowerCase();
|
|
423
|
+
return FOCUSABLE_ELEMENTS.has(lowerTag) || hasContentEditable;
|
|
424
|
+
}
|
|
425
|
+
function extractPropsFromJSXAttributes(attributes) {
|
|
426
|
+
const props = {};
|
|
427
|
+
for (const attr of attributes) {
|
|
428
|
+
if (tModule3.isJSXAttribute(attr) && tModule3.isJSXIdentifier(attr.name)) {
|
|
429
|
+
const keyName = attr.name.name;
|
|
430
|
+
if (keyName === "id" || keyName === "name" || keyName === "type") {
|
|
431
|
+
if (tModule3.isStringLiteral(attr.value)) {
|
|
432
|
+
props[keyName] = attr.value.value;
|
|
433
|
+
} else if (tModule3.isJSXExpressionContainer(attr.value) && tModule3.isStringLiteral(attr.value.expression)) {
|
|
434
|
+
props[keyName] = attr.value.expression.value;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return props;
|
|
440
|
+
}
|
|
441
|
+
function generateStableKey(tagName, componentName, path, props) {
|
|
442
|
+
const pathStr = path.join("-");
|
|
443
|
+
const lowerTag = tagName.toLowerCase();
|
|
444
|
+
if (props.id) {
|
|
445
|
+
return `${componentName}-${props.id}`;
|
|
446
|
+
}
|
|
447
|
+
if (props.name) {
|
|
448
|
+
return `${componentName}-${props.name}`;
|
|
449
|
+
}
|
|
450
|
+
const typeStr = props.type || "text";
|
|
451
|
+
return `${componentName}-${lowerTag}-${typeStr}-${pathStr}`;
|
|
452
|
+
}
|
|
453
|
+
function calculateJSXPath(path) {
|
|
454
|
+
const pathArray = [];
|
|
455
|
+
let currentPath = path.parentPath;
|
|
456
|
+
while (currentPath) {
|
|
457
|
+
if (currentPath.isJSXElement()) {
|
|
458
|
+
const parent = currentPath.parentPath;
|
|
459
|
+
if (parent && parent.isJSXElement()) {
|
|
460
|
+
const parentNode = parent.node;
|
|
461
|
+
let index = 0;
|
|
462
|
+
for (let i = 0; i < parentNode.children.length; i++) {
|
|
463
|
+
const child = parentNode.children[i];
|
|
464
|
+
if (child === currentPath.node) {
|
|
465
|
+
index = i;
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
pathArray.unshift(index);
|
|
470
|
+
} else if (parent && parent.isReturnStatement()) {
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
} else if (currentPath.isReturnStatement()) {
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
currentPath = currentPath.parentPath;
|
|
477
|
+
}
|
|
478
|
+
return pathArray.length > 0 ? pathArray : [0];
|
|
479
|
+
}
|
|
480
|
+
function findComponentName(path) {
|
|
481
|
+
let classPath = path;
|
|
482
|
+
while (classPath) {
|
|
483
|
+
if (classPath.isClassDeclaration()) {
|
|
484
|
+
if (classPath.node.id && tModule3.isIdentifier(classPath.node.id)) {
|
|
485
|
+
return classPath.node.id.name;
|
|
486
|
+
}
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
classPath = classPath.parentPath;
|
|
490
|
+
}
|
|
491
|
+
return "Component";
|
|
492
|
+
}
|
|
493
|
+
function babelPluginWSXFocus() {
|
|
494
|
+
const t = tModule3;
|
|
495
|
+
return {
|
|
496
|
+
name: "babel-plugin-wsx-focus",
|
|
497
|
+
visitor: {
|
|
498
|
+
JSXOpeningElement(path) {
|
|
499
|
+
const element = path.node;
|
|
500
|
+
if (!t.isJSXIdentifier(element.name)) {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const elementName = element.name.name;
|
|
504
|
+
const hasKey = element.attributes.some(
|
|
505
|
+
(attr) => t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name) && attr.name.name === "data-wsx-key"
|
|
506
|
+
);
|
|
507
|
+
if (hasKey) {
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
const props = extractPropsFromJSXAttributes(element.attributes);
|
|
511
|
+
const hasContentEditable = element.attributes.some(
|
|
512
|
+
(attr) => t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name) && (attr.name.name === "contenteditable" || attr.name.name === "contentEditable")
|
|
513
|
+
);
|
|
514
|
+
if (!isFocusableElement(elementName, hasContentEditable)) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
const componentName = findComponentName(path);
|
|
518
|
+
const pathArray = calculateJSXPath(path);
|
|
519
|
+
const key = generateStableKey(elementName, componentName, pathArray, props);
|
|
520
|
+
const keyAttr = t.jsxAttribute(
|
|
521
|
+
t.jsxIdentifier("data-wsx-key"),
|
|
522
|
+
t.stringLiteral(key)
|
|
523
|
+
);
|
|
524
|
+
element.attributes.push(keyAttr);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// src/vite-plugin-wsx-babel.ts
|
|
29
531
|
function getJSXFactoryImportPath(_options) {
|
|
30
532
|
return "@wsxjs/wsx-core";
|
|
31
533
|
}
|
|
32
|
-
function
|
|
534
|
+
function vitePluginWSXWithBabel(options = {}) {
|
|
33
535
|
const {
|
|
34
536
|
jsxFactory = "h",
|
|
35
537
|
jsxFragment = "Fragment",
|
|
36
|
-
|
|
37
|
-
|
|
538
|
+
extensions = [".wsx"],
|
|
539
|
+
autoStyleInjection = true
|
|
38
540
|
} = options;
|
|
39
541
|
return {
|
|
40
|
-
name: "vite-plugin-wsx",
|
|
542
|
+
name: "vite-plugin-wsx-babel",
|
|
41
543
|
enforce: "pre",
|
|
42
|
-
// 确保在 React 插件之前执行
|
|
43
|
-
// 处理 .wsx 文件加载
|
|
44
|
-
load(id) {
|
|
45
|
-
const isWSXFile = extensions.some((ext) => id.endsWith(ext));
|
|
46
|
-
if (!isWSXFile) {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
if (debug) {
|
|
50
|
-
console.log(`[WSX Plugin] Loading: ${id}`);
|
|
51
|
-
}
|
|
52
|
-
return null;
|
|
53
|
-
},
|
|
54
|
-
// 在transform阶段处理文件
|
|
55
544
|
async transform(code, id) {
|
|
56
545
|
const isWSXFile = extensions.some((ext) => id.endsWith(ext));
|
|
57
546
|
if (!isWSXFile) {
|
|
58
547
|
return null;
|
|
59
548
|
}
|
|
60
|
-
|
|
61
|
-
|
|
549
|
+
let cssFileExists = false;
|
|
550
|
+
let cssFilePath = "";
|
|
551
|
+
let componentName = "";
|
|
552
|
+
if (autoStyleInjection) {
|
|
553
|
+
const fileDir = (0, import_path.dirname)(id);
|
|
554
|
+
const fileName = (0, import_path.basename)(id, extensions.find((ext) => id.endsWith(ext)) || "");
|
|
555
|
+
const cssFilePathWithoutQuery = (0, import_path.join)(fileDir, `${fileName}.css`);
|
|
556
|
+
cssFileExists = (0, import_fs.existsSync)(cssFilePathWithoutQuery);
|
|
557
|
+
componentName = fileName;
|
|
558
|
+
if (cssFileExists) {
|
|
559
|
+
cssFilePath = `./${fileName}.css?inline`;
|
|
560
|
+
}
|
|
62
561
|
}
|
|
63
562
|
let transformedCode = code;
|
|
64
563
|
const hasWSXCoreImport = code.includes('from "@wsxjs/wsx-core"');
|
|
65
564
|
const hasJSXInImport = hasWSXCoreImport && (new RegExp(`[{,]\\s*${jsxFactory}\\s*[},]`).test(code) || new RegExp(`[{,]\\s*${jsxFragment}\\s*[},]`).test(code));
|
|
66
|
-
if (debug) {
|
|
67
|
-
console.log(`[WSX Plugin] Checking JSX imports for: ${id}`);
|
|
68
|
-
console.log(` - hasWSXCoreImport: ${hasWSXCoreImport}`);
|
|
69
|
-
console.log(` - hasJSXInImport: ${hasJSXInImport}`);
|
|
70
|
-
console.log(` - has < character: ${code.includes("<")}`);
|
|
71
|
-
console.log(` - has Fragment: ${code.includes("Fragment")}`);
|
|
72
|
-
}
|
|
73
565
|
if ((code.includes("<") || code.includes("Fragment")) && !hasJSXInImport) {
|
|
74
566
|
const importPath = getJSXFactoryImportPath(options);
|
|
75
567
|
const importStatement = `import { ${jsxFactory}, ${jsxFragment} } from "${importPath}";
|
|
76
568
|
`;
|
|
77
569
|
transformedCode = importStatement + transformedCode;
|
|
78
|
-
|
|
79
|
-
|
|
570
|
+
}
|
|
571
|
+
try {
|
|
572
|
+
const babelResult = (0, import_core.transformSync)(transformedCode, {
|
|
573
|
+
filename: id,
|
|
574
|
+
// Pass the actual filename so Babel knows it's .wsx
|
|
575
|
+
presets: [
|
|
576
|
+
[
|
|
577
|
+
"@babel/preset-typescript",
|
|
578
|
+
{
|
|
579
|
+
isTSX: true,
|
|
580
|
+
// Enable JSX syntax
|
|
581
|
+
allExtensions: true
|
|
582
|
+
// Process all extensions, including .wsx
|
|
583
|
+
}
|
|
584
|
+
]
|
|
585
|
+
],
|
|
586
|
+
plugins: [
|
|
587
|
+
// CRITICAL: Style injection plugin must run FIRST
|
|
588
|
+
// This ensures _autoStyles property exists before state transformations
|
|
589
|
+
...autoStyleInjection && cssFileExists ? [
|
|
590
|
+
[
|
|
591
|
+
babelPluginWSXStyle,
|
|
592
|
+
{
|
|
593
|
+
cssFileExists,
|
|
594
|
+
cssFilePath,
|
|
595
|
+
componentName
|
|
596
|
+
}
|
|
597
|
+
]
|
|
598
|
+
] : [],
|
|
599
|
+
// Focus key generation plugin runs early to add data-wsx-key attributes
|
|
600
|
+
// This must run before JSX is transformed to h() calls
|
|
601
|
+
babelPluginWSXFocus,
|
|
602
|
+
// State decorator transformation runs after style injection
|
|
603
|
+
babelPluginWSXState,
|
|
604
|
+
[
|
|
605
|
+
"@babel/plugin-proposal-decorators",
|
|
606
|
+
{
|
|
607
|
+
version: "2023-05",
|
|
608
|
+
decoratorsBeforeExport: true
|
|
609
|
+
}
|
|
610
|
+
],
|
|
611
|
+
[
|
|
612
|
+
"@babel/plugin-proposal-class-properties",
|
|
613
|
+
{
|
|
614
|
+
loose: false
|
|
615
|
+
}
|
|
616
|
+
],
|
|
617
|
+
"@babel/plugin-transform-class-static-block"
|
|
618
|
+
// Support static class blocks
|
|
619
|
+
]
|
|
620
|
+
// parserOpts not needed - @babel/preset-typescript and plugins handle it
|
|
621
|
+
});
|
|
622
|
+
if (babelResult && babelResult.code) {
|
|
623
|
+
transformedCode = babelResult.code;
|
|
80
624
|
}
|
|
625
|
+
} catch {
|
|
626
|
+
}
|
|
627
|
+
const hasJSXAfterBabel = transformedCode.includes('from "@wsxjs/wsx-core"') && (new RegExp(`[{,]\\s*${jsxFactory}\\s*[},]`).test(transformedCode) || new RegExp(`[{,]\\s*${jsxFragment}\\s*[},]`).test(transformedCode));
|
|
628
|
+
if ((transformedCode.includes("<") || transformedCode.includes("Fragment")) && !hasJSXAfterBabel) {
|
|
629
|
+
const importPath = getJSXFactoryImportPath(options);
|
|
630
|
+
const importStatement = `import { ${jsxFactory}, ${jsxFragment} } from "${importPath}";
|
|
631
|
+
`;
|
|
632
|
+
transformedCode = importStatement + transformedCode;
|
|
81
633
|
}
|
|
82
634
|
try {
|
|
83
635
|
const result = await (0, import_esbuild.transform)(transformedCode, {
|
|
84
|
-
loader: "
|
|
636
|
+
loader: "jsx",
|
|
637
|
+
// Already TypeScript-transformed by Babel
|
|
85
638
|
jsx: "transform",
|
|
86
639
|
jsxFactory,
|
|
87
640
|
jsxFragment,
|
|
88
641
|
target: "es2020",
|
|
89
642
|
format: "esm"
|
|
90
|
-
// Esbuild supports decorators natively with tsx loader
|
|
91
643
|
});
|
|
92
|
-
if (debug) {
|
|
93
|
-
console.log(`[WSX Plugin] JSX transformed: ${id}`);
|
|
94
|
-
}
|
|
95
644
|
return {
|
|
96
645
|
code: result.code,
|
|
97
646
|
map: null
|
|
98
647
|
};
|
|
99
648
|
} catch (error) {
|
|
100
|
-
console.error(`[WSX Plugin] Transform error for ${id}:`, error);
|
|
649
|
+
console.error(`[WSX Plugin Babel] Transform error for ${id}:`, error);
|
|
101
650
|
throw error;
|
|
102
651
|
}
|
|
103
|
-
},
|
|
104
|
-
// We handle JSX transformation directly in the transform hook
|
|
105
|
-
// No need to modify global esbuild config
|
|
106
|
-
// 构建开始时的日志
|
|
107
|
-
buildStart() {
|
|
108
|
-
if (debug) {
|
|
109
|
-
console.log(`[WSX Plugin] Build started with extensions: ${extensions.join(", ")}`);
|
|
110
|
-
console.log(`[WSX Plugin] JSX Factory: ${jsxFactory}, Fragment: ${jsxFragment}`);
|
|
111
|
-
}
|
|
112
652
|
}
|
|
113
653
|
};
|
|
114
654
|
}
|
|
115
|
-
var vite_plugin_wsx_default = vitePluginWSX;
|
|
116
655
|
// Annotate the CommonJS export names for ESM import in node:
|
|
117
656
|
0 && (module.exports = {
|
|
118
657
|
wsx
|