@mybricks/to-code-taro 1.1.7 → 1.2.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/dist/cjs/generate/generateTaroProjectJson.js +47 -2
- package/dist/cjs/handleCom.js +1 -1
- package/dist/cjs/handleModule.js +6 -8
- package/dist/cjs/handleSlot.d.ts +1 -0
- package/dist/cjs/handleSlot.js +6 -3
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/processors/processModule.js +1 -0
- package/dist/cjs/processors/processSceneLogic.js +5 -1
- package/dist/cjs/taro-template.json +6 -6008
- package/dist/cjs/toCodeTaro.js +89 -0
- package/dist/cjs/utils/logic/handleProcess.js +26 -18
- package/dist/cjs/utils/templates/component.js +1 -1
- package/dist/cjs/utils/templates/index.d.ts +2 -0
- package/dist/cjs/utils/templates/index.js +7 -2
- package/dist/cjs/utils/templates/renderManager.js +2 -2
- package/dist/cjs/utils/templates/scene.d.ts +1 -1
- package/dist/cjs/utils/templates/scene.js +4 -2
- package/dist/esm/generate/generateTaroProjectJson.js +43 -2
- package/dist/esm/handleCom.js +1 -1
- package/dist/esm/handleModule.js +5 -7
- package/dist/esm/handleSlot.d.ts +1 -0
- package/dist/esm/handleSlot.js +10 -7
- package/dist/esm/index.js +2 -1
- package/dist/esm/processors/processModule.js +1 -0
- package/dist/esm/processors/processSceneLogic.js +4 -2
- package/dist/esm/taro-template.json +6 -6008
- package/dist/esm/toCodeTaro.js +112 -0
- package/dist/esm/utils/logic/handleProcess.js +39 -18
- package/dist/esm/utils/templates/component.js +2 -2
- package/dist/esm/utils/templates/index.d.ts +2 -0
- package/dist/esm/utils/templates/index.js +5 -0
- package/dist/esm/utils/templates/renderManager.js +3 -3
- package/dist/esm/utils/templates/scene.d.ts +1 -1
- package/dist/esm/utils/templates/scene.js +4 -1
- package/package.json +2 -9
- package/README.md +0 -58
- package/dist/cjs/core/mybricks/MyBricksDescriptor.js +0 -213
- package/dist/cjs/core/mybricks/Subject.js +0 -119
- package/dist/cjs/core/mybricks/constant.js +0 -70
- package/dist/cjs/core/mybricks/context.js +0 -33
- package/dist/cjs/core/mybricks/createEnv.js +0 -39
- package/dist/cjs/core/mybricks/createJSHandle.js +0 -191
- package/dist/cjs/core/mybricks/createModuleEventsHandle.js +0 -49
- package/dist/cjs/core/mybricks/createReactiveInputHandler.js +0 -63
- package/dist/cjs/core/mybricks/event.js +0 -70
- package/dist/cjs/core/mybricks/index.js +0 -871
- package/dist/cjs/core/mybricks/log.js +0 -37
- package/dist/cjs/core/mybricks/utils.js +0 -69
- package/dist/cjs/core/mybricks/variables.js +0 -217
- package/dist/cjs/core/utils/ComContext.js +0 -51
- package/dist/cjs/core/utils/PopupRenderer.js +0 -63
- package/dist/cjs/core/utils/comRefResolver.js +0 -112
- package/dist/cjs/core/utils/event.js +0 -53
- package/dist/cjs/core/utils/hooks.js +0 -238
- package/dist/cjs/core/utils/index.js +0 -77
- package/dist/cjs/core/utils/pageRouter.js +0 -104
- package/dist/cjs/core/utils/pool.js +0 -63
- package/dist/cjs/core/utils/popupRouter.js +0 -98
- package/dist/cjs/core/utils/slots.js +0 -183
- package/dist/cjs/core/utils/tabbar.js +0 -56
- package/dist/cjs/core/utils/useContext.js +0 -112
- package/dist/cjs/core/utils/with.js +0 -109
- package/dist/esm/core/mybricks/MyBricksDescriptor.js +0 -232
- package/dist/esm/core/mybricks/Subject.js +0 -149
- package/dist/esm/core/mybricks/constant.js +0 -18
- package/dist/esm/core/mybricks/context.js +0 -5
- package/dist/esm/core/mybricks/createEnv.js +0 -9
- package/dist/esm/core/mybricks/createJSHandle.js +0 -189
- package/dist/esm/core/mybricks/createModuleEventsHandle.js +0 -26
- package/dist/esm/core/mybricks/createReactiveInputHandler.js +0 -40
- package/dist/esm/core/mybricks/event.js +0 -44
- package/dist/esm/core/mybricks/index.js +0 -952
- package/dist/esm/core/mybricks/log.js +0 -8
- package/dist/esm/core/mybricks/utils.js +0 -41
- package/dist/esm/core/mybricks/variables.js +0 -190
- package/dist/esm/core/utils/ComContext.js +0 -16
- package/dist/esm/core/utils/PopupRenderer.js +0 -27
- package/dist/esm/core/utils/comRefResolver.js +0 -134
- package/dist/esm/core/utils/event.js +0 -32
- package/dist/esm/core/utils/hooks.js +0 -287
- package/dist/esm/core/utils/index.js +0 -12
- package/dist/esm/core/utils/pageRouter.js +0 -112
- package/dist/esm/core/utils/pool.js +0 -52
- package/dist/esm/core/utils/popupRouter.js +0 -117
- package/dist/esm/core/utils/slots.js +0 -194
- package/dist/esm/core/utils/tabbar.js +0 -47
- package/dist/esm/core/utils/useContext.js +0 -100
- package/dist/esm/core/utils/with.js +0 -115
package/dist/esm/toCodeTaro.js
CHANGED
|
@@ -32,6 +32,16 @@ import { processModules } from "./processors/processModule";
|
|
|
32
32
|
* Taro 代码生成主函数
|
|
33
33
|
*/
|
|
34
34
|
var toCodeTaro = function toCodeTaro(tojson, config) {
|
|
35
|
+
// 将 modules 中的场景展开到 scenes 中,使 toCode 能识别并处理模块
|
|
36
|
+
if (tojson.modules) {
|
|
37
|
+
Object.values(tojson.modules).forEach(function (mod) {
|
|
38
|
+
if (mod.json) {
|
|
39
|
+
tojson.scenes.push(mod.json);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
// 预处理:从 modules.json 重建 frames 中模块的事件数据
|
|
43
|
+
rebuildModuleFrames(tojson);
|
|
44
|
+
}
|
|
35
45
|
return getCode({
|
|
36
46
|
tojson: tojson,
|
|
37
47
|
toCodejson: toCode(tojson)
|
|
@@ -60,6 +70,15 @@ var getCode = function getCode(params, config) {
|
|
|
60
70
|
|
|
61
71
|
// 构建场景和事件映射
|
|
62
72
|
var sceneMap = buildSceneMap(tojson.scenes);
|
|
73
|
+
// 将模块场景也加入 sceneMap,使 getSceneById 能查到模块
|
|
74
|
+
if (tojson.modules) {
|
|
75
|
+
Object.values(tojson.modules).forEach(function (mod) {
|
|
76
|
+
var _mod$json;
|
|
77
|
+
if ((_mod$json = mod.json) !== null && _mod$json !== void 0 && _mod$json.id) {
|
|
78
|
+
sceneMap[mod.json.id] = mod.json;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
63
82
|
var eventsMap = buildEventsMap(tojson.frames);
|
|
64
83
|
var getSceneById = createGetSceneById(sceneMap);
|
|
65
84
|
var getExtensionEventById = createGetExtensionEventById(eventsMap);
|
|
@@ -242,6 +261,99 @@ var getCode = function getCode(params, config) {
|
|
|
242
261
|
};
|
|
243
262
|
};
|
|
244
263
|
|
|
264
|
+
/**
|
|
265
|
+
* 预处理:从 modules.json 重建 frames 中模块的事件数据
|
|
266
|
+
*
|
|
267
|
+
* 背景:tojson.frames 中模块的 frame 可能是空壳(coms: {}, diagrams[0].conAry: []),
|
|
268
|
+
* 而完整的组件事件数据在 modules[moduleId].json 中(coms/cons/outputEvents)。
|
|
269
|
+
* toCode 的 handleFrame 需要 frame.coms 包含组件事件子 frames 才能生成事件代码。
|
|
270
|
+
*/
|
|
271
|
+
var rebuildModuleFrames = function rebuildModuleFrames(tojson) {
|
|
272
|
+
var modules = tojson.modules;
|
|
273
|
+
if (!modules) return;
|
|
274
|
+
Object.values(modules).forEach(function (mod) {
|
|
275
|
+
var moduleJson = mod.json;
|
|
276
|
+
if (!moduleJson) return;
|
|
277
|
+
var frame = tojson.frames.find(function (f) {
|
|
278
|
+
return f.id === moduleJson.id;
|
|
279
|
+
});
|
|
280
|
+
if (!frame || Object.keys(frame.coms).length > 0) return;
|
|
281
|
+
var coms = moduleJson.coms || {};
|
|
282
|
+
var cons = moduleJson.cons || {};
|
|
283
|
+
Object.values(coms).forEach(function (com) {
|
|
284
|
+
var _com$model2;
|
|
285
|
+
var outputEvents = (_com$model2 = com.model) === null || _com$model2 === void 0 ? void 0 : _com$model2.outputEvents;
|
|
286
|
+
if (!outputEvents || Object.keys(outputEvents).length === 0) return;
|
|
287
|
+
var comFrames = [];
|
|
288
|
+
Object.entries(outputEvents).forEach(function (_ref4) {
|
|
289
|
+
var _activeEvent$options;
|
|
290
|
+
var _ref5 = _slicedToArray(_ref4, 2),
|
|
291
|
+
pinId = _ref5[0],
|
|
292
|
+
events = _ref5[1];
|
|
293
|
+
var activeEvent = Array.isArray(events) ? events.find(function (e) {
|
|
294
|
+
return e.active;
|
|
295
|
+
}) : null;
|
|
296
|
+
if (!(activeEvent !== null && activeEvent !== void 0 && (_activeEvent$options = activeEvent.options) !== null && _activeEvent$options !== void 0 && _activeEvent$options.id)) return;
|
|
297
|
+
var diagramId = activeEvent.options.id;
|
|
298
|
+
var consKey = "".concat(com.id, "-").concat(pinId);
|
|
299
|
+
var connections = cons[consKey] || [];
|
|
300
|
+
var conAry = connections.map(function (con) {
|
|
301
|
+
return {
|
|
302
|
+
id: con.id,
|
|
303
|
+
from: {
|
|
304
|
+
id: pinId,
|
|
305
|
+
title: activeEvent.options.title || pinId,
|
|
306
|
+
parent: {
|
|
307
|
+
id: com.id,
|
|
308
|
+
type: "com"
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
to: {
|
|
312
|
+
id: con.pinId,
|
|
313
|
+
title: con.pinId,
|
|
314
|
+
parent: {
|
|
315
|
+
id: con.comId,
|
|
316
|
+
type: "com"
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
startPinParentKey: con.startPinParentKey || con.frameKey,
|
|
320
|
+
finishPinParentKey: con.finishPinParentKey || con.targetFrameKey
|
|
321
|
+
};
|
|
322
|
+
});
|
|
323
|
+
if (conAry.length === 0) return;
|
|
324
|
+
comFrames.push({
|
|
325
|
+
id: "".concat(com.id, "_").concat(pinId, "_frame"),
|
|
326
|
+
title: activeEvent.options.title || "".concat(com.title, " > ").concat(pinId),
|
|
327
|
+
type: "com",
|
|
328
|
+
coms: {},
|
|
329
|
+
autoRunComs: {},
|
|
330
|
+
inputs: [],
|
|
331
|
+
outputs: [],
|
|
332
|
+
frames: [],
|
|
333
|
+
diagrams: [{
|
|
334
|
+
id: diagramId,
|
|
335
|
+
title: activeEvent.options.title || "".concat(com.title, " > ").concat(pinId),
|
|
336
|
+
starter: {
|
|
337
|
+
type: "com",
|
|
338
|
+
comId: com.id,
|
|
339
|
+
pinId: pinId
|
|
340
|
+
},
|
|
341
|
+
conAry: conAry,
|
|
342
|
+
runtimeBefore: [],
|
|
343
|
+
runtimeAfter: []
|
|
344
|
+
}]
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
if (comFrames.length > 0) {
|
|
348
|
+
frame.coms[com.id] = {
|
|
349
|
+
id: com.id,
|
|
350
|
+
frames: comFrames
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
};
|
|
356
|
+
|
|
245
357
|
/**
|
|
246
358
|
* 初始化配置
|
|
247
359
|
*/
|
|
@@ -114,6 +114,8 @@ export var handleProcess = function handleProcess(event, config) {
|
|
|
114
114
|
// 处理节点调用
|
|
115
115
|
// 边遍历边构建映射:确保引用时只能看到已声明的变量
|
|
116
116
|
var outputToInputPinMap = new Map();
|
|
117
|
+
// 跟踪已声明的变量名,用于去重(同一组件+pin 多次调用时追加后缀)
|
|
118
|
+
var declaredVarCount = new Map();
|
|
117
119
|
process.nodesInvocation.forEach(function (props) {
|
|
118
120
|
var _props$nextParam, _config$getCallTempla, _props$meta$model;
|
|
119
121
|
var componentType = props.componentType,
|
|
@@ -121,12 +123,13 @@ export var handleProcess = function handleProcess(event, config) {
|
|
|
121
123
|
runType = props.runType;
|
|
122
124
|
var nextValue = getNextValue(props, config, event, outputToInputPinMap);
|
|
123
125
|
var isSameScope = checkIsSameScope(event, props);
|
|
124
|
-
var
|
|
126
|
+
var _getNextCode = getNextCode(props, config, isSameScope, event, declaredVarCount),
|
|
127
|
+
nextCode = _getNextCode.code,
|
|
128
|
+
declaredVarName = _getNextCode.varName;
|
|
125
129
|
|
|
126
|
-
//
|
|
127
|
-
var inputPinId = props.id.replace(/[^a-zA-Z0-9_]/g, '_');
|
|
130
|
+
// 声明后记录映射,供后续引用(存储完整变量名,支持去重后缀)
|
|
128
131
|
(_props$nextParam = props.nextParam) === null || _props$nextParam === void 0 || _props$nextParam.forEach(function (np) {
|
|
129
|
-
outputToInputPinMap.set("".concat(props.meta.id, "_").concat(np.id),
|
|
132
|
+
outputToInputPinMap.set("".concat(props.meta.id, "_").concat(np.id), declaredVarName);
|
|
130
133
|
});
|
|
131
134
|
if (code) {
|
|
132
135
|
code += "\n";
|
|
@@ -167,20 +170,24 @@ export var handleProcess = function handleProcess(event, config) {
|
|
|
167
170
|
var componentNameWithId = getComponentNameWithId(props, config, event);
|
|
168
171
|
code += "".concat(indent, "/** \u8C03\u7528 ").concat(props.meta.title, " */") + "\n".concat(indent).concat(nextCode).concat(componentNameWithId, "(").concat(runType === "input" ? nextValue : "", ")");
|
|
169
172
|
} else if (category === "var") {
|
|
173
|
+
var _props$extData;
|
|
170
174
|
var varKey = getSafeVarName(props.meta);
|
|
175
|
+
// 处理 xpath:如 "/a" -> "a","/a/b" -> "a.b",用于读取变量的指定属性
|
|
176
|
+
var xpath = (_props$extData = props.extData) === null || _props$extData === void 0 ? void 0 : _props$extData.xpath;
|
|
177
|
+
var xpathArg = xpath ? ", \"".concat(xpath.replace(/^\//, "").replace(/\//g, "."), "\"") : "";
|
|
171
178
|
if (props.meta.global) {
|
|
172
179
|
config.addParentDependencyImport({
|
|
173
180
|
packageName: "../../common/global",
|
|
174
181
|
dependencyNames: ["globalVars"],
|
|
175
182
|
importType: "named"
|
|
176
183
|
});
|
|
177
|
-
code += "".concat(indent, "/** ").concat(props.title, " \u5168\u5C40\u53D8\u91CF ").concat(props.meta.title, " */") + "\n".concat(indent).concat(nextCode, "globalVars.").concat(varKey, ".").concat(props.id, "(").concat(nextValue, ")");
|
|
184
|
+
code += "".concat(indent, "/** ").concat(props.title, " \u5168\u5C40\u53D8\u91CF ").concat(props.meta.title, " */") + "\n".concat(indent).concat(nextCode, "globalVars.").concat(varKey, ".").concat(props.id, "(").concat(nextValue).concat(xpathArg, ")");
|
|
178
185
|
} else {
|
|
179
186
|
var currentProvider = getCurrentProvider({
|
|
180
187
|
isSameScope: isSameScope,
|
|
181
188
|
props: props
|
|
182
189
|
}, config);
|
|
183
|
-
code += "".concat(indent, "/** ").concat(props.title, " \u53D8\u91CF ").concat(props.meta.title, " */") + "\n".concat(indent).concat(nextCode, "this.$vars.").concat(varKey, ".").concat(props.id, "(").concat(nextValue, ")");
|
|
190
|
+
code += "".concat(indent, "/** ").concat(props.title, " \u53D8\u91CF ").concat(props.meta.title, " */") + "\n".concat(indent).concat(nextCode, "this.$vars.").concat(varKey, ".").concat(props.id, "(").concat(nextValue).concat(xpathArg, ")");
|
|
184
191
|
}
|
|
185
192
|
} else if (category === "fx") {
|
|
186
193
|
if (props.meta.global) {
|
|
@@ -283,17 +290,31 @@ var getComponentNameWithId = function getComponentNameWithId(props, config, even
|
|
|
283
290
|
var sanitizedName = name.replace(/-/g, '_');
|
|
284
291
|
return "".concat(sanitizedName, "_").concat(props.meta.id);
|
|
285
292
|
};
|
|
286
|
-
var getNextCode = function getNextCode(props, config, isSameScope, event) {
|
|
293
|
+
var getNextCode = function getNextCode(props, config, isSameScope, event, declaredVarCount) {
|
|
287
294
|
var nextParam = props.nextParam;
|
|
288
295
|
if (!nextParam.length) {
|
|
289
|
-
return
|
|
296
|
+
return {
|
|
297
|
+
code: "",
|
|
298
|
+
varName: ""
|
|
299
|
+
};
|
|
290
300
|
}
|
|
291
301
|
var componentNameWithId = getComponentNameWithId(props, config, event);
|
|
292
302
|
// 使用输入端口ID(props.id)来区分同一组件的不同调用
|
|
293
303
|
// 这样可以确保 trigger 和 cancel 两个输入端口生成不同的变量名
|
|
294
304
|
var pinId = props.id;
|
|
295
305
|
var sanitizedPinId = pinId.replace(/[^a-zA-Z0-9_]/g, '_');
|
|
296
|
-
|
|
306
|
+
var varName = "".concat(componentNameWithId, "_").concat(sanitizedPinId, "_result");
|
|
307
|
+
|
|
308
|
+
// 去重:同一变量名多次声明时追加后缀
|
|
309
|
+
var count = declaredVarCount.get(varName) || 0;
|
|
310
|
+
declaredVarCount.set(varName, count + 1);
|
|
311
|
+
if (count > 0) {
|
|
312
|
+
varName = "".concat(varName, "_").concat(count);
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
code: "const ".concat(varName, ": any = "),
|
|
316
|
+
varName: varName
|
|
317
|
+
};
|
|
297
318
|
};
|
|
298
319
|
|
|
299
320
|
/**
|
|
@@ -341,21 +362,21 @@ var getNextValue = function getNextValue(props, config, event, outputToInputPinM
|
|
|
341
362
|
// 变量组件直接返回 Subject,不加 .id 后缀
|
|
342
363
|
if ((_param$meta2 = param.meta) !== null && _param$meta2 !== void 0 && (_param$meta2 = _param$meta2.def) !== null && _param$meta2 !== void 0 && (_param$meta2 = _param$meta2.namespace) !== null && _param$meta2 !== void 0 && _param$meta2.includes(".var")) {
|
|
343
364
|
var _param$meta3;
|
|
344
|
-
// 从 outputToInputPinMap
|
|
365
|
+
// 从 outputToInputPinMap 中查找完整变量名(已包含去重后缀)
|
|
345
366
|
var _key = "".concat((_param$meta3 = param.meta) === null || _param$meta3 === void 0 ? void 0 : _param$meta3.id, "_").concat(param.id);
|
|
346
|
-
var
|
|
347
|
-
if (
|
|
348
|
-
return
|
|
367
|
+
var _fullVarName = outputToInputPinMap === null || outputToInputPinMap === void 0 ? void 0 : outputToInputPinMap.get(_key);
|
|
368
|
+
if (_fullVarName) {
|
|
369
|
+
return _fullVarName;
|
|
349
370
|
}
|
|
350
371
|
return "".concat(componentNameWithId, "_result");
|
|
351
372
|
}
|
|
352
373
|
|
|
353
|
-
// 从 outputToInputPinMap
|
|
374
|
+
// 从 outputToInputPinMap 中查找完整变量名(已包含去重后缀)
|
|
354
375
|
// key = `${componentId}_${outputPinId}`
|
|
355
376
|
var key = "".concat((_param$meta4 = param.meta) === null || _param$meta4 === void 0 ? void 0 : _param$meta4.id, "_").concat(param.id);
|
|
356
|
-
var
|
|
357
|
-
if (
|
|
358
|
-
return "".concat(
|
|
377
|
+
var fullVarName = outputToInputPinMap === null || outputToInputPinMap === void 0 ? void 0 : outputToInputPinMap.get(key);
|
|
378
|
+
if (fullVarName) {
|
|
379
|
+
return "".concat(fullVarName, ".").concat(param.id);
|
|
359
380
|
}
|
|
360
381
|
return "".concat(componentNameWithId, "_result.").concat(param.id);
|
|
361
382
|
});
|
|
@@ -462,7 +483,7 @@ var generateJsApiComponentCode = function generateJsApiComponentCode(params) {
|
|
|
462
483
|
|
|
463
484
|
// 导入 createJSHandle
|
|
464
485
|
config.addParentDependencyImport({
|
|
465
|
-
packageName: "
|
|
486
|
+
packageName: "@mybricks/taro-core",
|
|
466
487
|
dependencyNames: ["createJSHandle"],
|
|
467
488
|
importType: "named"
|
|
468
489
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { indentation,
|
|
1
|
+
import { indentation, toSlotFunctionName } from "./index";
|
|
2
2
|
/** 生成组件 Inputs 映射代码 */
|
|
3
3
|
export var genComponentInputsCode = function genComponentInputsCode(indent, providerName, comId) {
|
|
4
4
|
return "".concat(indent, "inputs['").concat(comId, "'] = useBindInputs(controllers.current.").concat(providerName, ", '").concat(comId, "');\n");
|
|
@@ -21,7 +21,7 @@ export var genSlotRenderRef = function genSlotRenderRef(_ref) {
|
|
|
21
21
|
renderId = _ref.renderId,
|
|
22
22
|
indent = _ref.indent,
|
|
23
23
|
isLast = _ref.isLast;
|
|
24
|
-
var renderFunctionName =
|
|
24
|
+
var renderFunctionName = toSlotFunctionName(renderId);
|
|
25
25
|
return "".concat(indent).concat(slotId, ": {\n").concat(indent, " render: ").concat(renderFunctionName, ",\n").concat(indent, "}").concat(isLast ? '' : ',', "\n");
|
|
26
26
|
};
|
|
27
27
|
|
|
@@ -6,6 +6,8 @@ export declare const firstCharToUpperCase: (str: string) => string;
|
|
|
6
6
|
export declare const formatSlotContent: (uiContent: string, baseIndentSize: number, renderBodyIndent: string) => string;
|
|
7
7
|
/** 将字符串转为大驼峰 */
|
|
8
8
|
export declare const toPascalCase: (str: string) => string;
|
|
9
|
+
/** 生成插槽渲染函数名:S${ID} (SCREAMING_SNAKE_CASE) */
|
|
10
|
+
export declare const toSlotFunctionName: (renderId: string) => string;
|
|
9
11
|
/** Taro/React UI 组件代码生成 */
|
|
10
12
|
export declare const getUiComponentCode: (params: {
|
|
11
13
|
componentName: string;
|
|
@@ -23,6 +23,11 @@ export var toPascalCase = function toPascalCase(str) {
|
|
|
23
23
|
}).join("");
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
+
/** 生成插槽渲染函数名:S${ID} (SCREAMING_SNAKE_CASE) */
|
|
27
|
+
export var toSlotFunctionName = function toSlotFunctionName(renderId) {
|
|
28
|
+
return "S".concat(renderId.replace(/[^a-zA-Z0-9]/g, "_").toUpperCase());
|
|
29
|
+
};
|
|
30
|
+
|
|
26
31
|
/** 根节点data去除无用属性 */
|
|
27
32
|
var formatData = function formatData(data, isRoot) {
|
|
28
33
|
if (!data) return {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
2
2
|
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
3
3
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
4
|
-
import { indentation,
|
|
4
|
+
import { indentation, toSlotFunctionName } from "./index";
|
|
5
5
|
|
|
6
6
|
/** Render 函数管理器 */
|
|
7
7
|
export var RenderManager = /*#__PURE__*/function () {
|
|
@@ -50,7 +50,7 @@ export var RenderManager = /*#__PURE__*/function () {
|
|
|
50
50
|
logicCode = _ref.logicCode,
|
|
51
51
|
useWrap = _ref.useWrap,
|
|
52
52
|
description = _ref.description;
|
|
53
|
-
var renderFunctionName =
|
|
53
|
+
var renderFunctionName = toSlotFunctionName(renderId);
|
|
54
54
|
if (description) {
|
|
55
55
|
code += "".concat(indent, "/** ").concat(description, " */\n");
|
|
56
56
|
}
|
|
@@ -137,7 +137,7 @@ export var RenderManager = /*#__PURE__*/function () {
|
|
|
137
137
|
}, {
|
|
138
138
|
key: "genRenderRef",
|
|
139
139
|
value: function genRenderRef(slotId, renderId, indent) {
|
|
140
|
-
var renderFunctionName =
|
|
140
|
+
var renderFunctionName = toSlotFunctionName(renderId);
|
|
141
141
|
return "".concat(indent).concat(slotId, ": {\n").concat(indent, " render: ").concat(renderFunctionName, ",\n").concat(indent, "},\n");
|
|
142
142
|
}
|
|
143
143
|
}]);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** 生成响应式 data 管理器 */
|
|
2
2
|
export declare const genReactiveDataManager: (indent: string, utilsPackageName: string) => string;
|
|
3
3
|
/** 生成根组件定义代码 (useAppContext) */
|
|
4
|
-
export declare const genRootDefineCode: (indent: string, utilsPackageName: string, hasJsModules?: boolean) => string;
|
|
4
|
+
export declare const genRootDefineCode: (indent: string, utilsPackageName: string, hasJsModules?: boolean, isModule?: boolean) => string;
|
|
5
5
|
/** 生成普通插槽定义代码 */
|
|
6
6
|
export declare const genSlotDefineCode: (indent: string) => string;
|
|
7
7
|
/** 生成控制器初始化代码 */
|
|
@@ -6,9 +6,12 @@ export var genReactiveDataManager = function genReactiveDataManager(indent, util
|
|
|
6
6
|
/** 生成根组件定义代码 (useAppContext) */
|
|
7
7
|
export var genRootDefineCode = function genRootDefineCode(indent, utilsPackageName) {
|
|
8
8
|
var hasJsModules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
9
|
+
var isModule = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
9
10
|
// 使用 useAppContext 获取 comRefs / $vars / $fxs / appContext
|
|
10
11
|
var code = "".concat(indent, "const {comRefs, $vars, $fxs, appContext} = useAppContext();\n");
|
|
11
|
-
|
|
12
|
+
if (!isModule) {
|
|
13
|
+
code += "".concat(indent, "usePageLife();\n");
|
|
14
|
+
}
|
|
12
15
|
// 如果有 JS 计算组件,需要初始化 comModules
|
|
13
16
|
if (hasJsModules) {
|
|
14
17
|
code += "".concat(indent, "const comModules = jsModules({ createJSHandle });\n");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mybricks/to-code-taro",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "To code for Taro",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -8,15 +8,8 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"dev": "father dev",
|
|
10
10
|
"build": "father build",
|
|
11
|
-
"gen": "tsx src/generateFiles.ts",
|
|
12
11
|
"build:deps": "father prebundle",
|
|
13
|
-
"prepublishOnly": "father doctor && npm run build"
|
|
14
|
-
"test": "tsx test/index.ts",
|
|
15
|
-
"test:project": "tsx test/genProject.ts",
|
|
16
|
-
"test:template": "tsx test/genTemplate.ts",
|
|
17
|
-
"test:watch": "ts-node-dev --respawn test/index.ts",
|
|
18
|
-
"test:harmony": "tsx test/write-harmony-output.ts",
|
|
19
|
-
"test:taro": "tsx test/write-taro-output.ts"
|
|
12
|
+
"prepublishOnly": "father doctor && npm run build"
|
|
20
13
|
},
|
|
21
14
|
"repository": {
|
|
22
15
|
"type": "git",
|
package/README.md
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
# MyBricks To Code Taro
|
|
2
|
-
|
|
3
|
-
MyBricks Taro 代码生成插件,负责将 MyBricks 的 DSL (JSON) 转换为完整的 Taro 项目代码。
|
|
4
|
-
|
|
5
|
-
## 核心功能
|
|
6
|
-
|
|
7
|
-
- **DSL 转换**: 解析 MyBricks 协议,生成符合 Taro 规范的 TSX、Less 和配置。
|
|
8
|
-
- **项目组装**: 基于内置模板,自动生成完整的 Taro 项目文件结构。
|
|
9
|
-
- **依赖管理**: 自动分析并注入页面所需的组件和工具库依赖。
|
|
10
|
-
- **响应式支持**: 深度集成 MyBricks 逻辑流,支持变量、Fx、事件流与页面生命周期的自动绑定。
|
|
11
|
-
|
|
12
|
-
## 开发指南
|
|
13
|
-
|
|
14
|
-
### 常用命令
|
|
15
|
-
|
|
16
|
-
#### 1. 开发模式
|
|
17
|
-
```bash
|
|
18
|
-
npm run dev
|
|
19
|
-
```
|
|
20
|
-
启动 `father dev`,在开发过程中实时编译代码。
|
|
21
|
-
|
|
22
|
-
#### 2. 项目构建
|
|
23
|
-
```bash
|
|
24
|
-
npm run build
|
|
25
|
-
```
|
|
26
|
-
执行生产环境构建,生成 `dist` 目录下的产物。
|
|
27
|
-
|
|
28
|
-
#### 3. 运行通用测试
|
|
29
|
-
```bash
|
|
30
|
-
npm run test
|
|
31
|
-
```
|
|
32
|
-
运行 `test/index.ts`,验证核心转换逻辑是否正确(不输出物理文件)。
|
|
33
|
-
|
|
34
|
-
#### 4. 生成测试项目 (常用)
|
|
35
|
-
```bash
|
|
36
|
-
npm run test:project
|
|
37
|
-
```
|
|
38
|
-
执行 `test/genProject.ts`,根据 `test/test-data.json` 生成完整的物理 Taro 项目。
|
|
39
|
-
生成的产物将放在 `src/_output/project` 目录下,您可以直接将该目录拷贝到 Taro 开发环境中运行。
|
|
40
|
-
|
|
41
|
-
#### 5. 重新生成项目模板
|
|
42
|
-
```bash
|
|
43
|
-
npm run test:template
|
|
44
|
-
```
|
|
45
|
-
执行 `test/genTemplate.ts`。当您修改了 `src/_template` 目录下的基础模板文件后,**必须运行此命令**,以同步更新 `src/taro-template.json`。这个 JSON 文件是生成完整项目时的骨架。
|
|
46
|
-
|
|
47
|
-
## 项目结构
|
|
48
|
-
|
|
49
|
-
- `src/core`: 运行时核心工具库(由生成项目引用)。
|
|
50
|
-
- `src/utils`: 生成器内部使用的各种转换工具。
|
|
51
|
-
- `src/processors`: 场景、模块、事件等具体维度的处理逻辑。
|
|
52
|
-
- `src/_template`: 项目生成的基础骨架模板。
|
|
53
|
-
- `test/`: 包含测试数据和用于验证生成的脚本。
|
|
54
|
-
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
MyBricks Team
|
|
58
|
-
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/core/mybricks/MyBricksDescriptor.js
|
|
20
|
-
var MyBricksDescriptor_exports = {};
|
|
21
|
-
__export(MyBricksDescriptor_exports, {
|
|
22
|
-
MyBricksDescriptor: () => MyBricksDescriptor
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(MyBricksDescriptor_exports);
|
|
25
|
-
var import_constant = require("./constant");
|
|
26
|
-
var import_createModuleEventsHandle = require("./createModuleEventsHandle");
|
|
27
|
-
var import_log = require("./log");
|
|
28
|
-
var DEFAULT_GETVAR_RESULT = {
|
|
29
|
-
setValue(value) {
|
|
30
|
-
(0, import_log.log)("setValue - 变量未定义,请检查");
|
|
31
|
-
},
|
|
32
|
-
getValue() {
|
|
33
|
-
(0, import_log.log)("getValue - 变量未定义,请检查");
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
var getVar = ({ that, varName }) => {
|
|
38
|
-
var _a, _b;
|
|
39
|
-
const myBricksDescriptor = that[import_constant.MYBRICKS_DESCRIPTOR];
|
|
40
|
-
if (!myBricksDescriptor) {
|
|
41
|
-
return DEFAULT_GETVAR_RESULT;
|
|
42
|
-
}
|
|
43
|
-
const { vars, params, parent } = myBricksDescriptor;
|
|
44
|
-
const var0 = vars == null ? void 0 : vars[varName];
|
|
45
|
-
if (var0) {
|
|
46
|
-
return var0.ext();
|
|
47
|
-
}
|
|
48
|
-
if (["page", "popup", "module"].includes(params.type)) {
|
|
49
|
-
const var02 = (_b = (_a = params.appContext) == null ? void 0 : _a.globalVars) == null ? void 0 : _b[varName];
|
|
50
|
-
if (var02) {
|
|
51
|
-
return var02.ext();
|
|
52
|
-
}
|
|
53
|
-
return DEFAULT_GETVAR_RESULT;
|
|
54
|
-
}
|
|
55
|
-
return getVar({ that: parent, varName });
|
|
56
|
-
};
|
|
57
|
-
var DEFAULT_GETOUTPUT_RESULT = {
|
|
58
|
-
setValue(value) {
|
|
59
|
-
(0, import_log.log)("setValue - 输出未定义,请检查");
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
var getOutput = ({ that, outputName }) => {
|
|
63
|
-
var _a;
|
|
64
|
-
const myBricksDescriptor = that[import_constant.MYBRICKS_DESCRIPTOR];
|
|
65
|
-
if (!myBricksDescriptor) {
|
|
66
|
-
return DEFAULT_GETOUTPUT_RESULT;
|
|
67
|
-
}
|
|
68
|
-
const { params, controller, parent } = myBricksDescriptor;
|
|
69
|
-
const { type, outputNameMap, pageId } = params;
|
|
70
|
-
const outputKey = outputNameMap ? outputNameMap[outputName] || outputName : outputName;
|
|
71
|
-
if (type === "module") {
|
|
72
|
-
if ((_a = controller.events) == null ? void 0 : _a[outputKey]) {
|
|
73
|
-
return {
|
|
74
|
-
setValue(value) {
|
|
75
|
-
var _a2;
|
|
76
|
-
(_a2 = controller.events) == null ? void 0 : _a2[outputKey](value);
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
} else if (type === "popup") {
|
|
81
|
-
if (["commit", "apply", "cancel", "close"].includes(outputKey)) {
|
|
82
|
-
return {
|
|
83
|
-
setValue(value) {
|
|
84
|
-
var _a2, _b;
|
|
85
|
-
(_b = (_a2 = params.appContext) == null ? void 0 : _a2.page) == null ? void 0 : _b[outputKey](pageId, value);
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
} else if (type === "slot") {
|
|
90
|
-
if (that.params.outputs && outputKey in that.params.outputs) {
|
|
91
|
-
return {
|
|
92
|
-
setValue(value) {
|
|
93
|
-
that.params.outputs[outputKey](value);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
if (["page", "popup", "module"].includes(type)) {
|
|
99
|
-
return DEFAULT_GETOUTPUT_RESULT;
|
|
100
|
-
}
|
|
101
|
-
return getOutput({ that: parent, outputName });
|
|
102
|
-
};
|
|
103
|
-
var DEFAULT_GETINPUT_RESULT = {
|
|
104
|
-
getValue(value) {
|
|
105
|
-
(0, import_log.log)("getValue - 输入未定义,请检查");
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
var getInput = ({ that, inputName }) => {
|
|
109
|
-
const myBricksDescriptor = that[import_constant.MYBRICKS_DESCRIPTOR];
|
|
110
|
-
if (!myBricksDescriptor) {
|
|
111
|
-
return DEFAULT_GETINPUT_RESULT;
|
|
112
|
-
}
|
|
113
|
-
const { params, controller, parent } = myBricksDescriptor;
|
|
114
|
-
const { type, inputNameMap, pageId } = params;
|
|
115
|
-
const inputKey = (inputNameMap ? inputNameMap[inputName] : inputName) || inputName;
|
|
116
|
-
if (type === "slot") {
|
|
117
|
-
if (inputKey in that.params.inputValues) {
|
|
118
|
-
return {
|
|
119
|
-
getValue() {
|
|
120
|
-
return that.params.inputValues[inputKey];
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
} else if (type === "page" || type === "popup") {
|
|
125
|
-
if (inputKey === "open") {
|
|
126
|
-
return {
|
|
127
|
-
getValue() {
|
|
128
|
-
var _a, _b, _c;
|
|
129
|
-
return (_c = (_b = (_a = params.appContext) == null ? void 0 : _a.page) == null ? void 0 : _b.getParams(pageId)) == null ? void 0 : _c[import_constant.SUBJECT_VALUE];
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
return DEFAULT_GETINPUT_RESULT;
|
|
134
|
-
} else if (type === "module") {
|
|
135
|
-
return {
|
|
136
|
-
getValue() {
|
|
137
|
-
var _a, _b;
|
|
138
|
-
if (inputKey in controller.data) {
|
|
139
|
-
return controller.data[inputKey];
|
|
140
|
-
}
|
|
141
|
-
return (_b = (_a = controller.controller) == null ? void 0 : _a[inputKey]) == null ? void 0 : _b[import_constant.SUBJECT_VALUE];
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
if (["page", "popup", "module"].includes(type)) {
|
|
146
|
-
return DEFAULT_GETINPUT_RESULT;
|
|
147
|
-
}
|
|
148
|
-
return getInput({ that: parent, inputName });
|
|
149
|
-
};
|
|
150
|
-
function MyBricksDescriptor(params) {
|
|
151
|
-
return (target, key, descriptor) => {
|
|
152
|
-
const originalMethod = descriptor.value;
|
|
153
|
-
descriptor.value = function(...args) {
|
|
154
|
-
let controller = null;
|
|
155
|
-
let vars = null;
|
|
156
|
-
Object.keys(this.__proto__).filter((key2) => !key2.startsWith("__ob_")).forEach((key2) => {
|
|
157
|
-
const value = this[key2];
|
|
158
|
-
if (value == null ? void 0 : value[import_constant.BASECONTROLLER_MARK]) {
|
|
159
|
-
const descriptor2 = Object.getOwnPropertyDescriptor(this, key2);
|
|
160
|
-
if (!(descriptor2 == null ? void 0 : descriptor2.get)) {
|
|
161
|
-
controller = value;
|
|
162
|
-
}
|
|
163
|
-
} else if (value == null ? void 0 : value[import_constant.VARS_MARK]) {
|
|
164
|
-
const descriptor2 = Object.getOwnPropertyDescriptor(this, key2);
|
|
165
|
-
if (!(descriptor2 == null ? void 0 : descriptor2.get)) {
|
|
166
|
-
vars = value;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
this[import_constant.MYBRICKS_DESCRIPTOR] = {
|
|
171
|
-
controller,
|
|
172
|
-
vars,
|
|
173
|
-
params,
|
|
174
|
-
parent: this.getParent()
|
|
175
|
-
};
|
|
176
|
-
if (controller) {
|
|
177
|
-
if (this.events) {
|
|
178
|
-
controller.events = (0, import_createModuleEventsHandle.createModuleEventsHandle)({ that: this, appContext: params.appContext });
|
|
179
|
-
}
|
|
180
|
-
if (this.data) {
|
|
181
|
-
controller.data = this.data;
|
|
182
|
-
}
|
|
183
|
-
if (this.controller) {
|
|
184
|
-
controller.controller = this.controller;
|
|
185
|
-
}
|
|
186
|
-
const classController = controller;
|
|
187
|
-
Object.getOwnPropertyNames(classController).forEach((key2) => {
|
|
188
|
-
const controllerContext = classController[key2][import_constant.CONTROLLER_CONTEXT];
|
|
189
|
-
if (controllerContext) {
|
|
190
|
-
controllerContext["this"] = this;
|
|
191
|
-
controllerContext["getVar"] = (varName) => {
|
|
192
|
-
return getVar({ that: this, varName });
|
|
193
|
-
};
|
|
194
|
-
controllerContext["getOutput"] = (outputName) => {
|
|
195
|
-
return getOutput({ that: this, outputName });
|
|
196
|
-
};
|
|
197
|
-
controllerContext["getInput"] = (inputName) => {
|
|
198
|
-
return getInput({ that: this, inputName });
|
|
199
|
-
};
|
|
200
|
-
controllerContext["appContext"] = params.appContext;
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
const result = originalMethod.apply(this, args);
|
|
205
|
-
return result;
|
|
206
|
-
};
|
|
207
|
-
return descriptor;
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
211
|
-
0 && (module.exports = {
|
|
212
|
-
MyBricksDescriptor
|
|
213
|
-
});
|