@@ -4,7 +4,11 @@ namespace ts {
44 extendedDiagnostics ?: boolean ;
55 }
66
7- export function createSourceMapGenerator ( host : EmitHost , file : string , sourceRoot : string , sourcesDirectoryPath : string , generatorOptions : SourceMapGeneratorOptions ) : SourceMapGenerator {
7+ declare let TextDecoder : undefined | ( new ( ) => { decode ( buffer : ArrayBuffer | ArrayBufferView ) : string } ) ;
8+ const decoder = new ( typeof TextDecoder !== "undefined" ? TextDecoder : require ( "util" ) . TextDecoder ) ( "ascii" ) ;
9+ let mappingsBuffer : Uint8Array ;
10+
11+ export function createSourceMapGenerator ( host : EmitHost , file : string , guessedInputLength : number , sourceRoot : string , sourcesDirectoryPath : string , generatorOptions : SourceMapGeneratorOptions ) : SourceMapGenerator {
812 const { enter, exit } = generatorOptions . extendedDiagnostics
913 ? performance . createTimer ( "Source Map" , "beforeSourcemap" , "afterSourcemap" )
1014 : performance . nullTimer ;
@@ -17,15 +21,18 @@ namespace ts {
1721
1822 const names : string [ ] = [ ] ;
1923 let nameToNameIndexMap : ESMap < string , number > | undefined ;
20- let mappings = "" ;
21- let mappingsBuffer = new Uint8Array ( 1024 ) ;
22- let mappingsBufferPos = 0 ;
24+ mappingsBuffer ||= new Uint8Array ( guessedInputLength + 1 >> 1 ) ;
25+ let lastMappings : string | undefined ;
26+ let mappingsPos = 0 ;
2327 function setMapping ( charCode : number ) {
24- if ( mappingsBufferPos >= mappingsBuffer . length ) {
25- mappings += String . fromCharCode . apply ( undefined , mappingsBuffer ) ;
26- mappingsBufferPos = 0 ;
28+ // resize to 1.5 * length
29+ if ( mappingsPos >= mappingsBuffer . length ) {
30+ const oldLength = mappingsBuffer . length + 1 ;
31+ const replacementBuffer = new Uint8Array ( oldLength + ( ( oldLength + 1 ) >> 1 ) ) ;
32+ replacementBuffer . set ( mappingsBuffer ) ;
33+ mappingsBuffer = replacementBuffer ;
2734 }
28- mappingsBuffer [ mappingsBufferPos ++ ] = charCode ;
35+ mappingsBuffer [ mappingsPos ++ ] = charCode ;
2936 }
3037
3138 function base64VLQFormatEncode ( inValue : number ) {
@@ -297,16 +304,9 @@ namespace ts {
297304 exit ( ) ;
298305 }
299306
300- function flushMappings ( ) : void {
301- if ( mappingsBufferPos > 0 ) {
302- mappings += String . fromCharCode . apply ( undefined , mappingsBuffer . subarray ( 0 , mappingsBufferPos ) ) ;
303- mappingsBufferPos = 0 ;
304- }
305- }
306-
307307 function toJSON ( ) : RawSourceMap {
308308 commitPendingMapping ( ) ;
309- flushMappings ( ) ;
309+ const mappings = ( lastMappings ??= decoder . decode ( mappingsBuffer . subarray ( 0 , mappingsPos ) ) ) ;
310310 return {
311311 version : 3 ,
312312 file,
0 commit comments