UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

198 lines (163 loc) 4.74 kB
import { getWindow } from 'ssr-window'; import { resolvePath, toRoutePath, standardizePageDir } from '../../common/utils'; import { oldRouteConfig } from '../../common/regExp'; import { PAGE_DIR } from '../../common/constants'; var window = getWindow(); // eslint-disable-next-line no-var var importPages; try { if (process.env.NODE_ENV === 'production') { // eslint-disable-next-line no-undef importPages = ZMP_IMPORT_PAGES; } // eslint-disable-next-line no-empty } catch (err) {} var loadAsyncRoute = function loadAsyncRoute(context, fileDir) { var resolve = context.resolve; if (!importPages || !importPages[fileDir]) { throw Error("Cannot find /" + fileDir); } importPages[fileDir]().then(function (res) { resolve({ component: res.default }); }); }; var initRoutes = function initRoutes(routes, isOldConfig) { var appRoutes; if (routes && importPages) { appRoutes = routes.map(function (path, index) { var _toRoutePath = toRoutePath(path), routePath = _toRoutePath.path, fileDir = _toRoutePath.fileDir; if (isOldConfig && index === 0) { return { path: '/', name: 'home', async: function async(context) { return loadAsyncRoute(context, fileDir); } }; } var alias; if (routePath) { alias = routePath.substring(0, routePath.length - 1); } var routeObj = { path: routePath, async: function async(context) { return loadAsyncRoute(context, fileDir); } }; if (alias) { routeObj.alias = alias; } return routeObj; }); if (!isOldConfig) { appRoutes = [{ path: '/', name: 'home', async: function async(context) { return loadAsyncRoute(context, 'index'); } }].concat(appRoutes); } } return appRoutes; }; var initDevRouter = function initDevRouter(routes, isOldConfig) { if (routes) { // eslint-disable-next-line no-param-reassign routes = routes.map(function (path, index) { var _resolvePath = resolvePath(path), _resolvePath$dir = _resolvePath.dir, dir = _resolvePath$dir === void 0 ? [] : _resolvePath$dir, fileName = _resolvePath.fileName; var pathToPage = ''; if (dir.length) { pathToPage = dir.join('/') + "/"; } if (isOldConfig && index === 0) { return { path: '/', name: 'home', async: function async(_ref) { var resolve = _ref.resolve; import( /* @vite-ignore */ "/src/" + PAGE_DIR + "/" + pathToPage + fileName).then(function (res) { resolve({ component: res.default }); }); } }; } var _toRoutePath2 = toRoutePath(path), routePath = _toRoutePath2.path; var alias; if (routePath) { alias = routePath.substring(0, routePath.length - 1); } var routeObj = { path: routePath, async: function async(_ref2) { var resolve = _ref2.resolve; import( /* @vite-ignore */ "/src/" + PAGE_DIR + "/" + pathToPage + fileName).then(function (res) { resolve({ component: res.default }); }); } }; if (alias) { routeObj.alias = alias; } return routeObj; }); if (!isOldConfig) { // eslint-disable-next-line no-param-reassign routes = [{ path: '/', name: 'home', async: function async(_ref3) { var resolve = _ref3.resolve; import( /* @vite-ignore */ "/src/" + PAGE_DIR + "/index").then(function (res) { resolve({ component: res.default }); }); } }].concat(routes); } } return routes; }; export default (function () { var routes; if (window.APP_CONFIG) { routes = window.APP_CONFIG.pages; } if (routes) { var isOldConfig = false; if (routes.length && routes[0] && oldRouteConfig.test(routes[0])) { isOldConfig = true; } routes = routes.map(function (route) { return standardizePageDir(route); }); // eslint-disable-next-line no-restricted-globals if (process.env.NODE_ENV === 'production' && !process.env.previewOnZalo) { try { routes = initRoutes(routes, isOldConfig); } catch (err) { throw Error('An error occurred while resolving pages'); } } else { routes = initDevRouter(routes, isOldConfig); } } return routes; });