@@ -16,6 +16,9 @@ function get_file_basename(filename: string) {
16
16
return filename . split ( / [ / \\ ] / ) . pop ( ) ;
17
17
}
18
18
19
+ /**
20
+ * Represents intermediate states of the preprocessing.
21
+ */
19
22
class PreprocessResult {
20
23
// sourcemap_list is sorted in reverse order from last map (index 0) to first map (index -1)
21
24
// so we use sourcemap_list.unshift() to add new maps
@@ -27,16 +30,16 @@ class PreprocessResult {
27
30
get_location : ReturnType < typeof getLocator > ;
28
31
29
32
constructor ( public source : string , public filename : string ) {
30
- this . update_source ( { string : source } ) ;
33
+ this . update_source ( { string : source } ) ;
31
34
32
35
// preprocess source must be relative to itself or equal null
33
36
this . file_basename = filename == null ? null : get_file_basename ( filename ) ;
34
37
}
35
38
36
- update_source ( { string : source , map, dependencies} : SourceUpdate ) {
39
+ update_source ( { string : source , map, dependencies } : SourceUpdate ) {
37
40
this . source = source ;
38
41
this . get_location = getLocator ( source ) ;
39
-
42
+
40
43
if ( map ) {
41
44
this . sourcemap_list . unshift ( map ) ;
42
45
}
@@ -46,12 +49,9 @@ class PreprocessResult {
46
49
}
47
50
}
48
51
49
- processed ( ) : Processed {
52
+ to_processed ( ) : Processed {
50
53
// Combine all the source maps for each preprocessor function into one
51
- const map : RawSourceMap = combine_sourcemaps (
52
- this . file_basename ,
53
- this . sourcemap_list
54
- ) ;
54
+ const map : RawSourceMap = combine_sourcemaps ( this . file_basename , this . sourcemap_list ) ;
55
55
56
56
return {
57
57
// TODO return separated output, in future version where svelte.compile supports it:
@@ -61,7 +61,7 @@ class PreprocessResult {
61
61
62
62
code : this . source ,
63
63
dependencies : [ ...new Set ( this . dependencies ) ] ,
64
- map : ( map as object ) ,
64
+ map : map as object ,
65
65
toString : ( ) => this . source
66
66
} ;
67
67
}
@@ -91,29 +91,31 @@ function processed_content_to_sws(
91
91
}
92
92
93
93
/**
94
- * Convert the whole tag including content (replacing it with `processed`)
95
- * into a `StringWithSourcemap` representing the transformed code .
94
+ * Given the whole tag including content, return a `StringWithSourcemap`
95
+ * representing the tag content replaced with `processed` .
96
96
*/
97
97
function processed_tag_to_sws (
98
98
processed : Processed ,
99
99
tag_name : 'style' | 'script' ,
100
100
attributes : string ,
101
- content : string ,
102
- { file_basename, get_location } : Source ) : StringWithSourcemap {
103
- const build_sws = ( content : string , offset : number ) =>
104
- StringWithSourcemap . from_source ( file_basename , content , get_location ( offset ) ) ;
101
+ { source, file_basename, get_location } : Source ) : StringWithSourcemap {
102
+ const build_sws = ( source : string , offset : number ) =>
103
+ StringWithSourcemap . from_source ( file_basename , source , get_location ( offset ) ) ;
105
104
106
105
const tag_open = `<${ tag_name } ${ attributes || '' } >` ;
107
106
const tag_close = `</${ tag_name } >` ;
108
107
109
108
const tag_open_sws = build_sws ( tag_open , 0 ) ;
110
- const tag_close_sws = build_sws ( tag_close , tag_open . length + content . length ) ;
109
+ const tag_close_sws = build_sws ( tag_close , tag_open . length + source . length ) ;
111
110
112
111
const content_sws = processed_content_to_sws ( processed , get_location ( tag_open . length ) , file_basename ) ;
113
112
114
113
return tag_open_sws . concat ( content_sws ) . concat ( tag_close_sws ) ;
115
114
}
116
115
116
+ /**
117
+ * Calculate the updates required to process all instances of the specified tag.
118
+ */
117
119
async function process_tag (
118
120
tag_name : 'style' | 'script' ,
119
121
preprocessor : Preprocessor ,
@@ -147,8 +149,8 @@ async function process_tag(
147
149
if ( processed && processed . dependencies ) dependencies . push ( ...processed . dependencies ) ;
148
150
if ( ! processed || ! processed . map && processed . code === content ) return no_change ( ) ;
149
151
150
- return processed_tag_to_sws ( processed , tag_name , attributes , content ,
151
- { ... source , get_location : offset => source . get_location ( offset + tag_offset ) } ) ;
152
+ return processed_tag_to_sws ( processed , tag_name , attributes ,
153
+ { source : content , get_location : offset => source . get_location ( offset + tag_offset ) , filename , file_basename } ) ;
152
154
}
153
155
154
156
return { ...await replace_in_code ( tag_regex , process_single_tag , source ) , dependencies} ;
@@ -203,5 +205,5 @@ export default async function preprocess(
203
205
result . update_source ( await process_tag ( 'style' , preprocess , result ) ) ;
204
206
}
205
207
206
- return result . processed ( ) ;
208
+ return result . to_processed ( ) ;
207
209
}
0 commit comments