UNPKG

@luban-cli/cli-plugin-service

Version:
238 lines 10.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateRoutes = void 0; const core_1 = require("@babel/core"); const generator_1 = __importDefault(require("@babel/generator")); const traverse_1 = __importDefault(require("@babel/traverse")); const type = require("@babel/types"); function getAst(sourceFile) { return __awaiter(this, void 0, void 0, function* () { let result = null; try { result = yield core_1.transformFileSync(sourceFile, { ast: true, sourceType: "module", presets: ["@babel/preset-typescript"], }); } catch (e) { console.log(e); } return result === null || result === void 0 ? void 0 : result.ast; }); } function produceOriginRoute(ast) { let originRouteNode = null; traverse_1.default(ast, { ObjectProperty(path) { const key = path.node.key; if (key.name === "routes" && type.isArrayExpression(path.node.value)) { originRouteNode = path.node.value; } }, }); const staticRouteNodeDeclaration = type.variableDeclaration("const", [ type.variableDeclarator(type.identifier("originRoute"), originRouteNode), ]); const exportDefaultDeclaration = type.exportDefaultDeclaration(type.identifier("originRoute")); const _ast = { type: "Program", body: [staticRouteNodeDeclaration, exportDefaultDeclaration], directives: [], sourceFile: "", sourceType: "module", leadingComments: null, innerComments: null, trailingComments: null, loc: null, start: null, end: null, interpreter: null, }; return _ast; } function produceStaticRoute(ast) { let staticRouteNode = null; const importComponentDeclaration = []; traverse_1.default(ast, { ObjectProperty(path) { const key = path.node.key; if (key.name === "component" && type.isStringLiteral(path.node.value)) { const _path = path.node.value.value; const pathItem = path.node.value.value.split("/"); const name = pathItem[pathItem.length - 1].toUpperCase(); path.node.value = type.identifier(name); importComponentDeclaration.push(type.importDeclaration([type.importDefaultSpecifier(type.identifier(name))], type.stringLiteral(_path))); } if (key.name === "routes" && type.isArrayExpression(path.node.value)) { staticRouteNode = path.node.value; } }, }); const staticRouteNodeDeclaration = type.variableDeclaration("const", [ type.variableDeclarator(type.identifier("staticRoute"), staticRouteNode), ]); const exportDefaultDeclaration = type.exportDefaultDeclaration(type.identifier("staticRoute")); const _ast = { type: "Program", body: [...importComponentDeclaration, staticRouteNodeDeclaration, exportDefaultDeclaration], directives: [], sourceFile: "", sourceType: "module", leadingComments: null, innerComments: null, trailingComments: null, loc: null, start: null, end: null, interpreter: null, }; return _ast; } function produceDynamicRoute(ast) { let dynamicRouteNode = null; let declareFallbackOption = false; let userFallbackPath = ""; traverse_1.default(ast, { ObjectProperty(path) { const key = path.node.key; if (key.name === "component" && type.isStringLiteral(path.node.value)) { const componentPath = path.node.value.value; let routePath = ""; if (Array.isArray(path.container)) { const node = path.container.find((c) => { if (type.isObjectProperty(c)) { if (type.isIdentifier(c.key)) { return c.key.name === "path"; } } }); if (type.isObjectProperty(node)) { routePath = node.value.value; } } const pathSnippets = routePath.split("/"); const chunkName = pathSnippets.join("-"); const callLoadableExpression = type.callExpression(type.identifier("Loadable"), [ type.objectExpression([ type.objectProperty(type.identifier("loader"), type.arrowFunctionExpression([], type.callExpression(type.identifier("import"), [ type.addComment(type.stringLiteral(`${componentPath}`), "leading", `webpackChunkName: "page${chunkName}"`), ]))), type.objectProperty(type.identifier("loading"), type.identifier("DefaultFallback")), type.objectProperty(type.identifier("modules"), type.arrayExpression([type.stringLiteral(`${componentPath}`)])), type.objectProperty(type.identifier("webpack"), type.arrowFunctionExpression([], type.arrayExpression([ type.callExpression(type.identifier("require.resolveWeak"), [ type.stringLiteral(`${componentPath}`), ]), ]))), ]), ]); path.node.value = type.conditionalExpression(type.identifier("__IS_BROWSER__"), callLoadableExpression, type.memberExpression(type.callExpression(type.identifier("require"), [type.stringLiteral(componentPath)]), type.identifier("default"))); } if (key.name === "fallback" && type.isStringLiteral(path.node.value)) { declareFallbackOption = true; userFallbackPath = path.node.value.value; } if (key.name === "routes" && type.isArrayExpression(path.node.value)) { dynamicRouteNode = path.node.value; } }, }); const dynamicRouteDeclaration = type.variableDeclaration("const", [ type.variableDeclarator(type.identifier("dynamicRoute"), dynamicRouteNode), ]); const exportDefaultDeclaration = type.exportDefaultDeclaration(type.identifier("dynamicRoute")); const importLoadableDeclaration = type.importDeclaration([type.importDefaultSpecifier(type.identifier("Loadable"))], type.stringLiteral("react-loadable")); const importFallbackDeclaration = declareFallbackOption ? type.importDeclaration([type.importDefaultSpecifier(type.identifier("DefaultFallback"))], type.stringLiteral(userFallbackPath)) : type.importDeclaration([ type.importSpecifier(type.identifier("DefaultFallback"), type.identifier("DefaultFallback")), ], type.stringLiteral("./defaultFallback")); const _ast = { type: "Program", body: [ importLoadableDeclaration, importFallbackDeclaration, dynamicRouteDeclaration, exportDefaultDeclaration, ], directives: [], sourceFile: "", sourceType: "module", leadingComments: null, innerComments: null, trailingComments: null, loc: null, start: null, end: null, interpreter: null, }; return _ast; } function isUseStore(ast) { let useStore = false; traverse_1.default(ast, { ObjectProperty: (path) => { const key = path.node.key; if (key.name === "models" && type.isIdentifier(path.node.value)) { useStore = true; } }, }); return useStore; } function getPreparerComponentPath(ast) { let preparerComponentPath = ""; traverse_1.default(ast, { ObjectProperty: (path) => { const key = path.node.key; if (key.name === "preparer" && type.isStringLiteral(path.node.value)) { preparerComponentPath = path.node.value.value; } }, }); return preparerComponentPath; } function generateRoutes(entryFile, routesFile) { return __awaiter(this, void 0, void 0, function* () { const entryAst = yield getAst(entryFile); const routesAstForDynamic = yield getAst(routesFile); const routesAstForStatic = yield getAst(routesFile); const routesAst = yield getAst(routesFile); let originRouteCode = ""; let dynamicRouteCode = ""; let staticRouteCode = ""; let useStore = false; let preparerComponentPath = ""; if (entryAst) { useStore = isUseStore(entryAst); preparerComponentPath = getPreparerComponentPath(entryAst); } if (routesAst) { originRouteCode = generator_1.default(produceOriginRoute(routesAst)).code; } if (routesAstForDynamic) { const dynamicRouteAst = produceDynamicRoute(routesAstForDynamic); dynamicRouteCode = generator_1.default(dynamicRouteAst).code; } if (routesAstForStatic) { const staticRouteAst = produceStaticRoute(routesAstForStatic); staticRouteCode = generator_1.default(staticRouteAst).code; } return { originRouteCode, dynamicRouteCode, staticRouteCode, useStore, preparerComponentPath }; }); } exports.generateRoutes = generateRoutes; //# sourceMappingURL=generateRoutes.js.map