UglifyJS icon indicating copy to clipboard operation
UglifyJS copied to clipboard

Invalid sourcemaps being created

Open nzakas opened this issue 11 years ago • 13 comments

I've been using Uglify to generate source maps via the API, such as:

var result = uglify.minify(filename, { outSourceMap: mapFilename });

When I do this, the resulting sourcemap had the following errors:

  1. The file field is mapFilename instead of filename
  2. The filename in sources field contains extra path information, which confuses Dev tools (should have the path info as relative to file only)

nzakas avatar May 02 '15 18:05 nzakas

Having the same problem. Source Maps unusable in their current state.

getify avatar May 17 '15 03:05 getify

I'm working on a patch for this, but I think I've come to the conclusion that we need another option added (or at least a redefined one) for it to truly work correctly. Here's my thinking thus far (please input):

  1. outSourceMap is not a filesystem path. It's supposed to be a relative URL (including filename) . But relative to what? Perhaps relative to sourceRoot? More on that later.

  2. According to the documentation, outSourceMap is supposed to set the file attribute inside the sourcemap. But as @nzakas identified, that's incorrect. According to the spec, the file attribute should be the relative path+filename of the generated file (aka the minified file) the sourcemap is mapping to.

    But relative to what? According to the spec, all non-absolute URLs inside the sourcemap have to be relative to the location of the sourcemap itself.

    Moreover, the relative URL paths shown in the sources list (which are supposed to map to the original files) are again all relative to the sourcemap location.

  3. But herein lies the problem. We don't know where the sourcemap is going to be stored, so we can't possibly know how to make file and all the sources relative to it.

So, should we re-define outSourceMap to be the URL of where the map is stored relative to the sourceRoot? That would make sense, but then it means we have to make sourceRoot required if using source maps.

Or, should we specify another option, similar to how the CLI tool works with a outSourceMap and a outSourceMapURL? What then would outSourceMap be used for? And which setting would be what's appended to the end of the generated file in the //#sourceURL=.. comment?

Guidance appreciated as I work on this patch.

getify avatar May 17 '15 14:05 getify

Scenario 1

working directory: / (absolute filesystem path) file to be minified: /foo.js (absolute filesystem path) outSourceMap: foo.min.js.map (relative URL path)

/foo.min.js.map (URL: http://some.tld/foo.min.js.map):

{
    // ..
    file: "foo.min.js", // <-- how could we possibly know this filename in this URL path!?
    sources: [ "foo.js" ]
    // ..
}

/foo.min.js (URL: http://some.tld/foo.min.js):

// ..
//# sourceMappingURL=http://some.tld/foo.min.js.map

Scenario 2

working directory: / (absolute filesystem path) file to be minified: /1/foo.js (absolute filesystem path) outSourceMap: /2/foo.min.js.map (absolute URL path)

/2/foo.min.js.map (URL: http://some.tld/2/foo.min.js.map):

{
    // ..
    file: "../1/foo.min.js", // <-- how could we possibly know this filename in this URL path!?
    sources: [ "../foo.js" ]
    // ..
}

/1/foo.min.js (URL: http://some.tld/1/foo.min.js):

// ..
//# sourceMappingURL=http://some.tld/2/foo.min.js.map

Scenario 3

working directory: /1/ (absolute filesystem path) file to be minified: 2/3/4/5/foo.js (relative filesystem path) sourceRoot: ../4/ (relative URL path) outSourceMap: ../maps/foo.min.js.map (relative URL path)

/1/2/3/maps/foo.min.js.map (URL: http://some.tld/3/maps/foo.min.js.map):

{
    // ..
    file: "/3/4/5/foo.min.js", // <-- how could we possibly know this filename in this URL path!?
    sourceRoot: "../4/",
    sources: [ "5/foo.js" ]
    // ..
}

/1/2/3/4/5/foo.min.js (URL: http://some.tld/3/4/5/foo.min.js):

// ..
//# sourceMappingURL=http://some.tld/3/maps/foo.min.js.map

getify avatar May 18 '15 02:05 getify

Clear communication seems to be the issue here that we seem to adress I guess.

avdg avatar May 18 '15 12:05 avdg

OK, after working a lot more on this, I believe my earlier suggestions are the wrong way to go. I would like some feedback on these ideas, as I prepare a PR, please. Obviously, this stuff represents breaking changes to sourcemap support, so it'll have to be coordinated into a version update schedule.

Basically, I don't think we should try to make uglify smart enough to figure out all this relative pathing stuff automatically. Too many complicated scenarios to bake into the lib.

So here's my proposal instead:

TL;DR

  1. Add more options (and do some renaming for sanity) to give finer control over the data that's put into the sourcemap, so that the calling program can figure out what the various paths should be.
  2. Change the default behavior from inclulding the full file paths in the sources list of the map to including only the base filename.

Relatedly, this strategy is compatible with how Babel's sourcemap handling is (and is improving towards, from some issues I've filed). That means that users of both tools don't have to do so much work to figure out how to create compatible sourcemaps.

Details

The default behavior is changed for the entries in sources. Instead of matching the files passed to Uglify.minify(..), the sources list will now only include the base filenames for each entry. This prevents accidentally leaking private/sensitive local filesystem paths out into a publicly accessible sourcemap file.

API Options

The following are the new proposed sourcemap options (some added, some renamed). All of these are optional.

  • sourceMap (default: false): true/false to turn on or off sourcemap generation. Note: currently, the presence of the outSourceMap setting (used for the spec-optional file setting) is used to turn on/off sourcemap generation, which is incorrect overloaded behavior.
  • sourceMapIn (default: null): same as inSourceMap before. If specified, is either the previous sourcemap object itself, or the filepath to read a previous sourcemap in, for chaining sourcemaps together.
  • sourceMapRoot (default: null): same as sourceRoot before. If specified, sets the sourceRoot setting in the sourcemap. Used by a consumer of the sourcemap to prepend the value to any relative paths in other settings (file, sources, etc).
  • sourceMapTarget (default: null): sets the filesetting in the sourcemap. Note: this is currently set by the outSourceMap option, whose name misleads to suggest it should be the name of the sourcemap file, when in fact it should be the target of the mapping (the minified file).
  • sourceMapSources (default : [ ]): an array of sanitized/formatted filepaths to override the filenames listed in sources. Must be in the same order as the files passed to Uglify.minify(..). Pass "" in any entry to skip overriding that entry (and use the default base filename). Note: these are currently incorrectly being listed with full local filesystem paths (if that's what's passed to minify(..)), which is very bad.
  • sourceMapURL (default: null): if set to a string, adds the //# sourceMappingURL=.. comment to the end of the minified file with the string value specified. If set to true, adds comment but uses the dataURI encoded content of the sourcemap as the URL, which is necessary for certain Node.js debugging tools that cannot use an external map file. If the option is omitted, the comment is not added. Note: this comment is currently being set by the outSourceMap option, which is incorrect.

CLI

The CLI needs to be updated as well, to match this new and corrected sourcemap behavior. Specifically, these are the things that seem to need to change:

  • --source-map should be required to turn on the sourceMap API option. Its value, if provided, should only be used by the CLI tool for writing out the sourcemap file. If no value passed, sourcemap content should be sent to STDOUT. The API will not use this option's value at all.
  • --in-source-map should be renamed to --source-map-in, but otherwise work identically (corresponding to the sourceMapIn API option).
  • --source-map-root corresponds directly to the sourceMapRoot API option as described above. Its value is not used in any way for modifying paths -- purely informational to add to the sourcemap output.
  • --source-map-target should be added, and correspond to the sourceMapTarget option as described above, but only if a value is provided. Otherwise, should be ignored.
  • --prefix and -p should be removed from sourcemap functionality. Accordingly, the CLI's P_RELATIVE behavior for the sourcemaps should be removed.
  • --source-map-sources should be added, and correspond to the sourceMapSources API option as described above. It should be a space-separated list of source paths to include in the source map.
  • --source-map-url should change to the semantics of the sourceMapURL option as described above. If passed but no value set, assume the true behavior. If omitted, no comment will be added at all.

Documentation & Tests

Of course, all docs and tests need to be updated to reflect all this information.

Note: The options that can be manually passed to UglifyJS.SourceMap(..) (aka, "the hard way") are not being changed at all. The documentation should more clearly explain the relationship between these options and the ones described above in the API options.

getify avatar May 20 '15 15:05 getify

BTW, here's the running list of changes I've made so far towards a PR for the above proposal:

https://github.com/mishoo/UglifyJS2/compare/c6fa2915715f0a1a2b1527c23388bd7de56284c4...getify:master

As you can see, the changes are not really all that invasive. :)

getify avatar May 20 '15 15:05 getify

Update: working on the node CLI changes, and ran into an issue with yargs... they've posted a patch that I'm now testing, then I should be able to post my CLI changes into the above repo diff.

getify avatar May 27 '15 13:05 getify

OK, worked out the issues with yargs and they're releasing an update that addresses the needs I have here. And I've now posted the code that brings both the API and the CLI up to "spec" of what I outlined above. Again, here's that diff: https://github.com/mishoo/UglifyJS2/compare/c6fa2915715f0a1a2b1527c23388bd7de56284c4...getify:master

Would still very much like feedback on this, as I'm about to formalize it as a PR.

getify avatar May 28 '15 02:05 getify

@getify We've (NodeBB) been using a fork of UglifyJS2 with some fixes by @bfrohs: https://github.com/mishoo/UglifyJS2/pull/192

However the implementation there, while workable, is prone to breaking and thus I'm moving away from it. The PR is also over a year old, so it stands almost no chance of being merged in now, I think.

Unfortunately means NodeBB won't have source maps until your PR is created and accepted, but I'm willing to wait :smile:

As for my use case:

  • Majority of my minified files are in public/, relative to app root
  • However I do have some files found in node_modules/{some_plugin}
    • I jumped through some hoops and had the app serve these files (via express res.sendFile(), iirc), but obviously not an ideal solution
  • I save the minfile (and source map, if provided) to memory, and serve them via express
app.get('/nodebb.min.js', function(req, res) {
    res.type('text/javascript').send(meta.js.cache);
});

julianlam avatar Jul 06 '15 17:07 julianlam

@julianlam :+1:

getify avatar Jul 06 '15 18:07 getify

We should incorporate this to fix the problems with sourcemaps not being chained together properly: https://github.com/azu/multi-stage-sourcemap

getify avatar Jul 19 '15 13:07 getify

@getify A PR would be most welcome. As it will probably will lead to backwards incompatibility, some coordination on versioning will have to be discussed with @mishoo.

Thanks for the effort, and sorry for the huge delay.

rvanvelzen avatar Jul 30 '15 09:07 rvanvelzen

I'm using the node module for a Wintersmith app and the source files in the source map use the wrong directory:

{"version":3,"sources":["./contents/js/some-file.js",...

The the sourceMapSources option should fix this. I also noticed that this plugin, requires you to place all JS files in the root directory instead of contents: https://github.com/magicmonty/wintersmith-uglifyjs ...probably for the same reason.

Anyway...just giving this a +1.

Edit: To solve I've just been doing a "cd ./contents && uglify-script.js && cd ../"

tkane2000 avatar Dec 05 '15 20:12 tkane2000