Repository: sveltejs/svelte (JavaScript)
Date: 2018-04-29 01:04:25+00:00
Task type: Bug Fix

Issue:
Stats object different with generate: false
When running compile with `generate: false`, the stats object returned is a quite different to normal

e.g. `generate: false`
```js
Stats {
  startTime: 73029841.411771,
  stack: [],
  timings: 
   [ { label: 'parse',
       start: 73029841.442941,
       end: 73029845.902759,
       children: [] },
     { label: 'stylesheet',
       start: 73029845.913745,
       end: 73029846.028204,
       children: [] },
     { label: 'validate',
       start: 73029846.034336,
       end: 73029849.531215,
       children: [] } ],
  currentChildren: 
   [ { label: 'parse',
       start: 73029841.442941,
       end: 73029845.902759,
       children: [] },
     { label: 'stylesheet',
       start: 73029845.913745,
       end: 73029846.028204,
       children: [] },
     { label: 'validate',
       start: 73029846.034336,
       end: 73029849.531215,
       children: [] } ],
  onwarn: [Function: defaultOnwarn],
  warnings:  [],
  currentTiming: undefined }
```

and `generate: "dom"`
```js
{ timings: 
   { total: 18.931489005684853,
     parse: { total: 4.1629389971494675 },
     stylesheet: { total: 0.11548098921775818 },
     validate: { total: 3.2378130108118057 },
     compile: { total: 11.331197991967201 } },
  warnings: [],
  imports: 
   [ { source: './_components/Layout.html', specifiers: [Array] } ],
  hooks: {} }
```
Stats object different with generate: false
When running compile with `generate: false`, the stats object returned is a quite different to normal

e.g. `generate: false`
```js
Stats {
  startTime: 73029841.411771,
  stack: [],
  timings: 
   [ { label: 'parse',
       start: 73029841.442941,
       end: 73029845.902759,
       children: [] },
     { label: 'stylesheet',
       start: 73029845.913745,
       end: 73029846.028204,
       children: [] },
     { label: 'validate',
       start: 73029846.034336,
       end: 73029849.531215,
       children: [] } ],
  currentChildren: 
   [ { label: 'parse',
       start: 73029841.442941,
       end: 73029845.902759,
       children: [] },
     { label: 'stylesheet',
       start: 73029845.913745,
       end: 73029846.028204,
       children: [] },
     { label: 'validate',
       start: 73029846.034336,
       end: 73029849.531215,
       children: [] } ],
  onwarn: [Function: defaultOnwarn],
  warnings:  [],
  currentTiming: undefined }
```

and `generate: "dom"`
```js
{ timings: 
   { total: 18.931489005684853,
     parse: { total: 4.1629389971494675 },
     stylesheet: { total: 0.11548098921775818 },
     validate: { total: 3.2378130108118057 },
     compile: { total: 11.331197991967201 } },
  warnings: [],
  imports: 
   [ { source: './_components/Layout.html', specifiers: [Array] } ],
  hooks: {} }
```


Issue discussion:

Solution patch:
diff --git a/src/Stats.ts b/src/Stats.ts
--- a/src/Stats.ts
+++ b/src/Stats.ts
@@ -78,7 +78,9 @@ export default class Stats {
 			total: now() - this.startTime
 		}, collapseTimings(this.timings));
 
-		const imports = compiler.imports.map(node => {
+		// TODO would be good to have this info even
+		// if options.generate is false
+		const imports = compiler && compiler.imports.map(node => {
 			return {
 				source: node.source.value,
 				specifiers: node.specifiers.map(specifier => {
@@ -94,9 +96,12 @@ export default class Stats {
 			}
 		});
 
-		const hooks: Record<string, boolean> = {};
-		if (compiler.templateProperties.oncreate) hooks.oncreate = true;
-		if (compiler.templateProperties.ondestroy) hooks.ondestroy = true;
+		const hooks: Record<string, boolean> = compiler && {
+			oncreate: !!compiler.templateProperties.oncreate,
+			ondestroy: !!compiler.templateProperties.ondestroy,
+			onstate: !!compiler.templateProperties.onstate,
+			onupdate: !!compiler.templateProperties.onupdate
+		};
 
 		return {
 			timings,
diff --git a/src/index.ts b/src/index.ts
--- a/src/index.ts
+++ b/src/index.ts
@@ -132,7 +132,7 @@ function compile(source: string, _options: CompileOptions) {
 	stats.stop('validate');
 
 	if (options.generate === false) {
-		return { ast: ast, stats, js: null, css: null };
+		return { ast, stats: stats.render(null), js: null, css: null };
 	}
 
 	const compiler = options.generate === 'ssr' ? generateSSR : generate;


Test patch:
diff --git a/test/stats/index.js b/test/stats/index.js
--- a/test/stats/index.js
+++ b/test/stats/index.js
@@ -56,4 +56,12 @@ describe('stats', () => {
 			}
 		});
 	});
+
+	it('returns a stats object when options.generate is false', () => {
+		const { stats } = svelte.compile('', {
+			generate: false
+		});
+
+		assert.equal(typeof stats.timings.total, 'number');
+	});
 });
diff --git a/test/stats/samples/hooks/_config.js b/test/stats/samples/hooks/_config.js
--- a/test/stats/samples/hooks/_config.js
+++ b/test/stats/samples/hooks/_config.js
@@ -1,7 +1,10 @@
 export default {
 	test(assert, stats) {
 		assert.deepEqual(stats.hooks, {
-			oncreate: true
+			oncreate: true,
+			ondestroy: false,
+			onstate: false,
+			onupdate: false
 		});
 	}
 };
\ No newline at end of file