chore: source maps for Karma/Gulp

For Karma, the source mapa are inlined inside each source file.
For `build/*` output, separate `*.map` file is created.

This changes the API of `tools/transpiler/index.js`. I believe this API
is only used in `gulp-traceur.js` and `karma-traceur-preprocessor.js`.
Instead of returning the transpiled string, `compile()` returns a result
object such as:
```js
{
  js: ‘transpiled code’,
  sourceMap: null || {}
}
```

Closes #20
This commit is contained in:
Vojta Jina 2014-10-30 18:52:32 +01:00
parent 18cdab7450
commit 1dc5a22f07
6 changed files with 56 additions and 6 deletions

View File

@ -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

View File

@ -57,6 +57,7 @@ module.exports = function(config) {
traceurPreprocessor: {
options: {
outputLanguage: 'dart',
sourceMaps: true,
script: false,
modules: 'register',
types: true,

View File

@ -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,

View File

@ -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});
}

View File

@ -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`.

View File

@ -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) {