@@ -21,9 +21,19 @@ npm install @ampproject/remapping
21
21
``` typescript
22
22
function remapping(
23
23
map : SourceMap | SourceMap [],
24
- loader : (file : string ) => (SourceMap | null | undefined ),
24
+ loader : (file : string , ctx : LoaderContext ) => (SourceMap | null | undefined ),
25
25
options ? : { excludeContent: boolean , decodedMappings: boolean }
26
26
): SourceMap ;
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.
32
+ type LoaderContext = {
33
+ readonly importer: string ;
34
+ source: string ;
35
+ content: string | null | undefined ;
36
+ }
27
37
` ` `
28
38
29
39
` remapping ` takes the final output sourcemap, and a ` loader ` function. For every source file pointer
@@ -55,15 +65,20 @@ const minifiedTransformedMap = JSON.stringify({
55
65
56
66
const remapped = remapping (
57
67
minifiedTransformedMap ,
58
- (file ) => {
68
+ (file , ctx ) => {
59
69
60
70
// The "transformed.js" file is an transformed file.
61
71
if (file === ' transformed.js' ) {
72
+ // The root importer is empty.
73
+ console .assert (ctx .importer === ' ' );
74
+
62
75
return transformedMap ;
63
76
}
64
77
65
78
// Loader will be called to load transformedMap's source file pointers as well.
66
79
console .assert (file === ' helloworld.js' );
80
+ // `transformed.js`'s sourcemap points into `helloworld.js`.
81
+ console .assert (ctx .importer === ' transformed.js' );
67
82
return null ;
68
83
}
69
84
);
@@ -110,6 +125,76 @@ console.log(remapped);
110
125
// };
111
126
```
112
127
128
+ ### Advanced control of the loading graph
129
+
130
+ #### ` source `
131
+
132
+ 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
134
+ 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.
136
+
137
+ ``` js
138
+ const remapped = remapping (
139
+ minifiedTransformedMap,
140
+ (file , ctx ) => {
141
+
142
+ if (file === ' transformed.js' ) {
143
+ // We pretend the transformed.js file actually exists in the 'src/' directory. When the nested
144
+ // source files are loaded, they will now be relative to `src/`.
145
+ ctx .source = ' src/transformed.js' ;
146
+ return transformedMap;
147
+ }
148
+
149
+ console .assert (file === ' src/helloworld.js' );
150
+ // We could futher change the source of this original file, eg, to be inside a nested directory
151
+ // itself. This will be reflected in the remapped sourcemap.
152
+ ctx .source = ' src/nested/transformed.js' ;
153
+ return null ;
154
+ }
155
+ );
156
+
157
+ console .log (remapped);
158
+ // {
159
+ // …,
160
+ // sources: ['src/nested/helloworld.js'],
161
+ // };
162
+ ```
163
+
164
+
165
+ #### ` content `
166
+
167
+ 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.
170
+
171
+ ``` js
172
+ const remapped = remapping (
173
+ minifiedTransformedMap,
174
+ (file , ctx ) => {
175
+
176
+ if (file === ' transformed.js' ) {
177
+ // transformedMap does not include a `sourcesContent` field, so usually the remapped sourcemap
178
+ // would not include any `sourcesContent` values.
179
+ return transformedMap;
180
+ }
181
+
182
+ console .assert (file === ' helloworld.js' );
183
+ // We can read the file to provide the source content.
184
+ ctx .content = fs .readFileSync (file, ' utf8' );
185
+ return null ;
186
+ }
187
+ );
188
+
189
+ console .log (remapped);
190
+ // {
191
+ // …,
192
+ // sourcesContent: [
193
+ // 'console.log("Hello world!")',
194
+ // ],
195
+ // };
196
+ ```
197
+
113
198
### Options
114
199
115
200
#### excludeContent
0 commit comments