Skip to content

Commit 5b1ecf7

Browse files
authored
perf: skip line break mappings (#284)
1 parent bde9d7e commit 5b1ecf7

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

src/utils/Mappings.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,6 @@ export default class Mappings {
5454
let charInHiresBoundary = false;
5555

5656
while (originalCharIndex < chunk.end) {
57-
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
58-
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
59-
60-
if (this.hires === 'boundary') {
61-
// in hires "boundary", group segments per word boundary than per char
62-
if (wordRegex.test(original[originalCharIndex])) {
63-
// for first char in the boundary found, start the boundary by pushing a segment
64-
if (!charInHiresBoundary) {
65-
this.rawSegments.push(segment);
66-
charInHiresBoundary = true;
67-
}
68-
} else {
69-
// for non-word char, end the boundary by pushing a segment
70-
this.rawSegments.push(segment);
71-
charInHiresBoundary = false;
72-
}
73-
} else {
74-
this.rawSegments.push(segment);
75-
}
76-
}
77-
7857
if (original[originalCharIndex] === '\n') {
7958
loc.line += 1;
8059
loc.column = 0;
@@ -83,6 +62,27 @@ export default class Mappings {
8362
this.generatedCodeColumn = 0;
8463
first = true;
8564
} else {
65+
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
66+
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
67+
68+
if (this.hires === 'boundary') {
69+
// in hires "boundary", group segments per word boundary than per char
70+
if (wordRegex.test(original[originalCharIndex])) {
71+
// for first char in the boundary found, start the boundary by pushing a segment
72+
if (!charInHiresBoundary) {
73+
this.rawSegments.push(segment);
74+
charInHiresBoundary = true;
75+
}
76+
} else {
77+
// for non-word char, end the boundary by pushing a segment
78+
this.rawSegments.push(segment);
79+
charInHiresBoundary = false;
80+
}
81+
} else {
82+
this.rawSegments.push(segment);
83+
}
84+
}
85+
8686
loc.column += 1;
8787
this.generatedCodeColumn += 1;
8888
first = false;

test/MagicString.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,19 @@ describe('MagicString', () => {
528528
assert.equal(loc8.line, 1);
529529
assert.equal(loc8.column, 5);
530530
});
531+
532+
it ('generates a source map without unneeded line break mappings', () => {
533+
const s = new MagicString('function foo(){\n console.log("bar")\n}');
534+
535+
const map = s.generateMap({
536+
file: 'output.js',
537+
source: 'input.js',
538+
includeContent: true,
539+
hires: 'boundary'
540+
});
541+
542+
assert.equal(map.mappings, 'AAAA,QAAQ,CAAC,GAAG,CAAC,CAAC;AACd,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC;AACnB');
543+
});
531544
});
532545

533546
describe('getIndentString', () => {

0 commit comments

Comments
 (0)