add find app method

This commit is contained in:
Andrey Vlasov 2020-03-28 12:37:08 +03:00
parent c99f5a101a
commit 485fcad15c
3 changed files with 77 additions and 9 deletions

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -18,24 +18,91 @@ const systemJSImport = async (requestUrl: string) => {
return { component, mount, unmount }
}
const getPublicPath = (apps, path) => {
const searchKey = path.substr(1)
return `./${searchKey}/${apps[searchKey].version}/index.js`
class Apps extends Map {
constructor(apps) {
super()
this.merge(apps)
}
merge(apps) {
(Object.entries(
apps
).forEach(([path, options]) =>
this.set(path, options)
))
}
startWithPath(path, subPath) {
if (!subPath) {
return true;
}
const pathItem = String(path).split('/');
const subPathItems = String(subPath).split('/')
return subPathItems.reduce(
(memo, appItem, index) => memo && pathItem[index] === appItem,
true
)
}
findApp = (path) => {
const currentPath = [...this.keys()].reduce(
(memo, appRoute) => {
const correctedAppRoute = appRoute.replace(/^\/?/, '/')
const correctedMemo = memo ? memo.replace(/^\/?/, '/') : memo
return this.startWithPath(path, correctedAppRoute) && this.startWithPath(correctedAppRoute, correctedMemo)
? appRoute
: memo
}, void 0
)
return (
this.get(currentPath) || this.get('/') || {
version: ''
}
)
}
}
export default async ({ apps, navigations, config }) => {
const defaultNavigations = {
login: '/login',
main: '/main',
news: '/news',
org: '/org',
sections: '/sections',
'news.details': '/news/{{id}}',
'org.details': '/org/{{name}}',
'sections.details': '/sections/{{name}}'
}
const defaultApps = {
login: { version: '1.0.0' },
main: { version: '1.0.1', name: 'main' },
news: { version: '1.0.0' },
org: { version: '1.0.0' },
sections: { version: '1.0.0' }
}
export default async ({ rawapps, navigations, config }) => {
const apps = new Apps(rawapps)
const history = createBrowserHistory()
let prevPathname = window.location.pathname
const publicPath = getPublicPath(apps, history.location.pathname)
const app = apps.findApp(history.location.pathname)
const publicPath = `./${app.name}/${app.version}`
__webpack_public_path__ = `${publicPath}${__webpack_public_path__}`
const app = `${publicPath}/index.js`
const appPath = `${config.baseUrl}${publicPath}/index.js`
const { component, mount, unmount } = await systemJSImport(app);
const { component, mount, unmount } = await systemJSImport(appPath);
mount(component.default);

View File

@ -16,9 +16,10 @@
"outDir": "./dist/",
"sourceMap": true,
"esModuleInterop": true,
"downlevelIteration" :true,
"noImplicitAny": false,
"module": "commonjs",
"target": "es5",
"target": "es6",
"jsx": "react",
"typeRoots": ["node_modules/@types", "src/typings", "../../node_modules/@types"]
},