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:
parent
18cdab7450
commit
1dc5a22f07
@ -20,6 +20,7 @@ var through2 = require('through2');
|
|||||||
var watch = require('gulp-watch');
|
var watch = require('gulp-watch');
|
||||||
|
|
||||||
var js2es5Options = {
|
var js2es5Options = {
|
||||||
|
sourceMaps: true,
|
||||||
annotations: true, // parse annotations
|
annotations: true, // parse annotations
|
||||||
types: true, // parse types
|
types: true, // parse types
|
||||||
script: false, // parse as a module
|
script: false, // parse as a module
|
||||||
@ -36,6 +37,7 @@ var js2es5OptionsDev = merge(true, js2es5Options, {
|
|||||||
});
|
});
|
||||||
|
|
||||||
var js2dartOptions = {
|
var js2dartOptions = {
|
||||||
|
sourceMaps: true,
|
||||||
annotations: true, // parse annotations
|
annotations: true, // parse annotations
|
||||||
types: true, // parse types
|
types: true, // parse types
|
||||||
script: false, // parse as a module
|
script: false, // parse as a module
|
||||||
|
@ -57,6 +57,7 @@ module.exports = function(config) {
|
|||||||
traceurPreprocessor: {
|
traceurPreprocessor: {
|
||||||
options: {
|
options: {
|
||||||
outputLanguage: 'dart',
|
outputLanguage: 'dart',
|
||||||
|
sourceMaps: true,
|
||||||
script: false,
|
script: false,
|
||||||
modules: 'register',
|
modules: 'register',
|
||||||
types: true,
|
types: true,
|
||||||
|
@ -15,6 +15,8 @@ module.exports = function(config) {
|
|||||||
|
|
||||||
'node_modules/traceur/bin/traceur-runtime.js',
|
'node_modules/traceur/bin/traceur-runtime.js',
|
||||||
'node_modules/es6-module-loader/dist/es6-module-loader-sans-promises.src.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',
|
'node_modules/systemjs/lib/extension-register.js',
|
||||||
|
|
||||||
'file2modulename.js',
|
'file2modulename.js',
|
||||||
@ -31,6 +33,7 @@ module.exports = function(config) {
|
|||||||
traceurPreprocessor: {
|
traceurPreprocessor: {
|
||||||
options: {
|
options: {
|
||||||
outputLanguage: 'es5',
|
outputLanguage: 'es5',
|
||||||
|
sourceMaps: true,
|
||||||
script: false,
|
script: false,
|
||||||
modules: 'instantiate',
|
modules: 'instantiate',
|
||||||
types: true,
|
types: true,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var through = require('through2');
|
var through = require('through2');
|
||||||
var compiler = require('./index');
|
var compiler = require('./index');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
module.exports = gulpTraceur;
|
module.exports = gulpTraceur;
|
||||||
gulpTraceur.RUNTIME_PATH = compiler.RUNTIME_PATH;
|
gulpTraceur.RUNTIME_PATH = compiler.RUNTIME_PATH;
|
||||||
@ -22,13 +23,26 @@ function gulpTraceur(options, resolveModuleName) {
|
|||||||
try {
|
try {
|
||||||
var originalFilePath = file.history[0];
|
var originalFilePath = file.history[0];
|
||||||
var moduleName = resolveModuleName ? resolveModuleName(file.relative) : null;
|
var moduleName = resolveModuleName ? resolveModuleName(file.relative) : null;
|
||||||
var compiled = compiler.compile(options, {
|
var result = compiler.compile(options, {
|
||||||
inputPath: file.relative,
|
inputPath: file.relative,
|
||||||
outputPath: file.relative,
|
outputPath: file.relative,
|
||||||
moduleName: moduleName
|
moduleName: moduleName
|
||||||
}, file.contents.toString());
|
}, 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();
|
done();
|
||||||
} catch (errors) {
|
} catch (errors) {
|
||||||
if (errors.join) {
|
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});
|
||||||
|
}
|
||||||
|
@ -41,7 +41,19 @@ exports.compile = function compile(options, paths, source) {
|
|||||||
moduleName: moduleName
|
moduleName: moduleName
|
||||||
});
|
});
|
||||||
var CompilerCls = System.get('transpiler/src/compiler').Compiler;
|
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`.
|
// Transpile and evaluate the code in `src`.
|
||||||
|
@ -20,10 +20,23 @@ function createJs2DartPreprocessor(logger, basePath, config, emitter) {
|
|||||||
if (config.transformPath) {
|
if (config.transformPath) {
|
||||||
file.path = config.transformPath(file.originalPath);
|
file.path = config.transformPath(file.originalPath);
|
||||||
}
|
}
|
||||||
done(null, transpiler.compile(config.options, {
|
|
||||||
|
var result = transpiler.compile(config.options, {
|
||||||
inputPath: file.originalPath,
|
inputPath: file.originalPath,
|
||||||
|
outputPath: file.path,
|
||||||
moduleName: moduleName
|
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) {
|
} catch (errors) {
|
||||||
var errorString;
|
var errorString;
|
||||||
if (errors.forEach) {
|
if (errors.forEach) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user