Last week jQuery 1.9 was released. This is the last release before jQuery 2.0. It adds many new functions, one of them is the source map.
By accessing http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js , you can scroll to the last line and you will see below line :
This is source map, it is a separate file and it is put at the same directory as the source file. You can click here and see what it looks like. It's an very useful function, we will talk more about it here.
1. Start from source code transformation
There are three common source code transformation modes:
- Compression, reduce the size of the source file. For example, source code of jQuery 1.9 is 252KB before compression, after compression, it becomes 32KB.
- Merge multiple files to reduce HTTP requests
All these three modes make the actual codes different from the production codes. It makes the debug more difficult.
This is what source map saves you.
2. What's source map
In short source map is an info file which stores the location information, i.e, the mapping between every position of code after transformation and the position of the code before transformation. With this, the debugger will tell where the error is in the source code before transformation.
As of now only Chrome supports this capability, you can enable it by going to Developer Tools->Setting->"Enable source maps".
3. How to enable source map
You only need to add the below line at the last line of the code after transformation.
The map file can be put on the internet or in the local system.
4. How to generate source map
The commonly used method is to use Google's Closure compiler.
The command to generate map is:
java -jar compiler.jar \
--js script.js \
--create_source_map ./script-min.js.map \
The meaning of the parameters are:
--js : The source code file before transformation
--create_source_map : The generated source map file
--source_map_format :The version of the source map, now all use the V3 version
--js_output_file : The source code file after transformation
Other ways to generate source map can be referred here.
5. The format of the source map
The source map file looks like:
version : 3,
sourceRoot : "",
sources: ["foo.js", "bar.js"],
names: ["src", "maps", "are", "fun"],
The whole file is an object, it can be parsed by the interpreter. The properties are:
- version：Source map version, now it is 3
- file：file name after conversion
- sourceRoot：The directory where the source code file is before transformation.
- sources：Files before transformation, it is an array which may contain more than one file
- names：All variable names and properties before transformation
- mappings：A string which records the location information.
6. mappings property
This is the real interesting part. How are the locations in the two files mapped?
It depends on the mappings property. The mappings property has three layers:
- Line mapping : It is expressed with (;), each : separated one line of source code after transformation. The content before the first : means the first line of the source code.
- Position mapping : It is expressed with (,), each comma maps to one location of the source code after transformation. The content before the first comma represents the source code in the first position.
- Position conversion : Represented by VLQ code, it represents the position of the transformed file in the original source file.
For example, if the mappings property is like :
It means the transformed source code has two lines, the first line has two positions and the second line has only one position.
7. How position mapping works
Each position has 5 characters.Starting from left:
- First，It means which column is the current position in after transformation。
- Second : Which file is the position in the sources
- Third，Which line is this position in before transformation
- Fourth，Which column is this position in before transformation
- Fifth, which variables is this position in names property.
This is a translation from Chinese where the original meaning should be Public soup window. But because there is car called Volkswagen which in Chinese is "大众", so they give it this name.