vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
21 lines (20 loc) • 738 B
JavaScript
export { getGlobalObject };
export { assertIsSingleModuleInstance };
import { assert } from './assert.js';
/** Share information across module instances. */
function getGlobalObject(key, defaultValue) {
const globalObjects = getGlobalObjects();
const globalObject = (globalObjects[key] = globalObjects[key] || defaultValue);
return globalObject;
}
/** Assert that the module is instantiated only once. */
function assertIsSingleModuleInstance(key) {
const globalObjects = getGlobalObjects();
assert(!(key in globalObjects));
}
function getGlobalObjects() {
const projectKey = '_vike';
// @ts-ignore
const globalObjects = (globalThis[projectKey] = globalThis[projectKey] || {});
return globalObjects;
}