UNPKG

single-spa

Version:

The router for easy microfrontends

1 lines 110 kB
{"version":3,"file":"single-spa.min.js","sources":["../../../node_modules/.pnpm/custom-event@1.0.1/node_modules/custom-event/index.js","../../../src/applications/app-errors.js","../../../src/applications/app.helpers.js","../../../src/utils/assign.js","../../../src/utils/find.js","../../../src/lifecycles/lifecycle.helpers.js","../../../src/devtools/profiler.js","../../../src/lifecycles/bootstrap.js","../../../src/lifecycles/unmount.js","../../../src/lifecycles/mount.js","../../../src/parcels/mount-parcel.js","../../../src/lifecycles/update.js","../../../src/lifecycles/prop.helpers.js","../../../src/applications/timeouts.js","../../../src/lifecycles/load.js","../../../src/utils/runtime-environment.js","../../../src/navigation/navigation-events.js","../../../src/jquery-support.js","../../../src/lifecycles/unload.js","../../../src/applications/apps.js","../../../src/navigation/reroute.js","../../../src/start.js","../../../src/devtools/devtools.js","../../../src/single-spa.js"],"sourcesContent":["\nvar NativeCustomEvent = global.CustomEvent;\n\nfunction useNative () {\n try {\n var p = new NativeCustomEvent('cat', { detail: { foo: 'bar' } });\n return 'cat' === p.type && 'bar' === p.detail.foo;\n } catch (e) {\n }\n return false;\n}\n\n/**\n * Cross-browser `CustomEvent` constructor.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent.CustomEvent\n *\n * @public\n */\n\nmodule.exports = useNative() ? NativeCustomEvent :\n\n// IE >= 9\n'undefined' !== typeof document && 'function' === typeof document.createEvent ? function CustomEvent (type, params) {\n var e = document.createEvent('CustomEvent');\n if (params) {\n e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail);\n } else {\n e.initCustomEvent(type, false, false, void 0);\n }\n return e;\n} :\n\n// IE <= 8\nfunction CustomEvent (type, params) {\n var e = document.createEventObject();\n e.type = type;\n if (params) {\n e.bubbles = Boolean(params.bubbles);\n e.cancelable = Boolean(params.cancelable);\n e.detail = params.detail;\n } else {\n e.bubbles = false;\n e.cancelable = false;\n e.detail = void 0;\n }\n return e;\n}\n","import { objectType, toName } from \"./app.helpers\";\n\nlet errorHandlers = [];\n\nexport function handleAppError(err, app, newStatus) {\n const transformedErr = transformErr(err, app, newStatus);\n\n if (errorHandlers.length) {\n errorHandlers.forEach((handler) => handler(transformedErr));\n } else {\n setTimeout(() => {\n throw transformedErr;\n });\n }\n}\n\nexport function addErrorHandler(handler) {\n if (typeof handler !== \"function\") {\n throw Error(\n formatErrorMessage(\n 28,\n __DEV__ && \"a single-spa error handler must be a function\"\n )\n );\n }\n\n errorHandlers.push(handler);\n}\n\nexport function removeErrorHandler(handler) {\n if (typeof handler !== \"function\") {\n throw Error(\n formatErrorMessage(\n 29,\n __DEV__ && \"a single-spa error handler must be a function\"\n )\n );\n }\n\n let removedSomething = false;\n errorHandlers = errorHandlers.filter((h) => {\n const isHandler = h === handler;\n removedSomething = removedSomething || isHandler;\n return !isHandler;\n });\n\n return removedSomething;\n}\n\nexport function formatErrorMessage(code, msg, ...args) {\n return `single-spa minified message #${code}: ${\n msg ? msg + \" \" : \"\"\n }See https://single-spa.js.org/error/?code=${code}${\n args.length ? `&arg=${args.join(\"&arg=\")}` : \"\"\n }`;\n}\n\nexport function transformErr(ogErr, appOrParcel, newStatus) {\n const errPrefix = `${objectType(appOrParcel)} '${toName(\n appOrParcel\n )}' died in status ${appOrParcel.status}: `;\n\n let result;\n\n if (ogErr instanceof Error) {\n try {\n ogErr.message = errPrefix + ogErr.message;\n } catch (err) {\n /* Some errors have read-only message properties, in which case there is nothing\n * that we can do.\n */\n }\n result = ogErr;\n } else {\n console.warn(\n formatErrorMessage(\n 30,\n __DEV__ &&\n `While ${appOrParcel.status}, '${toName(\n appOrParcel\n )}' rejected its lifecycle function promise with a non-Error. This will cause stack traces to not be accurate.`,\n appOrParcel.status,\n toName(appOrParcel)\n )\n );\n try {\n result = Error(errPrefix + JSON.stringify(ogErr));\n } catch (err) {\n // If it's not an Error and you can't stringify it, then what else can you even do to it?\n result = ogErr;\n }\n }\n\n result.appOrParcelName = toName(appOrParcel);\n\n // We set the status after transforming the error so that the error message\n // references the state the application was in before the status change.\n appOrParcel.status = newStatus;\n\n return result;\n}\n","import { handleAppError } from \"./app-errors.js\";\n\n// App statuses\nexport const NOT_LOADED = \"NOT_LOADED\";\nexport const LOADING_SOURCE_CODE = \"LOADING_SOURCE_CODE\";\nexport const NOT_BOOTSTRAPPED = \"NOT_BOOTSTRAPPED\";\nexport const BOOTSTRAPPING = \"BOOTSTRAPPING\";\nexport const NOT_MOUNTED = \"NOT_MOUNTED\";\nexport const MOUNTING = \"MOUNTING\";\nexport const MOUNTED = \"MOUNTED\";\nexport const UPDATING = \"UPDATING\";\nexport const UNMOUNTING = \"UNMOUNTING\";\nexport const UNLOADING = \"UNLOADING\";\nexport const LOAD_ERROR = \"LOAD_ERROR\";\nexport const SKIP_BECAUSE_BROKEN = \"SKIP_BECAUSE_BROKEN\";\n\nexport function isActive(app) {\n return app.status === MOUNTED;\n}\n\nexport function shouldBeActive(app) {\n try {\n return app.activeWhen(window.location);\n } catch (err) {\n handleAppError(err, app, SKIP_BECAUSE_BROKEN);\n return false;\n }\n}\n\nexport function toName(app) {\n return app.name;\n}\n\nexport function isParcel(appOrParcel) {\n return Boolean(appOrParcel.unmountThisParcel);\n}\n\nexport function objectType(appOrParcel) {\n return isParcel(appOrParcel) ? \"parcel\" : \"application\";\n}\n","// Object.assign() is not available in IE11. And the babel compiled output for object spread\n// syntax checks a bunch of Symbol stuff and is almost a kb. So this function is the smaller replacement.\nexport function assign() {\n for (let i = arguments.length - 1; i > 0; i--) {\n for (let key in arguments[i]) {\n if (key === \"__proto__\") {\n continue;\n }\n arguments[i - 1][key] = arguments[i][key];\n }\n }\n\n return arguments[0];\n}\n","/* the array.prototype.find polyfill on npmjs.com is ~20kb (not worth it)\n * and lodash is ~200kb (not worth it)\n */\n\nexport function find(arr, func) {\n for (let i = 0; i < arr.length; i++) {\n if (func(arr[i])) {\n return arr[i];\n }\n }\n\n return null;\n}\n","import { find } from \"../utils/find.js\";\nimport { objectType, toName } from \"../applications/app.helpers.js\";\nimport { formatErrorMessage } from \"../applications/app-errors.js\";\n\nexport function validLifecycleFn(fn) {\n return fn && (typeof fn === \"function\" || isArrayOfFns(fn));\n\n function isArrayOfFns(arr) {\n return (\n Array.isArray(arr) && !find(arr, (item) => typeof item !== \"function\")\n );\n }\n}\n\nexport function flattenFnArray(appOrParcel, lifecycle) {\n let fns = appOrParcel[lifecycle] || [];\n fns = Array.isArray(fns) ? fns : [fns];\n if (fns.length === 0) {\n fns = [() => Promise.resolve()];\n }\n\n const type = objectType(appOrParcel);\n const name = toName(appOrParcel);\n\n return function (props) {\n return fns.reduce((resultPromise, fn, index) => {\n return resultPromise.then(() => {\n const thisPromise = fn(props);\n return smellsLikeAPromise(thisPromise)\n ? thisPromise\n : Promise.reject(\n formatErrorMessage(\n 15,\n __DEV__ &&\n `Within ${type} ${name}, the lifecycle function ${lifecycle} at array index ${index} did not return a promise`,\n type,\n name,\n lifecycle,\n index\n )\n );\n });\n }, Promise.resolve());\n };\n}\n\nexport function smellsLikeAPromise(promise) {\n return (\n promise &&\n typeof promise.then === \"function\" &&\n typeof promise.catch === \"function\"\n );\n}\n","let profileEntries = [];\n\nexport function getProfilerData() {\n return profileEntries;\n}\n\n/**\n *\n * @type {'application' | 'parcel' | 'routing'} ProfileType\n *\n * @param {ProfileType} type\n * @param {String} name\n * @param {number} start\n * @param {number} end\n */\nexport function addProfileEntry(\n type,\n name,\n kind,\n start,\n end,\n operationSucceeded\n) {\n profileEntries.push({\n type,\n name,\n start,\n end,\n kind,\n operationSucceeded,\n });\n}\n\nexport function clearProfilerData() {\n profileEntries = [];\n}\n","import {\n NOT_BOOTSTRAPPED,\n BOOTSTRAPPING,\n NOT_MOUNTED,\n SKIP_BECAUSE_BROKEN,\n toName,\n isParcel,\n} from \"../applications/app.helpers.js\";\nimport { reasonableTime } from \"../applications/timeouts.js\";\nimport { handleAppError, transformErr } from \"../applications/app-errors.js\";\nimport { addProfileEntry } from \"../devtools/profiler.js\";\n\nexport function toBootstrapPromise(appOrParcel, hardFail) {\n let startTime, profileEventType;\n\n return Promise.resolve().then(() => {\n if (appOrParcel.status !== NOT_BOOTSTRAPPED) {\n return appOrParcel;\n }\n\n if (__PROFILE__) {\n profileEventType = isParcel(appOrParcel) ? \"parcel\" : \"application\";\n startTime = performance.now();\n }\n\n appOrParcel.status = BOOTSTRAPPING;\n\n if (!appOrParcel.bootstrap) {\n // Default implementation of bootstrap\n return Promise.resolve().then(successfulBootstrap);\n }\n\n return reasonableTime(appOrParcel, \"bootstrap\")\n .then(successfulBootstrap)\n .catch((err) => {\n if (__PROFILE__) {\n addProfileEntry(\n profileEventType,\n toName(appOrParcel),\n \"bootstrap\",\n startTime,\n performance.now(),\n false\n );\n }\n\n if (hardFail) {\n throw transformErr(err, appOrParcel, SKIP_BECAUSE_BROKEN);\n } else {\n handleAppError(err, appOrParcel, SKIP_BECAUSE_BROKEN);\n return appOrParcel;\n }\n });\n });\n\n function successfulBootstrap() {\n appOrParcel.status = NOT_MOUNTED;\n\n if (__PROFILE__) {\n addProfileEntry(\n profileEventType,\n toName(appOrParcel),\n \"bootstrap\",\n startTime,\n performance.now(),\n true\n );\n }\n\n return appOrParcel;\n }\n}\n","import {\n UNMOUNTING,\n NOT_MOUNTED,\n MOUNTED,\n SKIP_BECAUSE_BROKEN,\n toName,\n isParcel,\n} from \"../applications/app.helpers.js\";\nimport { handleAppError, transformErr } from \"../applications/app-errors.js\";\nimport { reasonableTime } from \"../applications/timeouts.js\";\nimport { addProfileEntry } from \"../devtools/profiler.js\";\n\nexport function toUnmountPromise(appOrParcel, hardFail) {\n return Promise.resolve().then(() => {\n if (appOrParcel.status !== MOUNTED) {\n return appOrParcel;\n }\n\n let startTime, profileEventType;\n\n if (__PROFILE__) {\n startTime = performance.now();\n profileEventType = isParcel(appOrParcel) ? \"parcel\" : \"application\";\n }\n\n appOrParcel.status = UNMOUNTING;\n\n const unmountChildrenParcels = Object.keys(appOrParcel.parcels).map(\n (parcelId) => appOrParcel.parcels[parcelId].unmountThisParcel()\n );\n\n let parcelError;\n\n return Promise.all(unmountChildrenParcels)\n .then(unmountAppOrParcel, (parcelError) => {\n // There is a parcel unmount error\n return unmountAppOrParcel().then(() => {\n // Unmounting the app/parcel succeeded, but unmounting its children parcels did not\n const parentError = Error(parcelError.message);\n if (hardFail) {\n throw transformErr(parentError, appOrParcel, SKIP_BECAUSE_BROKEN);\n } else {\n handleAppError(parentError, appOrParcel, SKIP_BECAUSE_BROKEN);\n }\n });\n })\n .then(() => appOrParcel);\n\n function unmountAppOrParcel() {\n // We always try to unmount the appOrParcel, even if the children parcels failed to unmount.\n return reasonableTime(appOrParcel, \"unmount\").then(\n () => {\n // The appOrParcel needs to stay in a broken status if its children parcels fail to unmount\n if (!parcelError) {\n appOrParcel.status = NOT_MOUNTED;\n }\n\n if (__PROFILE__) {\n addProfileEntry(\n profileEventType,\n toName(appOrParcel),\n \"unmount\",\n startTime,\n performance.now(),\n true\n );\n }\n },\n (err) => {\n if (__PROFILE__) {\n addProfileEntry(\n profileEventType,\n toName(appOrParcel),\n \"unmount\",\n startTime,\n performance.now(),\n false\n );\n }\n\n if (hardFail) {\n throw transformErr(err, appOrParcel, SKIP_BECAUSE_BROKEN);\n } else {\n handleAppError(err, appOrParcel, SKIP_BECAUSE_BROKEN);\n }\n }\n );\n }\n });\n}\n","import {\n NOT_MOUNTED,\n MOUNTED,\n SKIP_BECAUSE_BROKEN,\n MOUNTING,\n toName,\n isParcel,\n} from \"../applications/app.helpers.js\";\nimport { handleAppError, transformErr } from \"../applications/app-errors.js\";\nimport { reasonableTime } from \"../applications/timeouts.js\";\nimport CustomEvent from \"custom-event\";\nimport { toUnmountPromise } from \"./unmount.js\";\nimport { addProfileEntry } from \"../devtools/profiler.js\";\n\nlet beforeFirstMountFired = false;\nlet firstMountFired = false;\n\nexport function toMountPromise(appOrParcel, hardFail) {\n return Promise.resolve().then(() => {\n if (appOrParcel.status !== NOT_MOUNTED) {\n return appOrParcel;\n }\n\n let startTime, profileEventType;\n\n if (__PROFILE__) {\n profileEventType = isParcel(appOrParcel) ? \"parcel\" : \"application\";\n startTime = performance.now();\n }\n\n if (!beforeFirstMountFired) {\n window.dispatchEvent(new CustomEvent(\"single-spa:before-first-mount\"));\n beforeFirstMountFired = true;\n }\n\n appOrParcel.status = MOUNTING;\n\n return reasonableTime(appOrParcel, \"mount\")\n .then(() => {\n appOrParcel.status = MOUNTED;\n\n if (!firstMountFired) {\n window.dispatchEvent(new CustomEvent(\"single-spa:first-mount\"));\n firstMountFired = true;\n }\n\n if (__PROFILE__) {\n addProfileEntry(\n profileEventType,\n toName(appOrParcel),\n \"mount\",\n startTime,\n performance.now(),\n true\n );\n }\n\n return appOrParcel;\n })\n .catch((err) => {\n // If we fail to mount the appOrParcel, we should attempt to unmount it before putting in SKIP_BECAUSE_BROKEN\n // We temporarily put the appOrParcel into MOUNTED status so that toUnmountPromise actually attempts to unmount it\n // instead of just doing a no-op.\n appOrParcel.status = MOUNTED;\n return toUnmountPromise(appOrParcel, true).then(\n setSkipBecauseBroken,\n setSkipBecauseBroken\n );\n\n function setSkipBecauseBroken() {\n if (__PROFILE__) {\n addProfileEntry(\n profileEventType,\n toName(appOrParcel),\n \"mount\",\n startTime,\n performance.now(),\n false\n );\n }\n\n if (!hardFail) {\n handleAppError(err, appOrParcel, SKIP_BECAUSE_BROKEN);\n return appOrParcel;\n } else {\n throw transformErr(err, appOrParcel, SKIP_BECAUSE_BROKEN);\n }\n }\n });\n });\n}\n","import {\n validLifecycleFn,\n flattenFnArray,\n} from \"../lifecycles/lifecycle.helpers.js\";\nimport {\n NOT_BOOTSTRAPPED,\n NOT_MOUNTED,\n MOUNTED,\n LOADING_SOURCE_CODE,\n SKIP_BECAUSE_BROKEN,\n toName,\n} from \"../applications/app.helpers.js\";\nimport { toBootstrapPromise } from \"../lifecycles/bootstrap.js\";\nimport { toMountPromise } from \"../lifecycles/mount.js\";\nimport { toUpdatePromise } from \"../lifecycles/update.js\";\nimport { toUnmountPromise } from \"../lifecycles/unmount.js\";\nimport { ensureValidAppTimeouts } from \"../applications/timeouts.js\";\nimport { formatErrorMessage } from \"../applications/app-errors.js\";\n\nlet parcelCount = 0;\nconst rootParcels = { parcels: {} };\n\n// This is a public api, exported to users of single-spa\nexport function mountRootParcel() {\n return mountParcel.apply(rootParcels, arguments);\n}\n\nexport function mountParcel(config, customProps) {\n const owningAppOrParcel = this;\n\n // Validate inputs\n if (!config || (typeof config !== \"object\" && typeof config !== \"function\")) {\n throw Error(\n formatErrorMessage(\n 2,\n __DEV__ &&\n \"Cannot mount parcel without a config object or config loading function\"\n )\n );\n }\n\n if (config.name && typeof config.name !== \"string\") {\n throw Error(\n formatErrorMessage(\n 3,\n __DEV__ &&\n `Parcel name must be a string, if provided. Was given ${typeof config.name}`,\n typeof config.name\n )\n );\n }\n\n const id = parcelCount++;\n let name = config.name || `parcel-${id}`;\n\n if (typeof customProps !== \"object\") {\n throw Error(\n formatErrorMessage(\n 4,\n __DEV__ &&\n `Parcel ${name} has invalid customProps -- must be an object but was given ${typeof customProps}`,\n name,\n typeof customProps\n )\n );\n }\n\n if (!customProps.domElement) {\n throw Error(\n formatErrorMessage(\n 5,\n __DEV__ &&\n `Parcel ${name} cannot be mounted without a domElement provided as a prop`,\n name\n )\n );\n }\n\n const passedConfigLoadingFunction = typeof config === \"function\";\n const configLoadingFunction = passedConfigLoadingFunction\n ? config\n : () => Promise.resolve(config);\n\n // Internal representation\n const parcel = {\n id,\n parcels: {},\n status: passedConfigLoadingFunction\n ? LOADING_SOURCE_CODE\n : NOT_BOOTSTRAPPED,\n customProps,\n parentName: toName(owningAppOrParcel),\n unmountThisParcel() {\n return mountPromise\n .then(() => {\n if (parcel.status !== MOUNTED) {\n throw Error(\n formatErrorMessage(\n 6,\n __DEV__ &&\n `Cannot unmount parcel '${name}' -- it is in a ${parcel.status} status`,\n name,\n parcel.status\n )\n );\n }\n return toUnmountPromise(parcel, true);\n })\n .then((value) => {\n if (parcel.parentName) {\n delete owningAppOrParcel.parcels[parcel.id];\n }\n\n return value;\n })\n .then((value) => {\n resolveUnmount(value);\n return value;\n })\n .catch((err) => {\n parcel.status = SKIP_BECAUSE_BROKEN;\n rejectUnmount(err);\n throw err;\n });\n },\n };\n\n // We return an external representation\n let externalRepresentation;\n\n // Add to owning app or parcel\n owningAppOrParcel.parcels[id] = parcel;\n\n let loadPromise = configLoadingFunction();\n\n if (!loadPromise || typeof loadPromise.then !== \"function\") {\n throw Error(\n formatErrorMessage(\n 7,\n __DEV__ &&\n `When mounting a parcel, the config loading function must return a promise that resolves with the parcel config`\n )\n );\n }\n\n loadPromise = loadPromise.then((config) => {\n if (!config) {\n throw Error(\n formatErrorMessage(\n 8,\n __DEV__ &&\n `When mounting a parcel, the config loading function returned a promise that did not resolve with a parcel config`\n )\n );\n }\n\n name = config.name || `parcel-${id}`;\n\n if (\n // ES Module objects don't have the object prototype\n Object.prototype.hasOwnProperty.call(config, \"bootstrap\") &&\n !validLifecycleFn(config.bootstrap)\n ) {\n throw Error(\n formatErrorMessage(\n 9,\n __DEV__ && `Parcel ${name} provided an invalid bootstrap function`,\n name\n )\n );\n }\n\n if (!validLifecycleFn(config.mount)) {\n throw Error(\n formatErrorMessage(\n 10,\n __DEV__ && `Parcel ${name} must have a valid mount function`,\n name\n )\n );\n }\n\n if (!validLifecycleFn(config.unmount)) {\n throw Error(\n formatErrorMessage(\n 11,\n __DEV__ && `Parcel ${name} must have a valid unmount function`,\n name\n )\n );\n }\n\n if (config.update && !validLifecycleFn(config.update)) {\n throw Error(\n formatErrorMessage(\n 12,\n __DEV__ && `Parcel ${name} provided an invalid update function`,\n name\n )\n );\n }\n\n const bootstrap = flattenFnArray(config, \"bootstrap\");\n const mount = flattenFnArray(config, \"mount\");\n const unmount = flattenFnArray(config, \"unmount\");\n\n parcel.status = NOT_BOOTSTRAPPED;\n parcel.name = name;\n parcel.bootstrap = bootstrap;\n parcel.mount = mount;\n parcel.unmount = unmount;\n parcel.timeouts = ensureValidAppTimeouts(config.timeouts);\n\n if (config.update) {\n parcel.update = flattenFnArray(config, \"update\");\n externalRepresentation.update = function (customProps) {\n parcel.customProps = customProps;\n\n return promiseWithoutReturnValue(toUpdatePromise(parcel));\n };\n }\n });\n\n // Start bootstrapping and mounting\n // The .then() causes the work to be put on the event loop instead of happening immediately\n const bootstrapPromise = loadPromise.then(() =>\n toBootstrapPromise(parcel, true)\n );\n const mountPromise = bootstrapPromise.then(() =>\n toMountPromise(parcel, true)\n );\n\n let resolveUnmount, rejectUnmount;\n\n const unmountPromise = new Promise((resolve, reject) => {\n resolveUnmount = resolve;\n rejectUnmount = reject;\n });\n\n externalRepresentation = {\n mount() {\n return promiseWithoutReturnValue(\n Promise.resolve().then(() => {\n if (parcel.status !== NOT_MOUNTED) {\n throw Error(\n formatErrorMessage(\n 13,\n __DEV__ &&\n `Cannot mount parcel '${name}' -- it is in a ${parcel.status} status`,\n name,\n parcel.status\n )\n );\n }\n\n // Add to owning app or parcel\n owningAppOrParcel.parcels[id] = parcel;\n\n return toMountPromise(parcel);\n })\n );\n },\n unmount() {\n return promiseWithoutReturnValue(parcel.unmountThisParcel());\n },\n getStatus() {\n return parcel.status;\n },\n loadPromise: promiseWithoutReturnValue(loadPromise),\n bootstrapPromise: promiseWithoutReturnValue(bootstrapPromise),\n mountPromise: promiseWithoutReturnValue(mountPromise),\n unmountPromise: promiseWithoutReturnValue(unmountPromise),\n };\n\n return externalRepresentation;\n}\n\nfunction promiseWithoutReturnValue(promise) {\n return promise.then(() => null);\n}\n","import {\n UPDATING,\n MOUNTED,\n SKIP_BECAUSE_BROKEN,\n toName,\n isParcel,\n} from \"../applications/app.helpers.js\";\nimport {\n transformErr,\n formatErrorMessage,\n} from \"../applications/app-errors.js\";\nimport { reasonableTime } from \"../applications/timeouts.js\";\nimport { addProfileEntry } from \"../devtools/profiler.js\";\n\nexport function toUpdatePromise(appOrParcel) {\n return Promise.resolve().then(() => {\n let startTime, profileEventType;\n\n if (__PROFILE__) {\n profileEventType = isParcel(appOrParcel) ? \"parcel\" : \"application\";\n startTime = performance.now();\n }\n\n if (appOrParcel.status !== MOUNTED) {\n throw Error(\n formatErrorMessage(\n 32,\n __DEV__ &&\n `Cannot update parcel '${toName(\n appOrParcel\n )}' because it is not mounted`,\n toName(appOrParcel)\n )\n );\n }\n\n appOrParcel.status = UPDATING;\n\n return reasonableTime(appOrParcel, \"update\")\n .then(() => {\n appOrParcel.status = MOUNTED;\n\n if (__PROFILE__) {\n addProfileEntry(\n profileEventType,\n toName(appOrParcel),\n \"update\",\n startTime,\n performance.now(),\n true\n );\n }\n\n return appOrParcel;\n })\n .catch((err) => {\n if (__PROFILE__) {\n addProfileEntry(\n profileEventType,\n toName(appOrParcel),\n \"update\",\n startTime,\n performance.now(),\n false\n );\n }\n\n throw transformErr(err, appOrParcel, SKIP_BECAUSE_BROKEN);\n });\n });\n}\n","import * as singleSpa from \"../single-spa.js\";\nimport { mountParcel } from \"../parcels/mount-parcel.js\";\nimport { assign } from \"../utils/assign.js\";\nimport { isParcel, toName } from \"../applications/app.helpers.js\";\nimport { formatErrorMessage } from \"../applications/app-errors.js\";\n\nexport function getProps(appOrParcel) {\n const name = toName(appOrParcel);\n let customProps =\n typeof appOrParcel.customProps === \"function\"\n ? appOrParcel.customProps(name, window.location)\n : appOrParcel.customProps;\n if (\n typeof customProps !== \"object\" ||\n customProps === null ||\n Array.isArray(customProps)\n ) {\n customProps = {};\n console.warn(\n formatErrorMessage(\n 40,\n __DEV__ &&\n `single-spa: ${name}'s customProps function must return an object. Received ${customProps}`\n ),\n name,\n customProps\n );\n }\n const result = assign({}, customProps, {\n name,\n mountParcel: mountParcel.bind(appOrParcel),\n singleSpa,\n });\n\n if (isParcel(appOrParcel)) {\n result.unmountSelf = appOrParcel.unmountThisParcel;\n }\n\n return result;\n}\n","import { assign } from \"../utils/assign\";\nimport { getProps } from \"../lifecycles/prop.helpers\";\nimport { objectType, toName } from \"./app.helpers\";\nimport { formatErrorMessage } from \"./app-errors\";\n\nconst defaultWarningMillis = 1000;\n\nconst globalTimeoutConfig = {\n bootstrap: {\n millis: 4000,\n dieOnTimeout: false,\n warningMillis: defaultWarningMillis,\n },\n mount: {\n millis: 3000,\n dieOnTimeout: false,\n warningMillis: defaultWarningMillis,\n },\n unmount: {\n millis: 3000,\n dieOnTimeout: false,\n warningMillis: defaultWarningMillis,\n },\n unload: {\n millis: 3000,\n dieOnTimeout: false,\n warningMillis: defaultWarningMillis,\n },\n update: {\n millis: 3000,\n dieOnTimeout: false,\n warningMillis: defaultWarningMillis,\n },\n};\n\nexport function setBootstrapMaxTime(time, dieOnTimeout, warningMillis) {\n if (typeof time !== \"number\" || time <= 0) {\n throw Error(\n formatErrorMessage(\n 16,\n __DEV__ &&\n `bootstrap max time must be a positive integer number of milliseconds`\n )\n );\n }\n\n globalTimeoutConfig.bootstrap = {\n millis: time,\n dieOnTimeout,\n warningMillis: warningMillis || defaultWarningMillis,\n };\n}\n\nexport function setMountMaxTime(time, dieOnTimeout, warningMillis) {\n if (typeof time !== \"number\" || time <= 0) {\n throw Error(\n formatErrorMessage(\n 17,\n __DEV__ &&\n `mount max time must be a positive integer number of milliseconds`\n )\n );\n }\n\n globalTimeoutConfig.mount = {\n millis: time,\n dieOnTimeout,\n warningMillis: warningMillis || defaultWarningMillis,\n };\n}\n\nexport function setUnmountMaxTime(time, dieOnTimeout, warningMillis) {\n if (typeof time !== \"number\" || time <= 0) {\n throw Error(\n formatErrorMessage(\n 18,\n __DEV__ &&\n `unmount max time must be a positive integer number of milliseconds`\n )\n );\n }\n\n globalTimeoutConfig.unmount = {\n millis: time,\n dieOnTimeout,\n warningMillis: warningMillis || defaultWarningMillis,\n };\n}\n\nexport function setUnloadMaxTime(time, dieOnTimeout, warningMillis) {\n if (typeof time !== \"number\" || time <= 0) {\n throw Error(\n formatErrorMessage(\n 19,\n __DEV__ &&\n `unload max time must be a positive integer number of milliseconds`\n )\n );\n }\n\n globalTimeoutConfig.unload = {\n millis: time,\n dieOnTimeout,\n warningMillis: warningMillis || defaultWarningMillis,\n };\n}\n\nexport function reasonableTime(appOrParcel, lifecycle) {\n const timeoutConfig = appOrParcel.timeouts[lifecycle];\n const warningPeriod = timeoutConfig.warningMillis;\n const type = objectType(appOrParcel);\n\n return new Promise((resolve, reject) => {\n let finished = false;\n let errored = false;\n\n appOrParcel[lifecycle](getProps(appOrParcel))\n .then((val) => {\n finished = true;\n resolve(val);\n })\n .catch((val) => {\n finished = true;\n reject(val);\n });\n\n setTimeout(() => maybeTimingOut(1), warningPeriod);\n setTimeout(() => maybeTimingOut(true), timeoutConfig.millis);\n\n const errMsg = formatErrorMessage(\n 31,\n __DEV__ &&\n `Lifecycle function ${lifecycle} for ${type} ${toName(\n appOrParcel\n )} lifecycle did not resolve or reject for ${timeoutConfig.millis} ms.`,\n lifecycle,\n type,\n toName(appOrParcel),\n timeoutConfig.millis\n );\n\n function maybeTimingOut(shouldError) {\n if (!finished) {\n if (shouldError === true) {\n errored = true;\n if (timeoutConfig.dieOnTimeout) {\n reject(Error(errMsg));\n } else {\n console.error(errMsg);\n //don't resolve or reject, we're waiting this one out\n }\n } else if (!errored) {\n const numWarnings = shouldError;\n const numMillis = numWarnings * warningPeriod;\n console.warn(errMsg);\n if (numMillis + warningPeriod < timeoutConfig.millis) {\n setTimeout(() => maybeTimingOut(numWarnings + 1), warningPeriod);\n }\n }\n }\n }\n });\n}\n\nexport function ensureValidAppTimeouts(timeouts) {\n const result = {};\n\n for (let key in globalTimeoutConfig) {\n result[key] = assign(\n {},\n globalTimeoutConfig[key],\n (timeouts && timeouts[key]) || {}\n );\n }\n\n return result;\n}\n","import {\n LOAD_ERROR,\n NOT_BOOTSTRAPPED,\n LOADING_SOURCE_CODE,\n SKIP_BECAUSE_BROKEN,\n NOT_LOADED,\n objectType,\n toName,\n} from \"../applications/app.helpers.js\";\nimport { ensureValidAppTimeouts } from \"../applications/timeouts.js\";\nimport {\n handleAppError,\n formatErrorMessage,\n} from \"../applications/app-errors.js\";\nimport {\n flattenFnArray,\n smellsLikeAPromise,\n validLifecycleFn,\n} from \"./lifecycle.helpers.js\";\nimport { getProps } from \"./prop.helpers.js\";\nimport { assign } from \"../utils/assign.js\";\nimport { addProfileEntry } from \"../devtools/profiler.js\";\n\nexport function toLoadPromise(appOrParcel) {\n return Promise.resolve().then(() => {\n if (appOrParcel.loadPromise) {\n return appOrParcel.loadPromise;\n }\n\n if (\n appOrParcel.status !== NOT_LOADED &&\n appOrParcel.status !== LOAD_ERROR\n ) {\n return appOrParcel;\n }\n\n let startTime;\n\n if (__PROFILE__) {\n startTime = performance.now();\n }\n\n appOrParcel.status = LOADING_SOURCE_CODE;\n\n let appOpts, isUserErr;\n\n return (appOrParcel.loadPromise = Promise.resolve()\n .then(() => {\n const loadPromise = appOrParcel.loadApp(getProps(appOrParcel));\n if (!smellsLikeAPromise(loadPromise)) {\n // The name of the app will be prepended to this error message inside of the handleAppError function\n isUserErr = true;\n throw Error(\n formatErrorMessage(\n 33,\n __DEV__ &&\n `single-spa loading function did not return a promise. Check the second argument to registerApplication('${toName(\n appOrParcel\n )}', loadingFunction, activityFunction)`,\n toName(appOrParcel)\n )\n );\n }\n return loadPromise.then((val) => {\n appOrParcel.loadErrorTime = null;\n\n appOpts = val;\n\n let validationErrMessage, validationErrCode;\n\n if (typeof appOpts !== \"object\") {\n validationErrCode = 34;\n if (__DEV__) {\n validationErrMessage = `does not export anything`;\n }\n }\n\n if (\n // ES Modules don't have the Object prototype\n Object.prototype.hasOwnProperty.call(appOpts, \"bootstrap\") &&\n !validLifecycleFn(appOpts.bootstrap)\n ) {\n validationErrCode = 35;\n if (__DEV__) {\n validationErrMessage = `does not export a valid bootstrap function or array of functions`;\n }\n }\n\n if (!validLifecycleFn(appOpts.mount)) {\n validationErrCode = 36;\n if (__DEV__) {\n validationErrMessage = `does not export a mount function or array of functions`;\n }\n }\n\n if (!validLifecycleFn(appOpts.unmount)) {\n validationErrCode = 37;\n if (__DEV__) {\n validationErrMessage = `does not export a unmount function or array of functions`;\n }\n }\n\n const type = objectType(appOpts);\n\n if (validationErrCode) {\n let appOptsStr;\n try {\n appOptsStr = JSON.stringify(appOpts);\n } catch {}\n console.error(\n formatErrorMessage(\n validationErrCode,\n __DEV__ &&\n `The loading function for single-spa ${type} '${toName(\n appOrParcel\n )}' resolved with the following, which does not have bootstrap, mount, and unmount functions`,\n type,\n toName(appOrParcel),\n appOptsStr\n ),\n appOpts\n );\n handleAppError(\n validationErrMessage,\n appOrParcel,\n SKIP_BECAUSE_BROKEN\n );\n return appOrParcel;\n }\n\n if (appOpts.devtools && appOpts.devtools.overlays) {\n appOrParcel.devtools.overlays = assign(\n {},\n appOrParcel.devtools.overlays,\n appOpts.devtools.overlays\n );\n }\n\n appOrParcel.status = NOT_BOOTSTRAPPED;\n appOrParcel.bootstrap = flattenFnArray(appOpts, \"bootstrap\");\n appOrParcel.mount = flattenFnArray(appOpts, \"mount\");\n appOrParcel.unmount = flattenFnArray(appOpts, \"unmount\");\n appOrParcel.unload = flattenFnArray(appOpts, \"unload\");\n appOrParcel.timeouts = ensureValidAppTimeouts(appOpts.timeouts);\n\n delete appOrParcel.loadPromise;\n\n if (__PROFILE__) {\n addProfileEntry(\n \"application\",\n toName(appOrParcel),\n \"load\",\n startTime,\n performance.now(),\n true\n );\n }\n\n return appOrParcel;\n });\n })\n .catch((err) => {\n delete appOrParcel.loadPromise;\n\n let newStatus;\n if (isUserErr) {\n newStatus = SKIP_BECAUSE_BROKEN;\n } else {\n newStatus = LOAD_ERROR;\n appOrParcel.loadErrorTime = new Date().getTime();\n }\n handleAppError(err, appOrParcel, newStatus);\n\n if (__PROFILE__) {\n addProfileEntry(\n \"application\",\n toName(appOrParcel),\n \"load\",\n startTime,\n performance.now(),\n false\n );\n }\n\n return appOrParcel;\n }));\n });\n}\n","export const isInBrowser = typeof window !== \"undefined\";\n","import { reroute } from \"./reroute.js\";\nimport { find } from \"../utils/find.js\";\nimport { formatErrorMessage } from \"../applications/app-errors.js\";\nimport { isInBrowser } from \"../utils/runtime-environment.js\";\n\n/* We capture navigation event listeners so that we can make sure\n * that application navigation listeners are not called until\n * single-spa has ensured that the correct applications are\n * unmounted and mounted.\n */\nconst capturedEventListeners = {\n hashchange: [],\n popstate: [],\n};\n\nexport const routingEventsListeningTo = [\"hashchange\", \"popstate\"];\n\nexport function navigateToUrl(obj) {\n let url;\n if (typeof obj === \"string\") {\n url = obj;\n } else if (this && this.href) {\n url = this.href;\n } else if (\n obj &&\n obj.currentTarget &&\n obj.currentTarget.href &&\n obj.preventDefault\n ) {\n url = obj.currentTarget.href;\n obj.preventDefault();\n } else {\n throw Error(\n formatErrorMessage(\n 14,\n __DEV__ &&\n `singleSpaNavigate/navigateToUrl must be either called with a string url, with an <a> tag as its context, or with an event whose currentTarget is an <a> tag`\n )\n );\n }\n\n const current = parseUri(window.location.href);\n const destination = parseUri(url);\n\n if (url.indexOf(\"#\") === 0) {\n window.location.hash = destination.hash;\n } else if (current.host !== destination.host && destination.host) {\n if (process.env.BABEL_ENV === \"test\") {\n return { wouldHaveReloadedThePage: true };\n } else {\n window.location.href = url;\n }\n } else if (\n destination.pathname === current.pathname &&\n destination.search === current.search\n ) {\n window.location.hash = destination.hash;\n } else {\n // different path, host, or query params\n window.history.pushState(null, null, url);\n }\n}\n\nexport function callCapturedEventListeners(eventArguments) {\n if (eventArguments) {\n const eventType = eventArguments[0].type;\n if (routingEventsListeningTo.indexOf(eventType) >= 0) {\n capturedEventListeners[eventType].forEach((listener) => {\n try {\n // The error thrown by application event listener should not break single-spa down.\n // Just like https://github.com/single-spa/single-spa/blob/85f5042dff960e40936f3a5069d56fc9477fac04/src/navigation/reroute.js#L140-L146 did\n listener.apply(this, eventArguments);\n } catch (e) {\n setTimeout(() => {\n throw e;\n });\n }\n });\n }\n }\n}\n\nlet urlRerouteOnly;\n\nfunction urlReroute() {\n reroute([], arguments);\n}\n\nfunction patchedUpdateState(updateState, methodName) {\n return function () {\n const urlBefore = window.location.href;\n const result = updateState.apply(this, arguments);\n const urlAfter = window.location.href;\n\n if (!urlRerouteOnly || urlBefore !== urlAfter) {\n // fire an artificial popstate event so that\n // single-spa applications know about routing that\n // occurs in a different application\n window.dispatchEvent(\n createPopStateEvent(window.history.state, methodName)\n );\n }\n\n return result;\n };\n}\n\nfunction createPopStateEvent(state, originalMethodName) {\n // https://github.com/single-spa/single-spa/issues/224 and https://github.com/single-spa/single-spa-angular/issues/49\n // We need a popstate event even though the browser doesn't do one by default when you call replaceState, so that\n // all the applications can reroute. We explicitly identify this extraneous event by setting singleSpa=true and\n // singleSpaTrigger=<pushState|replaceState> on the event instance.\n let evt;\n try {\n evt = new PopStateEvent(\"popstate\", { state });\n } catch (err) {\n // IE 11 compatibility https://github.com/single-spa/single-spa/issues/299\n // https://docs.microsoft.com/en-us/openspecs/ie_standards/ms-html5e/bd560f47-b349-4d2c-baa8-f1560fb489dd\n evt = document.createEvent(\"PopStateEvent\");\n evt.initPopStateEvent(\"popstate\", false, false, state);\n }\n evt.singleSpa = true;\n evt.singleSpaTrigger = originalMethodName;\n return evt;\n}\n\nexport let originalReplaceState = null;\n\nlet historyApiIsPatched = false;\n\n// We patch the history API so single-spa is notified of all calls to pushState/replaceState.\n// We patch addEventListener/removeEventListener so we can capture all popstate/hashchange event listeners,\n// and delay calling them until single-spa has finished mounting/unmounting applications\nexport function patchHistoryApi(opts) {\n if (historyApiIsPatched) {\n throw Error(\n formatErrorMessage(\n 43,\n __DEV__ &&\n `single-spa: patchHistoryApi() was called after the history api was already patched.`\n )\n );\n }\n\n // True by default, as a performance optimization that reduces\n // the number of extraneous popstate events\n urlRerouteOnly =\n opts && opts.hasOwnProperty(\"urlRerouteOnly\") ? opts.urlRerouteOnly : true;\n\n historyApiIsPatched = true;\n\n originalReplaceState = window.history.replaceState;\n\n // We will trigger an app change for any routing events.\n window.addEventListener(\"hashchange\", urlReroute);\n window.addEventListener(\"popstate\", urlReroute);\n\n // Monkeypatch addEventListener so that we can ensure correct timing\n const originalAddEventListener = window.addEventListener;\n const originalRemoveEventListener = window.removeEventListener;\n window.addEventListener = function (eventName, fn) {\n if (typeof fn === \"function\") {\n if (\n routingEventsListeningTo.indexOf(eventName) >= 0 &&\n !find(capturedEventListeners[eventName], (listener) => listener === fn)\n ) {\n capturedEventListeners[eventName].push(fn);\n return;\n }\n }\n\n return originalAddEventListener.apply(this, arguments);\n };\n\n window.removeEventListener = function (eventName, listenerFn) {\n if (typeof listenerFn === \"function\") {\n if (routingEventsListeningTo.indexOf(eventName) >= 0) {\n capturedEventListeners[eventName] = capturedEventListeners[\n eventName\n ].filter((fn) => fn !== listenerFn);\n }\n }\n\n return originalRemoveEventListener.apply(this, arguments);\n };\n\n window.history.pushState = patchedUpdateState(\n window.history.pushState,\n \"pushState\"\n );\n window.history.replaceState = patchedUpdateState(\n originalReplaceState,\n \"replaceState\"\n );\n}\n\n// Detect if single-spa has already been loaded on the page.\n// If so, warn because this can result in lots of problems, including\n// lots of extraneous popstate events and unexpected results for\n// apis like getAppNames().\nif (isInBrowser) {\n if (window.singleSpaNavigate) {\n console.warn(\n formatErrorMessage(\n 41,\n __DEV__ &&\n \"single-spa has been loaded twice on the page. This can result in unexpected behavior.\"\n )\n );\n } else {\n /* For convenience in `onclick` attributes, we expose a global function for navigating to\n * whatever an <a> tag's href is.\n */\n window.singleSpaNavigate = navigateToUrl;\n }\n}\n\nfunction parseUri(str) {\n const anchor = document.createElement(\"a\");\n anchor.href = str;\n return anchor;\n}\n","import { routingEventsListeningTo } from \"./navigation/navigation-events.js\";\n\nlet hasInitialized = false;\n\nexport function ensureJQuerySupport(jQuery = window.jQuery) {\n if (!jQuery) {\n if (window.$ && window.$.fn && window.$.fn.jquery) {\n jQuery = window.$;\n }\n }\n\n if (jQuery && !hasInitialized) {\n const originalJQueryOn = jQuery.fn.on;\n const originalJQueryOff = jQuery.fn.off;\n\n jQuery.fn.on = function (eventString, fn) {\n return captureRoutingEvents.call(\n this,\n originalJQueryOn,\n window.addEventListener,\n eventString,\n fn,\n arguments\n );\n };\n\n jQuery.fn.off = function (eventString, fn) {\n return captureRoutingEvents.call(\n this,\n originalJQueryOff,\n window.removeEventListener,\n eventString,\n fn,\n arguments\n );\n };\n\n hasInitialized = true;\n }\n}\n\nfunction captureRoutingEvents(\n originalJQueryFunction,\n nativeFunctionToCall,\n eventString,\n fn,\n originalArgs\n) {\n if (typeof eventString !== \"string\") {\n return originalJQueryFunction.apply(this, originalArgs);\n }\n\n const eventNames = eventString.split(/\\s+/);\n eventNames.forEach((eventName) => {\n if (routingEventsListeningTo.indexOf(eventName) >= 0) {\n nativeFunctionToCall(eventName, fn);\n eventString = eventString.replace(eventName, \"\");\n }\n });\n\n if (eventString.trim() === \"\") {\n return this;\n } else {\n return originalJQueryFunction.apply(this, originalArgs);\n }\n}\n","import {\n NOT_MOUNTED,\n UNLOADING,\n NOT_LOADED,\n LOAD_ERROR,\n SKIP_BECAUSE_BROKEN,\n toName,\n} from \"../applications/app.helpers.js\";\nimport { handleAppError } from \"../applications/app-errors.js\";\nimport { reasonableTime } from \"../applications/timeouts.js\";\nimport { addProfileEntry } from \"../devtools/profiler.js\";\n\nconst appsToUnload = {};\n\nexport function toUnloadPromise(appOrParcel) {\n return Promise.resolve().then(() => {\n const unloadInfo = appsToUnload[toName(appOrParcel)];\n\n if (!unloadInfo) {\n /* No one has called unloadApplication for this app,\n */\n return appOrParcel;\n }\n\n if (appOrParcel.status === NOT_LOADED) {\n /* This app is already unloaded. We just need to clean up\n * anything that still thinks we need to unload the app.\n */\n finishUnloadingApp(appOrParcel, unloadInfo);\n return appOrParcel;\n }\n\n if (appOrParcel.status === UNLOADING) {\n /* Both unloadApplication and reroute want to unload this app.\n * It only needs to be done once, though.\n */\n return unloadInfo.promise.then(() => appOrParcel);\n }\n\n if (\n appOrParcel.status !== NOT_MOUNTED &&\n appOrParcel.status !== LOAD_ERROR\n ) {\n /* The app cannot be unloaded until it is unmounted.\n */\n return appOrParcel;\n }\n\n let startTime;\n\n if (__PROFILE__) {\n startTime = performance.now();\n }\n\n const unloadPromise =\n appOrParcel.status === LOAD_ERROR\n ? Promise.resolve()\n : reasonableTime(appOrParcel, \"unload\");\n\n appOrParcel.status = UNLOADING;\n\n return unloadPromise\n .then(() => {\n if (__PROFILE__) {\n addProfileEntry(\n \"application\",\n toName(appOrParcel),\n \"unload\",\n startTime,\n performance.now(),\n true\n );\n }\n\n finishUnloadingApp(appOrParcel, unloadInfo);\n\n return appOrParcel;\n })\n .catch((err) => {\n if (__PROFILE__) {\n addProfileEntry(\n \"application\",\n toName(appOrParcel),\n \"unload\",\n startTime,\n performance.now(),\n false\n );\n }\n\n errorUnloadingApp(appOrParcel, unloadInfo, err);\n\n return appOrParcel;\n });\n });\n}\n\nfunction finishUnloadingApp(app, unloadInfo) {\n delete appsToUnload[toName(app)];\n\n // Unloaded apps don't have lifecycles\n delete app.bootstrap;\n delete app.mount;\n delete app.unmount;\n delete app.unload;\n\n app.status = NOT_LOADED;\n\n /* resolve the promise of whoever called unloadApplication.\n * This should be done after all other cleanup/bookkeeping\n */\n unloadInfo.resolve();\n}\n\nfunction errorUnloadingApp(app, unloadInfo, err) {\n delete appsToUnload[toName(app)];\n\n // Unloaded apps don't have lifecycles\n delete app.bootstrap;\n delete app.mount;\n delete app.unmount;\n delete app.unload;\n\n handleAppError(err, app, SKIP_BECAUSE_BROKEN);\n unloadInfo.reject(err);\n}\n\nexport function addAppToUnload(app, promiseGetter, resolve, reject) {\n appsToUnload[toName(app)] = { app, resolve, reject };\n Object.defineProperty(appsToUnload[toName(app)], \"promise\", {\n get: promiseGetter,\n });\n}\n\nexport function getAppUnloadInfo(appName) {\n return appsToUnload[appName];\n}\n","import { ensureJQuerySupport } from \"../jquery-support.js\";\nimport {\n isActive,\n toName,\n NOT_LOADED,\n NOT_BOOTSTRAPPED,\n