Skip to content

Commit

Permalink
perf: skip line break mappings (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Oct 11, 2024
1 parent bde9d7e commit 5b1ecf7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/utils/Mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,6 @@ export default class Mappings {
let charInHiresBoundary = false;

while (originalCharIndex < chunk.end) {
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];

if (this.hires === 'boundary') {
// in hires "boundary", group segments per word boundary than per char
if (wordRegex.test(original[originalCharIndex])) {
// for first char in the boundary found, start the boundary by pushing a segment
if (!charInHiresBoundary) {
this.rawSegments.push(segment);
charInHiresBoundary = true;
}
} else {
// for non-word char, end the boundary by pushing a segment
this.rawSegments.push(segment);
charInHiresBoundary = false;
}
} else {
this.rawSegments.push(segment);
}
}

if (original[originalCharIndex] === '\n') {
loc.line += 1;
loc.column = 0;
Expand All @@ -83,6 +62,27 @@ export default class Mappings {
this.generatedCodeColumn = 0;
first = true;
} else {
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];

if (this.hires === 'boundary') {
// in hires "boundary", group segments per word boundary than per char
if (wordRegex.test(original[originalCharIndex])) {
// for first char in the boundary found, start the boundary by pushing a segment
if (!charInHiresBoundary) {
this.rawSegments.push(segment);
charInHiresBoundary = true;
}
} else {
// for non-word char, end the boundary by pushing a segment
this.rawSegments.push(segment);
charInHiresBoundary = false;
}
} else {
this.rawSegments.push(segment);
}
}

loc.column += 1;
this.generatedCodeColumn += 1;
first = false;
Expand Down
13 changes: 13 additions & 0 deletions test/MagicString.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,19 @@ describe('MagicString', () => {
assert.equal(loc8.line, 1);
assert.equal(loc8.column, 5);
});

it ('generates a source map without unneeded line break mappings', () => {
const s = new MagicString('function foo(){\n console.log("bar")\n}');

const map = s.generateMap({
file: 'output.js',
source: 'input.js',
includeContent: true,
hires: 'boundary'
});

assert.equal(map.mappings, 'AAAA,QAAQ,CAAC,GAAG,CAAC,CAAC;AACd,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC;AACnB');
});
});

describe('getIndentString', () => {
Expand Down

0 comments on commit 5b1ecf7

Please sign in to comment.