From d1c1126b1aca6f5b44772b1ddc19b1651dc78928 Mon Sep 17 00:00:00 2001
From: Rich Harris <richard.a.harris@gmail.com>
Date: Wed, 27 Apr 2016 18:01:36 -0400
Subject: [PATCH] sanity check output of load hook

---
 src/Bundle.js                                     |  6 ++++++
 .../load-returns-string-or-null/_config.js        | 15 +++++++++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 test/function/load-returns-string-or-null/_config.js

diff --git a/src/Bundle.js b/src/Bundle.js
index f5fedc8..d91479f 100644
--- a/src/Bundle.js
+++ b/src/Bundle.js
@@ -167,6 +167,12 @@ export default class Bundle {
 				msg += `: ${err.message}`;
 				throw new Error( msg );
 			})
+			.then( source => {
+				if ( typeof source === 'string' ) return source;
+				if ( source && typeof source === 'object' && source.code ) return source;
+
+				throw new Error( `Error loading ${id}: load hook should return a string, a { code, map } object, or nothing/null` );
+			})
 			.then( source => transform( source, id, this.transformers ) )
 			.then( source => {
 				const { code, originalCode, ast, sourceMapChain } = source;
diff --git a/test/function/load-returns-string-or-null/_config.js b/test/function/load-returns-string-or-null/_config.js
new file mode 100644
index 0000000..ea80699
--- /dev/null
+++ b/test/function/load-returns-string-or-null/_config.js
@@ -0,0 +1,15 @@
+var assert = require( 'assert' );
+
+module.exports = {
+	description: 'throws error if load returns something wacky',
+	options: {
+		plugins: [{
+			load: function () {
+				return 42;
+			}
+		}]
+	},
+	error: function ( err ) {
+		assert.ok( /load hook should return a string, a \{ code, map \} object, or nothing\/null/.test( err.message ) );
+	}
+};