UNPKG

@uiw-admin/plugins

Version:
215 lines (213 loc) 8.81 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _temp = _interopRequireDefault(require("./temp")); var _chokidar = _interopRequireDefault(require("chokidar")); var _utils = require("./../utils"); /* * @Description: 简单版本的 自动加载 model */ var RoutesWebpackPlugin = /*#__PURE__*/function () { function RoutesWebpackPlugin(props) { var _this = this; (0, _classCallCheck2["default"])(this, RoutesWebpackPlugin); // json 文件地址 (0, _defineProperty2["default"])(this, "jsonFilePath", ''); // js文件地址 (0, _defineProperty2["default"])(this, "jsFilePath", ''); // ts 文件地址 (0, _defineProperty2["default"])(this, "tsFilePath", ''); // 渲染路由数组 (0, _defineProperty2["default"])(this, "routes", []); // 上一次的路由数据 (0, _defineProperty2["default"])(this, "preString", ''); // 下一次渲染路由数据 (0, _defineProperty2["default"])(this, "nextString", ''); // 根目录 (0, _defineProperty2["default"])(this, "cwd", ''); // 需要监听的文件目录 (0, _defineProperty2["default"])(this, "cwdConfig", ''); (0, _defineProperty2["default"])(this, "uiw", ''); // src/.uiw/modelsMap.json 地址 (0, _defineProperty2["default"])(this, "modelsMapJson", ''); // models 获取 src/.uiw/modelsMap.json 数据 (0, _defineProperty2["default"])(this, "modelsMapData", []); (0, _defineProperty2["default"])(this, "lazyLoad", false); (0, _defineProperty2["default"])(this, "jsonCode", ''); (0, _defineProperty2["default"])(this, "isTS", true); // 生成临时路由 (0, _defineProperty2["default"])(this, "createTemps", function (strs, isType) { if (['[]', 'export default []'].includes(strs)) { _this.preString = ''; _this.nextString = ''; } else { _this.preString = _this.nextString; _this.nextString = ''; } if (!_fs["default"].existsSync(_this.uiw)) { _fs["default"].mkdirSync(_this.uiw); } var content = ['js', 'ts'].includes(isType) ? _this.jsonCode : strs; var babelIcons = (0, _utils.babelPluginComponents)(content); var routeTemp = (0, _temp["default"])(babelIcons.code, babelIcons.iconsList, isType); _fs["default"].writeFileSync(_path["default"].resolve(process.cwd(), _this.isTS ? 'src/.uiw/routes.tsx' : 'src/.uiw/routes.js'), routeTemp, { encoding: 'utf-8', flag: 'w+' }); _this.createRouteMapModels(); }); // 判断上一次和下一次 (0, _defineProperty2["default"])(this, "checkPreAndNext", function (isType) { if (_this.preString !== _this.nextString) { // 读取文件数据 var routerTemp = JSON.stringify(_this.routes, null, 2); // .replace(/\"component\": (\"(.+?)\")/g, (global, m1, m2) => { // return `"component": ${m2.replace(/\^/g, '"')}`; // }) // .replace(/\\r\\n/g, '\r\n') // .replace(/\\n/g, '\r\n'); _this.createTemps(routerTemp, isType); } }); // 获取文件内容 (0, _defineProperty2["default"])(this, "getFileContent", function (isType) { var temps = '[]'; if (isType === 'json') { _this.nextString = _fs["default"].readFileSync(_this.jsonFilePath, { encoding: 'utf-8' }).toString().trim(); if (_this.nextString !== '') { _this.routes = (0, _utils.stringToJson)(_this.nextString); _this.nextString = JSON.stringify(_this.routes); _this.checkPreAndNext(isType); return; } } else if (['js', 'ts'].includes(isType)) { temps = 'export default []'; var filePath = ''; if (isType === 'js') { filePath = _this.jsFilePath; } else { filePath = _this.tsFilePath; } var content = _fs["default"].readFileSync(filePath, { encoding: 'utf-8' }); var _getJSONData = (0, _utils.getJSONData)(content), isJSON = _getJSONData.isJSON, jsonArr = _getJSONData.jsonArr, jsonCode = _getJSONData.jsonCode; if (isJSON) { _this.routes = jsonArr; _this.jsonCode = jsonCode || 'export default []'; // this.nextString = JSON.stringify(jsonArr); _this.nextString = _this.jsonCode; _this.checkPreAndNext(isType); return; } } _this.createTemps(temps, isType); }); // 判断文件优先级 (0, _defineProperty2["default"])(this, "JudgeFileType", function () { var isType = 'json'; if (_fs["default"].existsSync(_this.jsonFilePath)) { isType = 'json'; } else if (_fs["default"].existsSync(_this.tsFilePath)) { isType = 'ts'; } else if (_fs["default"].existsSync(_this.jsFilePath)) { isType = 'js'; } else { isType = false; } _this.getFileContent(isType); }); // 必须要存在这个文件 优先级 json > ts > js this.jsonFilePath = _path["default"].resolve(process.cwd(), 'config/routes.json'); this.modelsMapJson = _path["default"].resolve(process.cwd(), 'src/.uiw/modelsMap.json'); this.jsFilePath = _path["default"].resolve(process.cwd(), 'config/routes.js'); this.tsFilePath = _path["default"].resolve(process.cwd(), 'config/routes.ts'); this.uiw = _path["default"].resolve(process.cwd(), 'src/.uiw'); this.isTS = _fs["default"].existsSync(_path["default"].join(process.cwd(), 'tsconfig.json')); this.lazyLoad = !!(props !== null && props !== void 0 && props.lazyLoad); // ---- this.cwdConfig = _path["default"].resolve(process.cwd(), 'config'); this.cwd = _path["default"].resolve(process.cwd()); } (0, _createClass2["default"])(RoutesWebpackPlugin, [{ key: "readModelsMapJSON", value: function readModelsMapJSON() { if (!this.lazyLoad) { return; } if (_fs["default"].existsSync(this.modelsMapJson)) { var modelsStr = _fs["default"].readFileSync(this.modelsMapJson, { encoding: 'utf-8' }).toString().trim(); if (modelsStr !== '') { this.modelsMapData = (0, _utils.stringToJson)(modelsStr); } else { this.modelsMapData = []; } } } }, { key: "createRouteMapModels", value: function createRouteMapModels() { if (!this.lazyLoad) { _fs["default"].writeFileSync(_path["default"].resolve(process.cwd(), 'src/.uiw/routeMapModels.json'), "{}", { encoding: 'utf-8', flag: 'w+' }); return; } var routeModels = (0, _utils.getRouteMapModels)(this.routes, this.modelsMapData); var routeModelsStr = JSON.stringify(routeModels, function (_, value) { return value; }, 2).replace(/\\r\\n/g, '\r\n').replace(/\\n/g, '\r\n'); if (!_fs["default"].existsSync(this.uiw)) { _fs["default"].mkdirSync(this.uiw); } _fs["default"].writeFileSync(_path["default"].resolve(process.cwd(), 'src/.uiw/routeMapModels.json'), "".concat(routeModelsStr), { encoding: 'utf-8', flag: 'w+' }); } }, { key: "apply", value: function apply(compiler) { var _this2 = this; compiler.hooks.afterPlugins.tap('RoutesWebpackPlugin', function () { _this2.readModelsMapJSON(); _this2.JudgeFileType(); if (process.env.NODE_ENV === 'development') { _chokidar["default"].watch([_this2.cwdConfig, _this2.modelsMapJson], { cwd: _this2.cwd }).on('all', function (event, path) { if (['change', 'add', 'unlink'].includes(event) && /src\/\.uiw\/modelsMap.json/.test(path)) { _this2.readModelsMapJSON(); _this2.createRouteMapModels(); } if (['change', 'add', 'unlink'].includes(event) && /config(\\|\/)routes.(js|json|ts)/.test(path) // && ["config/routes.json", "config/routes.ts", "config/routes.js"].includes(path) ) { _this2.JudgeFileType(); } }); } }); } }]); return RoutesWebpackPlugin; }(); var _default = RoutesWebpackPlugin; exports["default"] = _default; module.exports = exports.default;