Insertion of globals breaks sourcemaps
See original GitHub issueWhen a source file that already has a sourcemap (from a prior transform) is transformed to insert globals, the sourcemap is not updated, which breaks all positions in the file thereafter.
A trivial reproducing test case using coffeeify
(this bug has nothing to do with coffeeify
specifically; any transform that uses sourcemaps is affected):
x.coffee
global.foo = 3
console.log('hello')
command line
browserify -t coffeeify -d x.coffee
This link shows the resulting sourcemap. Note how the addition of the (function (global){
line causes the mappings to be off by exactly one line at that point. This offset will persist through the entire bundle, as the mappings are calculated relative to the original output rather than the transformed output.
Issue Analytics
- State:
- Created 9 years ago
- Comments:16 (2 by maintainers)
Top Results From Across the Web
Sourcemaps are broken on Webpack 2 when you use ES6 ...
Before, I was trying to export a sourcemap, upload it to sentry, and not host it publicly. Got it working by just using...
Read more >VS Code: "Breakpoint ignored because generated code not ...
I have generated the js.map files by running the tsc --sourcemap app.ts command. After all of those steps when I set a breakpoint...
Read more >4 Reasons Why Your Source Maps are Broken - Sentry Blog
4 Reasons Why Your Source Maps are Broken · Missing or incorrect source map directive · Missing original source files · Bad source...
Read more >Using Source Maps with TypeScript
Have you heard of Source Maps? Source Maps are an idea that has come out of Mozilla for addressing the debugging issues that...
Read more >source-map | Yarn - Package Manager
MAX_SAFE_INTEGER ). It can now only handle them up to 2^32 - 1 . Breaking change: The source-map library now uses modern ECMAScript-isms:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A problem people still might be seeing (with source maps when global detection is not disabled and variables such as
global
are found (and withdebug: true
), as well as the presence of a simple transform likeuglifyify
) is the Windows-specific issue at https://github.com/thlorenz/combine-source-map/pull/23I’m adding further details below as a record (since it was little fun to debug!), but the PR already addresses the concern succinctly.
If the PR is still not accepted after my reminder, I think
browserify
ought to depend on the PR’s fork ofcombine-source-map
(orbrowserify
’s own copy of it) to resolve this (via updates or forks ofinsert-module-globals
andbrowser-pack
, the dependencies whichbrowserify
directly includes and which both (problematically) utilize the unfixedcombine-source-map
).I am attempting to use node-browserify with
uglifyify
to handle minification while preserving info about source files to source maps.(I’m also adding my own build of
mapstraction
to save the map to an external file (while being able to deal with base and relative paths) though this latter plugin only does anything of substance after its stream ends and that only occurs after the problem mentioned below.)Since I did not add
detectGlobals: false
,insert-module-globals
will be invoked. This creates athrough
stream whoseend
calls a functioncloseOver
which combines this stream’s chunks and invokescombine-source-map
’screate
(which just builds aCombiner
) and then invokes theCombiner
’saddFile
with these source contents, and in so doing gets its source converted into a map as such:var existingMap = resolveMap(opts.source);
.The map then gets passed to
Combiner.prototype._addExistingMap
which copies the mapvar mappings = mappingsFromMap(existingMap);
wheremappingsFromMap
refers tolib/mappings-from-map
.Later in
Combiner.prototype._addExistingMap
the following is called (though the code preceding it invokingthis.generator.addSourceContent
is also affected because of its use ofrebaseRelativePath
):In my case,
sourceFile
istypes/errors.js
whilemapping.source
iserrors.js
and when it is resolved, it becomestypes\errors.js
, so that is the mapping that is added (with Windows backslashes).Later, when browserify continues its normal work unrelated to inserting globals, apparently since we have
debug
enabled,Browserify.prototype._debug
gets called, including with this line:Although a Windows-style path is present in
row.file
(C:\<path to my repos>\typeson-registry\types\errors.js
), the conversion to a relative path with the backslashes escaped leadsrow.sourceFile
to getting a non-Windows pathtypes/errors.js
. Therow
is added to the stream and somehowbrowser-pack
(which had been invoked earlier by browserify) gets itswrite
method invoked with this row and it then invokesaddFile
(on the earlier created instance ofcombine-source-map
) with an object including the non-Windowsrow.sourceFile
path.The above-mentioned
_addExistingMap
method fromcombine-source-map
thus gets called again, callingaddSourceContent
(andaddMappings
) on an earlier createdinline-source-map
generator and to determine which path to supply (and determine what file if any ends up getting added).Before supplying the arguments, however,
_addExistingMap
will invokerebaseRelativePath
to check whether the currently supplied file (the non-Windows one in this case) matches any of the previous existing ones (which it should since it has been added already albeit in Windows form).Since the code within
rebaseRelativePath
ofcombine-source-map
does a mere check without normalization ofif (sourceFile === relativeRootedPath ||
, there is a failure to match due to the difference in slashes. This causes the problem that the following line,return path.join(path.dirname(sourceFile), relativeRootedPath);
is run which effectively adds another directory onto the path (e.g., so my file,types/errors.js
mistakenly becomestypes/types/errors.js
in the source map).The PR details another issue whereby non-recognition can also occur because a file (e.g., from CoffeeScript or TypeScript) is not recognized with its converted extension.
While the Windows comparison issue is one that can now be fixed within
combine-source-map
, it appears the issue I was seeing with some sources not having a parent was related to settingcommondir
tofalse
(wherecommondir
otherwise no longer has any effect–see #1075). I believe this option therefore ought to be removed or, if there is a reason why one would still need it (as when set to false, it currently causes/
to be passed asinsert-module-globals
’sbasedir
in place ofopts.basedir
/cwd
) renamed.