Skip to content

Commit 83af7bb

Browse files
authored
Merge pull request #1374 from sveltejs/gh-1368
Return consistently-shaped `stats` object
2 parents ff45a53 + d036931 commit 83af7bb

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/Stats.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export default class Stats {
7878
total: now() - this.startTime
7979
}, collapseTimings(this.timings));
8080

81-
const imports = compiler.imports.map(node => {
81+
// TODO would be good to have this info even
82+
// if options.generate is false
83+
const imports = compiler && compiler.imports.map(node => {
8284
return {
8385
source: node.source.value,
8486
specifiers: node.specifiers.map(specifier => {
@@ -94,9 +96,12 @@ export default class Stats {
9496
}
9597
});
9698

97-
const hooks: Record<string, boolean> = {};
98-
if (compiler.templateProperties.oncreate) hooks.oncreate = true;
99-
if (compiler.templateProperties.ondestroy) hooks.ondestroy = true;
99+
const hooks: Record<string, boolean> = compiler && {
100+
oncreate: !!compiler.templateProperties.oncreate,
101+
ondestroy: !!compiler.templateProperties.ondestroy,
102+
onstate: !!compiler.templateProperties.onstate,
103+
onupdate: !!compiler.templateProperties.onupdate
104+
};
100105

101106
return {
102107
timings,

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function compile(source: string, _options: CompileOptions) {
132132
stats.stop('validate');
133133

134134
if (options.generate === false) {
135-
return { ast: ast, stats, js: null, css: null };
135+
return { ast, stats: stats.render(null), js: null, css: null };
136136
}
137137

138138
const compiler = options.generate === 'ssr' ? generateSSR : generate;

test/stats/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ describe('stats', () => {
5656
}
5757
});
5858
});
59+
60+
it('returns a stats object when options.generate is false', () => {
61+
const { stats } = svelte.compile('', {
62+
generate: false
63+
});
64+
65+
assert.equal(typeof stats.timings.total, 'number');
66+
});
5967
});

test/stats/samples/hooks/_config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
export default {
22
test(assert, stats) {
33
assert.deepEqual(stats.hooks, {
4-
oncreate: true
4+
oncreate: true,
5+
ondestroy: false,
6+
onstate: false,
7+
onupdate: false
58
});
69
}
710
};

0 commit comments

Comments
 (0)