add lifecycle hooks

pull/1299/head
Rich-Harris 7 years ago
parent 788aa89b41
commit a5d5a6c952

@ -1,4 +1,5 @@
import { Node } from './interfaces'; import { Node } from './interfaces';
import Generator from './generators/Generator';
const now = (typeof process !== 'undefined' && process.hrtime) const now = (typeof process !== 'undefined' && process.hrtime)
? () => { ? () => {
@ -63,31 +64,36 @@ export default class Stats {
this.currentChildren = this.currentTiming ? this.currentTiming.children : this.timings; this.currentChildren = this.currentTiming ? this.currentTiming.children : this.timings;
} }
render({ imports }: { render(generator: Generator) {
imports: Node[]
}) {
const timings = Object.assign({ const timings = Object.assign({
total: now() - this.startTime total: now() - this.startTime
}, collapseTimings(this.timings)); }, collapseTimings(this.timings));
const imports = generator.imports.map(node => {
return {
source: node.source.value,
specifiers: node.specifiers.map(specifier => {
return {
name: (
specifier.type === 'ImportDefaultSpecifier' ? 'default' :
specifier.type === 'ImportNamespaceSpecifier' ? '*' :
specifier.imported.name
),
as: specifier.local.name
};
})
}
});
const hooks: Record<string, boolean> = {};
if (generator.templateProperties.oncreate) hooks.oncreate = true;
if (generator.templateProperties.ondestroy) hooks.ondestroy = true;
return { return {
timings, timings,
warnings: [], // TODO warnings: [], // TODO
imports: imports.map(node => { imports,
return { hooks
source: node.source.value,
specifiers: node.specifiers.map(specifier => {
return {
name: (
specifier.type === 'ImportDefaultSpecifier' ? 'default' :
specifier.type === 'ImportNamespaceSpecifier' ? '*' :
specifier.imported.name
),
as: specifier.local.name
};
})
}
})
}; };
} }
} }

@ -385,9 +385,7 @@ export default class Generator {
ast: this.ast, ast: this.ast,
js, js,
css, css,
stats: this.stats.render({ stats: this.stats.render(this),
imports: this.imports
}),
// TODO deprecate // TODO deprecate
code: js.code, code: js.code,

@ -0,0 +1,7 @@
export default {
test(assert, stats) {
assert.deepEqual(stats.hooks, {
oncreate: true
});
}
};

@ -0,0 +1,7 @@
<script>
export default {
oncreate() {
console.log('creating');
}
};
</script>
Loading…
Cancel
Save