@wgoo/cli
Version:
Wgoo Cli 是一个 React 组件库构建工具,通过 Wgoo Cli 可以快速搭建一套功能完备的 React 组件库。
96 lines (82 loc) • 1.92 kB
JavaScript
import DemoHome from './components/DemoHome.jsx';
import { decamelize } from '../common';
import { demos, config } from 'site-mobile-shared';
import { getLang, setDefaultLang } from '../common/locales';
import '../common/iframe-router';
const { locales, defaultLang } = config.site;
setDefaultLang(defaultLang);
function getLangFromRoute(route) {
const lang = route.path.split('/')[1];
const langs = Object.keys(locales);
if (langs.indexOf(lang) !== -1) {
return lang;
}
return getLang();
}
function getRoutes() {
const routes = [];
const names = Object.keys(demos);
const langs = locales ? Object.keys(locales) : [];
if (langs.length) {
routes.push({
name: 'NotFound',
path: '/:path(.*)+',
redirect: (route) => ({
name: getLangFromRoute(route),
}),
});
langs.forEach((lang) => {
routes.push({
name: lang,
path: `/${lang}`,
component: DemoHome,
meta: { lang },
});
});
} else {
routes.push({
name: 'NotFound',
path: '/:path(.*)+',
redirect: {
name: 'home',
},
});
routes.push({
name: 'home',
path: '/',
hash: '#/',
component: DemoHome,
});
}
names.forEach((name) => {
const component = decamelize(name);
if (langs.length) {
langs.forEach((lang) => {
routes.push({
name: `${lang}/${component}`,
path: `/${lang}/${component}`,
hash: `#/${lang}/${component}`,
component: demos[name],
meta: {
name,
lang,
},
});
});
} else {
routes.push({
name: component,
path: `/${component}`,
hash: `#/${component}`,
component: demos[name],
meta: {
name,
},
});
}
});
return routes;
}
export const router = {
routes: getRoutes()
}