Skip to content

Commit

Permalink
perf: delay running guessIdent until needed
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jun 5, 2022
1 parent d8ec89c commit 50612ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Bundle.js
Expand Up @@ -164,7 +164,7 @@ export default class Bundle {
const indentStringCounts = {};

this.sources.forEach((source) => {
const indentStr = source.content.indentStr;
const indentStr = source.content._getRawIndentString();

if (indentStr === null) return;

Expand Down
19 changes: 17 additions & 2 deletions src/MagicString.js
Expand Up @@ -33,7 +33,7 @@ export default class MagicString {
indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
sourcemapLocations: { writable: true, value: new BitSet() },
storedNames: { writable: true, value: {} },
indentStr: { writable: true, value: guessIndent(string) },
indentStr: { writable: true, value: undefined },
});

if (DEBUG) {
Expand Down Expand Up @@ -175,7 +175,19 @@ export default class MagicString {
return new SourceMap(this.generateDecodedMap(options));
}

_ensureindentStr() {
if (this.indentStr === undefined) {
this.indentStr = guessIndent(this.original);
}
}

_getRawIndentString() {
this._ensureindentStr();
return this.indentStr;
}

getIndentString() {
this._ensureindentStr();
return this.indentStr === null ? '\t' : this.indentStr;
}

Expand All @@ -187,7 +199,10 @@ export default class MagicString {
indentStr = undefined;
}

indentStr = indentStr !== undefined ? indentStr : this.indentStr || '\t';
if (indentStr === undefined) {
this._ensureindentStr();
indentStr = this.indentStr || '\t';
}

if (indentStr === '') return this; // noop

Expand Down

0 comments on commit 50612ba

Please sign in to comment.