diff --git a/dist/index.js b/dist/index.js index d34d62a..823798e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -233,7 +233,7 @@ eval("var g;\n\n// This works in non-strict mode\ng = (function() {\n\treturn th /***/ (function(module, exports, __webpack_require__) { "use strict"; -eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (_) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\n__webpack_require__(/*! systemjs/dist/s */ \"./node_modules/systemjs/dist/s.js\");\n__webpack_require__(/*! systemjs/dist/extras/amd */ \"./node_modules/systemjs/dist/extras/amd.js\");\n__webpack_require__(/*! systemjs/dist/extras/named-register */ \"./node_modules/systemjs/dist/extras/named-register.js\");\n__webpack_require__(/*! systemjs/dist/extras/named-exports */ \"./node_modules/systemjs/dist/extras/named-exports.js\");\n__webpack_require__(/*! systemjs/dist/extras/transform */ \"./node_modules/systemjs/dist/extras/transform.js\");\nvar history_1 = __webpack_require__(/*! history */ \"./node_modules/history/esm/history.js\");\nvar systemJSImport = function (requestUrl) { return __awaiter(void 0, void 0, void 0, function () {\n var _a, component, mount, unmount;\n return __generator(this, function (_b) {\n switch (_b.label) {\n case 0: return [4 /*yield*/, System.import(requestUrl)];\n case 1:\n _a = _b.sent(), component = _a.default, mount = _a.mount, unmount = _a.unmount;\n return [2 /*return*/, { component: component, mount: mount, unmount: unmount }];\n }\n });\n}); };\nvar getApp = function (apps, path) {\n var searchKey = path.substr(1);\n return \"./\" + searchKey + \"/\" + apps[searchKey].version + \"/index.js\";\n};\nexports.default = (function (_a) {\n var apps = _a.apps, navigations = _a.navigations, config = _a.config;\n return __awaiter(void 0, void 0, void 0, function () {\n var history, prevPathname, app, _b, component, mount, unmount;\n return __generator(this, function (_c) {\n switch (_c.label) {\n case 0:\n history = history_1.createBrowserHistory();\n prevPathname = window.location.pathname;\n app = getApp(apps, history.location.pathname);\n return [4 /*yield*/, systemJSImport(app)];\n case 1:\n _b = _c.sent(), component = _b.component, mount = _b.mount, unmount = _b.unmount;\n mount(component.default);\n history.listen(function (location) {\n if (location.pathname !== prevPathname) {\n prevPathname = location.pathname;\n unmount();\n }\n });\n return [2 /*return*/];\n }\n });\n });\n});\n\n\n//# sourceURL=webpack://bootstrap/./src/main.ts?"); +eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\n__webpack_require__(/*! systemjs/dist/s */ \"./node_modules/systemjs/dist/s.js\");\n__webpack_require__(/*! systemjs/dist/extras/amd */ \"./node_modules/systemjs/dist/extras/amd.js\");\n__webpack_require__(/*! systemjs/dist/extras/named-register */ \"./node_modules/systemjs/dist/extras/named-register.js\");\n__webpack_require__(/*! systemjs/dist/extras/named-exports */ \"./node_modules/systemjs/dist/extras/named-exports.js\");\n__webpack_require__(/*! systemjs/dist/extras/transform */ \"./node_modules/systemjs/dist/extras/transform.js\");\nconst history_1 = __webpack_require__(/*! history */ \"./node_modules/history/esm/history.js\");\nconst systemJSImport = (requestUrl) => __awaiter(void 0, void 0, void 0, function* () {\n const { default: component, mount, unmount } = yield System.import(requestUrl);\n return { component, mount, unmount };\n});\nclass Apps extends Map {\n constructor(apps) {\n super();\n this.findApp = (path) => {\n const currentPath = [...this.keys()].reduce((memo, appRoute) => {\n const correctedAppRoute = appRoute.replace(/^\\/?/, '/');\n const correctedMemo = memo ? memo.replace(/^\\/?/, '/') : memo;\n return this.startWithPath(path, correctedAppRoute) && this.startWithPath(correctedAppRoute, correctedMemo)\n ? appRoute\n : memo;\n }, void 0);\n return (this.get(currentPath) || this.get('/') || {\n version: ''\n });\n };\n this.merge(apps);\n }\n merge(apps) {\n (Object.entries(apps).forEach(([path, options]) => this.set(path, options)));\n }\n startWithPath(path, subPath) {\n if (!subPath) {\n return true;\n }\n const pathItem = String(path).split('/');\n const subPathItems = String(subPath).split('/');\n return subPathItems.reduce((memo, appItem, index) => memo && pathItem[index] === appItem, true);\n }\n}\nconst defaultNavigations = {\n login: '/login',\n main: '/main',\n news: '/news',\n org: '/org',\n sections: '/sections',\n 'news.details': '/news/{{id}}',\n 'org.details': '/org/{{name}}',\n 'sections.details': '/sections/{{name}}'\n};\nconst defaultApps = {\n login: { version: '1.0.0' },\n main: { version: '1.0.1', name: 'main' },\n news: { version: '1.0.0' },\n org: { version: '1.0.0' },\n sections: { version: '1.0.0' }\n};\nexports.default = ({ rawapps = defaultApps, navigations = defaultNavigations, config }) => __awaiter(void 0, void 0, void 0, function* () {\n const apps = new Apps(rawapps);\n const history = history_1.createBrowserHistory();\n let prevPathname = window.location.pathname;\n const app = apps.findApp(history.location.pathname);\n // console.log('publicPath', publicPath)\n const publicPath = `./${app.name}/${app.version}`;\n __webpack_require__.p = `${publicPath}${__webpack_require__.p}`;\n const appPath = `${config.baseUrl}/${publicPath}/index.js`;\n const { component, mount, unmount } = yield systemJSImport(appPath);\n mount(component.default);\n history.listen((location) => {\n if (location.pathname !== prevPathname) {\n prevPathname = location.pathname;\n unmount();\n }\n });\n});\n\n\n//# sourceURL=webpack://bootstrap/./src/main.ts?"); /***/ }) diff --git a/src/main.ts b/src/main.ts index 95d0fbf..d0d63a5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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); diff --git a/tsconfig.json b/tsconfig.json index ca61d18..69bbda2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"] },