rollup-plugin-visualizer
Version:
Visualize and analyze bundle sizes and module dependencies in Rollup, Rolldown, and Vite.
63 lines (62 loc) • 2.08 kB
JavaScript
export class ShareError extends Error {
code;
constructor(code, message) {
super(message);
this.code = code;
this.name = "ShareError";
}
}
// Processing budgets, not URL/paste limits. Large-report browser checks cover these separately.
export const DEFAULT_SHARE_LIMITS = Object.freeze({
maxFileBytes: 64 * 1024 * 1024,
maxBinaryBytes: 64 * 1024 * 1024,
maxCompressedBytes: 65 * 1024 * 1024,
maxTextBytes: 64 * 1024 * 1024,
maxRecords: 250_000,
maxEntries: 2_000_000,
maxTreeDepth: 256,
maxJsonDepth: 64,
maxExtensions: 1024,
});
export const resolveLimits = (overrides = {}) => {
const limits = { ...DEFAULT_SHARE_LIMITS, ...overrides };
for (const [key, value] of Object.entries(limits)) {
if (!Number.isSafeInteger(value) || value < 0)
throw new RangeError(`Invalid ${key} budget`);
}
return limits;
};
export const invalid = (message) => {
throw new ShareError("invalid-report", message);
};
export const budget = (value, maximum, name) => {
if (value > maximum)
throw new ShareError("resource-limit", `${name} exceeds the processing limit.`);
};
export const uint = (value) => {
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
return invalid("Expected a nonnegative safe integer size, count, or reference.");
}
return value;
};
export const own = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
export const setOwn = (target, key, value) => {
Object.defineProperty(target, key, {
value,
enumerable: true,
writable: true,
configurable: true,
});
};
export const boolState = (value, key) => own(value, key) ? (value[key] ? 2 : 1) : 0;
export const putBool = (value, key, state) => {
if (state > 2)
invalid("Invalid optional boolean state.");
if (state)
Object.defineProperty(value, key, {
value: state === 2,
enumerable: true,
writable: true,
configurable: true,
});
};