@uiw-admin/plugins
Version:
236 lines (229 loc) • 8.62 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.stringToJson = exports.getReactLazy = exports.getJSX = exports.getJSONData = exports.babelPluginComponents = exports.IsModel = void 0;
var _parser = require("@babel/parser");
var _traverse = _interopRequireDefault(require("@babel/traverse"));
var t = _interopRequireWildcard(require("@babel/types"));
var _generator = _interopRequireDefault(require("@babel/generator"));
var _template = _interopRequireDefault(require("@babel/template"));
var _index = require("./index");
var pluginsConfig = ['jsx', 'typescript', 'classProperties', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'functionBind', 'nullishCoalescingOperator', 'objectRestSpread', 'optionalChaining', 'decorators-legacy'];
var getAst = function getAst(content) {
var option = {
// 在严格模式下解析并允许模块声明
sourceType: 'module',
plugins: pluginsConfig
};
return (0, _parser.parse)(content, option);
};
function getVarInit(node, path) {
// 判断 默认导出变量的方式
if (t.isIdentifier(node) && path.scope.hasBinding(node.name)) {
// 导出变量的方式 从 path scope 取值 bindings 里面对应 node.name 的变量内容
var bindingNode = path.scope.getBinding(node.name).path.node;
// 判断对象类型 是否是 VariableDeclarator
if (t.isVariableDeclarator(bindingNode)) {
// 取 这个里面的 init 对象
bindingNode = bindingNode.init;
}
return bindingNode;
}
return node;
}
// 使用 ts 判断
function getTSNode(node) {
if (t.isTSTypeAssertion(node) || t.isTSAsExpression(node)) {
return node.expression;
} else {
return node;
}
}
var getReactLazy = function getReactLazy(path) {
// ------------------ 创建 React.lazy ------------------
// 第一步 创建 字符串外层 @/pages/TableList
var callOne = t.callExpression(t["import"](), [t.stringLiteral(path)]);
// 第二步 创建 ArrowFunctionExpression
var callTwo = t.arrowFunctionExpression([], callOne);
var callThree1 = t.memberExpression(t.identifier('React'), t.identifier('lazy'));
// 第三步,value 值
var callThree = t.callExpression(callThree1, [callTwo]);
// const call4 = t.callExpression(callThree1, [callThree])
// const callObj = t.objectProperty(t.identifier("a"), call4)
// console.log("callObj", callObj)
// ------------------ 创建 React.lazy 结束 ------------------
return callThree;
};
exports.getReactLazy = getReactLazy;
var getJSX = function getJSX(name) {
var one = t.jsxOpeningElement(t.jsxIdentifier(name), [], true);
var two = t.jsxElement(one, null, []);
return two;
};
exports.getJSX = getJSX;
var IsModel = function IsModel(content) {
var isModels = false;
var modelNames;
var isCreateModel = false;
var ast = (0, _parser.parse)(content, {
// 在严格模式下解析并允许模块声明
sourceType: 'module',
plugins: pluginsConfig
});
(0, _traverse["default"])(ast, {
ExportDefaultDeclaration: function (_ExportDefaultDeclaration) {
function ExportDefaultDeclaration(_x) {
return _ExportDefaultDeclaration.apply(this, arguments);
}
ExportDefaultDeclaration.toString = function () {
return _ExportDefaultDeclaration.toString();
};
return ExportDefaultDeclaration;
}(function (path) {
var node = path.node.declaration;
node = getTSNode(node);
node = getVarInit(node, path);
node = getTSNode(node);
if (t.isCallExpression(node) && node.arguments) {
node = node.arguments[0];
isCreateModel = true;
}
// 如果 node 是一个对象
// 并且 子集存在 state reducers, subscriptions, effects, name 则是一个 model 返回true
if (t.isObjectExpression(node) && node.properties.some(function (property) {
return ['state', 'reducers', 'subscriptions', 'effects', 'name'].includes(property.key.name);
})) {
isModels = true;
var modeObj = node.properties.find(function (property) {
return property.key.name === 'name';
});
if (t.isObjectProperty(modeObj) && t.isStringLiteral(modeObj.value)) {
modelNames = modeObj.value.value;
}
}
})
});
return {
isModels: isModels,
modelNames: modelNames,
isCreateModel: isCreateModel
};
};
// 转换成对象
exports.IsModel = IsModel;
var stringToJson = function stringToJson(str) {
var json = new Function('return ' + str)();
return json;
};
// ts/js 文件获取里面的 默认导出内容
exports.stringToJson = stringToJson;
var getJSONData = function getJSONData(content) {
var isJSON = false;
var jsonArr = [];
var jsonCode = '';
var ast = getAst(content);
var isReact = false;
(0, _traverse["default"])(ast, {
ExportDefaultDeclaration: function (_ExportDefaultDeclaration2) {
function ExportDefaultDeclaration(_x2) {
return _ExportDefaultDeclaration2.apply(this, arguments);
}
ExportDefaultDeclaration.toString = function () {
return _ExportDefaultDeclaration2.toString();
};
return ExportDefaultDeclaration;
}(function (path) {
var node = path.node.declaration;
node = getTSNode(node);
node = getVarInit(node, path);
node = getTSNode(node);
// 如果 node 是一个数组
if (t.isArrayExpression(node)) {
isJSON = true;
}
}),
ImportDefaultSpecifier: function ImportDefaultSpecifier(path) {
var node = path.node;
if (t.isImportDefaultSpecifier(node)) {
if (node.local.name === 'React') {
isReact = true;
}
}
}
});
jsonCode = (0, _generator["default"])(ast).code;
return {
isJSON: isJSON,
jsonArr: jsonArr,
jsonCode: isReact ? jsonCode : "import React from \"react\"\n".concat(jsonCode)
};
};
/** 对字符串进行解析处理图标和 404、403、500 页面加载 */
exports.getJSONData = getJSONData;
var babelPluginComponents = function babelPluginComponents(content) {
var ast = getAst(content);
var iconsList = [];
var importList = [];
(0, _traverse["default"])(ast, {
ImportSpecifier: function ImportSpecifier(path) {
var node = path.node;
if (t.isIdentifier(node.local)) {
var values = node.local.name;
importList.push(values);
}
},
ObjectProperty: function ObjectProperty(path) {
// 判断父级的父级是否是数组 如果是数组则进行转换
if (t.isArrayExpression(path.parentPath.parent)) {
var node = path.node;
// 对组件进行处理
if (t.isStringLiteral(node.key) && node.key.value === 'component' || t.isIdentifier(node.key) && node.key.name === 'component') {
if (t.isStringLiteral(node.value)) {
var valus = node.value.value;
if (['404', '403', '500'].includes(node.value.value)) {
node.value = getJSX("Exceptions".concat(valus));
} else {
node.value = getReactLazy(valus);
}
}
}
// 对icon图标进行处理
if (t.isStringLiteral(node.key) && node.key.value === 'icon' || t.isIdentifier(node.key) && node.key.name === 'icon') {
if (t.isStringLiteral(node.value)) {
var _valus = node.value.value;
var newValue = (0, _index.getToUpperCase)(_valus) + "_Icon";
node.value = getJSX("".concat(newValue));
iconsList.push(newValue);
}
}
// 对 navigate 进行转换
if (t.isStringLiteral(node.key) && node.key.value === 'navigate' || t.isIdentifier(node.key) && node.key.name === 'navigate') {
if (t.isStringLiteral(node.value)) {
var _valus2 = node.value.value;
var fn = (0, _template["default"])(_valus2);
var _newValue = fn();
if (t.isExpressionStatement(_newValue)) {
node.value = _newValue.expression;
}
}
}
}
}
});
var jsonCode = (0, _generator["default"])(ast).code;
// icon 去重
var newIcon = Array.from(new Set(iconsList));
importList.forEach(function (k) {
newIcon = newIcon.filter(function (ik) {
return ik !== k;
});
});
return {
iconsList: newIcon,
code: jsonCode
};
};
exports.babelPluginComponents = babelPluginComponents;