-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(common): Fix source map generation with
inputSourceMap
(#8546)
**Description:** - This PR fixes the source map generation when `inputSourceMap` is specified. - This PR fixes `minify()` not accepting parsed source map in the option. **Related issue:** - Closes #8372.
- Loading branch information
Showing
21 changed files
with
246 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
crates/swc/tests/minify/issue-7475/issue-8372/1/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"compress": { | ||
"negate_iife": false, | ||
"sequences": 30 | ||
}, | ||
"mangle": { | ||
"safari10": true | ||
}, | ||
"output": { | ||
"semicolons": false | ||
}, | ||
"sourceMap": { | ||
"filename": "input.js", | ||
"url": "input.map", | ||
"content": { | ||
"version": 3, | ||
"sources": ["input-preprocess.js"], | ||
"sourcesContent": ["const a = {aób: 'ó'}\n\nconsole.log(a)"], | ||
"names": ["console", "log", "aób"], | ||
"mappings": "AAEAA,QAAQC,GAAG,CAFD;IAACC,UAAK;AAAG" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const a = { aób: 'ó' } | ||
|
||
console.log(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let a={aób:"\xf3"};console.log(a); |
16 changes: 16 additions & 0 deletions
16
crates/swc/tests/minify/issue-7475/issue-8372/1/output.map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"mappings": "AAEAA,IAAAA,EAAQC,CAFEC,IAAA,MAAA,EAASF,QAAAC,GAAA,CAAAE", | ||
"names": [ | ||
"console", | ||
"log", | ||
"aób", | ||
"a" | ||
], | ||
"sources": [ | ||
"input-preprocess.js" | ||
], | ||
"sourcesContent": [ | ||
"const a = {aób: 'ó'}\n\nconsole.log(a)" | ||
], | ||
"version": 3 | ||
} |
12 changes: 12 additions & 0 deletions
12
crates/swc/tests/minify/issue-7475/issue-8372/2/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compress": { | ||
"negate_iife": false, | ||
"sequences": 30 | ||
}, | ||
"mangle": { | ||
"safari10": true | ||
}, | ||
"output": { | ||
"semicolons": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const a = { aób: 'ó' } | ||
|
||
console.log(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let a={aób:"\xf3"};console.log(a); |
16 changes: 16 additions & 0 deletions
16
crates/swc/tests/minify/issue-7475/issue-8372/2/output.map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"mappings": "AAAA,IAAMA,EAAI,CAAEC,IAAK,MAAI,EAErBC,QAAQC,GAAG,CAACH", | ||
"names": [ | ||
"a", | ||
"aób", | ||
"console", | ||
"log" | ||
], | ||
"sources": [ | ||
"$DIR/tests/minify/issue-7475/issue-8372/2/input.js" | ||
], | ||
"sourcesContent": [ | ||
"const a = { aób: 'ó' }\n\nconsole.log(a)" | ||
], | ||
"version": 3 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
#![deny(warnings)] | ||
|
||
pub mod regex; | ||
pub use anyhow::Error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use anyhow::{bail, Context, Result}; | ||
use serde::{Deserialize, Serialize}; | ||
use sourcemap::{vlq::parse_vlq_segment, RawToken, SourceMap}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(untagged)] | ||
pub enum SourceMapContent { | ||
Json(String), | ||
#[serde(rename_all = "camelCase")] | ||
Parsed { | ||
#[serde(default)] | ||
sources: Vec<String>, | ||
#[serde(default)] | ||
names: Vec<String>, | ||
#[serde(default)] | ||
mappings: String, | ||
#[serde(default)] | ||
file: Option<String>, | ||
#[serde(default)] | ||
source_root: Option<String>, | ||
#[serde(default)] | ||
sources_content: Option<Vec<Option<String>>>, | ||
}, | ||
} | ||
|
||
impl SourceMapContent { | ||
pub fn to_sourcemap(&self) -> Result<SourceMap> { | ||
match self { | ||
SourceMapContent::Json(s) => { | ||
SourceMap::from_slice(s.as_bytes()).context("failed to parse sourcemap") | ||
} | ||
SourceMapContent::Parsed { | ||
sources, | ||
names, | ||
mappings, | ||
file, | ||
source_root, | ||
sources_content, | ||
} => { | ||
let mut dst_col; | ||
let mut src_id = 0; | ||
let mut src_line = 0; | ||
let mut src_col = 0; | ||
let mut name_id = 0; | ||
|
||
let allocation_size = mappings.matches(&[',', ';'][..]).count() + 10; | ||
let mut tokens = Vec::with_capacity(allocation_size); | ||
|
||
let mut nums = Vec::with_capacity(6); | ||
|
||
for (dst_line, line) in mappings.split(';').enumerate() { | ||
if line.is_empty() { | ||
continue; | ||
} | ||
|
||
dst_col = 0; | ||
|
||
for segment in line.split(',') { | ||
if segment.is_empty() { | ||
continue; | ||
} | ||
|
||
nums.clear(); | ||
nums = parse_vlq_segment(segment)?; | ||
dst_col = (i64::from(dst_col) + nums[0]) as u32; | ||
|
||
let mut src = !0; | ||
let mut name = !0; | ||
|
||
if nums.len() > 1 { | ||
if nums.len() != 4 && nums.len() != 5 { | ||
bail!( | ||
"invalid vlq segment size; expected 4 or 5, got {}", | ||
nums.len() | ||
); | ||
} | ||
src_id = (i64::from(src_id) + nums[1]) as u32; | ||
if src_id >= sources.len() as u32 { | ||
bail!("invalid source reference: {}", src_id); | ||
} | ||
|
||
src = src_id; | ||
src_line = (i64::from(src_line) + nums[2]) as u32; | ||
src_col = (i64::from(src_col) + nums[3]) as u32; | ||
|
||
if nums.len() > 4 { | ||
name_id = (i64::from(name_id) + nums[4]) as u32; | ||
if name_id >= names.len() as u32 { | ||
bail!("invalid name reference: {}", name_id); | ||
} | ||
name = name_id; | ||
} | ||
} | ||
|
||
tokens.push(RawToken { | ||
dst_line: dst_line as u32, | ||
dst_col, | ||
src_line, | ||
src_col, | ||
src_id: src, | ||
name_id: name, | ||
}); | ||
} | ||
} | ||
|
||
let mut map = SourceMap::new( | ||
file.clone(), | ||
tokens, | ||
names.clone(), | ||
sources.clone(), | ||
sources_content.clone(), | ||
); | ||
map.set_source_root(source_root.clone()); | ||
Ok(map) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters