Alter AST before creating sourcemap

Sep 11, 2013 at 1:57 PM
I am wondering if the following is possible and if so, how I would go about doing it?

If I have the following JavaScript.
function define() {
    // do something

define(function () {
    console.log('Hello world'); 
And I want to minify it as well as generate a corresponding sourcemap.
But before I minify the code I wish to make the following change dynamically.
define('mykey', function () {
    console.log('Hello world');
I guess it will be easy to alter the AST but I am thinking that my sourcemaps will be wrong w.r.t the original content. The generated sourcemap needs to map back to the original content which does not contain 'mykey'.


Jacob T. Nielsen
Sep 11, 2013 at 2:58 PM
I think if you did that, the source map just wouldn't have any entry for the minimized items that weren't in the original source. I don't think it would be a big issue for the parameter, because the script debugger probably wouldn't be stopping at that point in the minimized source anyway. But for the statement, the debugger would stop at the opening brace in the original source, since that would be the last token in the minified source corresponding to the point at which it is stopping. Highlighting might be off, too - it would be worthwhile to give it a try and see. And of course, you'd be looking at the original source thinking, "why is it stopping here? What the heck is going on?" If there was a line in the source after the point where you inserted the line, stepping over the inserted line should get the source map right back on track. But that's a really interesting point: if you add stuff to the tree that isn't in the source and then generate a source map, weird things may happen when stepping into the "code." I'll try to think about possible way to minimize the effect.