diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index cb53883f66..b7c671910b 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -151,55 +151,6 @@ export default function dom ( parsed, source, options ) { ` ); } - const constructorBlock = new CodeBuilder(); - - constructorBlock.addLine( `options = options || {};` ); - if ( generator.usesRefs ) constructorBlock.addLine( `this.refs = {};` ); - - constructorBlock.addLine( - `this._state = ${templateProperties.data ? `${generator.helper( 'assign' )}( ${generator.alias( 'template' )}.data(), options.data )` : `options.data || {}`};` - ); - - if ( !generator.builders.metaBindings.isEmpty() ) { - constructorBlock.addBlock( generator.builders.metaBindings ); - } - - if ( computations.length ) { - constructorBlock.addLine( - `${generator.alias( 'recompute' )}( this._state, this._state, {}, true );` - ); - } - - if ( options.dev ) { - generator.expectedProperties.forEach( prop => { - constructorBlock.addLine( - `if ( !( '${prop}' in this._state ) ) console.warn( "Component was created without expected data property '${prop}'" );` - ); - }); - - constructorBlock.addBlock( - `if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );` - ); - } - - if ( generator.bindingGroups.length ) { - constructorBlock.addLine( `this._bindingGroups = [ ${Array( generator.bindingGroups.length ).fill( '[]' ).join( ', ' )} ];` ); - } - - constructorBlock.addBlock( deindent` - this._observers = { - pre: Object.create( null ), - post: Object.create( null ) - }; - - this._handlers = Object.create( null ); - - this._root = options._root || this; - this._yield = options._yield; - - ${builders.init} - ` ); - const sharedPath = options.shared === true ? 'svelte/shared.js' : options.shared; const prototypeBase = `${name}.prototype` + ( templateProperties.methods ? `, ${generator.alias( 'template' )}.methods` : '' ); @@ -215,7 +166,26 @@ export default function dom ( parsed, source, options ) { // TODO deprecate component.teardown() builders.main.addBlock( deindent` function ${name} ( options ) { - ${constructorBlock} + options = options || {}; + ${options.dev && `if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`} + ${generator.usesRefs && `this.refs = {};`} + this._state = ${templateProperties.data ? `${generator.helper( 'assign' )}( ${generator.alias( 'template' )}.data(), options.data )` : `options.data || {}`}; + ${generator.builders.metaBindings} + ${computations.length && `${generator.alias( 'recompute' )}( this._state, this._state, {}, true );`} + ${options.dev && Array.from( generator.expectedProperties ).map( prop => `if ( !( '${prop}' in this._state ) ) console.warn( "Component was created without expected data property '${prop}'" );`)} + ${generator.bindingGroups.length && `this._bindingGroups = [ ${Array( generator.bindingGroups.length ).fill( '[]' ).join( ', ' )} ];`} + + this._observers = { + pre: Object.create( null ), + post: Object.create( null ) + }; + + this._handlers = Object.create( null ); + + this._root = options._root || this; + this._yield = options._yield; + + ${builders.init} } ${generator.helper( 'assign' )}( ${prototypeBase}, ${proto}); diff --git a/src/utils/deindent.js b/src/utils/deindent.js index f618dd5277..be20e42ef4 100644 --- a/src/utils/deindent.js +++ b/src/utils/deindent.js @@ -9,9 +9,13 @@ export default function deindent ( strings, ...values ) { let trailingIndentation = getTrailingIndentation( result ); for ( let i = 1; i < strings.length; i += 1 ) { - const expression = values[ i - 1 ]; + let expression = values[ i - 1 ]; const string = strings[i].replace( pattern, '' ); + if ( Array.isArray( expression ) ) { + expression = expression.length ? expression.join( '\n' ) : null; + } + if ( expression || expression === '' ) { const value = String( expression ).replace( /\n/g, `\n${trailingIndentation}` ); result += value + string;