Why inline source maps?

JavascriptGulp Sourcemaps

Javascript Problem Overview


Today I learned that it is possible to include source maps directly into your minified JavaScript file instead of having them in a separate example.min.map file. I wonder: why would anybody want to do something like that?

The benefit of having source maps is clear to me: one can for example debug errors with the original, non-compressed source files while running the minified files. The benefit of minimization is also clear: the size of source files is greatly reduced, making it quicker for browsers to download.

So why on Earth I would want to include the source maps into the minified file, given that the maps have size even greater than the minified code itself?

Javascript Solutions


Solution 1 - Javascript

I searched around and the only reason I could see that people inline source maps is for use in development. Inlined source maps should not be used in production.

The rational for inlining the source maps with your minified files is that the browser is parsing the exact same JavaScript in development and production. Some minifiers like Closure Compiler do more than 'just' minify the code. Using the advanced options it can also do things like: dead code removal, function inlining, or aggressive variable renaming. This makes the minified code (potentially) functionally different than the source file.

This could still be done by referencing external source map files of course, but some people seem to prefer inlining for their build process.

Solution 2 - Javascript

If you are remote debugging Chrome on an android device, the Chrome debugger cannot just access any file it wants on the device and that includes separate map files. If you include them inline you don't have this issue.

Solution 3 - Javascript

JS bundling tools like Browserify or Webpack will bundle all your .js files input one or several bundles, even in developing mode. So in this case, adding inline source map to generated bundles is the easiest way to help debugging without bringing extra files.

Solution 4 - Javascript

In some situations you might want to include inline sourcemaps into evaluated code. E.g you have a coffeescript input field and you want to enable debbuging the code in coffeescript. There is a stackoverflow question about source maps in evaluated code:

https://stackoverflow.com/questions/14375567/getting-source-maps-working-with-evaluated-code

You could include @sourceURL in your comments to specify a URL of your eval code and load a map file (see page 8 of SourceMap Spec 3). But it is not always possible to write files to some location.

Solution 5 - Javascript

cheap-module-source-map is much better for a production build.

inline-source-map is used to make quick and dirty builds when testing

Solution 6 - Javascript

If you're developing a browser extension, inline-source-map is the only option for debugging since extension itself can't access the sourcemap files -- even if it's possible you have to specify all of your sourcemap files inside the manifest.json(config file for browser extensions).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAkseli PalénView Question on Stackoverflow
Solution 1 - JavascriptpseudosavantView Answer on Stackoverflow
Solution 2 - JavascriptTimView Answer on Stackoverflow
Solution 3 - JavascriptjasonslyviaView Answer on Stackoverflow
Solution 4 - JavascriptHelicaseView Answer on Stackoverflow
Solution 5 - JavascriptCarlView Answer on Stackoverflow
Solution 6 - JavascriptThe.Wolfgang.GrimmerView Answer on Stackoverflow