了解源地图

时间:2016-04-28 06:14:26

标签: javascript source-maps

我在basic.js文件中有以下代码和行号:

1 /**
2  * This is a multi-line comment.
3  * So the error shouldn't throw until a later line.
4  * Om nom nom.
5 */
6 throw new Error('Hello world!');

然后我缩小了文件:

1
2 throw new Error('Hello world!');
3 //@ sourceMappingURL=basic.js.map

映射:

{
    "version" : 3,
    "file" : "basic.min.js",
    "sources" : ["basic.js"],
    "names" : [],
    "mappings" : "AAIG,AAAH;CACC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"
}

我正在查看缩小文件中的第二行,因此我需要查看映射的这一部分:

CACC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"

我已使用此decoder对第一个分段CACC进行了解码并输出:

[1,0,1,1]

根据我this tutorial的理解,它指出basic.min.js中的第1列映射到0中索引为sources的文件,即{ {1}}及其第1行和第1列。但它已映射到原始数字中的第6行。我错过了什么?

1 个答案:

答案 0 :(得分:2)

你错过了

  1. 行和列基于零
  2. 段值实际上是delta添加到上一个值以获取当前位置
  3. 逐行:

    //basic.js
    0 /**
    1  * This is a multi-line comment.
    2  * So the error shouldn't throw until a later line.
    3  * Om nom nom.
    4 */
    5 throw new Error('Hello world!');
    
    //basic.min.js
    0 
    1 throw new Error('Hello world!');
    2 //@ sourceMappingURL=basic.js.map
    
    //line 0
    AAIG >  0,  0,  4,  3 // current value 0, 0, 4, 3
    AAAH >  0,  0,  0, -3 // current value 0, 0, 4, 0
    ;
    //line 1
    CACC >  1,  0,  1,  1 // current value 1, 0, 5, 1
    
    1 // output file column
    0 // input file index
    5 // input file line
    1 // input file column
    

    这让我们知道生成的文件的第1行第1列映射到文件0(文件0的数组是basic.js),第1行的第5行。

    查看可视化工具here