Files
site-builder/craft/src/utils/tiny-invariant-shim.ts

18 lines
826 B
TypeScript
Raw Normal View History

/**
* Diagnostic shim for tiny-invariant. The prod tiny-invariant strips
* the failure message, so we lose the actual reason for any Craft.js
* Invariant. Aliasing tiny-invariant -> this shim restores it.
*/
export default function invariant(condition: unknown, message?: string | (() => string)): asserts condition {
if (condition) return;
const provided = typeof message === 'function' ? message() : message;
const value = provided ? `Invariant failed: ${provided}` : 'Invariant failed';
// Surface the message + stack to the console BEFORE throwing so even
// an uncaught throw leaves a trace we can read.
// eslint-disable-next-line no-console
console.error('[CRAFT INVARIANT]', value);
// eslint-disable-next-line no-console
console.error(new Error('stack-only').stack);
throw new Error(value);
}