zmp-vue
Version:
Build full featured iOS & Android apps using ZMP & Vue
124 lines (98 loc) • 2.86 kB
JavaScript
import { getWindow } from 'ssr-window';
import { resolvePath, toRoutePath, standardizePageDir } from '../../common/utils';
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 initRoutes = function initRoutes(routes) {
var appRoutes;
if (routes && importPages) {
appRoutes = routes.map(function (path, index) {
var _toRoutePath = toRoutePath(path),
routePath = _toRoutePath.path,
fileDir = _toRoutePath.fileDir;
var alias;
if (routePath) {
alias = routePath.substring(0, routePath.length - 1);
}
var routeObj = {
path: routePath,
asyncComponent: importPages[fileDir]
};
if (alias) {
routeObj.alias = alias;
}
return routeObj;
});
appRoutes = [{
path: '/',
name: 'home',
asyncComponent: importPages.index
}].concat(appRoutes);
}
return appRoutes;
};
var initDevRouter = function initDevRouter(routes) {
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('/') + "/";
}
var _toRoutePath2 = toRoutePath(path),
routePath = _toRoutePath2.path;
var alias;
if (routePath) {
alias = routePath.substring(0, routePath.length - 1);
}
var routeObj = {
path: routePath,
asyncComponent: function asyncComponent() {
return import(
/* @vite-ignore */
"/src/" + PAGE_DIR + "/" + pathToPage + fileName + ".vue");
}
};
if (alias) {
routeObj.alias = alias;
}
return routeObj;
});
routes = [{
path: '/',
asyncComponent: function asyncComponent() {
return import(
/* @vite-ignore */
"/src/" + PAGE_DIR + "/index.vue");
}
}].concat(routes);
}
return routes;
};
export default (function () {
var routes;
if (window.APP_CONFIG) {
routes = window.APP_CONFIG.pages;
}
if (routes) {
routes = routes.map(function (route) {
return standardizePageDir(route);
}); // eslint-disable-next-line no-restricted-globals
if (process.env.NODE_ENV === 'production') {
routes = initRoutes(routes);
} else {
routes = initDevRouter(routes);
}
}
return routes;
});