vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
111 lines (110 loc) • 5.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertSingleInstance_onClientEntryServerRouting = assertSingleInstance_onClientEntryServerRouting;
exports.assertSingleInstance_onClientEntryClientRouting = assertSingleInstance_onClientEntryClientRouting;
exports.assertSingleInstance_onAssertModuleLoad = assertSingleInstance_onAssertModuleLoad;
// - Show warning if there are two different Vike versions loaded
// - Show warning if entry of Client Routing and entry of Server Routing are both loaded
// - Show warning if Vike is loaded twice
const unique_js_1 = require("./unique.js");
const getGlobalObject_js_1 = require("./getGlobalObject.js");
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
const PROJECT_VERSION_js_1 = require("./PROJECT_VERSION.js");
/* Use original assertWarning() after all CJS is removed from node_modules/vike/dist/
import { assertWarning } from './assert.js'
*/
let globalObject;
// getGlobalObjectSafe() can be called before this line
globalObject ?? (globalObject = genGlobalConfig());
function genGlobalConfig() {
return (0, getGlobalObject_js_1.getGlobalObject)('utils/assertSingleInstance.ts', {
instances: [],
alreadyLogged: new Set(),
});
}
// We need getGlobalObjectSafe() because globalObject is `undefined` when exported functions are called before globalObject is initialized
function getGlobalObjectSafe() {
globalObject ?? (globalObject = genGlobalConfig());
return globalObject;
}
const clientRuntimesClonflict = 'Client runtime of both Server Routing and Client Routing loaded https://vike.dev/client-runtimes-conflict';
const clientNotSingleInstance = 'Client runtime loaded twice https://vike.dev/client-runtime-duplicated';
function assertSingleInstance() {
const globalObject = getGlobalObjectSafe();
{
const versions = (0, unique_js_1.unique)(globalObject.instances);
assertWarning(versions.length <= 1,
// Do *NOT* patch Vike to remove this warning: you *will* eventually encounter the issues listed at https://vike.dev/warning/version-mismatch
// - This happened before: https://github.com/vikejs/vike/issues/1108#issuecomment-1719061509
`vike@${picocolors_1.default.bold(versions[0])} and vike@${picocolors_1.default.bold(versions[1])} loaded which is highly discouraged, see ${picocolors_1.default.underline('https://vike.dev/warning/version-mismatch')}`, { onlyOnce: true, showStackTrace: false });
}
if (globalObject.checkSingleInstance && globalObject.instances.length > 1) {
/*/ Not sure whether circular dependency can cause problems? In principle not since client-side code is ESM.
console.warn(clientNotSingleInstance)
/*/
assertWarning(false, clientNotSingleInstance, { onlyOnce: true, showStackTrace: true });
//*/
}
}
function assertSingleInstance_onClientEntryServerRouting(isProduction) {
const globalObject = getGlobalObjectSafe();
assertWarning(globalObject.isClientRouting !== true, clientRuntimesClonflict, {
onlyOnce: true,
showStackTrace: true,
});
assertWarning(globalObject.isClientRouting === undefined, clientNotSingleInstance, {
onlyOnce: true,
showStackTrace: true,
});
globalObject.isClientRouting = false;
if (isProduction)
globalObject.checkSingleInstance = true;
assertSingleInstance();
}
function assertSingleInstance_onClientEntryClientRouting(isProduction) {
const globalObject = getGlobalObjectSafe();
assertWarning(globalObject.isClientRouting !== false, clientRuntimesClonflict, {
onlyOnce: true,
showStackTrace: true,
});
assertWarning(globalObject.isClientRouting === undefined, clientNotSingleInstance, {
onlyOnce: true,
showStackTrace: true,
});
globalObject.isClientRouting = true;
if (isProduction)
globalObject.checkSingleInstance = true;
assertSingleInstance();
}
// Called by utils/assert.ts which is (most certainly) loaded by all entries. That way we don't have to call a callback for every entry. (There are a lot of entries: `client/router/`, `client/`, `node/runtime/`, `node/vite/`, `node/cli`.)
function assertSingleInstance_onAssertModuleLoad() {
const globalObject = getGlobalObjectSafe();
globalObject.instances.push(PROJECT_VERSION_js_1.PROJECT_VERSION);
assertSingleInstance();
}
function assertWarning(condition, errorMessage, { onlyOnce, showStackTrace }) {
const globalObject = getGlobalObjectSafe();
if (condition) {
return;
}
const msg = `[Vike][Warning] ${errorMessage}`;
if (onlyOnce) {
const { alreadyLogged } = globalObject;
const key = onlyOnce === true ? msg : onlyOnce;
if (alreadyLogged.has(key)) {
return;
}
else {
alreadyLogged.add(key);
}
}
if (showStackTrace) {
console.warn(new Error(msg));
}
else {
console.warn(msg);
}
}