Skip to content

Commit 07d0c55

Browse files
committed
WIP
1 parent aeb8992 commit 07d0c55

File tree

7 files changed

+101
-46
lines changed

7 files changed

+101
-46
lines changed

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export function is_inlinable_expression(node_or_nodes, state) {
357357
* @param {Expression} expression
358358
* @param {ClientTransformState} state
359359
*/
360-
export function trace_expression(node, expression, state) {
360+
export function trace(node, expression, state) {
361361
const loc = node.loc;
362362
if (!loc) {
363363
return expression;
@@ -383,5 +383,10 @@ export function trace_expression(node, expression, state) {
383383
}
384384
}
385385

386-
return b.call('$.trace_expression', b.thunk(expression), b.literal(code));
386+
return b.call(
387+
'$.trace',
388+
b.thunk(expression),
389+
b.literal(code),
390+
node.type === 'CallExpression' ? b.literal(true) : undefined
391+
);
387392
}

packages/svelte/src/compiler/phases/3-transform/client/visitors/BlockStatement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function BlockStatement(node, context) {
1515
return b.block([
1616
b.return(
1717
b.call(
18-
'$.log_traced_expressions',
18+
'$.log_trace',
1919
b.thunk(
2020
b.block(
2121
node.body.map(

packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { dev, is_ignored } from '../../../../state.js';
44
import * as b from '../../../../utils/builders.js';
55
import { get_rune } from '../../../scope.js';
66
import { transform_inspect_rune } from '../../utils.js';
7-
import { trace_expression } from '../utils.js';
7+
import { trace } from '../utils.js';
88

99
/**
1010
* @param {CallExpression} node
@@ -62,8 +62,8 @@ export function CallExpression(node, context) {
6262
);
6363
}
6464

65-
if (context.state.trace_dependencies) {
66-
return trace_expression(
65+
if (dev) {
66+
return trace(
6767
node,
6868
{
6969
...node,

packages/svelte/src/compiler/phases/3-transform/client/visitors/Identifier.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
/** @import { Context } from '../types' */
33
import is_reference from 'is-reference';
44
import * as b from '../../../../utils/builders.js';
5-
import { build_getter, trace_expression } from '../utils.js';
5+
import { build_getter, trace } from '../utils.js';
6+
import { dev } from '../../../../state.js';
67

78
/**
89
* @param {Identifier} node
@@ -42,8 +43,8 @@ export function Identifier(node, context) {
4243
}
4344
}
4445

45-
if (transformed && transformed !== node && context.state.trace_dependencies) {
46-
return trace_expression(node, transformed, context.state);
46+
if (transformed && transformed !== node && dev) {
47+
return trace(node, transformed, context.state);
4748
}
4849

4950
return transformed;

packages/svelte/src/compiler/phases/3-transform/client/visitors/MemberExpression.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/** @import { MemberExpression, Expression, Super, PrivateIdentifier } from 'estree' */
22
/** @import { Context } from '../types' */
3+
import { dev } from '../../../../state.js';
34
import * as b from '../../../../utils/builders.js';
4-
import { trace_expression } from '../utils.js';
5+
import { trace } from '../utils.js';
56

67
/**
78
* @param {MemberExpression} node
@@ -17,8 +18,8 @@ export function MemberExpression(node, context) {
1718
}
1819
}
1920

20-
if (context.state.trace_dependencies) {
21-
return trace_expression(
21+
if (dev) {
22+
return trace(
2223
node,
2324
transformed || {
2425
...node,

packages/svelte/src/internal/client/dev/tracing.js

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,123 @@
11
import { snapshot } from '../../server';
22
import { STATE_SYMBOL } from '../constants';
33

4-
/** @type { Map<string, any> | null } */
4+
/** @type { { label: string, time: number, sub: any, stack: string | void, value: any }[] | null } */
55
export let tracing_expressions = null;
66
export let tracing_expression_reactive = false;
77

88
/**
9-
* @template T
10-
* @param {() => T} fn
11-
* @param {string} label
9+
* @param {any} expressions
1210
*/
13-
export function log_traced_expressions(fn, label) {
14-
var previously_tracing_expressions = tracing_expressions;
15-
tracing_expressions = new Map();
16-
try {
17-
var val = fn();
11+
function log_expressions(expressions) {
12+
for (let expression of expressions) {
13+
const val = expression.value;
14+
const label = expression.label;
15+
const time = expression.time;
1816

19-
// eslint-disable-next-line no-console
20-
console.group(`${label} (trace)`);
21-
for (let [label, [val, stack]] of tracing_expressions) {
17+
if (time) {
2218
// eslint-disable-next-line no-console
2319
console.groupCollapsed(
24-
label,
20+
`${label} %c${time.toFixed(2)}ms`,
21+
'color: grey',
2522
val && typeof val === 'object' && STATE_SYMBOL in val ? snapshot(val, true) : val
2623
);
24+
} else {
2725
// eslint-disable-next-line no-console
28-
console.log(stack);
29-
// eslint-disable-next-line no-console
30-
console.groupEnd();
26+
console.groupCollapsed(
27+
label,
28+
val && typeof val === 'object' && STATE_SYMBOL in val ? snapshot(val, true) : val
29+
);
30+
}
31+
32+
if (expression.sub) {
33+
log_expressions(expression.sub);
3134
}
3235
// eslint-disable-next-line no-console
36+
console.log(expression.stack);
37+
// eslint-disable-next-line no-console
38+
console.groupEnd();
39+
}
40+
}
41+
42+
/**
43+
* @template T
44+
* @param {() => T} fn
45+
* @param {string} label
46+
*/
47+
export function log_trace(fn, label) {
48+
var previously_tracing_expressions = tracing_expressions;
49+
try {
50+
tracing_expressions = [];
51+
52+
var start = performance.now();
53+
var value = fn();
54+
var time = (performance.now() - start).toFixed(2);
55+
// eslint-disable-next-line no-console
56+
console.group(`${label} %c${time}ms`, 'color: grey');
57+
log_expressions(tracing_expressions);
58+
// eslint-disable-next-line no-console
3359
console.groupEnd();
3460

35-
return val;
36-
} finally {
3761
if (previously_tracing_expressions !== null) {
38-
for (let [label, entries] of tracing_expressions) {
39-
if (!previously_tracing_expressions.has(label)) {
40-
previously_tracing_expressions.set(label, entries);
41-
}
42-
}
62+
previously_tracing_expressions.push(...tracing_expressions);
4363
}
64+
65+
return value;
66+
} finally {
4467
tracing_expressions = previously_tracing_expressions;
4568
}
4669
}
4770

4871
/**
4972
* @template T
5073
* @param {() => T} fn
74+
* @param {boolean} [call_expression]
5175
* @param {string} label
5276
*/
53-
export function trace_expression(fn, label) {
77+
export function trace(fn, label, call_expression) {
78+
var previously_tracing_expressions = tracing_expressions;
5479
var previously_tracing_expression_reactive = tracing_expression_reactive;
80+
5581
try {
5682
tracing_expression_reactive = false;
57-
var val = fn();
83+
tracing_expressions = [];
84+
var value,
85+
time = 0;
86+
87+
if (call_expression) {
88+
var start = performance.now();
89+
value = fn();
90+
time = (performance.now() - start);
91+
} else {
92+
value = fn();
93+
}
5894

59-
if (
60-
tracing_expression_reactive &&
61-
tracing_expressions !== null &&
62-
!tracing_expressions.has(label)
63-
) {
95+
if (tracing_expressions !== null) {
6496
// The Stack isn't as useful in FF and doesn't show sourcemaps
65-
const stack = new Error().stack
97+
var stack = new Error().stack
6698
?.split('\n')
6799
[/Firefox/.test(navigator.userAgent) ? 3 : 2].trimStart();
68-
tracing_expressions.set(label, [val, stack]);
100+
101+
if (tracing_expression_reactive) {
102+
tracing_expressions.push({ label, value, time, stack, sub: null });
103+
104+
if (previously_tracing_expressions !== null) {
105+
previously_tracing_expressions.push(...tracing_expressions);
106+
}
107+
} else if (previously_tracing_expressions !== null) {
108+
previously_tracing_expressions.push({
109+
label,
110+
value,
111+
time,
112+
stack,
113+
sub: tracing_expressions
114+
});
115+
}
69116
}
70117

71-
return val;
118+
return value;
72119
} finally {
120+
tracing_expressions = previously_tracing_expressions;
73121
tracing_expression_reactive = previously_tracing_expression_reactive;
74122
}
75123
}

packages/svelte/src/internal/client/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export {
1111
skip_ownership_validation
1212
} from './dev/ownership.js';
1313
export { check_target, legacy_api } from './dev/legacy.js';
14-
export { log_traced_expressions, trace_expression } from './dev/tracing.js';
14+
export { log_trace, trace } from './dev/tracing.js';
1515
export { inspect } from './dev/inspect.js';
1616
export { await_block as await } from './dom/blocks/await.js';
1717
export { if_block as if } from './dom/blocks/if.js';

0 commit comments

Comments
 (0)