@ -10,7 +10,7 @@ import { createScopes, extractNames, Scope } from '../utils/annotateWithScopes';
import Stylesheet from './css/Stylesheet' ;
import Stylesheet from './css/Stylesheet' ;
import { test } from '../config' ;
import { test } from '../config' ;
import Fragment from './nodes/Fragment' ;
import Fragment from './nodes/Fragment' ;
import * as internal from '../internal/index ';
import internal_exports from './internal-exports ';
import { Node , Ast , CompileOptions } from '../interfaces' ;
import { Node , Ast , CompileOptions } from '../interfaces' ;
import error from '../utils/error' ;
import error from '../utils/error' ;
import getCodeFrame from '../utils/getCodeFrame' ;
import getCodeFrame from '../utils/getCodeFrame' ;
@ -160,9 +160,12 @@ export default class Component {
: this . name ;
: this . name ;
this . walk_module_js ( ) ;
this . walk_module_js ( ) ;
this . walk_instance_js ( ) ;
this . walk_instance_js _pre_template ( ) ;
this . fragment = new Fragment ( this , ast . html ) ;
this . fragment = new Fragment ( this , ast . html ) ;
this . walk_instance_js_post_template ( ) ;
if ( ! options . customElement ) this . stylesheet . reify ( ) ;
if ( ! options . customElement ) this . stylesheet . reify ( ) ;
this . stylesheet . warnOnUnusedSelectors ( stats ) ;
this . stylesheet . warnOnUnusedSelectors ( stats ) ;
@ -212,8 +215,8 @@ export default class Component {
// TODO use same regex for both
// TODO use same regex for both
result = result . replace ( options . generate === 'ssr' ? /(@+|#+)(\w*(?:-\w*)?)/g : /(@+)(\w*(?:-\w*)?)/g , ( match : string , sigil : string , name : string ) = > {
result = result . replace ( options . generate === 'ssr' ? /(@+|#+)(\w*(?:-\w*)?)/g : /(@+)(\w*(?:-\w*)?)/g , ( match : string , sigil : string , name : string ) = > {
if ( sigil === '@' ) {
if ( sigil === '@' ) {
if ( name in internal) {
if ( internal_exports. has ( name ) ) {
if ( options . dev && ` ${ name } Dev ` in internal ) name = ` ${ name } Dev ` ;
if ( options . dev && internal_exports . has ( ` ${ name } Dev ` ) ) name = ` ${ name } Dev ` ;
helpers . add ( name ) ;
helpers . add ( name ) ;
}
}
@ -510,7 +513,7 @@ export default class Component {
this . module _javascript = this . extract_javascript ( script ) ;
this . module _javascript = this . extract_javascript ( script ) ;
}
}
walk_instance_js () {
walk_instance_js _pre_template () {
const script = this . instance_script ;
const script = this . instance_script ;
if ( ! script ) return ;
if ( ! script ) return ;
@ -545,6 +548,12 @@ export default class Component {
this . track_mutations ( ) ;
this . track_mutations ( ) ;
this . extract_imports_and_exports ( script . content , this . imports , this . props ) ;
this . extract_imports_and_exports ( script . content , this . imports , this . props ) ;
}
walk_instance_js_post_template() {
const script = this . instance_script ;
if ( ! script ) return ;
this . hoist_instance_declarations ( ) ;
this . hoist_instance_declarations ( ) ;
this . extract_reactive_declarations ( ) ;
this . extract_reactive_declarations ( ) ;
this . extract_reactive_store_references ( ) ;
this . extract_reactive_store_references ( ) ;
@ -756,12 +765,13 @@ export default class Component {
// hoistable functions. TODO others?
// hoistable functions. TODO others?
const { hoistable_names , hoistable_nodes , imported_declarations , instance_scope : scope } = this ;
const { hoistable_names , hoistable_nodes , imported_declarations , instance_scope : scope } = this ;
const template_scope = this . fragment . scope ;
const top_level_function_declarations = new Map ( ) ;
const top_level_function_declarations = new Map ( ) ;
this . instance_script . content . body . forEach ( node = > {
this . instance_script . content . body . forEach ( node = > {
if ( node . type === 'VariableDeclaration' ) {
if ( node . type === 'VariableDeclaration' ) {
if ( node . declarations . every ( d = > d . init && d . init . type === 'Literal' && ! this . mutable_props . has ( d . id . name ) ) ) {
if ( node . declarations . every ( d = > d . init && d . init . type === 'Literal' && ! this . mutable_props . has ( d . id . name ) && ! template_scope . containsMutable ( [ d . id . name ] ) ) ) {
node . declarations . forEach ( d = > {
node . declarations . forEach ( d = > {
hoistable_names . add ( d . id . name ) ;
hoistable_names . add ( d . id . name ) ;
} ) ;
} ) ;