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,17 +64,12 @@ 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));
return { const imports = generator.imports.map(node => {
timings,
warnings: [], // TODO
imports: imports.map(node => {
return { return {
source: node.source.value, source: node.source.value,
specifiers: node.specifiers.map(specifier => { specifiers: node.specifiers.map(specifier => {
@ -87,7 +83,17 @@ export default class Stats {
}; };
}) })
} }
}) });
const hooks: Record<string, boolean> = {};
if (generator.templateProperties.oncreate) hooks.oncreate = true;
if (generator.templateProperties.ondestroy) hooks.ondestroy = true;
return {
timings,
warnings: [], // TODO
imports,
hooks
}; };
} }
} }

@ -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