mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
1. 路由支持模糊匹配,例如"pages/*.vue"解析成"*",通过模糊匹配可以实现404 2.根据精准匹配优先原则实现智能路由 3. pages下的名称为componets的文件夹被忽略,其中的vue文件不会被解析成路由 re #32
49 lines
1.6 KiB
JavaScript
49 lines
1.6 KiB
JavaScript
const render = require('json-templater/string');
|
|
const path = require('path');
|
|
const endOfLine = require('os').EOL;
|
|
const fs = require('fs-extra');
|
|
const getRoute = require('../preComplie/route');
|
|
|
|
function generateRoute(config) {
|
|
const OUTPUT_PATH = path.resolve(config.folders.PROJECT_CACHE_DIR, 'routeConfig.js');
|
|
const IMPORT_TEMPLATE = 'import {{name}} from \'{{path}}\';';
|
|
|
|
const MAIN_TEMPLATE = `
|
|
{{include}}
|
|
|
|
export default {{routes}};
|
|
`;
|
|
|
|
const { components, routes } = getRoute(config.folders.PROJECT_PAGE_DIR);
|
|
|
|
const componentsTemplate = [];
|
|
let template = '';
|
|
if (config.lazyRouter) {
|
|
const componentsObj = {};
|
|
components.forEach((item) => {
|
|
componentsObj[item.name] = item.path;
|
|
});
|
|
// component: () => import( /* webpackChunkName: "home" */ '../views/Home.vue')
|
|
template = render(MAIN_TEMPLATE, {
|
|
include: '',
|
|
routes: JSON.stringify(routes).replace(/"component":"(.+?)"/g, ($0, $1) => `"component": () => import( /* webpackChunkName: "${$1}" */ '${componentsObj[$1]}')`)
|
|
});
|
|
} else {
|
|
components.forEach((item) => {
|
|
componentsTemplate.push(render(IMPORT_TEMPLATE, {
|
|
name: item.name,
|
|
path: item.path
|
|
}));
|
|
});
|
|
|
|
template = render(MAIN_TEMPLATE, {
|
|
include: componentsTemplate.join(endOfLine),
|
|
routes: JSON.stringify(routes).replace(/"component":"(.+?)"/g, '"component": $1')
|
|
});
|
|
}
|
|
|
|
fs.outputFileSync(OUTPUT_PATH, template);
|
|
}
|
|
|
|
module.exports = generateRoute;
|