Invalid sourcemaps being created
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:
- The
filefield ismapFilenameinstead offilename - The filename in
sourcesfield contains extra path information, which confuses Dev tools (should have the path info as relative tofileonly)
Having the same problem. Source Maps unusable in their current state.
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):
-
outSourceMapis not a filesystem path. It's supposed to be a relative URL (including filename) . But relative to what? Perhaps relative tosourceRoot? More on that later. -
According to the documentation,
outSourceMapis supposed to set thefileattribute inside the sourcemap. But as @nzakas identified, that's incorrect. According to the spec, thefileattribute 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
sourceslist (which are supposed to map to the original files) are again all relative to the sourcemap location. -
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
fileand all thesourcesrelative 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.
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
Clear communication seems to be the issue here that we seem to adress I guess.
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
- 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.
- Change the default behavior from inclulding the full file paths in the
sourceslist 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 theoutSourceMapsetting (used for the spec-optionalfilesetting) is used to turn on/off sourcemap generation, which is incorrect overloaded behavior.sourceMapIn(default: null): same asinSourceMapbefore. 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 assourceRootbefore. If specified, sets thesourceRootsetting 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 thefilesetting in the sourcemap. Note: this is currently set by theoutSourceMapoption, 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 insources. Must be in the same order as thefilespassed toUglify.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 tominify(..)), 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 totrue, 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 theoutSourceMapoption, 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-mapshould be required to turn on thesourceMapAPI 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-mapshould be renamed to--source-map-in, but otherwise work identically (corresponding to thesourceMapInAPI option).--source-map-rootcorresponds directly to thesourceMapRootAPI 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-targetshould be added, and correspond to thesourceMapTargetoption as described above, but only if a value is provided. Otherwise, should be ignored.--prefixand-pshould be removed from sourcemap functionality. Accordingly, the CLI'sP_RELATIVEbehavior for the sourcemaps should be removed.--source-map-sourcesshould be added, and correspond to thesourceMapSourcesAPI option as described above. It should be a space-separated list of source paths to include in the source map.--source-map-urlshould change to the semantics of thesourceMapURLoption as described above. If passed but no value set, assume thetruebehavior. 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.
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. :)
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.
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 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 :+1:
We should incorporate this to fix the problems with sourcemaps not being chained together properly: https://github.com/azu/multi-stage-sourcemap
@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.
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 ../"