@@ -12,6 +12,9 @@ interface SourceUpdate {
12
12
dependencies ?: string [ ] ;
13
13
}
14
14
15
+ /**
16
+ * Represents intermediate states of the preprocessing.
17
+ */
15
18
class PreprocessResult {
16
19
// sourcemap_list is sorted in reverse order from last map (index 0) to first map (index -1)
17
20
// so we use sourcemap_list.unshift() to add new maps
@@ -22,13 +25,13 @@ class PreprocessResult {
22
25
get_location : ReturnType < typeof getLocator > ;
23
26
24
27
constructor ( public source : string , public filename : string ) {
25
- this . update_source ( { string : source } ) ;
28
+ this . update_source ( { string : source } ) ;
26
29
}
27
30
28
- update_source ( { string : source , map, dependencies} : SourceUpdate ) {
31
+ update_source ( { string : source , map, dependencies } : SourceUpdate ) {
29
32
this . source = source ;
30
33
this . get_location = getLocator ( source ) ;
31
-
34
+
32
35
if ( map ) {
33
36
this . sourcemap_list . unshift ( map ) ;
34
37
}
@@ -38,12 +41,9 @@ class PreprocessResult {
38
41
}
39
42
}
40
43
41
- processed ( ) : Processed {
44
+ to_processed ( ) : Processed {
42
45
// Combine all the source maps for each preprocessor function into one
43
- const map : RawSourceMap = combine_sourcemaps (
44
- this . filename ,
45
- this . sourcemap_list
46
- ) ;
46
+ const map : RawSourceMap = combine_sourcemaps ( this . filename , this . sourcemap_list ) ;
47
47
48
48
return {
49
49
// TODO return separated output, in future version where svelte.compile supports it:
@@ -53,7 +53,7 @@ class PreprocessResult {
53
53
54
54
code : this . source ,
55
55
dependencies : [ ...new Set ( this . dependencies ) ] ,
56
- map : ( map as object ) ,
56
+ map : map as object ,
57
57
toString : ( ) => this . source
58
58
} ;
59
59
}
@@ -78,29 +78,31 @@ function processed_content_to_sws(
78
78
}
79
79
80
80
/**
81
- * Convert the whole tag including content (replacing it with `processed`)
82
- * into a `StringWithSourcemap` representing the transformed code .
81
+ * Given the whole tag including content, return a `StringWithSourcemap`
82
+ * representing the tag content replaced with `processed` .
83
83
*/
84
84
function processed_tag_to_sws (
85
85
processed : Processed ,
86
86
tag_name : 'style' | 'script' ,
87
87
attributes : string ,
88
- content : string ,
89
- { filename, get_location } : Source ) : StringWithSourcemap {
90
- const build_sws = ( content : string , offset : number ) =>
91
- StringWithSourcemap . from_source ( filename , content , get_location ( offset ) ) ;
88
+ { source, filename, get_location } : Source ) : StringWithSourcemap {
89
+ const build_sws = ( source : string , offset : number ) =>
90
+ StringWithSourcemap . from_source ( filename , source , get_location ( offset ) ) ;
92
91
93
92
const tag_open = `<${ tag_name } ${ attributes || '' } >` ;
94
93
const tag_close = `</${ tag_name } >` ;
95
94
96
95
const tag_open_sws = build_sws ( tag_open , 0 ) ;
97
- const tag_close_sws = build_sws ( tag_close , tag_open . length + content . length ) ;
96
+ const tag_close_sws = build_sws ( tag_close , tag_open . length + source . length ) ;
98
97
99
98
const content_sws = processed_content_to_sws ( processed , get_location ( tag_open . length ) ) ;
100
99
101
100
return tag_open_sws . concat ( content_sws ) . concat ( tag_close_sws ) ;
102
101
}
103
102
103
+ /**
104
+ * Calculate the updates required to process all instances of the specified tag.
105
+ */
104
106
async function process_tag (
105
107
tag_name : 'style' | 'script' ,
106
108
preprocessor : Preprocessor ,
@@ -134,8 +136,8 @@ async function process_tag(
134
136
if ( ! processed ) return no_change ( ) ;
135
137
if ( processed . dependencies ) dependencies . push ( ...processed . dependencies ) ;
136
138
137
- return processed_tag_to_sws ( processed , tag_name , attributes , content ,
138
- { ... source , get_location : offset => source . get_location ( offset + tag_offset ) } ) ;
139
+ return processed_tag_to_sws ( processed , tag_name , attributes ,
140
+ { source : content , get_location : offset => source . get_location ( offset + tag_offset ) , filename } ) ;
139
141
}
140
142
141
143
return { ...await replace_in_code ( tag_regex , process_single_tag , source ) , dependencies} ;
@@ -190,5 +192,5 @@ export default async function preprocess(
190
192
result . update_source ( await process_tag ( 'style' , preprocess , result ) ) ;
191
193
}
192
194
193
- return result . processed ( ) ;
195
+ return result . to_processed ( ) ;
194
196
}
0 commit comments