fix: treeshake `$inspect.trace` code if unused in modules (#14774)

pull/14776/head
Dominic Gannaway 9 months ago committed by GitHub
parent 69d198eb44
commit 5bc4033421
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: treeshake `$inspect.trace` code if unused in modules

@ -243,13 +243,15 @@ export function analyze_module(ast, options) {
} }
} }
const analysis = { runes: true, tracing: false };
walk( walk(
/** @type {Node} */ (ast), /** @type {Node} */ (ast),
{ {
scope, scope,
scopes, scopes,
// @ts-expect-error TODO // @ts-expect-error TODO
analysis: { runes: true } analysis
}, },
visitors visitors
); );
@ -259,7 +261,8 @@ export function analyze_module(ast, options) {
name: options.filename, name: options.filename,
accessors: false, accessors: false,
runes: true, runes: true,
immutable: true immutable: true,
tracing: analysis.tracing
}; };
} }

@ -671,9 +671,15 @@ export function client_module(analysis, options) {
walk(/** @type {AST.SvelteNode} */ (analysis.module.ast), state, visitors) walk(/** @type {AST.SvelteNode} */ (analysis.module.ast), state, visitors)
); );
const body = [b.import_all('$', 'svelte/internal/client')];
if (analysis.tracing) {
body.push(b.imports([], 'svelte/internal/flags/tracing'));
}
return { return {
type: 'Program', type: 'Program',
sourceType: 'module', sourceType: 'module',
body: [b.import_all('$', 'svelte/internal/client'), ...module.body] body: [...body, ...module.body]
}; };
} }

@ -27,6 +27,7 @@ export interface Analysis {
name: string; // TODO should this be filename? it's used in `compileModule` as well as `compile` name: string; // TODO should this be filename? it's used in `compileModule` as well as `compile`
runes: boolean; runes: boolean;
immutable: boolean; immutable: boolean;
tracing: boolean;
// TODO figure out if we can move this to ComponentAnalysis // TODO figure out if we can move this to ComponentAnalysis
accessors: boolean; accessors: boolean;

Loading…
Cancel
Save