UNPKG

@areslabs/alita-core

Version:

alita-core

98 lines (78 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = compPreHandle; var _ErrorLogTraverse = _interopRequireDefault(require("../util/ErrorLogTraverse")); var t = _interopRequireWildcard(require("@babel/types")); 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 }; } /** * Copyright (c) Areslabs. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ /** * 预处理 * 1. <A name={"yk"}/> 修改为 <A name="yk"/> * * 2. wxNavigationOptions 设置到 小程序的页面配置上 * * 3. 移除JSX里面的注释 * * 4. <A x/> ==> <A x="true"/> * * ... * * @param ast * @param info * @returns {*} */ function compPreHandle(ast, info) { (0, _ErrorLogTraverse.default)(ast, { exit: path => { // 移除注释 if (path.node.leadingComments) { path.node.leadingComments = []; } if (path.node.trailingComments) { path.node.trailingComments = []; } // JSXExpressionContainer 只可能出现在3个地方: JSXAttribute,JSXElement, JSXFragment。 所以这里可以用else逻辑处理 if (path.type === 'JSXExpressionContainer' && path.node.expression.type === 'StringLiteral') { const pp = path.parentPath; if (pp.type === 'JSXAttribute') { // <A name={"yk"}/> 修改为 <A name="yk"/> path.replaceWith(path.node.expression); } else { // <A>{"yk"}</A> 修改为 <A>yk</A> path.replaceWith(t.jsxText(path.node.expression.value)); } return; } if (path.type === 'ClassProperty' && path.node.static && path.node.key.name === 'wxNavigationOptions') { const v = path.node.value; if (v.type === 'ObjectExpression') { const props = v.properties; for (let i = 0; i < props.length; i++) { const p = props[i]; const k = p.key.name; const v = p.value.value; info.json[k] = v; } } return; } // 去掉注释 {/**/} if (path.type === 'JSXEmptyExpression') { path.parentPath.remove(); return; } if (path.type === 'JSXAttribute' && !path.node.value) { path.node.value = t.stringLiteral('true'); } } }); return ast; }