2
2
3
3
> Remap sequential sourcemaps through transformations to point at the original source code
4
4
5
- Remapping allows you to take the sourcemaps generated through transforming your code and
6
- "remap" them to the original source locations. Think "my minified code, transformed with babel and
7
- bundled with webpack", all pointing to the correct location in your original source code.
5
+ Remapping allows you to take the sourcemaps generated through transforming your code and "remap"
6
+ them to the original source locations. Think "my minified code, transformed with babel and bundled
7
+ with webpack", all pointing to the correct location in your original source code.
8
8
9
9
With remapping, none of your source code transformations need to be aware of the input's sourcemap,
10
10
they only need to generate an output sourcemap. This greatly simplifies building custom
@@ -25,12 +25,13 @@ function remapping(
25
25
options ? : { excludeContent: boolean , decodedMappings: boolean }
26
26
): SourceMap ;
27
27
28
- // LoaderContext gives the loader the importing sourcemap, and the ability to override the "source"
29
- // location (where nested sources are resolved relative to, and where an original source exists),
30
- // and the ability to override the "content" of an original sourcemap for inclusion in the output
31
- // sourcemap.
28
+ // LoaderContext gives the loader the importing sourcemap, tree depth, the ability to override the
29
+ // "source" location (where child sources are resolved relative to, or the location of original
30
+ // source), and the ability to override the "content" of an original source for inclusion in the
31
+ // output sourcemap.
32
32
type LoaderContext = {
33
33
readonly importer: string ;
34
+ readonly depth: number ;
34
35
source: string ;
35
36
content: string | null | undefined ;
36
37
}
@@ -71,6 +72,9 @@ const remapped = remapping(
71
72
if (file === ' transformed.js' ) {
72
73
// The root importer is empty.
73
74
console .assert (ctx .importer === ' ' );
75
+ // The depth in the sourcemap tree we're currently loading.
76
+ // The root `minifiedTransformedMap` is depth 0, and its source children are depth 1, etc.
77
+ console .assert (ctx .depth === 1 );
74
78
75
79
return transformedMap ;
76
80
}
@@ -79,6 +83,8 @@ const remapped = remapping(
79
83
console .assert (file === ' helloworld.js' );
80
84
// `transformed.js`'s sourcemap points into `helloworld.js`.
81
85
console .assert (ctx .importer === ' transformed.js' );
86
+ // This is a source child of `transformed`, which is a source child of `minifiedTransformedMap`.
87
+ console .assert (ctx .depth === 2 );
82
88
return null ;
83
89
}
84
90
);
@@ -107,8 +113,9 @@ column of the 2nd line of the file `helloworld.js`".
107
113
### Multiple transformations of a file
108
114
109
115
As a convenience, if you have multiple single-source transformations of a file, you may pass an
110
- array of sourcemap files in the order of most-recent transformation sourcemap first. So our above
111
- example could have been writen as:
116
+ array of sourcemap files in the order of most-recent transformation sourcemap first. Note that this
117
+ changes the ` importer ` and ` depth ` of each call to our loader. So our above example could have been
118
+ written as:
112
119
113
120
``` js
114
121
const remapped = remapping (
@@ -130,9 +137,9 @@ console.log(remapped);
130
137
#### ` source `
131
138
132
139
The ` source ` property can overridden to any value to change the location of the current load. Eg,
133
- for an original source file, it allows us to change the filepath to the original source regardless
140
+ for an original source file, it allows us to change the location to the original source regardless
134
141
of what the sourcemap source entry says. And for transformed files, it allows us to change the
135
- resolving location for nested sources files of the loaded sourcemap.
142
+ relative resolving location for child sources of the loaded sourcemap.
136
143
137
144
``` js
138
145
const remapped = remapping (
@@ -165,8 +172,9 @@ console.log(remapped);
165
172
#### ` content `
166
173
167
174
The ` content ` property can be overridden when we encounter an original source file. Eg, this allows
168
- you to manually provide the source content of the file regardless of whether the ` sourcesContent `
169
- field is present in the parent sourcemap. Or, it can be set to ` null ` to remove the source content.
175
+ you to manually provide the source content of the original file regardless of whether the
176
+ ` sourcesContent ` field is present in the parent sourcemap. It can also be set to ` null ` to remove
177
+ the source content.
170
178
171
179
``` js
172
180
const remapped = remapping (
@@ -199,13 +207,12 @@ console.log(remapped);
199
207
200
208
#### excludeContent
201
209
202
- By default, ` excludeContent ` is ` false ` . Passing ` { excludeContent: true } `
203
- will exclude the ` sourcesContent ` field from the returned sourcemap. This is
204
- mainly useful when you want to reduce the size out the sourcemap.
210
+ By default, ` excludeContent ` is ` false ` . Passing ` { excludeContent: true } ` will exclude the
211
+ ` sourcesContent ` field from the returned sourcemap. This is mainly useful when you want to reduce
212
+ the size out the sourcemap.
205
213
206
214
#### decodedMappings
207
215
208
- By default, ` decodedMappings ` is ` false ` . Passing ` { decodedMappings: true } `
209
- will leave the ` mappings ` field in a [ decoded
210
- state] ( https://github.com/rich-harris/sourcemap-codec ) instead of encoding
211
- into a VLQ string.
216
+ By default, ` decodedMappings ` is ` false ` . Passing ` { decodedMappings: true } ` will leave the
217
+ ` mappings ` field in a [ decoded state] ( https://github.com/rich-harris/sourcemap-codec ) instead of
218
+ encoding into a VLQ string.
0 commit comments