Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
463 changes: 298 additions & 165 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [ "parcel_sourcemap", "parcel_sourcemap_node", "parcel_sourcemap_wasm" ]
resolver = "2"

[profile.release]
lto = true
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@
"README.md",
"!.gitignore"
],
"binary": {
"napi_versions": [
4
]
"napi": {
"name": "parcel-sourcemap"
},
"engines": {
"node": "^12.18.3 || >=14"
Expand All @@ -65,15 +63,15 @@
"@babel/preset-env": "^7.14.2",
"@babel/preset-flow": "^7.13.13",
"@babel/register": "^7.13.16",
"@napi-rs/cli": "^2.14.8",
"@napi-rs/cli": "^3.7.2",
"cross-env": "^7.0.3",
"flow-bin": "^0.151.0",
"flow-copy-source": "^2.0.9",
"fs-extra": "^10.0.0",
"globby": "^11.0.3",
"husky": "6.0.0",
"lint-staged": "^11.0.0",
"mocha": "^8.4.0",
"mocha": "^11.8.0",
"prettier": "^2.3.0",
"shx": "^0.3.3",
"source-map": "^0.7.3",
Expand All @@ -83,4 +81,4 @@
"./dist/node.js": "./dist/wasm.js",
"./dist/wasm-bindings.js": "./dist/wasm-bindings-web.js"
}
}
}
2 changes: 1 addition & 1 deletion parcel_sourcemap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/parcel-bundler/source-map"

[dependencies]
"vlq" = "0.5.1"
rkyv = "0.7.38"
rkyv = "0.8.18"
serde = {version = "1", features = ["derive"], optional = true}
serde_json = { version = "1", optional = true }
base64-simd = { version = "0.7", optional = true }
Expand Down
104 changes: 43 additions & 61 deletions parcel_sourcemap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(clippy::all)]
// #![deny(clippy::all)]

pub mod mapping;
pub mod mapping_line;
Expand All @@ -16,14 +16,7 @@ pub use sourcemap_error::{SourceMapError, SourceMapErrorType};
use std::borrow::Cow;
use std::io;

use rkyv::{
archived_root,
ser::{
serializers::{AlignedSerializer, AllocScratch, CompositeSerializer},
Serializer,
},
AlignedVec, Archive, Deserialize, Infallible, Serialize,
};
use rkyv::{util::AlignedVec, Archive, Deserialize, Serialize};

use vlq_utils::{is_mapping_separator, read_relative_vlq};

Expand Down Expand Up @@ -75,7 +68,7 @@ impl SourceMap {

pub fn add_mapping_with_offset(
&mut self,
mapping: Mapping,
mapping: &Mapping,
line_offset: i64,
column_offset: i64,
) -> Result<(), SourceMapError> {
Expand Down Expand Up @@ -140,7 +133,7 @@ impl SourceMap {
pub fn get_mappings(&self) -> Vec<Mapping> {
let mut mappings = Vec::new();
for (generated_line, mapping_line) in self.inner.mapping_lines.iter().enumerate() {
for mapping in mapping_line.mappings.iter() {
for mapping in &mapping_line.mappings {
mappings.push(Mapping {
generated_line: generated_line as u32,
generated_column: mapping.generated_column,
Expand Down Expand Up @@ -233,10 +226,10 @@ impl SourceMap {
}
}

pub fn add_sources<I: AsRef<str>>(&mut self, sources: Vec<I>) -> Vec<u32> {
pub fn add_sources<I: AsRef<str>>(&mut self, sources: &[I]) -> Vec<u32> {
self.inner.sources.reserve(sources.len());
let mut result_vec = Vec::with_capacity(sources.len());
for s in sources.iter() {
for s in sources {
result_vec.push(self.add_source(s.as_ref()));
}
result_vec
Expand Down Expand Up @@ -265,18 +258,18 @@ impl SourceMap {
}

pub fn add_name(&mut self, name: &str) -> u32 {
return match self.inner.names.iter().position(|s| name.eq(s)) {
match self.inner.names.iter().position(|s| name.eq(s)) {
Some(i) => i as u32,
None => {
self.inner.names.push(String::from(name));
(self.inner.names.len() - 1) as u32
}
};
}
}

pub fn add_names<I: AsRef<str>>(&mut self, names: Vec<I>) -> Vec<u32> {
pub fn add_names<I: AsRef<str>>(&mut self, names: &[I]) -> Vec<u32> {
self.inner.names.reserve(names.len());
return names.iter().map(|n| self.add_name(n.as_ref())).collect();
names.iter().map(|n| self.add_name(n.as_ref())).collect()
}

pub fn get_name_index(&self, name: &str) -> Option<u32> {
Expand Down Expand Up @@ -341,21 +334,14 @@ impl SourceMap {

// Write the sourcemap instance to a buffer
pub fn to_buffer(&self, output: &mut AlignedVec) -> Result<(), SourceMapError> {
output.clear();
let mut serializer = CompositeSerializer::new(
AlignedSerializer::new(output),
AllocScratch::default(),
Infallible,
);
serializer.serialize_value(&self.inner)?;
*output = rkyv::to_bytes::<rkyv::rancor::Error>(&self.inner)?;
Ok(())
}

// Create a sourcemap instance from a buffer
pub fn from_buffer(project_root: &str, buf: &[u8]) -> Result<SourceMap, SourceMapError> {
let archived = unsafe { archived_root::<SourceMapInner>(buf) };
// TODO: see if we can use the archived data directly rather than deserializing at all...
let inner = archived.deserialize(&mut Infallible)?;
let inner = rkyv::from_bytes::<SourceMapInner, rkyv::rancor::Error>(buf)?;
Ok(SourceMap {
project_root: String::from(project_root),
inner,
Expand All @@ -370,14 +356,14 @@ impl SourceMap {
self.inner.sources.reserve(sourcemap.inner.sources.len());
let mut source_indexes = Vec::with_capacity(sourcemap.inner.sources.len());
let sources = std::mem::take(&mut sourcemap.inner.sources);
for s in sources.iter() {
for s in &sources {
source_indexes.push(self.add_source(s));
}

self.inner.names.reserve(sourcemap.inner.names.len());
let mut names_indexes = Vec::with_capacity(sourcemap.inner.names.len());
let names = std::mem::take(&mut sourcemap.inner.names);
for n in names.iter() {
for n in &names {
names_indexes.push(self.add_name(n));
}

Expand All @@ -396,12 +382,10 @@ impl SourceMap {
let generated_line = (line as i64) + line_offset;
if generated_line >= 0 {
let mut line = mapping_line;
for mapping in line.mappings.iter_mut() {
match &mut mapping.original {
Some(original_mapping_location) => {
original_mapping_location.source = match source_indexes
.get(original_mapping_location.source as usize)
{
for mapping in &mut line.mappings {
if let Some(original_mapping_location) = &mut mapping.original {
original_mapping_location.source =
match source_indexes.get(original_mapping_location.source as usize) {
Some(new_source_index) => *new_source_index,
None => {
return Err(SourceMapError::new(
Expand All @@ -410,19 +394,17 @@ impl SourceMap {
}
};

original_mapping_location.name = match original_mapping_location.name {
Some(name_index) => match names_indexes.get(name_index as usize) {
Some(new_name_index) => Some(*new_name_index),
None => {
return Err(SourceMapError::new(
SourceMapErrorType::NameOutOfRange,
));
}
},
None => None,
};
}
None => {}
original_mapping_location.name = match original_mapping_location.name {
Some(name_index) => match names_indexes.get(name_index as usize) {
Some(new_name_index) => Some(*new_name_index),
None => {
return Err(SourceMapError::new(
SourceMapErrorType::NameOutOfRange,
));
}
},
None => None,
};
}
}

Expand All @@ -439,15 +421,15 @@ impl SourceMap {
.sources
.reserve(original_sourcemap.inner.sources.len());
let mut source_indexes = Vec::with_capacity(original_sourcemap.inner.sources.len());
for s in original_sourcemap.inner.sources.iter() {
for s in &original_sourcemap.inner.sources {
source_indexes.push(self.add_source(s));
}

self.inner
.names
.reserve(original_sourcemap.inner.names.len());
let mut names_indexes = Vec::with_capacity(original_sourcemap.inner.names.len());
for n in original_sourcemap.inner.names.iter() {
for n in &original_sourcemap.inner.names {
names_indexes.push(self.add_name(n));
}

Expand All @@ -460,8 +442,8 @@ impl SourceMap {
}
}

for line_content in self.inner.mapping_lines.iter_mut() {
for mapping in line_content.mappings.iter_mut() {
for line_content in &mut self.inner.mapping_lines {
for mapping in &mut line_content.mappings {
let original_location_option = &mut mapping.original;
if let Some(original_location) = original_location_option {
let found_mapping = original_sourcemap.find_closest_mapping(
Expand Down Expand Up @@ -517,9 +499,9 @@ impl SourceMap {
pub fn add_vlq_map<I: AsRef<str>>(
&mut self,
input: &[u8],
sources: Vec<I>,
sources_content: Vec<I>,
names: Vec<I>,
sources: &[I],
sources_content: &[I],
names: &[I],
line_offset: i64,
column_offset: i64,
) -> Result<(), SourceMapError> {
Expand All @@ -540,8 +522,8 @@ impl SourceMap {
}
}

let mut input = input.iter().cloned().peekable();
while let Some(byte) = input.peek().cloned() {
let mut input = input.iter().copied().peekable();
while let Some(byte) = input.peek().copied() {
match byte {
b';' => {
generated_line += 1;
Expand All @@ -557,7 +539,7 @@ impl SourceMap {

// Read source, original line, and original column if the
// mapping has them.
let original = if input.peek().cloned().map_or(true, is_mapping_separator) {
let original = if input.peek().copied().is_none_or(is_mapping_separator) {
None
} else {
read_relative_vlq(&mut source, &mut input)?;
Expand All @@ -574,7 +556,7 @@ impl SourceMap {
));
}
},
if input.peek().cloned().map_or(true, is_mapping_separator) {
if input.peek().copied().is_none_or(is_mapping_separator) {
None
} else {
read_relative_vlq(&mut name, &mut input)?;
Expand Down Expand Up @@ -701,9 +683,9 @@ impl SourceMap {
let mut sm = Self::new(project_root);
sm.add_vlq_map(
json.mappings.as_bytes(),
json.sources,
sources_content,
json.names,
&json.sources,
&sources_content,
&json.names,
0,
0,
)?;
Expand Down
2 changes: 1 addition & 1 deletion parcel_sourcemap/src/mapping_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl MappingLine {
pub fn ensure_sorted(&mut self) {
if !self.is_sorted {
self.mappings
.sort_by(|a, b| a.generated_column.cmp(&b.generated_column));
.sort_by_key(|a| a.generated_column);
self.is_sorted = true
}
}
Expand Down
7 changes: 2 additions & 5 deletions parcel_sourcemap/src/sourcemap_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rkyv::ser::serializers::{AllocScratchError, CompositeSerializerError};
use std::{convert::Infallible, io};

// Errors that can occur during processing/modifying source map
Expand Down Expand Up @@ -161,11 +160,9 @@ impl From<Infallible> for SourceMapError {
}
}

impl From<CompositeSerializerError<Infallible, AllocScratchError, Infallible>> for SourceMapError {
impl From<rkyv::rancor::Error> for SourceMapError {
#[inline]
fn from(
_err: CompositeSerializerError<Infallible, AllocScratchError, Infallible>,
) -> SourceMapError {
fn from(_err: rkyv::rancor::Error) -> SourceMapError {
SourceMapError::new(SourceMapErrorType::BufferError)
}
}
Expand Down
13 changes: 6 additions & 7 deletions parcel_sourcemap/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Based on https://github.com/getsentry/rust-sourcemap/blob/master/src/utils.rs
use std::borrow::Cow;
use std::iter::repeat;
use std::iter::repeat_n;

pub fn is_abs_path(s: &str) -> bool {
if s.starts_with('/') || s.starts_with('\\') {
Expand All @@ -26,7 +26,7 @@ fn get_common_prefix_len<'a>(items: &'a [Cow<'a, [&'a str]>]) -> usize {

let shortest = &items[0];
let mut max_idx = None;
for seq in items.iter() {
for seq in items {
let mut seq_max_idx = None;
for (idx, &comp) in shortest.iter().enumerate() {
if seq.get(idx) != Some(&comp) {
Expand All @@ -47,10 +47,9 @@ fn get_common_prefix_len<'a>(items: &'a [Cow<'a, [&'a str]>]) -> usize {
}

fn chunk_path(p: &str) -> Vec<&str> {
return p
.split(&['/', '\\'][..])
p.split(&['/', '\\'][..])
.filter(|x| !x.is_empty() && *x != ".")
.collect();
.collect()
}

// Helper function to calculate the path from a base file to a target file.
Expand All @@ -67,7 +66,7 @@ pub fn make_relative_path(base: &str, target: &str) -> String {
if target_str.contains(':') {
String::from(target_str)
} else {
return chunk_path(target_str).join("/");
chunk_path(target_str).join("/")
}
} else {
let target_path: Vec<&str> = chunk_path(target_str);
Expand All @@ -77,7 +76,7 @@ pub fn make_relative_path(base: &str, target: &str) -> String {
Cow::Borrowed(target_path.as_slice()),
];
let prefix_len = get_common_prefix_len(&items);
let mut rel_list: Vec<&str> = repeat("..").take(base_dir.len() - prefix_len).collect();
let mut rel_list: Vec<&str> = repeat_n("..", base_dir.len() - prefix_len).collect();
rel_list.extend_from_slice(&target_path[prefix_len..]);
rel_list.join("/")
}
Expand Down
8 changes: 4 additions & 4 deletions parcel_sourcemap_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ version = "2.1.1"
crate-type = ["cdylib"]

[dependencies]
napi = {version = "2.12.2", features = ["napi4", "serde-json"]}
napi-derive = "2.12.2"
napi = {version = "3.12.1", features = ["napi4", "serde-json", "compat-mode"]}
napi-derive = "3.6.3"
parcel_sourcemap = {path = "../parcel_sourcemap"}
serde = "1"
serde_json = "1"
rkyv = "0.7.38"
rkyv = "0.8.18"

[target.'cfg(target_os = "macos")'.dependencies]
jemallocator = {version = "0.3.2", features = ["disable_initial_exec_tls"]}
jemallocator = {version = "0.5.4", features = ["disable_initial_exec_tls"]}

[build-dependencies]
napi-build = "2"
Loading