Merge pull request #281 from sveltejs/gh-275

two-way component binding in SSR
pull/294/head
Rich Harris 9 years ago committed by GitHub
commit 744a8643c7

@ -12,7 +12,7 @@ export default {
nodeResolve({ jsnext: true, module: true }), nodeResolve({ jsnext: true, module: true }),
commonjs() commonjs()
], ],
external: [ path.resolve( 'src/index.js' ), 'fs', 'magic-string' ], external: [ path.resolve( 'src/index.js' ), 'fs', 'path', 'magic-string' ],
paths: { paths: {
[ path.resolve( 'src/index.js' ) ]: '../compiler/svelte.js' [ path.resolve( 'src/index.js' ) ]: '../compiler/svelte.js'
}, },

@ -7,9 +7,26 @@ import Generator from '../Generator.js';
class SsrGenerator extends Generator { class SsrGenerator extends Generator {
constructor ( parsed, source, names, visitors ) { constructor ( parsed, source, names, visitors ) {
super( parsed, source, names, visitors ); super( parsed, source, names, visitors );
this.bindings = [];
this.renderCode = ''; this.renderCode = '';
} }
addBinding ( binding, name ) {
const conditions = [ `!( '${binding.name}' in root )`].concat( // TODO handle contextual bindings...
this.current.conditions.map( c => `(${c})` )
);
this.bindings.push( deindent`
if ( ${conditions.join( '&&' )} ) {
tmp = template.components.${name}.data();
if ( '${binding.value}' in tmp ) {
root.${binding.name} = tmp.${binding.value};
settled = false;
}
}
` );
}
append ( code ) { append ( code ) {
this.renderCode += code; this.renderCode += code;
} }
@ -25,6 +42,7 @@ export default function ssr ( parsed, source, options, names ) {
const builders = { const builders = {
main: new CodeBuilder(), main: new CodeBuilder(),
bindings: new CodeBuilder(),
render: new CodeBuilder(), render: new CodeBuilder(),
renderCss: new CodeBuilder() renderCss: new CodeBuilder()
}; };
@ -32,7 +50,8 @@ export default function ssr ( parsed, source, options, names ) {
// create main render() function // create main render() function
generator.push({ generator.push({
contexts: {}, contexts: {},
indexes: {} indexes: {},
conditions: []
}); });
parsed.html.children.forEach( node => generator.visit( node ) ); parsed.html.children.forEach( node => generator.visit( node ) );
@ -47,6 +66,21 @@ export default function ssr ( parsed, source, options, names ) {
); );
}); });
if ( generator.bindings.length ) {
const bindings = generator.bindings.join( '\n\n' );
builders.render.addBlock( deindent`
var settled = false;
var tmp;
while ( !settled ) {
settled = true;
${bindings}
}
` );
}
builders.render.addBlock( builders.render.addBlock(
`return \`${generator.renderCode}\`;` `return \`${generator.renderCode}\`;`
); );
@ -102,6 +136,10 @@ export default function ssr ( parsed, source, options, names ) {
${name}.filename = ${JSON.stringify( options.filename )}; ${name}.filename = ${JSON.stringify( options.filename )};
${name}.data = function () {
return ${templateProperties.data ? `template.data()` : `{}`};
};
${name}.render = function ( root, options ) { ${name}.render = function ( root, options ) {
${builders.render} ${builders.render}
}; };

@ -8,10 +8,19 @@ export default {
} }
} }
const props = node.attributes const attributes = [];
.map( attribute => { const bindings = [];
if ( attribute.type !== 'Attribute' ) return;
node.attributes.forEach( attribute => {
if ( attribute.type === 'Attribute' ) {
attributes.push( attribute );
} else if ( attribute.type === 'Binding' ) {
bindings.push( attribute );
}
});
const props = attributes
.map( attribute => {
let value; let value;
if ( attribute.value === true ) { if ( attribute.value === true ) {
@ -32,9 +41,12 @@ export default {
return `${attribute.name}: ${value}`; return `${attribute.name}: ${value}`;
}) })
.filter( Boolean )
.join( ', ' ); .join( ', ' );
bindings.forEach( binding => {
generator.addBinding( binding, node.name );
});
let open = `\${template.components.${node.name}.render({${props}}`; let open = `\${template.components.${node.name}.render({${props}}`;
if ( node.children.length ) { if ( node.children.length ) {

@ -1,12 +1,19 @@
export default { export default {
enter ( generator, node ) { enter ( generator, node ) {
const { snippet } = generator.contextualise( node.expression ); const { snippet } = generator.contextualise( node.expression );
generator.append( '${ ' + snippet + ' ? `' ); generator.append( '${ ' + snippet + ' ? `' );
generator.push({
conditions: generator.current.conditions.concat( snippet )
});
}, },
leave ( generator, node ) { leave ( generator, node ) {
generator.append( '` : `' ); generator.append( '` : `' );
if ( node.else ) node.else.children.forEach( child => generator.visit( child ) ); if ( node.else ) node.else.children.forEach( child => generator.visit( child ) );
generator.append( '` }' ); generator.append( '` }' );
generator.pop();
} }
}; };

@ -1,10 +1,17 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path';
import { compile } from '../index.js'; import { compile } from '../index.js';
function capitalise ( name ) {
return name[0].toUpperCase() + name.slice( 1 );
}
require.extensions[ '.html' ] = function ( module, filename ) { require.extensions[ '.html' ] = function ( module, filename ) {
const { code } = compile( fs.readFileSync( filename, 'utf-8' ), { const { code } = compile( fs.readFileSync( filename, 'utf-8' ), {
filename, filename,
name: capitalise( path.basename( filename ).replace( /\.html$/, '' ) ),
generate: 'ssr' generate: 'ssr'
}); });
return module._compile( code, filename ); return module._compile( code, filename );
}; };

@ -0,0 +1,11 @@
:foo:
<script>
export default {
data () {
return {
x: 1
};
}
};
</script>

@ -0,0 +1,11 @@
{{y}}<Foo bind:y='x'/>{{y}}
<script>
import Foo from './Foo.html';
export default {
components: {
Foo
}
};
</script>

@ -0,0 +1,11 @@
:foo:
<script>
export default {
data () {
return {
x: 1
};
}
};
</script>

@ -0,0 +1,11 @@
{{x}}<Foo bind:x/>{{x}}
<script>
import Foo from './Foo.html';
export default {
components: {
Foo
}
};
</script>
Loading…
Cancel
Save