@@ -37,10 +37,11 @@ export function sourcemap_add_offset(
37
37
} as SourceMappings ;
38
38
}
39
39
40
- function merge_tables < T > ( this_table : T [ ] , other_table ) : [ T [ ] , number [ ] ] {
40
+ function merge_tables < T > ( this_table : T [ ] , other_table ) : [ T [ ] , number [ ] , boolean ] {
41
41
const new_table = this_table . slice ( ) ;
42
42
const idx_map = [ ] ;
43
43
other_table = other_table || [ ] ;
44
+ let has_changed = false ;
44
45
for ( const [ other_idx , other_val ] of other_table . entries ( ) ) {
45
46
const this_idx = this_table . indexOf ( other_val ) ;
46
47
if ( this_idx >= 0 ) {
@@ -49,9 +50,10 @@ function merge_tables<T>(this_table: T[], other_table): [T[], number[]] {
49
50
const new_idx = new_table . length ;
50
51
new_table [ new_idx ] = other_val ;
51
52
idx_map [ other_idx ] = new_idx ;
53
+ has_changed = true ;
52
54
}
53
55
}
54
- return [ new_table , idx_map ] ;
56
+ return [ new_table , idx_map , has_changed ] ;
55
57
}
56
58
57
59
export class StringWithSourcemap {
@@ -69,18 +71,20 @@ export class StringWithSourcemap {
69
71
if ( other . string == '' ) return this ;
70
72
71
73
// combine sources and names
72
- const [ sources , new_source_idx ] = merge_tables ( this . map . sources , other . map . sources ) ;
73
- const [ names , new_name_idx ] = merge_tables ( this . map . names , other . map . names ) ;
74
+ const [ sources , new_source_idx , sources_changed ] = merge_tables ( this . map . sources , other . map . sources ) ;
75
+ const [ names , new_name_idx , names_changed ] = merge_tables ( this . map . names , other . map . names ) ;
74
76
75
77
// update source refs and name refs
76
- const other_mappings = other . map . mappings . map ( ( line ) =>
77
- line . map ( seg => {
78
- const new_seg = seg . slice ( ) as MappingSegment ;
79
- if ( seg [ 1 ] ) new_seg [ 1 ] = new_source_idx [ seg [ 1 ] ] ;
80
- if ( seg [ 4 ] ) new_seg [ 4 ] = new_name_idx [ seg [ 4 ] ] ;
81
- return new_seg ;
82
- } )
83
- ) ;
78
+ const other_mappings =
79
+ ( sources_changed || names_changed )
80
+ ? other . map . mappings . slice ( ) . map ( line =>
81
+ line . map ( seg => {
82
+ if ( seg [ 1 ] ) seg [ 1 ] = new_source_idx [ seg [ 1 ] ] ;
83
+ if ( seg [ 4 ] ) seg [ 4 ] = new_name_idx [ seg [ 4 ] ] ;
84
+ return seg ;
85
+ } )
86
+ )
87
+ : other . map . mappings ;
84
88
85
89
// combine the mappings
86
90
@@ -89,17 +93,18 @@ export class StringWithSourcemap {
89
93
// 2. first line of second map
90
94
// columns of 2 must be shifted
91
95
92
- const col_offset = last_line_length ( this . string ) ;
96
+ const column_offset = last_line_length ( this . string ) ;
93
97
94
98
const first_line : MappingSegment [ ] =
95
99
other_mappings . length == 0
96
100
? [ ]
97
- : col_offset == 0
101
+ : column_offset == 0
98
102
? other_mappings [ 0 ] . slice ( ) as MappingSegment [ ]
99
- : other_mappings [ 0 ] . map ( seg => (
103
+ : other_mappings [ 0 ] . slice ( ) . map ( seg => {
100
104
// shift column
101
- [ seg [ 0 ] + col_offset ] . concat ( seg . slice ( 1 ) ) as MappingSegment
102
- ) ) ;
105
+ seg [ 0 ] += column_offset ;
106
+ return seg ;
107
+ } ) ;
103
108
104
109
const mappings : MappingSegment [ ] [ ] =
105
110
this . map . mappings . slice ( 0 , - 1 )
0 commit comments