Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple --in-source-map arguments #145

Closed
edc opened this issue Mar 6, 2013 · 29 comments · Fixed by #3058
Closed

Support multiple --in-source-map arguments #145

edc opened this issue Mar 6, 2013 · 29 comments · Fixed by #3058

Comments

@edc
Copy link

edc commented Mar 6, 2013

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?

@justinTNT
Copy link

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

@pharcosyle
Copy link

+1

@edc
Copy link
Author

edc commented Mar 8, 2013

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.

@mishoo
Copy link
Owner

mishoo commented Mar 14, 2013

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).

@edc
Copy link
Author

edc commented Mar 17, 2013

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.)

@TheHippo
Copy link

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

@pyykkis
Copy link

pyykkis commented Mar 20, 2013

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.

@Inversion-des
Copy link

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).

@mishoo
Copy link
Owner

mishoo commented Mar 20, 2013

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

@sokra
Copy link

sokra commented Mar 28, 2013

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.

@mgutz
Copy link

mgutz commented Apr 6, 2013

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.

@russelldavis
Copy link

+1

@dotnetwise
Copy link

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

@ashmoran
Copy link

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.

@mahnunchik
Copy link

+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.

@uiteoi
Copy link

uiteoi commented Jan 21, 2014

+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.

@cpixl
Copy link

cpixl commented Jan 24, 2014

+1

1 similar comment
@juriejan
Copy link

+1

@lydell
Copy link

lydell commented Mar 16, 2014

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)
      )
    )
  }
})

@valpackett
Copy link

+1 to supporting //@sourceMappingURL

@koloboid
Copy link

koloboid commented Aug 6, 2014

+1 to //@sourceMappingURL

@dotnetwise
Copy link

+1 to read the original sources for //@sourceMappingURL

dotnetwise added a commit to dotnetwise/UglifyJS2 that referenced this issue Oct 16, 2014
Fixes mishoo#152
Somehow addresses mishoo#145 by preserving the original sources in the inSourceMap (if they are there)
@Arnavion
Copy link
Contributor

API support for multiple input sourcemaps is at #706

@trycoon
Copy link

trycoon commented Sep 20, 2016

Any progress with this? It's really useful if you ever need to debug a application in production, which most of us do.

@kzc
Copy link
Contributor

kzc commented Sep 20, 2016

Uglify is a volunteer effort. Likely no one is working on this. Pull requests welcome.

adamhooper added a commit to overview/overview-pdf-viewer that referenced this issue May 11, 2017
The upstream approach destroyed the source map, which is too high a
cost. mishoo/UglifyJS#145 prevents UglifyJS
from concatenating source maps, and I'm too lazy to implement any
workarounds.
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Apr 6, 2018
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Apr 6, 2018
@sekoyo
Copy link

sekoyo commented Jun 9, 2019

Hi thanks for merging this support in @alexlamsl. Are there some docs on how to use this?

It's a shame that the sourcemaps can't be automatically detected from the sourceMappingURL comment but anyway I'm trying this:

uglifyjs base.js button.js --source-map "filename='button.map.js'" --output all.js

But it doesn't work, and how do I add multiple (e.g. base.map.js too)? Thanks

@connorjclark
Copy link

It's a shame that the sourcemaps can't be automatically detected from the sourceMappingURL comment but anyway I'm trying this

FWIW I did this here: #3220
and also here: terser/terser#298

@sekoyo
Copy link

sekoyo commented Jun 9, 2019

It's a shame that the sourcemaps can't be automatically detected from the sourceMappingURL comment but anyway I'm trying this

FWIW I did this here: #3220
and also here: terser-js/terser#298

Oh that's a shame they weren't merged, that is the API and logic that I would imagine would be the default solution out the box!

@connorjclark
Copy link

I suggest using that branch if it's a game changer for you. I used it in production at my last job, it worked well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.