Skip to content

Commit

Permalink
fix: treeshake $inspect.trace code if unused in modules (#14774)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Dec 19, 2024
1 parent 69d198e commit 5bc4033
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-colts-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: treeshake `$inspect.trace` code if unused in modules
7 changes: 5 additions & 2 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -259,7 +261,8 @@ export function analyze_module(ast, options) {
name: options.filename,
accessors: false,
runes: true,
immutable: true
immutable: true,
tracing: analysis.tracing
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
};
}
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/phases/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5bc4033

Please sign in to comment.