@@ -17,6 +17,7 @@ namespace ts {
17
17
18
18
const names : string [ ] = [ ] ;
19
19
let nameToNameIndexMap : ESMap < string , number > | undefined ;
20
+ const mappingCharCodes : number [ ] = [ ] ;
20
21
let mappings = "" ;
21
22
22
23
// Last recorded and encoded mappings
@@ -221,7 +222,7 @@ namespace ts {
221
222
if ( lastGeneratedLine < pendingGeneratedLine ) {
222
223
// Emit line delimiters
223
224
do {
224
- mappings += ";" ;
225
+ mappingCharCodes . push ( CharacterCodes . semicolon ) ; // ';'
225
226
lastGeneratedLine ++ ;
226
227
lastGeneratedCharacter = 0 ;
227
228
}
@@ -231,30 +232,30 @@ namespace ts {
231
232
Debug . assertEqual ( lastGeneratedLine , pendingGeneratedLine , "generatedLine cannot backtrack" ) ;
232
233
// Emit comma to separate the entry
233
234
if ( hasLast ) {
234
- mappings += "," ;
235
+ mappingCharCodes . push ( CharacterCodes . comma ) ; // ','
235
236
}
236
237
}
237
238
238
239
// 1. Relative generated character
239
- mappings += base64VLQFormatEncode ( pendingGeneratedCharacter - lastGeneratedCharacter ) ;
240
+ appendBase64VLQ ( pendingGeneratedCharacter - lastGeneratedCharacter ) ;
240
241
lastGeneratedCharacter = pendingGeneratedCharacter ;
241
242
242
243
if ( hasPendingSource ) {
243
244
// 2. Relative sourceIndex
244
- mappings += base64VLQFormatEncode ( pendingSourceIndex - lastSourceIndex ) ;
245
+ appendBase64VLQ ( pendingSourceIndex - lastSourceIndex ) ;
245
246
lastSourceIndex = pendingSourceIndex ;
246
247
247
248
// 3. Relative source line
248
- mappings += base64VLQFormatEncode ( pendingSourceLine - lastSourceLine ) ;
249
+ appendBase64VLQ ( pendingSourceLine - lastSourceLine ) ;
249
250
lastSourceLine = pendingSourceLine ;
250
251
251
252
// 4. Relative source character
252
- mappings += base64VLQFormatEncode ( pendingSourceCharacter - lastSourceCharacter ) ;
253
+ appendBase64VLQ ( pendingSourceCharacter - lastSourceCharacter ) ;
253
254
lastSourceCharacter = pendingSourceCharacter ;
254
255
255
256
if ( hasPendingName ) {
256
257
// 5. Relative nameIndex
257
- mappings += base64VLQFormatEncode ( pendingNameIndex - lastNameIndex ) ;
258
+ appendBase64VLQ ( pendingNameIndex - lastNameIndex ) ;
258
259
lastNameIndex = pendingNameIndex ;
259
260
}
260
261
}
@@ -263,8 +264,16 @@ namespace ts {
263
264
exit ( ) ;
264
265
}
265
266
267
+ function serializeMappings ( ) : void {
268
+ for ( let i = 0 , len = mappingCharCodes . length ; i < len ; i += 1024 ) {
269
+ mappings += String . fromCharCode . apply ( undefined , mappingCharCodes . slice ( i , i + 1024 ) ) ;
270
+ }
271
+ mappingCharCodes . length = 0 ;
272
+ }
273
+
266
274
function toJSON ( ) : RawSourceMap {
267
275
commitPendingMapping ( ) ;
276
+ serializeMappings ( ) ;
268
277
return {
269
278
version : 3 ,
270
279
file,
@@ -275,6 +284,31 @@ namespace ts {
275
284
sourcesContent,
276
285
} ;
277
286
}
287
+
288
+ function appendBase64VLQ ( inValue : number ) : void {
289
+ // Add a new least significant bit that has the sign of the value.
290
+ // if negative number the least significant bit that gets added to the number has value 1
291
+ // else least significant bit value that gets added is 0
292
+ // eg. -1 changes to binary : 01 [1] => 3
293
+ // +1 changes to binary : 01 [0] => 2
294
+ if ( inValue < 0 ) {
295
+ inValue = ( ( - inValue ) << 1 ) + 1 ;
296
+ }
297
+ else {
298
+ inValue = inValue << 1 ;
299
+ }
300
+
301
+ // Encode 5 bits at a time starting from least significant bits
302
+ do {
303
+ let currentDigit = inValue & 31 ; // 11111
304
+ inValue = inValue >> 5 ;
305
+ if ( inValue > 0 ) {
306
+ // There are still more digits to decode, set the msb (6th bit)
307
+ currentDigit = currentDigit | 32 ;
308
+ }
309
+ mappingCharCodes . push ( base64FormatEncode ( currentDigit ) ) ;
310
+ } while ( inValue > 0 ) ;
311
+ }
278
312
}
279
313
280
314
// Sometimes tools can see the following line as a source mapping url comment, so we mangle it a bit (the [M])
@@ -544,34 +578,6 @@ namespace ts {
544
578
- 1 ;
545
579
}
546
580
547
- function base64VLQFormatEncode ( inValue : number ) {
548
- // Add a new least significant bit that has the sign of the value.
549
- // if negative number the least significant bit that gets added to the number has value 1
550
- // else least significant bit value that gets added is 0
551
- // eg. -1 changes to binary : 01 [1] => 3
552
- // +1 changes to binary : 01 [0] => 2
553
- if ( inValue < 0 ) {
554
- inValue = ( ( - inValue ) << 1 ) + 1 ;
555
- }
556
- else {
557
- inValue = inValue << 1 ;
558
- }
559
-
560
- // Encode 5 bits at a time starting from least significant bits
561
- let encodedStr = "" ;
562
- do {
563
- let currentDigit = inValue & 31 ; // 11111
564
- inValue = inValue >> 5 ;
565
- if ( inValue > 0 ) {
566
- // There are still more digits to decode, set the msb (6th bit)
567
- currentDigit = currentDigit | 32 ;
568
- }
569
- encodedStr = encodedStr + String . fromCharCode ( base64FormatEncode ( currentDigit ) ) ;
570
- } while ( inValue > 0 ) ;
571
-
572
- return encodedStr ;
573
- }
574
-
575
581
interface MappedPosition {
576
582
generatedPosition : number ;
577
583
source : string | undefined ;
0 commit comments