add virtual module

This commit is contained in:
Andrey Vlasov
2020-03-28 23:03:10 +03:00
parent a907153d8b
commit 6975f2b973
8 changed files with 113 additions and 99 deletions

48
src/apps.ts Normal file
View File

@@ -0,0 +1,48 @@
export 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: ''
}
)
}
}

View File

@@ -4,12 +4,8 @@ import "systemjs/dist/extras/named-register";
import "systemjs/dist/extras/named-exports";
import "systemjs/dist/extras/transform";
import { createBrowserHistory } from 'history'
declare var System: {
import(string): Promise<any>
}
declare var __webpack_public_path__: string;
import { Apps } from './apps'
import { defineVirtualModule } from './virtual-module'
const systemJSImport = async (requestUrl: string) => {
const { default: component, mount, unmount } = await System.import(
@@ -18,85 +14,37 @@ const systemJSImport = async (requestUrl: string) => {
return { component, mount, unmount }
}
class Apps extends Map {
// const defaultNavigations = {
// login: '/login',
// main: '/main',
// news: '/news',
// org: '/org',
// sections: '/sections',
// 'news.details': '/news/{{id}}',
// 'org.details': '/org/{{name}}',
// 'sections.details': '/sections/{{name}}'
// }
constructor(apps) {
super()
this.merge(apps)
}
// const defaultApps = {
// login: { version: '1.0.0', name: 'login' },
// main: { version: '1.0.1', name: 'main' },
// news: { version: '1.0.0', name: 'news' },
// org: { version: '1.0.0', name: 'org' },
// sections: { version: '1.0.0', name: 'sections' }
// }
merge(apps) {
(Object.entries(
apps
).forEach(([path, options]) =>
this.set(path, options)
))
}
export default async ({ apps: rawApps, navigations, config }) => {
defineVirtualModule({navigations, config})
startWithPath(path, subPath) {
if (!subPath) {
return true;
}
const apps = new Apps(rawApps)
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: ''
}
)
}
}
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', name: 'login' },
main: { version: '1.0.1', name: 'main' },
news: { version: '1.0.0', name: 'news' },
org: { version: '1.0.0', name: 'org' },
sections: { version: '1.0.0', name: 'sections' }
}
export default async ({ apps: rowApps, navigations, config }) => {
const apps = new Apps(rowApps)
const history = createBrowserHistory()
let prevPathname = window.location.pathname
const app = apps.findApp(history.location.pathname)
const publicPath = `/${app.name}/${app.version}`
const publicPath = `/${app.name}/${app.version}`
__webpack_public_path__ = `${config.baseUrl}${publicPath}${__webpack_public_path__}`
@@ -106,7 +54,6 @@ export default async ({ apps: rowApps, navigations, config }) => {
mount(component.default);
history.listen((location) => {
if (location.pathname !== prevPathname) {
prevPathname = location.pathname

View File

@@ -16,4 +16,6 @@ declare module '*.png' {
declare module '*.mp4' {
const value: any;
export = value;
}
}
declare var __webpack_public_path__: string;

12
src/virtual-module.ts Normal file
View File

@@ -0,0 +1,12 @@
export const defineVirtualModule = (params) => {
const virtualModule = createVirtualModule(params)
// @ts-ignore
global.define('root.scope.env', [], virtualModule)
}
const createVirtualModule = ({config, navigations}) => ({
getConfig: () => config,
getConfigValue: (key) => config[key],
getNavigations: () => navigations,
getNavigationsValue: (key) => navigations[key],
})