UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

55 lines (54 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGlobalContextSyncErrMsg = void 0; exports.createGlobalContextShared = createGlobalContextShared; const getGlobalContextSyncErrMsg = "The global context isn't set yet, call getGlobalContextSync() later or use getGlobalContext() instead."; exports.getGlobalContextSyncErrMsg = getGlobalContextSyncErrMsg; const getPageConfigsRuntime_js_1 = require("./getPageConfigsRuntime.js"); const executeHookGeneric_js_1 = require("./hooks/executeHookGeneric.js"); const utils_js_1 = require("./utils.js"); async function createGlobalContextShared(virtualFileExports, globalObject, addGlobalContext) { const globalContext = createGlobalContextBase(virtualFileExports); const globalContextAddendum = await addGlobalContext?.(globalContext); (0, utils_js_1.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 (0, executeHookGeneric_js_1.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. (0, utils_js_1.objectAssign)(globalObject.globalContext, globalContext); } return globalObject.globalContext; } function createGlobalContextBase(virtualFileExports) { const { pageFilesAll, allPageIds, pageConfigs, pageConfigGlobal, globalConfig, pageConfigsUserFriendly } = (0, getPageConfigsRuntime_js_1.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; }