skip definition if unnecessary

pull/1839/head
Rich Harris 7 years ago
parent b026f04fbb
commit f648bb6548

@ -51,6 +51,7 @@ export default class Component {
ast: Ast; ast: Ast;
source: string; source: string;
code: MagicString;
name: string; name: string;
options: CompileOptions; options: CompileOptions;
fragment: Fragment; fragment: Fragment;
@ -77,8 +78,7 @@ export default class Component {
module_exports: Array<{ name: string, as: string }> = []; module_exports: Array<{ name: string, as: string }> = [];
partly_hoisted: string[] = []; partly_hoisted: string[] = [];
fully_hoisted: string[] = []; fully_hoisted: string[] = [];
has_reactive_assignments = false;
code: MagicString;
indirectDependencies: Map<string, Set<string>> = new Map(); indirectDependencies: Map<string, Set<string>> = new Map();
template_references: Set<string> = new Set(); template_references: Set<string> = new Set();
@ -663,6 +663,8 @@ export default class Component {
else { else {
this.code.appendLeft(node.end, `; $$make_dirty('${name}')`); this.code.appendLeft(node.end, `; $$make_dirty('${name}')`);
} }
this.has_reactive_assignments = true;
} }
} }

@ -239,8 +239,6 @@ export default class Expression {
// TODO handle destructuring assignments // TODO handle destructuring assignments
const { name } = flattenReference(node.left); const { name } = flattenReference(node.left);
pending_assignments.add(name); pending_assignments.add(name);
// code.appendLeft(node.end, `; $$make_dirty('${name}')`);
} }
} else { } else {
if (node.type === 'AssignmentExpression') { if (node.type === 'AssignmentExpression') {
@ -290,6 +288,8 @@ export default class Expression {
const insert = [...pending_assignments].map(name => `$$make_dirty('${name}');`); const insert = [...pending_assignments].map(name => `$$make_dirty('${name}');`);
pending_assignments = new Set(); pending_assignments = new Set();
component.has_reactive_assignments = true;
body = deindent` body = deindent`
{ {
const $$result = ${body}; const $$result = ${body};

@ -168,6 +168,10 @@ export default function dom(
}); });
} }
const args = ['$$self'];
if (component.props.length > 0 || component.has_reactive_assignments) args.push('$$props');
if (component.has_reactive_assignments) args.push('$$make_dirty');
builder.addBlock(deindent` builder.addBlock(deindent`
function create_fragment(${component.alias('component')}, ctx) { function create_fragment(${component.alias('component')}, ctx) {
${block.getContents()} ${block.getContents()}
@ -176,22 +180,37 @@ export default function dom(
${component.module_javascript} ${component.module_javascript}
${component.fully_hoisted.length > 0 && component.fully_hoisted.join('\n\n')} ${component.fully_hoisted.length > 0 && component.fully_hoisted.join('\n\n')}
`);
function ${component.alias('define')}($$self, $$props, $$make_dirty) { let has_definition = (
${component.javascript || ( component.javascript ||
component.props.length > 0 && component.props.length > 0 ||
`let { ${component.props.map(x => x.name === x.as ? x.as : `${x.as}: ${x.name}`).join(', ')} } = $$props;` component.partly_hoisted.length > 0 ||
)} component.declarations.length > 0
);
${component.partly_hoisted.length > 0 && component.partly_hoisted.join('\n\n')} const definition = has_definition
? component.alias('define')
: '@noop';
$$self.$$.get = () => ({ ${component.declarations.join(', ')} }); if (has_definition) {
builder.addBlock(deindent`
function ${definition}(${args.join(', ')}) {
${component.javascript || (
component.props.length > 0 &&
`let { ${component.props.map(x => x.name === x.as ? x.as : `${x.as}: ${x.name}`).join(', ')} } = $$props;`
)}
${set && `$$self.$$.set = ${set};`} ${component.partly_hoisted.length > 0 && component.partly_hoisted.join('\n\n')}
${inject_refs && `$$self.$$.inject_refs = ${inject_refs};`} $$self.$$.get = () => ({ ${component.declarations.join(', ')} });
}
`); ${set && `$$self.$$.set = ${set};`}
${inject_refs && `$$self.$$.inject_refs = ${inject_refs};`}
}
`);
}
if (options.customElement) { if (options.customElement) {
builder.addBlock(deindent` builder.addBlock(deindent`
@ -201,7 +220,7 @@ export default function dom(
${css.code && `this.shadowRoot.innerHTML = \`<style>${escape(css.code, { onlyEscapeAtSymbol: true }).replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`} ${css.code && `this.shadowRoot.innerHTML = \`<style>${escape(css.code, { onlyEscapeAtSymbol: true }).replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
@init(this, { target: this.shadowRoot }, ${component.alias('define')}, create_fragment, ${not_equal}); @init(this, { target: this.shadowRoot }, ${definition}, create_fragment, ${not_equal});
${dev_props_check} ${dev_props_check}
@ -221,7 +240,7 @@ export default function dom(
return ${JSON.stringify(component.props.map(x => x.as))}; return ${JSON.stringify(component.props.map(x => x.as))};
} }
${body.join('\n\n')} ${body.length > 0 && body.join('\n\n')}
} }
customElements.define("${component.tag}", ${name}); customElements.define("${component.tag}", ${name});
@ -232,12 +251,12 @@ export default function dom(
constructor(options) { constructor(options) {
super(${options.dev && `options`}); super(${options.dev && `options`});
${should_add_css && `if (!document.getElementById("${component.stylesheet.id}-style")) @add_css();`} ${should_add_css && `if (!document.getElementById("${component.stylesheet.id}-style")) @add_css();`}
@init(this, options, define, create_fragment, ${not_equal}); @init(this, options, ${definition}, create_fragment, ${not_equal});
${dev_props_check} ${dev_props_check}
} }
${body.join('\n\n')} ${body.length > 0 && body.join('\n\n')}
} }
`); `);
} }

@ -389,9 +389,7 @@ export default class ElementWrapper extends Wrapper {
if (this.bindings.length === 0) return; if (this.bindings.length === 0) return;
if (this.node.name === 'select' || this.isMediaNode()) { renderer.component.has_reactive_assignments = true;
this.renderer.hasComplexBindings = true;
}
const needsLock = this.node.name !== 'input' || !/radio|checkbox|range|color/.test(this.getStaticAttributeValue('type')); const needsLock = this.node.name !== 'input' || !/radio|checkbox|range|color/.test(this.getStaticAttributeValue('type'));
@ -415,7 +413,7 @@ export default class ElementWrapper extends Wrapper {
groups.forEach(group => { groups.forEach(group => {
const handler = block.getUniqueName(`${this.var}_${group.events.join('_')}_handler`); const handler = block.getUniqueName(`${this.var}_${group.events.join('_')}_handler`);
this.renderer.component.declarations.push(handler); renderer.component.declarations.push(handler);
const needsLock = group.bindings.some(binding => binding.needsLock); const needsLock = group.bindings.some(binding => binding.needsLock);
@ -521,16 +519,12 @@ export default class ElementWrapper extends Wrapper {
.join(' || '); .join(' || ');
if (this.node.name === 'select' || group.bindings.find(binding => binding.name === 'indeterminate' || binding.isReadOnlyMediaAttribute)) { if (this.node.name === 'select' || group.bindings.find(binding => binding.name === 'indeterminate' || binding.isReadOnlyMediaAttribute)) {
renderer.hasComplexBindings = true;
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`if (${someInitialStateIsUndefined}) @add_render_callback(() => ${callee}.call(${this.var}));` `if (${someInitialStateIsUndefined}) @add_render_callback(() => ${callee}.call(${this.var}));`
); );
} }
if (group.events[0] === 'resize') { if (group.events[0] === 'resize') {
renderer.hasComplexBindings = true;
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`@add_render_callback(() => ${callee}.call(${this.var}));` `@add_render_callback(() => ${callee}.call(${this.var}));`
); );

@ -193,6 +193,8 @@ export default class InlineComponentWrapper extends Wrapper {
} }
const munged_bindings = this.node.bindings.map(binding => { const munged_bindings = this.node.bindings.map(binding => {
component.has_reactive_assignments = true;
const name = component.getUniqueName(`${this.var}_${binding.name}_binding`); const name = component.getUniqueName(`${this.var}_${binding.name}_binding`);
component.declarations.push(name); component.declarations.push(name);

@ -125,6 +125,8 @@ export default class WindowWrapper extends Wrapper {
block.builders.destroy.addBlock(deindent` block.builders.destroy.addBlock(deindent`
window.removeEventListener("${event}", ctx.${handler_name}); window.removeEventListener("${event}", ctx.${handler_name});
`); `);
component.has_reactive_assignments = true;
}); });
// special case... might need to abstract this out if we add more special cases // special case... might need to abstract this out if we add more special cases

@ -63,4 +63,5 @@ SvelteComponent.css = {
}; };
var warned = false; var warned = false;
export default SvelteComponent; export default SvelteComponent;
export { preload };
Loading…
Cancel
Save