dont generate intermediate data objects for components unnecessarily (#442)

pull/444/head
Rich-Harris 8 years ago
parent 5c7bc411ec
commit 66d48c4830

@ -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}_initialData` );
if ( initialProps.length ) { const initialPropString = stringifyProps( initialProps );
statements.push( deindent`
var ${name}_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}_initialData` );
return `if ( ${binding.prop} in ${binding.obj} ) ${initialData}.${binding.name} = ${binding.value};`;
statements.push( `var ${name}_initialData = ${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}` ); componentInitProperties.push( `data: ${initialData}` );
} else if ( initialProps.length ) {
componentInitProperties.push( `data: ${initialPropString}` );
}
} }
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