UNPKG

build-plugin-rax-app

Version:

The basic webpack configuration for rax project

160 lines 7.66 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var loader_utils_1 = require("loader-utils"); var path_1 = require("path"); var app_helpers_1 = require("@builder/app-helpers"); var miniapp_builder_shared_1 = require("miniapp-builder-shared"); var constants_1 = require("../constants"); function default_1(appJSON) { var options = (0, loader_utils_1.getOptions)(this) || {}; var target = options.target, mpa = options.mpa; var initialStaticConfig = transformAppConfig(appJSON); var isRootAppJsonPath = this.resourcePath === (0, path_1.join)(this.rootContext, 'src', 'app.json'); var staticConfig = __assign(__assign({}, initialStaticConfig), { routes: formatRoutes(initialStaticConfig.routes, { target: target, currentSubDir: (0, path_1.dirname)(this.resourcePath), rootContext: this.rootContext, isRootAppJsonPath: isRootAppJsonPath, }) }); if (mpa && isRootAppJsonPath) { return "\n const appConfig = ".concat(appJSON, ";\n export default appConfig;\n "); } var _a = getImportComponentInfo.call(this, staticConfig, target), normalImportExpression = _a.normalImportExpression, normalImports = _a.normalImports, dynamicImports = _a.dynamicImports, requireRoutes = _a.requireRoutes; var routes = staticConfig.routes, otherConfig = __rest(staticConfig, ["routes"]); return "\n ".concat(normalImportExpression, "\n const staticConfig = ").concat(JSON.stringify(otherConfig), ";\n\n staticConfig.routes = [];\n\n ").concat(addNormalImportRouteExpression(normalImports), "\n\n ").concat(addDynamicImportRouteExpression.call(this, dynamicImports), ";\n\n ").concat(addRequireRouteExpression.call(this, requireRoutes), "\n export default staticConfig;\n "); } exports.default = default_1; function getImportComponentInfo(appConfig, target) { var _this = this; var dynamicImports = []; var normalImports = []; var requireRoutes = []; if (target === 'web') { appConfig.routes.forEach(function (route) { var _a = route.lazy, lazy = _a === void 0 ? true : _a; if (lazy) { dynamicImports.push(route); } else { normalImports.push(route); } }); } else { requireRoutes = appConfig.routes; } var normalImportExpression = normalImports.reduce(function (curr, next) { // import Home from 'source'; return "".concat(curr, "\n import ").concat(getComponentName(next), " from '").concat(getPagePathByRoute(next, { rootContext: _this.rootContext }), "';"); }, ''); return { normalImportExpression: normalImportExpression, normalImports: normalImports, dynamicImports: dynamicImports, requireRoutes: requireRoutes, }; } function addDynamicImportRouteExpression(dynamicImports) { var _this = this; var expression = ''; dynamicImports.forEach(function (route) { expression += "staticConfig.routes.push({\n ...".concat(JSON.stringify(route), ",\n lazy: true,\n component: () => import(/* webpackChunkName: \"").concat(getComponentName(route).toLowerCase(), ".chunk\" */ '").concat(getPagePathByRoute(route, { rootContext: _this.rootContext }), "')\n .then((mod) => mod.default || mod)\n });"); }); return expression; } function addNormalImportRouteExpression(normalImports) { var expression = ''; normalImports.forEach(function (route) { expression += "staticConfig.routes.push({\n ...".concat(JSON.stringify(route), ",\n component: ").concat(getComponentName(route), ",\n });"); }); return expression; } function addRequireRouteExpression(normalImports) { var _this = this; var expression = ''; normalImports.forEach(function (route) { expression += "staticConfig.routes.push({\n ...".concat(JSON.stringify(route), ",\n component: () => require('").concat(getPagePathByRoute(route, { rootContext: _this.rootContext }), "').default,\n });"); }); return expression; } function getComponentName(route) { if (!route.path) { throw new Error("There need path field in this route: ".concat(route)); } if (route.path === '/') return 'Index'; // /about => About // /list-a => List_a // /index.html => Index_html // /pages/home => Pages_home return "".concat(route.path[1].toUpperCase()).concat(route.path.substr(2).replace(/(-|\.|\/)/g, '_')); } function transformAppConfig(jsonContent) { var _a; var appConfig = JSON.parse(jsonContent); if ((_a = appConfig.tabBar) === null || _a === void 0 ? void 0 : _a.items) { appConfig.tabBar.items = appConfig.tabBar.items.map(function (item) { var path = item.path, name = item.name, text = item.text, pageName = item.pageName, otherConfigs = __rest(item, ["path", "name", "text", "pageName"]); return __assign(__assign({}, otherConfigs), { text: text || name, pageName: pageName || path }); }); } return appConfig; } function formatRoutes(routes, _a) { var target = _a.target, currentSubDir = _a.currentSubDir, rootContext = _a.rootContext, isRootAppJsonPath = _a.isRootAppJsonPath; return filterByTarget(routes, { target: target }) .filter(function (_a) { var source = _a.source; // Only filter miniapp native page return !constants_1.MINIAPP_PLATFORMS.includes(target) || !miniapp_builder_shared_1.pathHelper.isNativePage((0, path_1.join)(currentSubDir, source), target); }) .map(function (route) { if (isRootAppJsonPath) return route; if (route.pageSource) { return __assign(__assign({}, route), { source: formatSourcePath(route.pageSource, { rootContext: rootContext }) }); } return __assign(__assign({}, route), { source: formatSourcePath((0, path_1.join)(currentSubDir, route.source), { rootContext: rootContext }) }); }); } function filterByTarget(routes, _a) { var target = _a.target; return routes.filter(function (_a) { var targets = _a.targets; if (!targets) return true; return targets.includes(target); }); } function formatSourcePath(filepath, _a) { var rootContext = _a.rootContext; return (0, app_helpers_1.formatPath)(filepath).replace((0, app_helpers_1.formatPath)("".concat(rootContext, "/src/")), ''); } function getPagePathByRoute(_a, _b) { var source = _a.source, pageSource = _a.pageSource; var rootContext = _b.rootContext; return (0, app_helpers_1.formatPath)(pageSource || (0, path_1.join)(rootContext, 'src', source)); } //# sourceMappingURL=RouteLoader.js.map