diff --git a/.changeset/modern-colts-drive.md b/.changeset/modern-colts-drive.md new file mode 100644 index 000000000000..6c727d220e8d --- /dev/null +++ b/.changeset/modern-colts-drive.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: treeshake `$inspect.trace` code if unused in modules diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index a4167be3a1c8..c0e4a655712c 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -243,13 +243,15 @@ export function analyze_module(ast, options) { } } + const analysis = { runes: true, tracing: false }; + walk( /** @type {Node} */ (ast), { scope, scopes, // @ts-expect-error TODO - analysis: { runes: true } + analysis }, visitors ); @@ -259,7 +261,8 @@ export function analyze_module(ast, options) { name: options.filename, accessors: false, runes: true, - immutable: true + immutable: true, + tracing: analysis.tracing }; } diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js index 8e05b44a839e..90901d29ce7d 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js @@ -671,9 +671,15 @@ export function client_module(analysis, options) { 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 { type: 'Program', sourceType: 'module', - body: [b.import_all('$', 'svelte/internal/client'), ...module.body] + body: [...body, ...module.body] }; } diff --git a/packages/svelte/src/compiler/phases/types.d.ts b/packages/svelte/src/compiler/phases/types.d.ts index b4dcc270cd26..fe32dbba3e4a 100644 --- a/packages/svelte/src/compiler/phases/types.d.ts +++ b/packages/svelte/src/compiler/phases/types.d.ts @@ -27,6 +27,7 @@ export interface Analysis { name: string; // TODO should this be filename? it's used in `compileModule` as well as `compile` runes: boolean; immutable: boolean; + tracing: boolean; // TODO figure out if we can move this to ComponentAnalysis accessors: boolean;