UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

213 lines (169 loc) 6.23 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _ssrWindow = require("ssr-window"); var _utils = require("../../common/utils"); var _regExp = require("../../common/regExp"); var _constants = require("../../common/constants"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var window = (0, _ssrWindow.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 = (0, _utils.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 = (0, _utils.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; Promise.resolve("/src/" + _constants.PAGE_DIR + "/" + pathToPage + fileName).then(function (s) { return _interopRequireWildcard(require(s)); }).then(function (res) { resolve({ component: res.default }); }); } }; } var _toRoutePath2 = (0, _utils.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; Promise.resolve("/src/" + _constants.PAGE_DIR + "/" + pathToPage + fileName).then(function (s) { return _interopRequireWildcard(require(s)); }).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; Promise.resolve("/src/" + _constants.PAGE_DIR + "/index").then(function (s) { return _interopRequireWildcard(require(s)); }).then(function (res) { resolve({ component: res.default }); }); } }].concat(routes); } } return routes; }; var _default = function _default() { var routes; if (window.APP_CONFIG) { routes = window.APP_CONFIG.pages; } if (routes) { var isOldConfig = false; if (routes.length && routes[0] && _regExp.oldRouteConfig.test(routes[0])) { isOldConfig = true; } routes = routes.map(function (route) { return (0, _utils.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; }; exports.default = _default;