diff --git a/gulpfile.js b/gulpfile.js index 2fc0a65931..3b5f9318eb 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -20,6 +20,7 @@ var through2 = require('through2'); var watch = require('gulp-watch'); var js2es5Options = { + sourceMaps: true, annotations: true, // parse annotations types: true, // parse types script: false, // parse as a module @@ -36,6 +37,7 @@ var js2es5OptionsDev = merge(true, js2es5Options, { }); var js2dartOptions = { + sourceMaps: true, annotations: true, // parse annotations types: true, // parse types script: false, // parse as a module diff --git a/karma-dart.conf.js b/karma-dart.conf.js index ecdf4b027e..8980f26c93 100644 --- a/karma-dart.conf.js +++ b/karma-dart.conf.js @@ -57,6 +57,7 @@ module.exports = function(config) { traceurPreprocessor: { options: { outputLanguage: 'dart', + sourceMaps: true, script: false, modules: 'register', types: true, diff --git a/karma-js.conf.js b/karma-js.conf.js index 641dc06662..24b9bebb07 100644 --- a/karma-js.conf.js +++ b/karma-js.conf.js @@ -15,6 +15,8 @@ module.exports = function(config) { 'node_modules/traceur/bin/traceur-runtime.js', 'node_modules/es6-module-loader/dist/es6-module-loader-sans-promises.src.js', + // Including systemjs because it defines `__eval`, which produces correct stack traces. + 'node_modules/systemjs/dist/system.src.js', 'node_modules/systemjs/lib/extension-register.js', 'file2modulename.js', @@ -31,6 +33,7 @@ module.exports = function(config) { traceurPreprocessor: { options: { outputLanguage: 'es5', + sourceMaps: true, script: false, modules: 'instantiate', types: true, diff --git a/tools/transpiler/gulp-traceur.js b/tools/transpiler/gulp-traceur.js index cf1a91b0a3..3f63b35187 100644 --- a/tools/transpiler/gulp-traceur.js +++ b/tools/transpiler/gulp-traceur.js @@ -1,6 +1,7 @@ 'use strict'; var through = require('through2'); var compiler = require('./index'); +var path = require('path'); module.exports = gulpTraceur; gulpTraceur.RUNTIME_PATH = compiler.RUNTIME_PATH; @@ -22,13 +23,26 @@ function gulpTraceur(options, resolveModuleName) { try { var originalFilePath = file.history[0]; var moduleName = resolveModuleName ? resolveModuleName(file.relative) : null; - var compiled = compiler.compile(options, { + var result = compiler.compile(options, { inputPath: file.relative, outputPath: file.relative, moduleName: moduleName }, file.contents.toString()); - file.contents = new Buffer(compiled); - this.push(file); + + var transpiledContent = result.js; + var sourceMap = result.sourceMap; + + if (sourceMap) { + var sourceMapFile = cloneFile(file, { + path: file.path.replace(/\.js$/, '.map'), + contents: JSON.stringify(sourceMap) + }); + + transpiledContent += '\n//# sourceMappingURL=./' + path.basename(sourceMapFile.path); + this.push(sourceMapFile); + } + + this.push(cloneFile(file, {contents: transpiledContent})); done(); } catch (errors) { if (errors.join) { @@ -40,3 +54,8 @@ function gulpTraceur(options, resolveModuleName) { } }); }; + +function cloneFile(file, override) { + var File = file.constructor; + return new File({path: override.path || file.path, cwd: override.cwd || file.cwd, contents: new Buffer(override.contents || file.contents), base: override.base || file.base}); +} diff --git a/tools/transpiler/index.js b/tools/transpiler/index.js index 577facc33a..67aededa4c 100644 --- a/tools/transpiler/index.js +++ b/tools/transpiler/index.js @@ -41,7 +41,19 @@ exports.compile = function compile(options, paths, source) { moduleName: moduleName }); var CompilerCls = System.get('transpiler/src/compiler').Compiler; - return (new CompilerCls(localOptions)).compile(source, inputPath, outputPath); + + var compiler = new CompilerCls(localOptions); + var result = { + js: compiler.compile(source, inputPath, outputPath), + sourceMap: null + }; + + var sourceMapString = compiler.getSourceMap(); + if (sourceMapString) { + result.sourceMap = JSON.parse(sourceMapString); + } + + return result; }; // Transpile and evaluate the code in `src`. diff --git a/tools/transpiler/karma-traceur-preprocessor.js b/tools/transpiler/karma-traceur-preprocessor.js index 26a09f7d67..efbf384170 100644 --- a/tools/transpiler/karma-traceur-preprocessor.js +++ b/tools/transpiler/karma-traceur-preprocessor.js @@ -20,10 +20,23 @@ function createJs2DartPreprocessor(logger, basePath, config, emitter) { if (config.transformPath) { file.path = config.transformPath(file.originalPath); } - done(null, transpiler.compile(config.options, { + + var result = transpiler.compile(config.options, { inputPath: file.originalPath, + outputPath: file.path, moduleName: moduleName - }, content)); + }, content); + + var transpiledContent = result.js; + var sourceMap = result.sourceMap; + + if (sourceMap) { + transpiledContent += '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'; + transpiledContent += new Buffer(JSON.stringify(sourceMap)).toString('base64') + '\n'; + file.sourceMap = sourceMap; + } + + done(null, transpiledContent); } catch (errors) { var errorString; if (errors.forEach) {