Skip to content

Commit

Permalink
Merge pull request #226 from sveltejs/gh-221
Browse files Browse the repository at this point in the history
SSR – handle component directives at positions other than end
  • Loading branch information
Rich-Harris authored Dec 24, 2016
2 parents 620b9ae + 11dc7dc commit 2a13e29
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/generators/server-side-rendering/visitors/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,32 @@ export default {
}
}

const props = node.attributes.map( attribute => {
if ( attribute.type !== 'Attribute' ) return;

let value;

if ( attribute.value === true ) {
value = `true`;
} else if ( attribute.value.length === 0 ) {
value = `''`;
} else if ( attribute.value.length === 1 ) {
const chunk = attribute.value[0];
if ( chunk.type === 'Text' ) {
value = isNaN( parseFloat( chunk.data ) ) ? JSON.stringify( chunk.data ) : chunk.data;
const props = node.attributes
.map( attribute => {
if ( attribute.type !== 'Attribute' ) return;

let value;

if ( attribute.value === true ) {
value = `true`;
} else if ( attribute.value.length === 0 ) {
value = `''`;
} else if ( attribute.value.length === 1 ) {
const chunk = attribute.value[0];
if ( chunk.type === 'Text' ) {
value = isNaN( parseFloat( chunk.data ) ) ? JSON.stringify( chunk.data ) : chunk.data;
} else {
const { snippet } = generator.contextualise( chunk.expression );
value = snippet;
}
} else {
const { snippet } = generator.contextualise( chunk.expression );
value = snippet;
value = '`' + attribute.value.map( stringify ).join( '' ) + '`';
}
} else {
value = '`' + attribute.value.map( stringify ).join( '' ) + '`';
}

return `${attribute.name}: ${value}`;
}).join( ', ' );
return `${attribute.name}: ${value}`;
})
.filter( Boolean )
.join( ', ' );

let open = `\${template.components.${node.name}.render({${props}}`;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{{foo}}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><p>42</p></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><p>42</p></div>
13 changes: 13 additions & 0 deletions test/server-side-rendering/component-refs-and-attributes/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div><Widget ref:widget foo='{{foo}}'/></div>

<script>
import Widget from './Widget.html';

export default {
components: { Widget },

data () {
return { foo: 42 }
}
};
</script>

0 comments on commit 2a13e29

Please sign in to comment.