3 releases (1 stable)
1.2.0 | Dec 11, 2023 |
---|---|
1.1.0 |
|
1.0.0 |
|
0.1.1 | Dec 7, 2023 |
0.1.0 | Dec 7, 2023 |
#23 in #source-map
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.4–3.5MB
~84K SLoC