Skip to content

Commit d094d7e

Browse files
committed
Merge pull request #2834 from Medium/nick-sourcemap
Make sourcemap generation a bit faster
2 parents b550b20 + 470af20 commit d094d7e

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

lib/less/source-map-output.js

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
11
module.exports = function (environment) {
22

3+
/**
4+
* @param source The code
5+
* @param ignoredCharsCount Number of characters at the start of the file to ignore.
6+
* @constructor
7+
*/
8+
var FileInfo = function (source, ignoredCharsCount) {
9+
this.ignoredCharsCount = ignoredCharsCount;
10+
this.source = source.slice(ignoredCharsCount);
11+
this.sourceLines = this.source.split('\n');
12+
};
13+
14+
/** Translate absolute source offset to line/column offset. */
15+
FileInfo.prototype.getLocation = function (index) {
16+
index = Math.max(0, index - this.ignoredCharsCount);
17+
var line = 0;
18+
for (; line < this.sourceLines.length && index >= this.sourceLines[line].length + 1; line++) {
19+
index -= this.sourceLines[line].length + 1; // +1 for the '\n' character
20+
}
21+
return {line: line + 1, column: index};
22+
};
23+
324
var SourceMapOutput = function (options) {
425
this._css = [];
526
this._rootNode = options.rootNode;
6-
this._contentsMap = options.contentsMap;
7-
this._contentsIgnoredCharsMap = options.contentsIgnoredCharsMap;
27+
28+
this._contentsInfoMap = {};
29+
for (var key in options.contentsMap) {
30+
if (options.contentsMap.hasOwnProperty(key)) {
31+
this._contentsInfoMap[key] = new FileInfo(
32+
options.contentsMap[key], options.contentsIgnoredCharsMap[key] || 0);
33+
}
34+
}
35+
836
if (options.sourceMapFilename) {
937
this._sourceMapFilename = options.sourceMapFilename.replace(/\\/g, '/');
1038
}
@@ -48,39 +76,22 @@ module.exports = function (environment) {
4876
}
4977

5078
var lines,
51-
sourceLines,
5279
columns,
53-
sourceColumns,
5480
i;
5581

56-
if (fileInfo) {
57-
var inputSource = this._contentsMap[fileInfo.filename];
58-
59-
// remove vars/banner added to the top of the file
60-
if (this._contentsIgnoredCharsMap[fileInfo.filename]) {
61-
// adjust the index
62-
index -= this._contentsIgnoredCharsMap[fileInfo.filename];
63-
if (index < 0) { index = 0; }
64-
// adjust the source
65-
inputSource = inputSource.slice(this._contentsIgnoredCharsMap[fileInfo.filename]);
66-
}
67-
inputSource = inputSource.substring(0, index);
68-
sourceLines = inputSource.split("\n");
69-
sourceColumns = sourceLines[sourceLines.length - 1];
70-
}
71-
7282
lines = chunk.split("\n");
7383
columns = lines[lines.length - 1];
7484

7585
if (fileInfo) {
86+
var location = this._contentsInfoMap[fileInfo.filename].getLocation(index);
7687
if (!mapLines) {
7788
this._sourceMapGenerator.addMapping({ generated: { line: this._lineNumber + 1, column: this._column},
78-
original: { line: sourceLines.length, column: sourceColumns.length},
89+
original: location,
7990
source: this.normalizeFilename(fileInfo.filename)});
8091
} else {
8192
for (i = 0; i < lines.length; i++) {
8293
this._sourceMapGenerator.addMapping({ generated: { line: this._lineNumber + i + 1, column: i === 0 ? this._column : 0},
83-
original: { line: sourceLines.length + i, column: i === 0 ? sourceColumns.length : 0},
94+
original: { line: location.line + i, column: i === 0 ? location.column : 0},
8495
source: this.normalizeFilename(fileInfo.filename)});
8596
}
8697
}
@@ -105,11 +116,8 @@ module.exports = function (environment) {
105116

106117
if (this._outputSourceFiles) {
107118
for (var filename in this._contentsMap) {
108-
if (this._contentsMap.hasOwnProperty(filename)) {
109-
var source = this._contentsMap[filename];
110-
if (this._contentsIgnoredCharsMap[filename]) {
111-
source = source.slice(this._contentsIgnoredCharsMap[filename]);
112-
}
119+
if (this._contentsInfoMap.hasOwnProperty(filename)) {
120+
var source = this._contentsInfoMap[filename].source;
113121
this._sourceMapGenerator.setSourceContent(this.normalizeFilename(filename), source);
114122
}
115123
}

0 commit comments

Comments
 (0)