@@ -83,8 +83,81 @@ async function replace_async(
83
83
return out . concat ( final_content ) ;
84
84
}
85
85
86
- /**
87
- * Convert a preprocessor output and its leading prefix and trailing suffix into StringWithSourceMap
86
+ /**
87
+ * Import decoded sourcemap from mozilla/source-map/SourceMapGenerator
88
+ * Forked from source-map/lib/source-map-generator.js
89
+ * from methods _serializeMappings and toJSON.
90
+ * We cannot use source-map.d.ts types, because we access hidden properties.
91
+ */
92
+ function decoded_sourcemap_from_generator ( generator : any ) {
93
+ let previous_generated_line = 1 ;
94
+ const converted_mappings = [ [ ] ] ;
95
+ let result_line ;
96
+ let result_segment ;
97
+ let mapping ;
98
+
99
+ const source_idx = generator . _sources . toArray ( )
100
+ . reduce ( ( acc , val , idx ) => ( acc [ val ] = idx , acc ) , { } ) ;
101
+
102
+ const name_idx = generator . _names . toArray ( )
103
+ . reduce ( ( acc , val , idx ) => ( acc [ val ] = idx , acc ) , { } ) ;
104
+
105
+ const mappings = generator . _mappings . toArray ( ) ;
106
+ result_line = converted_mappings [ 0 ] ;
107
+
108
+ for ( let i = 0 , len = mappings . length ; i < len ; i ++ ) {
109
+ mapping = mappings [ i ] ;
110
+
111
+ if ( mapping . generatedLine > previous_generated_line ) {
112
+ while ( mapping . generatedLine > previous_generated_line ) {
113
+ converted_mappings . push ( [ ] ) ;
114
+ previous_generated_line ++ ;
115
+ }
116
+ result_line = converted_mappings [ mapping . generatedLine - 1 ] ; // line is one-based
117
+ } else if ( i > 0 ) {
118
+ const previous_mapping = mappings [ i - 1 ] ;
119
+ if (
120
+ // sorted by selectivity
121
+ mapping . generatedColumn === previous_mapping . generatedColumn &&
122
+ mapping . originalColumn === previous_mapping . originalColumn &&
123
+ mapping . name === previous_mapping . name &&
124
+ mapping . generatedLine === previous_mapping . generatedLine &&
125
+ mapping . originalLine === previous_mapping . originalLine &&
126
+ mapping . source === previous_mapping . source
127
+ ) {
128
+ continue ;
129
+ }
130
+ }
131
+ result_line . push ( [ mapping . generatedColumn ] ) ;
132
+ result_segment = result_line [ result_line . length - 1 ] ;
133
+
134
+ if ( mapping . source != null ) {
135
+ result_segment . push ( ...[
136
+ source_idx [ mapping . source ] ,
137
+ mapping . originalLine - 1 , // line is one-based
138
+ mapping . originalColumn
139
+ ] ) ;
140
+ if ( mapping . name != null ) {
141
+ result_segment . push ( name_idx [ mapping . name ] ) ;
142
+ }
143
+ }
144
+ }
145
+
146
+ const map = {
147
+ version : generator . _version ,
148
+ sources : generator . _sources . toArray ( ) ,
149
+ names : generator . _names . toArray ( ) ,
150
+ mappings : converted_mappings
151
+ } ;
152
+ if ( generator . _file != null ) {
153
+ ( map as any ) . file = generator . _file ;
154
+ }
155
+ // not needed: map.sourcesContent and map.sourceRoot
156
+ return map ;
157
+ }
158
+
159
+ /**
160
+ * Convert a preprocessor output and its leading prefix and trailing suffix into StringWithSourceMap
88
161
*/
89
162
function get_replacement (
90
163
filename : string ,
@@ -109,6 +182,10 @@ function get_replacement(
109
182
if ( typeof ( decoded_map . mappings ) === 'string' ) {
110
183
decoded_map . mappings = decode_mappings ( decoded_map . mappings ) ;
111
184
}
185
+ if ( ( decoded_map as any ) . _mappings && decoded_map . constructor . name === 'SourceMapGenerator' ) {
186
+ // import decoded sourcemap from mozilla/source-map/SourceMapGenerator
187
+ decoded_map = decoded_sourcemap_from_generator ( decoded_map ) ;
188
+ }
112
189
sourcemap_add_offset ( decoded_map , get_location ( offset + prefix . length ) ) ;
113
190
}
114
191
const processed_with_map = StringWithSourcemap . from_processed ( processed . code , decoded_map ) ;
@@ -164,7 +241,7 @@ export default async function preprocess(
164
241
165
242
async function preprocess_tag_content ( tag_name : 'style' | 'script' , preprocessor : Preprocessor ) {
166
243
const get_location = getLocator ( source ) ;
167
- const tag_regex = tag_name == 'style'
244
+ const tag_regex = tag_name === 'style'
168
245
? / < ! - - [ ^ ] * ?- - > | < s t y l e ( \s [ ^ ] * ?) ? (?: > ( [ ^ ] * ?) < \/ s t y l e > | \/ > ) / gi
169
246
: / < ! - - [ ^ ] * ?- - > | < s c r i p t ( \s [ ^ ] * ?) ? (?: > ( [ ^ ] * ?) < \/ s c r i p t > | \/ > ) / gi;
170
247
0 commit comments