Skip to content

Commit 1dc5a22

Browse files
committed
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 angular#20
1 parent 18cdab7 commit 1dc5a22

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

gulpfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var through2 = require('through2');
2020
var watch = require('gulp-watch');
2121

2222
var js2es5Options = {
23+
sourceMaps: true,
2324
annotations: true, // parse annotations
2425
types: true, // parse types
2526
script: false, // parse as a module
@@ -36,6 +37,7 @@ var js2es5OptionsDev = merge(true, js2es5Options, {
3637
});
3738

3839
var js2dartOptions = {
40+
sourceMaps: true,
3941
annotations: true, // parse annotations
4042
types: true, // parse types
4143
script: false, // parse as a module

karma-dart.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module.exports = function(config) {
5757
traceurPreprocessor: {
5858
options: {
5959
outputLanguage: 'dart',
60+
sourceMaps: true,
6061
script: false,
6162
modules: 'register',
6263
types: true,

karma-js.conf.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = function(config) {
1515

1616
'node_modules/traceur/bin/traceur-runtime.js',
1717
'node_modules/es6-module-loader/dist/es6-module-loader-sans-promises.src.js',
18+
// Including systemjs because it defines `__eval`, which produces correct stack traces.
19+
'node_modules/systemjs/dist/system.src.js',
1820
'node_modules/systemjs/lib/extension-register.js',
1921

2022
'file2modulename.js',
@@ -31,6 +33,7 @@ module.exports = function(config) {
3133
traceurPreprocessor: {
3234
options: {
3335
outputLanguage: 'es5',
36+
sourceMaps: true,
3437
script: false,
3538
modules: 'instantiate',
3639
types: true,

tools/transpiler/gulp-traceur.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var through = require('through2');
33
var compiler = require('./index');
4+
var path = require('path');
45

56
module.exports = gulpTraceur;
67
gulpTraceur.RUNTIME_PATH = compiler.RUNTIME_PATH;
@@ -22,13 +23,26 @@ function gulpTraceur(options, resolveModuleName) {
2223
try {
2324
var originalFilePath = file.history[0];
2425
var moduleName = resolveModuleName ? resolveModuleName(file.relative) : null;
25-
var compiled = compiler.compile(options, {
26+
var result = compiler.compile(options, {
2627
inputPath: file.relative,
2728
outputPath: file.relative,
2829
moduleName: moduleName
2930
}, file.contents.toString());
30-
file.contents = new Buffer(compiled);
31-
this.push(file);
31+
32+
var transpiledContent = result.js;
33+
var sourceMap = result.sourceMap;
34+
35+
if (sourceMap) {
36+
var sourceMapFile = cloneFile(file, {
37+
path: file.path.replace(/\.js$/, '.map'),
38+
contents: JSON.stringify(sourceMap)
39+
});
40+
41+
transpiledContent += '\n//# sourceMappingURL=./' + path.basename(sourceMapFile.path);
42+
this.push(sourceMapFile);
43+
}
44+
45+
this.push(cloneFile(file, {contents: transpiledContent}));
3246
done();
3347
} catch (errors) {
3448
if (errors.join) {
@@ -40,3 +54,8 @@ function gulpTraceur(options, resolveModuleName) {
4054
}
4155
});
4256
};
57+
58+
function cloneFile(file, override) {
59+
var File = file.constructor;
60+
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});
61+
}

tools/transpiler/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ exports.compile = function compile(options, paths, source) {
4141
moduleName: moduleName
4242
});
4343
var CompilerCls = System.get('transpiler/src/compiler').Compiler;
44-
return (new CompilerCls(localOptions)).compile(source, inputPath, outputPath);
44+
45+
var compiler = new CompilerCls(localOptions);
46+
var result = {
47+
js: compiler.compile(source, inputPath, outputPath),
48+
sourceMap: null
49+
};
50+
51+
var sourceMapString = compiler.getSourceMap();
52+
if (sourceMapString) {
53+
result.sourceMap = JSON.parse(sourceMapString);
54+
}
55+
56+
return result;
4557
};
4658

4759
// Transpile and evaluate the code in `src`.

tools/transpiler/karma-traceur-preprocessor.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,23 @@ function createJs2DartPreprocessor(logger, basePath, config, emitter) {
2020
if (config.transformPath) {
2121
file.path = config.transformPath(file.originalPath);
2222
}
23-
done(null, transpiler.compile(config.options, {
23+
24+
var result = transpiler.compile(config.options, {
2425
inputPath: file.originalPath,
26+
outputPath: file.path,
2527
moduleName: moduleName
26-
}, content));
28+
}, content);
29+
30+
var transpiledContent = result.js;
31+
var sourceMap = result.sourceMap;
32+
33+
if (sourceMap) {
34+
transpiledContent += '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,';
35+
transpiledContent += new Buffer(JSON.stringify(sourceMap)).toString('base64') + '\n';
36+
file.sourceMap = sourceMap;
37+
}
38+
39+
done(null, transpiledContent);
2740
} catch (errors) {
2841
var errorString;
2942
if (errors.forEach) {

0 commit comments

Comments
 (0)