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 deindent from '../../utils/deindent.js';
import flattenReference from '../../utils/flattenReference.js'; import flattenReference from '../../utils/flattenReference.js';
export default class Fragment { export default class Block {
constructor ( options ) { constructor ( options ) {
Object.assign( this, options ); Object.assign( this, options );
} }
@ -25,6 +25,6 @@ export default class Fragment {
} }
child ( options ) { 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 deindent from '../../utils/deindent.js';
import CodeBuilder from '../../utils/CodeBuilder.js'; import CodeBuilder from '../../utils/CodeBuilder.js';
import Generator from '../Generator.js'; import Generator from '../Generator.js';
import Fragment from './Fragment.js'; import Block from './Block.js';
import visit from './visit.js'; import visit from './visit.js';
class SsrGenerator extends Generator { class SsrGenerator extends Generator {
@ -32,7 +32,7 @@ export default function ssr ( parsed, source, options ) {
}; };
// create main render() function // create main render() function
const mainFragment = new Fragment({ const mainBlock = new Block({
generator, generator,
contexts: new Map(), contexts: new Map(),
indexes: new Map(), indexes: new Map(),
@ -40,7 +40,7 @@ export default function ssr ( parsed, source, options ) {
}); });
parsed.html.children.forEach( node => { parsed.html.children.forEach( node => {
visit( generator, mainFragment, node ); visit( generator, mainBlock, node );
}); });
builders.render.addLine( builders.render.addLine(

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

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

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

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

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

@ -1,4 +1,4 @@
export default function visitRawMustacheTag ( generator, fragment, node ) { export default function visitRawMustacheTag ( generator, block, node ) {
const { snippet } = generator.contextualise( fragment, node.expression ); const { snippet } = generator.contextualise( block, node.expression );
generator.append( '${' + snippet + '}' ); 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, '\\${' ) ); generator.append( node.data.replace( /\${/g, '\\${' ) );
} }
Loading…
Cancel
Save