UglifyJS icon indicating copy to clipboard operation
UglifyJS copied to clipboard

Support multiple --in-source-map arguments

Open edc opened this issue 13 years ago • 29 comments

When using UglifyJS2 to concatenate multiple JavaScript source files generated by compile-to-JS transpilers like CoffeeScript, since each JS file has its own source map file, it is critical to have UglifyJS2 supports multiple --in-source-map arguments so it can consolidate the source map files as it concatenates the JavaScript source files. Or did I miss anything obvious?

edc avatar Mar 06 '13 03:03 edc

yes, it's a great tool, but this is sorely missing.

justinTNT avatar Mar 06 '13 12:03 justinTNT

+1

pharcosyle avatar Mar 07 '13 20:03 pharcosyle

For those interested, I created a very simple utility called mapcat that consolidates source map files while concatenating js files. It's very primitive at the moment, but I can use it as the step before UglifyJS2. Feel free to expand it as you see fit.

edc avatar Mar 08 '13 19:03 edc

Handling this through an external tool like mapcat seems totally reasonable.

Perhaps a good solution (at UglifyJS level) would be to scan the input files for the //@sourceMappingURL comment, and use that map if we can find it? What do you think? (it's somewhat tricky to implement, however).

mishoo avatar Mar 14 '13 10:03 mishoo

That is indeed tricky. How about just allow people to specify multiple --in-source-map, like:

uglify --in-source-map module1.map --in-source-map module2.map

You could also allow regular JS - those hand-written and without associated map file - to be supplied as well:

uglify --in-source-map module1.map --in-source-map module2.map module3.js

(mapcat currently does not support this. otherwise it could be a used as a good preprocessor.)

edc avatar Mar 17 '13 04:03 edc

+1 (or someone gets the coffeescript compiler to properly join multiple files and their source maps.)

TheHippo avatar Mar 17 '13 21:03 TheHippo

Browserify took a bit different route (which I find quite compelling):

browserify('main.coffee').transform(coffeeify).bundle({debug: true})

builds a single file with main.coffee and all dependencies included. Source map is inlined at the end of result stream with base64 encoding. Works like a charm.

Could Uglify decode the source map and replace it with a multilevel source map? I'd love to do something like

browserify('main.coffee')
  .transform(coffeeify)
  .bundle({debug: true})
  .pipe(UglifyJS.transformStream({debug: true})
  .pipe(fs.createWriteStream('dist.js'));

..and have multilevel sourcemap inlined all the way.

jkeskikangas avatar Mar 20 '13 06:03 jkeskikangas

Also was surprised to find out than uglifyjs supports joining multiple files generated by CoffeeScript, but cannot use sourceMappingURL included in every such file (instead of using --in-source-map).

Inversion-des avatar Mar 20 '13 15:03 Inversion-des

@Inversion-des That's the plan. I'll do that.

mishoo avatar Mar 20 '13 15:03 mishoo

I recently implemented some new features to the source-map library which should make it really easy to work with SourceMaps:

// Concatenating files including SourceMaps
new SourceNode(null, null, null, [
  SourceNode.fromStringWithSourceMap(contentFile1, sourceMapFile1),
  SourceNode.fromStringWithSourceMap(contentFile2, sourceMapFile2)
]).toStringWithSourceMap()
// => { code, map }
// Mutliple levels of SourceMaps (like coffee -> js -> min)
var aggregatedMap = SourceMapGenerator.fromSourceMap(sourceMapJsToMinJs);
aggregatedMap.applySourceMap(sourceMapCoffeeScriptToJs);
aggregatedMap.toString()

Even if uglifyjs wouldn't support --in-source-file, one can easily build a small tool which aggregates the SourceMaps.


@mishoo You can use this by just ignoring the inSourceMap while minimizing and apply all inSourceMaps after that process.

sokra avatar Mar 28 '13 19:03 sokra

I've been doing this for a while with projmate, which is a cross between grunt and browserify. See coffee-backbone-sourcemap example. Basically, the project compiles all CoffeeScript and JavaScript in a directory into a single CommonJS module (based on stitch) into a separate build directory. Most transpliers generate the map in the same directory which is annoying.

mgutz avatar Apr 06 '13 04:04 mgutz

+1

russelldavis avatar Jul 18 '13 21:07 russelldavis

Hai Mihai, dă-i bice ! This is a MUST have basic feature that needs to be supported when using with r.js

dotnetwise avatar Jul 22 '13 22:07 dotnetwise

I'm really new to JavaScript, never mind source maps, so I just ran unwittingly into this. It'd be a big help if UglifyJS could read multiple source maps, as it would remove the mapcat step from my build pipeline. Using mapcat works fine, but involved an hour or two of searching around and plugging it in. It's a shame UglifyJS can read in multiple JS files but not multiple source maps, as it makes the feature a bit asymmetric.

ashmoran avatar Jul 30 '13 11:07 ashmoran

+1 to automatic detection comment like //@sourceMappingURL

It solve the problem of joining multiple files with source map and without (https://github.com/pmuellr/cat-source-map) . Several --in-source-map options look ugly. Also It can be a source of error related to the files order.

mahnunchik avatar Jan 21 '14 10:01 mahnunchik

+1 for automatic detection for sourceMappingURL as this would simultaneously fix issue #151.

This could also deprecate the use of --in-source-map option resulting in a much cleaner and easy to use API.

uiteoi avatar Jan 21 '14 11:01 uiteoi

+1

cpixl avatar Jan 24 '14 03:01 cpixl

+1

juriejan avatar Feb 11 '14 16:02 juriejan

I’ve written source-map-resolve which can resolve a source map from a sourceMappingURL comment in a file. This is roughly how you could use it:

var resolveSourceMapSync = require("source-map-resolve").resolveSourceMapSync
var SourceMapConsumer    = require("source-map").SourceMapConsumer

// `files` is an array of {path: ..., content: ...} objects.
// `minifiedMap` is the source map UglifyJS has produced.
// `minifiedOutputPath` is the path to where `minifiedMap` will be written.

files.forEach(function(file) {
  var previousMap = resolveSourceMapSync(file.content, file.path, fs.readFileSync)
  if (previousMap) {
    minifiedMap.applySourceMap(
      new SourceMapConsumer(previousMap.map),
      file.path,
      path.relative(
        path.dirname(minifiedOutputPath),
        path.dirname(previousMap.sourcesRelativeTo)
      )
    )
  }
})

lydell avatar Mar 16 '14 12:03 lydell

+1 to supporting //@sourceMappingURL

valpackett avatar Jul 15 '14 10:07 valpackett