Skip to content

fix: ensure $inspect.trace code is treeshaken out if unused #14770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/tough-dingos-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure $inspect.trace code is treeshaken out if unused
3 changes: 3 additions & 0 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"./internal/flags/legacy": {
"default": "./src/internal/flags/legacy.js"
},
"./internal/flags/tracing": {
"default": "./src/internal/flags/tracing.js"
},
"./internal/server": {
"default": "./src/internal/server/index.js"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/svelte/scripts/check-treeshakeability.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ for (const key in pkg.exports) {
if (key === './internal') continue;
if (key === './internal/disclose-version') continue;
if (key === './internal/flags/legacy') continue;
if (key === './internal/flags/tracing') continue;

for (const type of ['browser', 'default']) {
if (!pkg.exports[key][type]) continue;
Expand Down Expand Up @@ -91,6 +92,7 @@ const bundle = await bundle_code(
</script>

<svelte:head><title>hi</title></svelte:head>
<input bind:value={foo} />

<a href={foo} class={foo}>a</a>
<a {...foo}>a</a>
Expand Down Expand Up @@ -134,6 +136,15 @@ if (!bundle.includes('component_context.l')) {
console.error(`❌ Legacy code not treeshakeable`);
}

if (!bundle.includes(`'CreatedAt'`)) {
// eslint-disable-next-line no-console
console.error(`✅ $inspect.trace code treeshakeable`);
} else {
failed = true;
// eslint-disable-next-line no-console
console.error(`❌ $inspect.trace code not treeshakeable`);
}

if (failed) {
// eslint-disable-next-line no-console
console.error(bundle);
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ export function analyze_component(root, source, options) {
template,
elements: [],
runes,
tracing: false,
immutable: runes || options.immutable,
exports: [],
uses_props: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export function CallExpression(node, context) {

context.state.scope.tracing = b.thunk(b.literal(label + ' ' + loc));
}

context.state.analysis.tracing = true;
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ export function client_component(analysis, options) {
body.unshift(b.imports([], 'svelte/internal/flags/legacy'));
}

if (analysis.tracing) {
body.unshift(b.imports([], 'svelte/internal/flags/tracing'));
}

if (options.discloseVersion) {
body.unshift(b.imports([], 'svelte/internal/disclose-version'));
}
Expand Down
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 @@ -39,6 +39,7 @@ export interface ComponentAnalysis extends Analysis {
/** Used for CSS pruning and scoping */
elements: Array<AST.RegularElement | AST.SvelteElement>;
runes: boolean;
tracing: boolean;
exports: Array<{ name: string; alias: string | null }>;
/** Whether the component uses `$$props` */
uses_props: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { STATE_SYMBOL, STATE_SYMBOL_METADATA } from './constants.js';
import { UNINITIALIZED } from '../../constants.js';
import * as e from './errors.js';
import { get_stack } from './dev/tracing.js';
import { tracing_mode_flag } from '../flags/index.js';

/**
* @template T
Expand All @@ -25,7 +26,7 @@ import { get_stack } from './dev/tracing.js';
export function proxy(value, parent = null, prev) {
/** @type {Error | null} */
var stack = null;
if (DEV) {
if (DEV && tracing_mode_flag) {
stack = get_stack('CreatedAt');
}
// if non-proxyable, or is already a proxy, return `value`
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as e from '../errors.js';
import { destroy_effect } from './effects.js';
import { inspect_effects, set_inspect_effects } from './sources.js';
import { get_stack } from '../dev/tracing.js';
import { tracing_mode_flag } from '../../flags/index.js';

/**
* @template V
Expand Down Expand Up @@ -62,7 +63,7 @@ export function derived(fn) {
parent: parent_derived ?? active_effect
};

if (DEV) {
if (DEV && tracing_mode_flag) {
signal.created = get_stack('CreatedAt');
}

Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
BLOCK_EFFECT
} from '../constants.js';
import * as e from '../errors.js';
import { legacy_mode_flag } from '../../flags/index.js';
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js';
import { get_stack } from '../dev/tracing.js';

export let inspect_effects = new Set();
Expand Down Expand Up @@ -60,7 +60,7 @@ export function source(v, stack) {
version: 0
};

if (DEV) {
if (DEV && tracing_mode_flag) {
signal.created = stack ?? get_stack('CreatedAt');
signal.debug = null;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ export function internal_set(source, value) {
source.v = value;
source.version = increment_version();

if (DEV) {
if (DEV && tracing_mode_flag) {
source.updated = get_stack('UpdatedAt');
}

Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { destroy_derived, execute_derived, update_derived } from './reactivity/d
import * as e from './errors.js';
import { lifecycle_outside_component } from '../shared/errors.js';
import { FILENAME } from '../../constants.js';
import { legacy_mode_flag } from '../flags/index.js';
import { legacy_mode_flag, tracing_mode_flag } from '../flags/index.js';
import { tracing_expressions, get_stack } from './dev/tracing.js';

const FLUSH_MICROTASK = 0;
Expand Down Expand Up @@ -917,6 +917,7 @@ export function get(signal) {

if (
DEV &&
tracing_mode_flag &&
tracing_expressions !== null &&
active_reaction !== null &&
tracing_expressions.reaction === active_reaction
Expand Down
5 changes: 5 additions & 0 deletions packages/svelte/src/internal/flags/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export let legacy_mode_flag = false;
export let tracing_mode_flag = false;

export function enable_legacy_mode_flag() {
legacy_mode_flag = true;
}

export function enable_tracing_mode_flag() {
tracing_mode_flag = true;
}
3 changes: 3 additions & 0 deletions packages/svelte/src/internal/flags/tracing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { enable_tracing_mode_flag } from './index.js';

enable_tracing_mode_flag();
Loading