Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions src/Exceptionless.Web/ClientApp/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ if (codespaceName && codespaceDomain) {
}

const SVELTE_RUNTIME_DIAGNOSTICS_GLOBAL = '__exceptionlessSvelteEffectDepthDiagnostics';
const SUPPORTED_SVELTE_RUNTIME_VERSION = '5.56.10';

// Svelte's useful effect-depth diagnostics are development-only. Preserve the
// minimal state-write tracking needed to diagnose production-only loops.
Expand All @@ -45,14 +44,23 @@ function svelteEffectDepthDiagnostics(): Plugin {
const effectDepthNeedle = '\t} catch (error) {\n\t\tif (DEV) {\n\t\t\t// stack contains no useful information, replace it\n';
const effectDepthReplacement = `\t} catch (error) {\n\t\tglobalThis.${SVELTE_RUNTIME_DIAGNOSTICS_GLOBAL}?.attachEffectDepthError(error, last_scheduled_effect);\n\n\t\tif (DEV) {\n\t\t\t// stack contains no useful information, replace it\n`;

function findRuntimePatchTarget(code: string, id: string, needle: string) {
const start = code.indexOf(needle);
if (start < 0) {
throw new Error(`Unable to instrument ${id}; the expected Svelte runtime source was not found.`);
}

if (code.indexOf(needle, start + needle.length) >= 0) {
throw new Error(`Unable to instrument ${id}; the expected Svelte runtime source was found more than once.`);
}

return start;
}

function instrumentRuntime(code: string, id: string, replacements: [needle: string, replacement: string][]) {
const instrumented = new MagicString(code, { filename: id });
for (const [needle, replacement] of replacements) {
const start = code.indexOf(needle);
if (start < 0) {
throw new Error(`Unable to instrument ${id}; the expected Svelte runtime source was not found.`);
}

const start = findRuntimePatchTarget(code, id, needle);
instrumented.overwrite(start, start + needle.length, replacement);
}

Expand All @@ -63,23 +71,12 @@ function svelteEffectDepthDiagnostics(): Plugin {
}

function assertRuntimeShape() {
const packageJson = JSON.parse(readFileSync(packagePath, 'utf8')) as { version?: string };
if (packageJson.version !== SUPPORTED_SVELTE_RUNTIME_VERSION) {
throw new Error(
`Svelte effect-depth diagnostics support ${SUPPORTED_SVELTE_RUNTIME_VERSION}, but ${packageJson.version ?? 'an unknown version'} is installed. Review Svelte's runtime before updating the supported version.`
);
}

const sources = readFileSync(sourcesPath, 'utf8');
const batch = readFileSync(batchPath, 'utf8');
if (
!sources.includes(stateUpdateNeedle) ||
!batch.includes(flushStartNeedle) ||
!batch.includes(flushEndNeedle) ||
!batch.includes(effectDepthNeedle)
) {
throw new Error('Svelte runtime internals changed; update the effect-depth diagnostic instrumentation before building.');
}
findRuntimePatchTarget(sources, sourcesPath, stateUpdateNeedle);
findRuntimePatchTarget(batch, batchPath, flushStartNeedle);
findRuntimePatchTarget(batch, batchPath, flushEndNeedle);
findRuntimePatchTarget(batch, batchPath, effectDepthNeedle);
}

return {
Expand Down
Loading