#source-map #merge #multiple

merge-source-map

Merge multiple sourcemaps

3 releases (1 stable)

1.2.0 Dec 11, 2023
1.1.0 Dec 11, 2023
1.0.0 Dec 8, 2023
0.1.1 Dec 7, 2023
0.1.0 Dec 7, 2023

#779 in Web programming

Download history 10/week @ 2023-12-31 7/week @ 2024-01-07 13/week @ 2024-01-14 1/week @ 2024-01-28 35/week @ 2024-02-25 25/week @ 2024-03-03 69/week @ 2024-03-10 63/week @ 2024-03-17 19/week @ 2024-03-24 38/week @ 2024-03-31 55/week @ 2024-04-07 20/week @ 2024-04-14

135 downloads per month

MIT license

12KB
119 lines

merge-source-map

English | 中文

Merge multiple sourcemaps.

Install

cargo add sourcemap merge-source-map

Usage

Here I will use a case to let you know how to use it.

Requirement

Suppose you now have a file named index.ts:

function sayHello(name: string) {
  console.log(`Hello, ${name}`);
}

First use tsc(with sourceMap and inlineSources options) to compile it to index.js:

function sayHello(name) {
  console.log("Hello, ".concat(name));
}

At the same time, a file named index.js.map will be obtained:

{
  "version": 3,
  "file": "index.js",
  "sourceRoot": "",
  "sources": [
    "index.ts"
  ],
  "names": [],
  "mappings": "AAAA,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO,CAAC,GAAG,CAAC,iBAAU,IAAI,CAAE,CAAC,CAAC;AAChC,CAAC",
  "sourcesContent": [
    "function sayHello(name: string) {\n  console.log(`Hello, ${name}`);\n}\n"
  ]
}

Then hand the index.js compiled product to swc for compression, and get the compressed product and another file named minify.js.map:

function sayHello(o){console.log("Hello, ".concat(o))}
{
  "version": 3,
  "file": "minify.js",
  "sourceRoot": "",
  "sources": [
    "index.js"
  ],
  "names": [
    "sayHello",
    "name",
    "console",
    "log",
    "concat"
  ],
  "mappings": "AAAA,SAASA,SAASC,CAAI,EAClBC,QAAQC,GAAG,CAAC,UAAUC,MAAM,CAACH,GACjC",
  "sourcesContent": [
    "function sayHello(name) {\n    console.log(\"Hello, \".concat(name));\n}\n"
  ]
}

So how to merge two sourcemaps?

Merge sourcemaps

use merge_source_map::merge;
use sourcemap::SourceMap;

fn main() {
    let sourcemap1 = r#"{
        "version": 3,
        "file": "index.js",
        "sourceRoot": "",
        "sources": [
          "index.ts"
        ],
        "names": [],
        "mappings": "AAAA,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO,CAAC,GAAG,CAAC,iBAAU,IAAI,CAAE,CAAC,CAAC;AAChC,CAAC",
        "sourcesContent": [
          "function sayHello(name: string) {\n  console.log(`Hello, ${name}`);\n}\n"
        ]
    }"#;
    let sourcemap2 = r#"{
        "version": 3,
        "file": "minify.js",
        "sourceRoot": "",
        "sources": [
          "index.js"
        ],
        "names": [
          "sayHello",
          "name",
          "console",
          "log",
          "concat"
        ],
        "mappings": "AAAA,SAASA,SAASC,CAAI,EAClBC,QAAQC,GAAG,CAAC,UAAUC,MAAM,CAACH,GACjC",
        "sourcesContent": [
          "function sayHello(name) {\n    console.log(\"Hello, \".concat(name));\n}\n"
        ]
    }"#;

    // merge sourcemap
    let merged = merge(
        vec![
            SourceMap::from_reader(sourcemap1.as_bytes()).unwrap(),
            SourceMap::from_reader(sourcemap2.as_bytes()).unwrap(),
        ],
        Default::default(),
    );
    let mut buf = vec![];
    merged.to_writer(&mut buf).unwrap();
    let merged = String::from_utf8(buf).unwrap();
}

Merged sourcemap:

{
  "version": 3,
  "sources": [
    "index.ts"
  ],
  "sourcesContent": [
    "function sayHello(name: string) {\n  console.log(`Hello, ${name}`);\n}\n"
  ],
  "names": [],
  "mappings": "AAAA,SAAS,SAAS,CAAY,EAC5B,QAAQ,GAAG,CAAC,UAAA,MAAA,CAAU,GACxB"
}

You can view result here.

License

MIT

Dependencies

~2.7–4MB
~105K SLoC