Merge pull request #444 from sveltejs/gh-442

dont generate intermediate data objects for components unnecessarily
pull/445/head
Rich Harris 8 years ago committed by GitHub
commit bb5d9cc08b

@ -6,6 +6,18 @@ function capDown ( name ) {
return `${name[0].toLowerCase()}${name.slice( 1 )}`; return `${name[0].toLowerCase()}${name.slice( 1 )}`;
} }
function stringifyProps ( props ) {
if ( !props.length ) return '{}';
const joined = props.join( ', ' );
if ( joined.length > 40 ) {
// make larger data objects readable
return `{\n\t${props.join( ',\n\t' )}\n}`;
}
return `{ ${joined} }`;
}
export default { export default {
enter ( generator, node ) { enter ( generator, node ) {
const hasChildren = node.children.length > 0; const hasChildren = node.children.length > 0;
@ -88,32 +100,28 @@ export default {
const initialProps = local.staticAttributes const initialProps = local.staticAttributes
.concat( local.dynamicAttributes ) .concat( local.dynamicAttributes )
.map( attribute => `${attribute.name}: ${attribute.value}` ); .map( attribute => `${attribute.name}: ${attribute.value}` );
const initialData = current.getUniqueName( `${name}_initial_data` );
if ( initialProps.length ) { const initialPropString = stringifyProps( initialProps );
statements.push( deindent`
var ${initialData} = {
${initialProps.join( ',\n' )}
};
` );
} else {
statements.push( `var ${initialData} = {};` );
}
if ( local.bindings.length ) { if ( local.bindings.length ) {
const bindings = local.bindings.map( binding => { const initialData = current.getUniqueName( `${name}_initial_data` );
return `if ( ${binding.prop} in ${binding.obj} ) ${initialData}.${binding.name} = ${binding.value};`;
statements.push( `var ${name}_initial_data = ${initialPropString};` );
local.bindings.forEach( binding => {
statements.push( `if ( ${binding.prop} in ${binding.obj} ) ${initialData}.${binding.name} = ${binding.value};` );
}); });
statements.push( bindings.join( '\n' ) ); componentInitProperties.push( `data: ${initialData}` );
} else if ( initialProps.length ) {
componentInitProperties.push( `data: ${initialPropString}` );
} }
componentInitProperties.push(`data: ${initialData}`);
} }
const expression = node.name === ':Self' ? generator.name : generator.importedComponents.get( node.name ) || `${generator.alias( 'template' )}.components.${node.name}`; const expression = node.name === ':Self' ? generator.name : generator.importedComponents.get( node.name ) || `${generator.alias( 'template' )}.components.${node.name}`;
local.init.addBlockAtStart( deindent` local.init.addBlockAtStart( deindent`
${statements.join( '\n\n' )} ${statements.join( '\n' )}
var ${name} = new ${expression}({ var ${name} = new ${expression}({
${componentInitProperties.join(',\n')} ${componentInitProperties.join(',\n')}
}); });

Loading…
Cancel
Save