Skip to content

Commit ed9a9a9

Browse files
authored
Merge pull request sveltejs#1682 from sveltejs/sveltejsgh-1659
support debug tag in SSR mode
2 parents 09865eb + 154ee73 commit ed9a9a9

File tree

6 files changed

+135
-1
lines changed

6 files changed

+135
-1
lines changed

src/compile/nodes/DebugTag.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Block from '../dom/Block';
44
import Expression from './shared/Expression';
55
import deindent from '../../utils/deindent';
66
import addToSet from '../../utils/addToSet';
7+
import { stringify } from '../../utils/stringify';
78

89
export default class DebugTag extends Node {
910
expressions: Expression[];
@@ -25,8 +26,8 @@ export default class DebugTag extends Node {
2526

2627
const { code } = this.compiler;
2728

28-
// Debug all
2929
if (this.expressions.length === 0) {
30+
// Debug all
3031
code.overwrite(this.start + 1, this.start + 7, 'debugger', {
3132
storeName: true
3233
});
@@ -67,4 +68,22 @@ export default class DebugTag extends Node {
6768
`);
6869
}
6970
}
71+
72+
ssr() {
73+
if (!this.compiler.options.dev) return;
74+
75+
const filename = this.compiler.file || null;
76+
const { line, column } = this.compiler.locate(this.start + 1);
77+
78+
const obj = this.expressions.length === 0
79+
? `ctx`
80+
: `{ ${this.expressions
81+
.map(e => e.node.name)
82+
.map(name => `${name}: ctx.${name}`)
83+
.join(', ')} }`;
84+
85+
const str = '${@debug(' + `${filename && stringify(filename)}, ${line}, ${column}, ${obj})}`;
86+
87+
this.compiler.target.append(str);
88+
}
7089
}

src/shared/ssr.js

+6
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ export function validateSsrComponent(component, name) {
5454
}
5555

5656
return component;
57+
}
58+
59+
export function debug(file, line, column, values) {
60+
console.log(`{@debug} ${file ? file + ' ' : ''}(${line}:${column})`);
61+
console.log(values);
62+
return '';
5763
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
options: {
3+
generate: 'ssr',
4+
dev: true
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var { debug, each, escape } = require("svelte/shared.js");
2+
3+
var SvelteComponent = {};
4+
SvelteComponent.data = function() {
5+
return {};
6+
};
7+
8+
SvelteComponent.render = function(state, options = {}) {
9+
var components = new Set();
10+
11+
function addComponent(component) {
12+
components.add(component);
13+
}
14+
15+
var result = { head: '', addComponent };
16+
var html = SvelteComponent._render(result, state, options);
17+
18+
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\n');
19+
20+
return {
21+
html,
22+
head: result.head,
23+
css: { code: cssCode, map: null },
24+
toString() {
25+
return html;
26+
}
27+
};
28+
};
29+
30+
SvelteComponent._render = function(__result, ctx, options) {
31+
__result.addComponent(SvelteComponent);
32+
33+
ctx = Object.assign({}, ctx);
34+
35+
return `${ each(ctx.things, item => Object.assign({}, ctx, { thing: item }), ctx => `<span>${escape(ctx.thing.name)}</span>
36+
${debug(null, 2, 2, { foo: ctx.foo })}`)}
37+
38+
<p>foo: ${escape(ctx.foo)}</p>`;
39+
};
40+
41+
SvelteComponent.css = {
42+
code: '',
43+
map: null
44+
};
45+
46+
module.exports = SvelteComponent;
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
3+
var { debug, each, escape } = require("svelte/shared.js");
4+
5+
var SvelteComponent = {};;
6+
7+
SvelteComponent.data = function() {
8+
return {};
9+
};
10+
11+
SvelteComponent.render = function(state, options = {}) {
12+
var components = new Set();
13+
14+
function addComponent(component) {
15+
components.add(component);
16+
}
17+
18+
var result = { head: '', addComponent };
19+
var html = SvelteComponent._render(result, state, options);
20+
21+
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\n');
22+
23+
return {
24+
html,
25+
head: result.head,
26+
css: { code: cssCode, map: null },
27+
toString() {
28+
return html;
29+
}
30+
};
31+
}
32+
33+
SvelteComponent._render = function(__result, ctx, options) {
34+
__result.addComponent(SvelteComponent);
35+
36+
ctx = Object.assign({}, ctx);
37+
38+
return `${ each(ctx.things, item => Object.assign({}, ctx, { thing: item }), ctx => `<span>${escape(ctx.thing.name)}</span>
39+
${debug(null, 2, 2, { foo: ctx.foo })}`)}
40+
41+
<p>foo: ${escape(ctx.foo)}</p>`;
42+
};
43+
44+
SvelteComponent.css = {
45+
code: '',
46+
map: null
47+
};
48+
49+
var warned = false;
50+
51+
module.exports = SvelteComponent;
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{#each things as thing}
2+
<span>{thing.name}</span>
3+
{@debug foo}
4+
{/each}
5+
6+
<p>foo: {foo}</p>

0 commit comments

Comments
 (0)