From de39f923598a91d9178ce8a71c9969c28df95b54 Mon Sep 17 00:00:00 2001 From: Christian Alfoni Date: Sun, 28 Dec 2014 14:23:28 +0100 Subject: [PATCH] Moved runtime inclusion to after compilation, due to generator hoisting --- index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index bdb1028..8986191 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ module.exports = function(source) { var map; var options = {}; var result; + var runtime; this.cacheable && this.cacheable(); @@ -38,20 +39,26 @@ module.exports = function(source) { } }); + // Move runtime option from options to variable + runtime = options.runtime; + delete options.runtime; + // Handle Traceur runtime if(filename === traceur.RUNTIME_PATH) { return content; } - if(options.runtime) { - content = 'require("' + traceur.RUNTIME_PATH + '");' + content; - } // Parse code through Traceur try { - delete options.runtime; var compiler = new traceur.Compiler(options); result = compiler.compile(content, filename); + // Include runtime after compilation due to generators hoisting the + // runtime usage to the very top + if(runtime) { + result = 'require("' + traceur.RUNTIME_PATH + '");' + result; + } + // Process source map (if available) and return result if(options.sourceMaps) { map = JSON.parse(compiler.getSourceMap());