@areslabs/alita-core
Version:
alita-core
104 lines (81 loc) • 4.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleChanged = void 0;
var _util = require("../util/util");
var _prettierWxml = _interopRequireDefault(require("../util/prettierWxml"));
var t = _interopRequireWildcard(require("@babel/types"));
var _traverse = _interopRequireDefault(require("@babel/traverse"));
var _uast = require("../util/uast");
var _constants = require("../constants");
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const handleChanged = (info, finalJSPath) => {
const newWxOutFiles = {};
const {
templates,
outComp,
childTemplates
} = info.RFInfo;
const newTemp = [];
for (let i = 0; i < templates.length; i++) {
newTemp.push(t.jsxText("\n "));
newTemp.push(templates[i]);
newTemp.push(t.jsxText("\n "));
}
newTemp.push(t.jsxText("\n "));
const ast = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier("InnerTmpOpeningElement"), []), t.jsxClosingElement(t.jsxIdentifier("InnerTmpOpeningElement")), newTemp, false);
const past = t.program([t.expressionStatement(ast)]);
(0, _traverse.default)(past, {
exit: path => {
if (path.type === "JSXExpressionContainer" && path.node.expression.type === "JSXEmptyExpression") {
path.remove();
return;
}
if (path.type === 'JSXElement' && path.node.openingElement) {
const openingElement = path.node.openingElement;
const name = openingElement.name.name;
if (name && _constants.wxBaseComp.has(name.toLocaleLowerCase()) && (info.im[name] || info.RFInfo.outComp && info.RFInfo.outComp.includes(name))) {
const aliasName = `WX${name}`;
openingElement.name.name = aliasName;
if (path.node.closingElement) {
const closingElement = path.node.closingElement;
closingElement.name.name = aliasName;
}
}
}
}
});
const templateWxmlPath = finalJSPath.replace(".js", ".wxml");
let templateWxml = (0, _uast.geneReactCode)(ast);
templateWxml = templateWxml.replace("<InnerTmpOpeningElement>", "");
templateWxml = templateWxml.replace("</InnerTmpOpeningElement>", "");
for (let i = 0; i < childTemplates.length; i++) {
const name = childTemplates[i]; // 如果只使用一个child 小程序会报递归, 然后就不渲染了
const subT = `
<template name="${name}">
<block wx:if="{{_t.l(d)}}">{{d}}</block>
<${_constants.genericCompName} wx:elif="{{d.${_constants.genericCompDiuu}}}" diuu="{{d.${_constants.genericCompDiuu}}}" style="{{_t.s(d.${_constants.genericCompDiuu}style)}}"/>
<template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"/>
<block wx:else>
<block wx:for="{{d}}" wx:key="key">
<block wx:if="{{_t.l(item)}}">{{item}}</block>
<${_constants.genericCompName} wx:elif="{{item.${_constants.genericCompDiuu}}}" diuu="{{item.${_constants.genericCompDiuu}}}" style="{{_t.s(item.${_constants.genericCompDiuu}style)}}"/>
<template wx:else is="{{item.tempName}}" data="{{...item}}"/>
</block>
</block>
</template>
`;
templateWxml = subT + templateWxml;
}
const utilWxsPath = `${_util.RootPrefixPlaceHolader}/commonwxs.wxs`;
templateWxml = `<wxs src="${utilWxsPath}" module="_t" />
${templateWxml}
<template wx:if="{{(_r && _r.tempName)}}" is="{{_r.tempName}}" data="{{..._r}}"/>
`;
newWxOutFiles[templateWxmlPath] = (0, _prettierWxml.default)(templateWxml);
return newWxOutFiles;
};
exports.handleChanged = handleChanged;