escodegen
escodegen copied to clipboard
How to generate sourcemap from AST transform?
I see in the Wiki at https://github.com/estools/escodegen/wiki/Source-Map-Usage an example with Esprima, but it doesn't include a transform step:
var ast = esprima.parse('var a = 1', {
loc: true,
source: 'my_file.js'
});
var output = escodegen.generate(ast, {
sourceMap: true, // Settings source in esprima's options gives us
// filenames already.
sourceMapWithCode: true // Get both code and source map
});
var code = output.code; // Generated source code
var map = output.map.toString(); // Generated source map JSON
Is it just as easy with a transform, like the following (haven't tried yet)?
var ast = esprima.parse('var a = 1', {
loc: true,
source: 'my_file.js'
});
estraverse.replace(ast, transform); // <-- added this line.
var output = escodegen.generate(ast, {
sourceMap: true, // Settings source in esprima's options gives us
// filenames already.
sourceMapWithCode: true // Get both code and source map
});
var code = output.code; // Generated source code
var map = output.map.toString(); // Generated source map JSON
If that works, I have another question: what if we have an AST generated from code that was already previously transformed and includes a sourcemap comment at the bottom?