vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
52 lines (51 loc) • 2.82 kB
JavaScript
export { createGlobalContextShared };
export { getGlobalContextSyncErrMsg };
const getGlobalContextSyncErrMsg = "The global context isn't set yet, call getGlobalContextSync() later or use getGlobalContext() instead.";
import { getPageConfigsRuntime } from './getPageConfigsRuntime.js';
import { executeHookGenericGlobalCumulative } from './hooks/executeHookGeneric.js';
import { objectAssign } from './utils.js';
async function createGlobalContextShared(virtualFileExports, globalObject, addGlobalContext) {
const globalContext = createGlobalContextBase(virtualFileExports);
const globalContextAddendum = await addGlobalContext?.(globalContext);
objectAssign(globalContext, globalContextAddendum);
if (!globalObject.globalContext) {
globalObject.globalContext = globalContext;
// - We deliberately call onCreateGlobalContext() only at the beginning and only once per process.
// - TO-DO/eventually: HMR
// - Once Photon supports it: `server.hot.send({ type: 'full-server-reload' })`
// - Either use:
// - import.meta.hot
// - https://vite.dev/guide/api-hmr.html
// - Use a Vite transformer to inject import.meta.hot code into each user-land `+onCreateGlobalContext.js` file
// - Seems more idiomatic
// - globalContext._viteDevServer.hot.send()
// - Send 'full-server-reload' signal whenever a onCreateGlobalContext() function is modified => we need a globalObject to track all hooks and see if one of them is new/modified.
// - Seems less idiomatic
await executeHookGenericGlobalCumulative('onCreateGlobalContext', globalContext._pageConfigGlobal, null, globalContext);
}
else {
// Singleton: ensure all `globalContext` user-land references are preserved & updated.
// We don't use objectReplace() in order to keep user-land properties.
objectAssign(globalObject.globalContext, globalContext);
}
return globalObject.globalContext;
}
function createGlobalContextBase(virtualFileExports) {
const { pageFilesAll, allPageIds, pageConfigs, pageConfigGlobal, globalConfig, pageConfigsUserFriendly } = getPageConfigsRuntime(virtualFileExports);
const globalContext = {
/**
* Useful for distinguishing `globalContext` from other objects and narrowing down TypeScript unions.
*
* https://vike.dev/globalContext#typescript
*/
isGlobalContext: true,
_virtualFileExports: virtualFileExports,
_pageFilesAll: pageFilesAll,
_pageConfigs: pageConfigs,
_pageConfigGlobal: pageConfigGlobal,
_allPageIds: allPageIds,
config: globalConfig.config,
pages: pageConfigsUserFriendly
};
return globalContext;
}