Please define sourcemap, describe in your own words the purpose/value of sourcemaps, demonstrate this with a real world example and elaborate further where-ever you see fit. Thank you.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (7)
When we write the code (often JavaScript in this case), we create a source. The source code looks the way we first input it.
But when we serve the code, often we don't want to send the "source" code to the end user, we send a compressed version. Things like "UglifyJS" turn the code we write, changing all the variables into single letter vars and remove all the whitespace in the file. What we end up is code that behaves the same way but is shipped to the end use in fewer bytes. This means faster webpages. Overall we try to send as little data as possible if we can help it.
mishoo / UglifyJS
JavaScript parser / mangler / compressor / beautifier toolkit
UglifyJS 3
UglifyJS is a JavaScript parser, minifier, compressor and beautifier toolkit.
Note:
uglify-js
supports JavaScript and most language features in ECMAScript.uglify-js
.uglify-js@3
has a simplified API and CLI that is not backwards compatible withuglify-js@2
.Install
First make sure you have installed the latest version of node.js (You may need to restart your computer after this step).
From NPM for use as a command line app:
From NPM for programmatic use:
Command line usage
UglifyJS can take multiple input files. It's recommended that you pass the input files first, then pass the options. UglifyJS will parse input files in sequence and apply any compression options. The files are parsed in the same global scope, that is, a reference…
We also use transpilers such babelJS, which let us write more "modern" javascript features and compile them into code which works across all browsers, even the older ones.
babel / babel
🐠 Babel is a compiler for writing next generation JavaScript.
The compiler for writing next generation JavaScript
Supporting Babel
Babel (pronounced "babble") is a community-driven project used by many companies and projects, and is maintained by a group of volunteers. If you'd like to help support the future of the project, please consider:
Sponsors
Our top sponsors are shown below! [Become a sponsor]
Intro
Babel is a tool that helps you write code in the latest version of JavaScript. When your supported environments don't support certain features natively, Babel will help you compile those features down to a supported version.
In
Out
But what happens if we ever want to go back in the other direction, perhaps to debug? Now we have to map the compressed or compiled code back to the source. It's a series of instructions that can help turn things back into source code if we need it. If a sourcemap is created, there's probably an associated tool that knows how to interpret the sourcemap.
I think that's the gist, if anyone thinks I made a mistake, let me know!
Hi ben! I'm slightly confused: what kind of situation is that where you need to debug and you only have an uglified version of your code, and not the original source?
It's when something good wrong on production sure, but your code has been minified to one line, very hard to figure out what's going on when error is on Line 1.
Not sure if chrome's new pretty print option for source files will fix that tho now?
I’m not sure the whole landscape but I’m pretty sure there are tools that allow deciphering prod bugs in this way, and I feel like there are also uses for this mapping in CSS -> SASS land.
But not really my expertise, perhaps someone else will jump in and give a better explanation.
I tried to reply to this comments parent but can only see some text that says “thread” where the reply should be so I’m replying here.
Chromes pretty print is still not enough. I’m unsure if the latest dev tools could point to the correct line or not, but it would still likely not matter.
If you pretty print a single line compiled file, there will still be multiple things wrong while debugging such as
You'll have an issue on a browser that doesn't support your code as-is (spread syntax, for example).
The code in the debugger is not your code - it's the foreign code. You can have your own code, but you have to step through this stripped-down code.
Unless your source is mapped, you won't know what code you're actually stepping through.
Loud and clear. Thanks, Ben!