Skip to content

Commit

Permalink
fix: Properly remap source_content to source_index (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMoorJasper authored Dec 12, 2021
1 parent 93c0627 commit 27def14
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
11 changes: 5 additions & 6 deletions parcel_sourcemap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,12 @@ impl SourceMap {

pub fn get_source_index(&self, source: &str) -> Result<Option<u32>, SourceMapError> {
let normalized_source = make_relative_path(self.project_root.as_str(), source);
match self
Ok(self
.inner
.sources
.iter()
.position(|s| normalized_source.eq(s))
{
Some(i) => Ok(Some(i as u32)),
None => Ok(None),
}
.map(|v| v as u32))
}

pub fn get_source(&self, index: u32) -> Result<&str, SourceMapError> {
Expand Down Expand Up @@ -529,7 +526,9 @@ impl SourceMap {

self.inner.sources_content.reserve(sources_content.len());
for (i, source_content) in sources_content.iter().enumerate() {
self.set_source_content(i, source_content)?;
if let Some(source_index) = source_indexes.get(i) {
self.set_source_content(*source_index as usize, source_content)?;
}
}

let mut input = input.iter().cloned().peekable();
Expand Down
1 change: 0 additions & 1 deletion parcel_sourcemap_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ fn get_source_index(ctx: CallContext) -> Result<JsNumber> {

let source = ctx.get::<JsString>(0)?.into_utf8()?;
let source_index = source_map_instance.get_source_index(source.as_str()?)?;

match source_index {
Some(i) => ctx.env.create_uint32(i),
None => ctx.env.create_int32(-1),
Expand Down
12 changes: 5 additions & 7 deletions parcel_sourcemap_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,11 @@ impl SourceMap {
}

pub fn getSourceIndex(&self, source: &str) -> Result<JsValue, JsValue> {
Ok(JsValue::from(
self.map
.get_source_index(source)?
.map(|v| i32::try_from(v).unwrap())
.unwrap_or(-1),
))
let mapped_val: i32 = match self.map.get_source_index(source)? {
Some(found_source_index) => i32::try_from(found_source_index).unwrap_or(-1),
None => -1,
};
Ok(JsValue::from(mapped_val))
}

pub fn addIndexedMappings(&mut self, mappings_arr: &[i32]) {
Expand Down Expand Up @@ -258,7 +257,6 @@ impl SourceMap {

pub fn getSourceContentBySource(&self, source: &str) -> Result<JsValue, JsValue> {
let source_index = self.map.get_source_index(source)?;

match source_index {
Some(i) => {
let source_content = self.map.get_source_content(i)?;
Expand Down
18 changes: 17 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe('SourceMap - Basics', () => {
assert.deepEqual(map.addSources(['test.js', 'execute.js']), [2, 3]);

assert.deepEqual(map.addSource('abc.js'), 4);
assert.deepEqual(map.addSource("dist/rörfokus/4784.js"), 5);
assert.deepEqual(map.addSource('dist/rörfokus/4784.js'), 5);
});

it('Should be able to handle absolute url sources', () => {
Expand Down Expand Up @@ -409,4 +409,20 @@ describe('SourceMap - Basics', () => {

assert.equal(map.getSourceContent('helloworld.coffee'), 'module.exports = () => "hello world";');
});

it('Should deduplicate sources and sourcesContent from a VLQ Map', () => {
let map = new SourceMap('/test-root');
map.addVLQMap({
mappings: SIMPLE_SOURCE_MAP.mappings,
sources: ['./index.js', './index.js'],
sourcesContent: ['first', 'second'],
names: ['a'],
});

const stringifiedMap = map.toVLQ();
assert.equal(stringifiedMap.mappings, SIMPLE_SOURCE_MAP.mappings);
assert.deepEqual(stringifiedMap.sources, ['index.js']);
assert.deepEqual(stringifiedMap.sourcesContent, ['second']);
assert.deepEqual(stringifiedMap.names, ['a']);
});
});

1 comment on commit 27def14

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parcel sourcemap benchmark

Benchmark suite Current: 27def14 Previous: 93c0627 Ratio
consume#consume buffer 56009 ops/sec (±1.2e+2%) 126155 ops/sec (±8.9%) 2.25
consume#consume vlq mappings 54404 ops/sec (±14%) 45033 ops/sec (±13%) 0.83
consume#consume JS Mappings 53737 ops/sec (±6.1%) 45897 ops/sec (±5.7%) 0.85
serialize#Save buffer 413 ops/sec (±1.1%) 414 ops/sec (±0.85%) 1.00
serialize#Serialize to vlq 321 ops/sec (±1.2%) 291 ops/sec (±1.6%) 0.91
modify#negative column offset 99959 ops/sec (±17%) 105143 ops/sec (±17%) 1.05
modify#positive column offset 90309 ops/sec (±23%) 99950 ops/sec (±25%) 1.11
modify#positive line offset 40427 ops/sec (±7.0%) 38577 ops/sec (±6.8%) 0.95
modify#negative line offset 38988 ops/sec (±6.5%) 36065 ops/sec (±6.5%) 0.93
append#addSourceMap 200 ops/sec (±0.44%) 176 ops/sec (±1.4%) 0.88

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.