align SSR compiler with DOM compiler

pull/453/head
Rich-Harris 8 years ago
parent f88788cc29
commit 8148230e49

@ -1,7 +1,7 @@
import deindent from '../../utils/deindent.js';
import flattenReference from '../../utils/flattenReference.js';
export default class Fragment {
export default class Block {
constructor ( options ) {
Object.assign( this, options );
}
@ -25,6 +25,6 @@ export default class Fragment {
}
child ( options ) {
return new Fragment( Object.assign( {}, this, options, { parent: this } ) );
return new Block( Object.assign( {}, this, options, { parent: this } ) );
}
}

@ -1,7 +1,7 @@
import deindent from '../../utils/deindent.js';
import CodeBuilder from '../../utils/CodeBuilder.js';
import Generator from '../Generator.js';
import Fragment from './Fragment.js';
import Block from './Block.js';
import visit from './visit.js';
class SsrGenerator extends Generator {
@ -32,7 +32,7 @@ export default function ssr ( parsed, source, options ) {
};
// create main render() function
const mainFragment = new Fragment({
const mainBlock = new Block({
generator,
contexts: new Map(),
indexes: new Map(),
@ -40,7 +40,7 @@ export default function ssr ( parsed, source, options ) {
});
parsed.html.children.forEach( node => {
visit( generator, mainFragment, node );
visit( generator, mainBlock, node );
});
builders.render.addLine(

@ -1,11 +1,11 @@
import flattenReference from '../../../utils/flattenReference.js';
import visit from '../visit.js';
export default function visitComponent ( generator, fragment, node ) {
export default function visitComponent ( generator, block, node ) {
function stringify ( chunk ) {
if ( chunk.type === 'Text' ) return chunk.data;
if ( chunk.type === 'MustacheTag' ) {
const { snippet } = generator.contextualise( fragment, chunk.expression );
const { snippet } = generator.contextualise( block, chunk.expression );
return '${__escape( ' + snippet + ')}';
}
}
@ -34,7 +34,7 @@ export default function visitComponent ( generator, fragment, node ) {
if ( chunk.type === 'Text' ) {
value = isNaN( chunk.data ) ? JSON.stringify( chunk.data ) : chunk.data;
} else {
const { snippet } = generator.contextualise( fragment, chunk.expression );
const { snippet } = generator.contextualise( block, chunk.expression );
value = snippet;
}
} else {
@ -45,7 +45,7 @@ export default function visitComponent ( generator, fragment, node ) {
})
.concat( bindings.map( binding => {
const { name, keypath } = flattenReference( binding.value );
const value = fragment.contexts.has( name ) ? keypath : `root.${keypath}`;
const value = block.contexts.has( name ) ? keypath : `root.${keypath}`;
return `${binding.name}: ${value}`;
}))
.join( ', ' );
@ -53,7 +53,7 @@ export default function visitComponent ( generator, fragment, node ) {
const expression = node.name === ':Self' ? generator.name : generator.importedComponents.get( node.name ) || `${generator.alias( 'template' )}.components.${node.name}`;
bindings.forEach( binding => {
fragment.addBinding( binding, expression );
block.addBinding( binding, expression );
});
let open = `\${${expression}.render({${props}}`;
@ -67,7 +67,7 @@ export default function visitComponent ( generator, fragment, node ) {
generator.elementDepth += 1;
node.children.forEach( child => {
visit( generator, fragment, child );
visit( generator, block, child );
});
generator.elementDepth -= 1;

@ -1,30 +1,30 @@
import visit from '../visit.js';
export default function visitEachBlock ( generator, fragment, node ) {
const { dependencies, snippet } = generator.contextualise( fragment, node.expression );
export default function visitEachBlock ( generator, block, node ) {
const { dependencies, snippet } = generator.contextualise( block, node.expression );
const open = `\${ ${snippet}.map( ${ node.index ? `( ${node.context}, ${node.index} )` : node.context} => \``;
generator.append( open );
// TODO should this be the generator's job? It's duplicated between
// here and the equivalent DOM compiler visitor
const contexts = new Map( fragment.contexts );
const contexts = new Map( block.contexts );
contexts.set( node.context, node.context );
const indexes = new Map( fragment.indexes );
const indexes = new Map( block.indexes );
if ( node.index ) indexes.set( node.index, node.context );
const contextDependencies = new Map( fragment.contextDependencies );
const contextDependencies = new Map( block.contextDependencies );
contextDependencies.set( node.context, dependencies );
const childFragment = fragment.child({
const childBlock = block.child({
contexts,
indexes,
contextDependencies
});
node.children.forEach( child => {
visit( generator, childFragment, child );
visit( generator, childBlock, child );
});
const close = `\` ).join( '' )}`;

@ -7,13 +7,13 @@ const meta = {
':Window': visitWindow
};
export default function visitElement ( generator, fragment, node ) {
export default function visitElement ( generator, block, node ) {
if ( node.name in meta ) {
return meta[ node.name ]( generator, fragment, node );
return meta[ node.name ]( generator, block, node );
}
if ( generator.components.has( node.name ) || node.name === ':Self' ) {
visitComponent( generator, fragment, node );
visitComponent( generator, block, node );
return;
}
@ -30,7 +30,7 @@ export default function visitElement ( generator, fragment, node ) {
return chunk.data;
}
const { snippet } = generator.contextualise( fragment, chunk.expression );
const { snippet } = generator.contextualise( block, chunk.expression );
return '${' + snippet + '}';
}).join( '' ) + `"`;
}
@ -49,7 +49,7 @@ export default function visitElement ( generator, fragment, node ) {
generator.elementDepth += 1;
node.children.forEach( child => {
visit( generator, fragment, child );
visit( generator, block, child );
});
generator.elementDepth -= 1;

@ -1,23 +1,23 @@
import visit from '../visit.js';
export default function visitIfBlock ( generator, fragment, node ) {
const { snippet } = generator.contextualise( fragment, node.expression );
export default function visitIfBlock ( generator, block, node ) {
const { snippet } = generator.contextualise( block, node.expression );
generator.append( '${ ' + snippet + ' ? `' );
const childFragment = fragment.child({
conditions: fragment.conditions.concat( snippet )
const childBlock = block.child({
conditions: block.conditions.concat( snippet )
});
node.children.forEach( child => {
visit( generator, childFragment, child );
visit( generator, childBlock, child );
});
generator.append( '` : `' );
if ( node.else ) {
node.else.children.forEach( child => {
visit( generator, childFragment, child );
visit( generator, childBlock, child );
});
}

@ -1,4 +1,4 @@
export default function visitMustacheTag ( generator, fragment, node ) {
const { snippet } = generator.contextualise( fragment, node.expression );
export default function visitMustacheTag ( generator, block, node ) {
const { snippet } = generator.contextualise( block, node.expression );
generator.append( '${__escape( ' + snippet + ' )}' );
}

@ -1,4 +1,4 @@
export default function visitRawMustacheTag ( generator, fragment, node ) {
const { snippet } = generator.contextualise( fragment, node.expression );
export default function visitRawMustacheTag ( generator, block, node ) {
const { snippet } = generator.contextualise( block, node.expression );
generator.append( '${' + snippet + '}' );
}

@ -1,3 +1,3 @@
export default function visitText ( generator, fragment, node ) {
export default function visitText ( generator, block, node ) {
generator.append( node.data.replace( /\${/g, '\\${' ) );
}
Loading…
Cancel
Save