Skip to content

Commit 25e15fb

Browse files
Buffer.
1 parent aa732cf commit 25e15fb

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/compiler/sourcemap.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ namespace ts {
55
}
66

77
declare let TextDecoder: undefined | (new() => { decode(buffer: ArrayBuffer | ArrayBufferView): string });
8-
const decoder = new (typeof TextDecoder !== "undefined" ? TextDecoder : require("util").TextDecoder)("ascii");
8+
const decoder = typeof Buffer === "undefined" ?
9+
new (typeof TextDecoder !== "undefined" ? TextDecoder : require("util").TextDecoder)("ascii") :
10+
undefined;
11+
const decode: (buffer: ArrayBuffer) => string =
12+
decoder
13+
? buffer => decoder.decode(buffer)
14+
: buffer => (buffer as Buffer).toString("ascii");
915
let mappingsBuffer: Uint8Array;
1016

1117
export function createSourceMapGenerator(host: EmitHost, file: string, guessedInputLength: number, sourceRoot: string, sourcesDirectoryPath: string, generatorOptions: SourceMapGeneratorOptions): SourceMapGenerator {
@@ -21,7 +27,7 @@ namespace ts {
2127

2228
const names: string[] = [];
2329
let nameToNameIndexMap: ESMap<string, number> | undefined;
24-
mappingsBuffer ||= new Uint8Array(guessedInputLength + 1 >> 1);
30+
mappingsBuffer ||= typeof Buffer === undefined ? new Uint8Array(guessedInputLength + 1 >> 1) : Buffer.alloc(guessedInputLength + 1 >> 1);
2531
let lastMappings: string | undefined;
2632
let mappingsPos = 0;
2733
function setMapping(charCode: number) {
@@ -306,7 +312,7 @@ namespace ts {
306312

307313
function toJSON(): RawSourceMap {
308314
commitPendingMapping();
309-
const mappings = (lastMappings ??= decoder.decode(mappingsBuffer.subarray(0, mappingsPos)));
315+
const mappings = (lastMappings ??= decode(mappingsBuffer.subarray(0, mappingsPos)));
310316
return {
311317
version: 3,
312318
file,

0 commit comments

Comments
 (0)