Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why inline source maps?

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?

like image 838
Akseli Palén Avatar asked Dec 27 '14 20:12

Akseli Palén


People also ask

What are inline source maps?

Source map types supported by webpack can be split into two categories: Inline source maps add the mapping data directly to the generated files. Separate source maps emit the mapping data to separate source map files and link the source to them using a comment. Hidden source maps omit the comment on purpose.

Why do we need source map?

Source maps can be convenient during development. They provide better means to debug applications as you can still examine the original code over a generated one. They can be valuable even for production usage and allow you to debug issues while serving a client-friendly version of your application.

Should you use source maps in production?

Most JavaScript and CSS sources are usually minified and source maps serve as a memory map to the compressed files. It's generally a good practice to minify and combine your assets (Javascript & CSS) when deploying to production.

What is inline source map webpack?

After build, webpack will generate the source map and include it in the generated bundle file itself. That is why this configuration is called inline source map. This option is best for development. It is not good for production as it makes bundle size bigger.


3 Answers

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.

like image 190
pseudosavant Avatar answered Oct 22 '22 08:10

pseudosavant


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.

like image 30
Tim Avatar answered Oct 22 '22 06:10

Tim


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.

like image 27
jasonslyvia Avatar answered Oct 22 '22 07:10

jasonslyvia