Skip to content
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

[WIP] use external generator for SSR #1718

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3,719 changes: 1,988 additions & 1,731 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@
],
"sourceMap": true,
"instrument": true
},
"dependencies": {
"@sveltejs/generate-ssr": "github:sveltejs/generate-ssr"
}
}
3 changes: 1 addition & 2 deletions src/compile/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { test } from '../config';
import Fragment from './nodes/Fragment';
import shared from './shared';
import { DomTarget } from './dom/index';
import { SsrTarget } from './ssr/index';
import { Node, GenerateOptions, ShorthandImport, Ast, CompileOptions, CustomElementOptions } from '../interfaces';

interface Computation {
Expand Down Expand Up @@ -137,7 +136,7 @@ export default class Compiler {
options: CompileOptions,
stats: Stats,
dom: boolean,
target: DomTarget | SsrTarget
target?: DomTarget
) {
stats.start('compile');
this.stats = stats;
Expand Down
20 changes: 0 additions & 20 deletions src/compile/nodes/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import ThenBlock from './ThenBlock';
import CatchBlock from './CatchBlock';
import createDebuggingComment from '../../utils/createDebuggingComment';
import Expression from './shared/Expression';
import { SsrTarget } from '../ssr';

export default class AwaitBlock extends Node {
expression: Expression;
Expand Down Expand Up @@ -194,23 +193,4 @@ export default class AwaitBlock extends Node {
});
});
}

ssr() {
const target: SsrTarget = <SsrTarget>this.compiler.target;
const { snippet } = this.expression;

target.append('${(function(__value) { if(@isPromise(__value)) return `');

this.pending.children.forEach((child: Node) => {
child.ssr();
});

target.append('`; return function(ctx) { return `');

this.then.children.forEach((child: Node) => {
child.ssr();
});

target.append(`\`;}(Object.assign({}, ctx, { ${this.value}: __value }));}(${snippet})) }`);
}
}
7 changes: 0 additions & 7 deletions src/compile/nodes/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,4 @@ export default class Comment extends Node {
super(compiler, parent, scope, info);
this.data = info.data;
}

ssr() {
// Allow option to preserve comments, otherwise ignore
if (this.compiler.options.preserveComments) {
this.compiler.target.append(`<!--${this.data}-->`);
}
}
}
127 changes: 0 additions & 127 deletions src/compile/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import CodeBuilder from '../../utils/CodeBuilder';
import getTailSnippet from '../../utils/getTailSnippet';
import getObject from '../../utils/getObject';
import { quoteNameIfNecessary, quotePropIfNecessary } from '../../utils/quoteIfNecessary';
import { escape, escapeTemplate, stringify } from '../../utils/stringify';
import Node from './shared/Node';
import Block from '../dom/Block';
import Attribute from './Attribute';
import mapChildren from './shared/mapChildren';
import Binding from './Binding';
import EventHandler from './EventHandler';
import Expression from './shared/Expression';
import { AppendTarget } from '../../interfaces';
import addToSet from '../../utils/addToSet';

export default class Component extends Node {
Expand Down Expand Up @@ -513,131 +511,6 @@ export default class Component extends Node {
remount(name: string) {
return `${this.var}._mount(${name}._slotted.default, null);`;
}

ssr() {
function stringifyAttribute(chunk: Node) {
if (chunk.type === 'Text') {
return escapeTemplate(escape(chunk.data));
}

return '${@escape( ' + chunk.snippet + ')}';
}

const bindingProps = this.bindings.map(binding => {
const { name } = getObject(binding.value.node);
const tail = binding.value.node.type === 'MemberExpression'
? getTailSnippet(binding.value.node)
: '';

return `${quoteNameIfNecessary(binding.name)}: ctx${quotePropIfNecessary(name)}${tail}`;
});

function getAttributeValue(attribute) {
if (attribute.isTrue) return `true`;
if (attribute.chunks.length === 0) return `''`;

if (attribute.chunks.length === 1) {
const chunk = attribute.chunks[0];
if (chunk.type === 'Text') {
return stringify(chunk.data);
}

return chunk.snippet;
}

return '`' + attribute.chunks.map(stringifyAttribute).join('') + '`';
}

const usesSpread = this.attributes.find(attr => attr.isSpread);

const props = usesSpread
? `Object.assign(${
this.attributes
.map(attribute => {
if (attribute.isSpread) {
return attribute.expression.snippet;
} else {
return `{ ${quoteNameIfNecessary(attribute.name)}: ${getAttributeValue(attribute)} }`;
}
})
.concat(bindingProps.map(p => `{ ${p} }`))
.join(', ')
})`
: `{ ${this.attributes
.map(attribute => `${quoteNameIfNecessary(attribute.name)}: ${getAttributeValue(attribute)}`)
.concat(bindingProps)
.join(', ')} }`;

const expression = (
this.name === 'svelte:self'
? this.compiler.name
: this.name === 'svelte:component'
? `((${this.expression.snippet}) || @missingComponent)`
: `%components-${this.name}`
);

this.bindings.forEach(binding => {
const conditions = [];

let node = this;
while (node = node.parent) {
if (node.type === 'IfBlock') {
// TODO handle contextual bindings...
conditions.push(`(${node.expression.snippet})`);
}
}

conditions.push(
`!('${binding.name}' in ctx)`,
`${expression}.data`
);

const { name } = getObject(binding.value.node);

this.compiler.target.bindings.push(deindent`
if (${conditions.reverse().join('&&')}) {
tmp = ${expression}.data();
if ('${name}' in tmp) {
ctx${quotePropIfNecessary(binding.name)} = tmp.${name};
settled = false;
}
}
`);
});

let open = `\${@validateSsrComponent(${expression}, '${this.name}')._render(__result, ${props}`;

const options = [];
options.push(`store: options.store`);

if (this.children.length) {
const appendTarget: AppendTarget = {
slots: { default: '' },
slotStack: ['default']
};

this.compiler.target.appendTargets.push(appendTarget);

this.children.forEach((child: Node) => {
child.ssr();
});

const slotted = Object.keys(appendTarget.slots)
.map(name => `${quoteNameIfNecessary(name)}: () => \`${appendTarget.slots[name]}\``)
.join(', ');

options.push(`slotted: { ${slotted} }`);

this.compiler.target.appendTargets.pop();
}

if (options.length) {
open += `, { ${options.join(', ')} }`;
}

this.compiler.target.append(open);
this.compiler.target.append(')}');
}
}

function isComputed(node: Node) {
Expand Down
18 changes: 0 additions & 18 deletions src/compile/nodes/DebugTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,4 @@ export default class DebugTag extends Node {
`);
}
}

ssr() {
if (!this.compiler.options.dev) return;

const filename = this.compiler.file || null;
const { line, column } = this.compiler.locate(this.start + 1);

const obj = this.expressions.length === 0
? `ctx`
: `{ ${this.expressions
.map(e => e.node.name)
.map(name => `${name}: ctx.${name}`)
.join(', ')} }`;

const str = '${@debug(' + `${filename && stringify(filename)}, ${line}, ${column}, ${obj})}`;

this.compiler.target.append(str);
}
}
31 changes: 0 additions & 31 deletions src/compile/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,35 +493,4 @@ export default class EachBlock extends Node {
// TODO consider keyed blocks
return `for (var #i = 0; #i < ${this.iterations}.length; #i += 1) ${this.iterations}[#i].m(${name}._slotted.default, null);`;
}

ssr() {
const { compiler } = this;
const { snippet } = this.expression;

const props = this.contexts.map(prop => `${prop.key.name}: item${prop.tail}`);

const getContext = this.index
? `(item, i) => Object.assign({}, ctx, { ${props.join(', ')}, ${this.index}: i })`
: `item => Object.assign({}, ctx, { ${props.join(', ')} })`;

const open = `\${ ${this.else ? `${snippet}.length ? ` : ''}@each(${snippet}, ${getContext}, ctx => \``;
compiler.target.append(open);

this.children.forEach((child: Node) => {
child.ssr();
});

const close = `\`)`;
compiler.target.append(close);

if (this.else) {
compiler.target.append(` : \``);
this.else.children.forEach((child: Node) => {
child.ssr();
});
compiler.target.append(`\``);
}

compiler.target.append('}');
}
}
Loading