@ -5,8 +5,7 @@ import { string_literal } from '../utils/stringify';
import Renderer from './Renderer' ;
import { INode as TemplateNode } from '../nodes/interfaces' ; // TODO
import Text from '../nodes/Text' ;
import { extract_names } from '../utils/scope' ;
import { LabeledStatement , Statement , ExpressionStatement , AssignmentExpression , Node } from 'estree' ;
import { LabeledStatement , Statement , Node } from 'estree' ;
export default function ssr (
component : Component ,
@ -72,37 +71,17 @@ export default function ssr(
} )
: [ ] ;
const injected = Array . from ( component . injected_reactive_declaration_vars ) . filter ( name = > {
const variable = component . var_lookup . get ( name ) ;
return variable . injected ;
} ) ;
const reactive_declarations = component . reactive_declarations . map ( d = > {
const body : Statement = ( d . node as LabeledStatement ) . body ;
let statement = b ` ${ body } ` ;
if ( d . declaration ) {
const declared = extract_names ( d . declaration ) ;
const injected = declared . filter ( name = > {
return name [ 0 ] !== '$' && component . var_lookup . get ( name ) . injected ;
} ) ;
const self_dependencies = injected . filter ( name = > d . dependencies . has ( name ) ) ;
if ( injected . length ) {
// in some cases we need to do `let foo; [expression]`, in
// others we can do `let [expression]`
const separate = (
self_dependencies . length > 0 ||
declared . length > injected . length
) ;
const { left , right } = ( body as ExpressionStatement ) . expression as AssignmentExpression ;
statement = separate
? b `
$ { injected . map ( name = > b ` let ${ name } ; ` ) }
$ { statement } `
: b `
let $ { left } = $ { right } ` ;
}
} else { // TODO do not add label if it's not referenced
if ( ! d . declaration ) { // TODO do not add label if it's not referenced
statement = b ` $ : { ${ statement } } ` ;
}
@ -119,6 +98,8 @@ export default function ssr(
$ { reactive_store_values }
$ { injected . map ( name = > b ` let ${ name } ; ` ) }
$ { reactive_declarations }
$ $rendered = $ { literal } ;
@ -129,6 +110,8 @@ export default function ssr(
: b `
$ { reactive_store_values }
$ { injected . map ( name = > b ` let ${ name } ; ` ) }
$ { reactive_declarations }
return $ { literal } ; ` ;