Search code examples
terser

Terser - How to includeSources when original map file also contains source?


I'm building index.js using the typescript transpiler, which produces index.js.map containing the original typescript files.

I would now like to compress that file and preserve the original source in the index.js.map file so I can step through the typescript code and not the transpiled code.

The script I'm running:

terser index.js --source-map \"url='./index.js.map',includeSources\" -o index.js -c

Replaces index.js with a compressed version and replaces index.js.map file with the transpiled source and not the original typescript source. Is terser able to preserve the source in the sourcemap file?


Solution

  • The answer is readily available in the documentation:

    Composed source map When you're compressing JS code that was output by a compiler such as CoffeeScript, mapping to the JS code won't be too helpful. Instead, you'd like to map back to the original code (i.e. CoffeeScript). Terser has an option to take an input source map. Assuming you have a mapping from CoffeeScript → compiled JS, Terser can generate a map from CoffeeScript → compressed JS by mapping every token in the compiled JS to its original location.

    To use this feature pass --source-map "content='/path/to/input/source.map'" or --source-map "content=inline" if the source map is included inline with the sources.