Frage

I am writing a tool that will add a few lines to the top and the bottom of many JavaScript files using a browserify transform. I am trying to figure out how to generate source maps that include both the original source transformation as well as remove the offset created by my additional lines. For example:

# original source file - test.coffee
console.log "test"

This is transformed using the coffeescript compiler into

// Generated by CoffeeScript 1.6.3
(function() {
      console.log("test");

}).call(this);

/*
//@ sourceMappingURL=test.map
*/

The coffeescript compiler also provides a sourceMappingURL that points to a mapping like

{
  "version": 3,
  "file": "test.js",
  "sourceRoot": "",
  "sources": [
    "test.coffee"
  ],
  "names": [],
  "mappings": ";AAAA;CAAA,CAAA,CAAA,GAAA,CAAO;CAAP"
}

I need to modify the JavaScript file output by adding a few lines before and after.

my.instrumentation.line(1);
my.instrumentation.line(2);
// Generated by CoffeeScript 1.6.3
(function() {
      console.log("test");

}).call(this);
my.instrumentation.line(8);
my.instrumentation.line(9);

I need to figure out how to use the mapping file that the coffeescript compiler provides to map through my changes, and through the coffeescript compiler, all the way back to the original coffeescript source.

At the moment, I can do this source mapping as long as there is no additional sourcemap to work with. For example, if I was starting with JS and not CS, I can generate a sourcemap using Thorsten Lorzen's inline-source-map library and that works correctly as long as there's not a second level of transformation going on. I'm quite stuck with what to do when combining multiple source maps on the same original source.

Any help or advice would be greatly appreciatated.

War es hilfreich?

Lösung

I need to figure out how to use the mapping file that the coffeescript compiler provides to map through my changes, and through the coffeescript compiler, all the way back to the original coffeescript source.

Use the APIs provided by the Mozilla source-map project.

Use the applySourceMap method. It does the following:

Applies a SourceMap for a source file to the SourceMap. Each mapping to the supplied source file is rewritten using the supplied SourceMap. Note: The resolution for the resulting mappings is the minimum of this map and the supplied map.

and the allGeneratedPostionsfor method. It does the following:

Returns all generated line and column information for the original source, line, and column provided. If no column is provided, returns all mappings corresponding to a either the line we are searching for or the next closest line that has any mappings. Otherwise, returns all mappings corresponding to the given line and either the column we are searching for or the next closest column that has any offsets.

Andere Tipps

For me applySourceMap didn't work. When I prepended a snippet, it didn't update the following file references. For me prepending with SourceNode.prototype.prepend(chunk) from the same library https://github.com/mozilla/source-map worked.

https://gist.github.com/prumand/73ae1a01d22029d7969ce8a5dcaa453d

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top