vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
92 lines (91 loc) • 4.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGlobalContextSyncErrMsg = void 0;
exports.createGlobalContextShared = createGlobalContextShared;
const utils_js_1 = require("./utils.js");
const parseVirtualFileExports_js_1 = require("./getPageFiles/parseVirtualFileExports.js");
const resolveVikeConfigPublic_js_1 = require("./page-configs/resolveVikeConfigPublic.js");
const execHook_js_1 = require("./hooks/execHook.js");
const prepareGlobalContextForPublicUsage_js_1 = require("./prepareGlobalContextForPublicUsage.js");
const getHook_js_1 = require("./hooks/getHook.js");
const getGlobalContextSyncErrMsg = "The global context isn't set yet, call getGlobalContextSync() later or use getGlobalContext() instead.";
exports.getGlobalContextSyncErrMsg = getGlobalContextSyncErrMsg;
async function createGlobalContextShared(virtualFileExports, globalObject, addGlobalContext) {
const globalContext = createGlobalContextBase(virtualFileExports);
const globalContextAddendum = await addGlobalContext?.(globalContext);
(0, utils_js_1.objectAssign)(globalContext, globalContextAddendum);
const onCreateGlobalContextHooks = (0, getHook_js_1.getHookFromPageConfigGlobalCumulative)(globalContext._pageConfigGlobal, 'onCreateGlobalContext');
let hooksCalled = false;
if (!hooksAreEqual(globalObject.onCreateGlobalContextHooks ?? [], onCreateGlobalContextHooks)) {
globalObject.onCreateGlobalContextHooks = onCreateGlobalContextHooks;
await (0, execHook_js_1.execHookGlobal)('onCreateGlobalContext', globalContext._pageConfigGlobal, null, globalContext, prepareGlobalContextForPublicUsage_js_1.prepareGlobalContextForPublicUsage);
hooksCalled = true;
}
if (!globalObject.globalContext) {
globalObject.globalContext = globalContext;
}
else {
// Singleton: ensure all `globalContext` user-land references are preserved & updated.
if (hooksCalled) {
(0, utils_js_1.objectReplace)(globalObject.globalContext, globalContext);
}
else {
// We don't use objectReplace() in order to keep user-land properties.
(0, utils_js_1.objectAssign)(globalObject.globalContext, globalContext, true);
}
}
return globalObject.globalContext;
}
function createGlobalContextBase(virtualFileExports) {
const { pageFilesAll, allPageIds, pageConfigs, pageConfigGlobal, vikeConfigPublicGlobal, vikeConfigPublicPagesEager, } = getConfigsAll(virtualFileExports);
const globalContext = {
/**
* Useful for distinguishing `globalContext` from other objects and narrowing down TypeScript unions.
*
* https://vike.dev/globalContext#typescript
*/
isGlobalContext: true,
_isOriginalObject: true,
_virtualFileExports: virtualFileExports,
_pageFilesAll: pageFilesAll,
_pageConfigs: pageConfigs,
_pageConfigGlobal: pageConfigGlobal,
_allPageIds: allPageIds,
_vikeConfigPublicGlobal: vikeConfigPublicGlobal,
config: vikeConfigPublicGlobal.config,
pages: vikeConfigPublicPagesEager,
};
(0, utils_js_1.changeEnumerable)(globalContext, '_isOriginalObject', false);
return globalContext;
}
function getConfigsAll(virtualFileExports) {
const { pageFilesAll, pageConfigs, pageConfigGlobal } = (0, parseVirtualFileExports_js_1.parseVirtualFileExports)(virtualFileExports);
const allPageIds = getAllPageIds(pageFilesAll, pageConfigs);
const vikeConfigPublicGlobal = (0, resolveVikeConfigPublic_js_1.resolveVikeConfigPublicGlobal)({
pageConfigGlobalValues: pageConfigGlobal.configValues,
});
const vikeConfigPublicPagesEager = Object.fromEntries(pageConfigs.map((pageConfig) => {
return (0, resolveVikeConfigPublic_js_1.resolveVikeConfigPublicPageEager)(pageConfigGlobal.configValues, pageConfig, pageConfig.configValues);
}));
return {
pageFilesAll,
allPageIds,
pageConfigs,
pageConfigGlobal,
vikeConfigPublicGlobal,
vikeConfigPublicPagesEager,
};
}
function getAllPageIds(pageFilesAll, pageConfigs) {
const fileIds = pageFilesAll.filter(({ isDefaultPageFile }) => !isDefaultPageFile).map(({ pageId }) => pageId);
const allPageIds = (0, utils_js_1.unique)(fileIds);
const allPageIds2 = pageConfigs.map((p) => p.pageId);
return [...allPageIds, ...allPageIds2];
}
function hooksAreEqual(hooks1, hooks2) {
const hooksFn1 = hooks1.map((hook) => hook.hookFn);
const hooksFn2 = hooks2.map((hook) => hook.hookFn);
return (hooksFn1.every((hook) => hooksFn2.includes(hook)) &&
//
hooksFn2.every((hook) => hooksFn1.includes(hook)));
}