@@ -21,9 +21,19 @@ npm install @ampproject/remapping
2121``` typescript
2222function remapping(
2323 map : SourceMap | SourceMap [],
24- loader : (file : string ) => (SourceMap | null | undefined ),
24+ loader : (file : string , ctx : LoaderContext ) => (SourceMap | null | undefined ),
2525 options ? : { excludeContent: boolean , decodedMappings: boolean }
2626): 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+ }
2737` ` `
2838
2939` remapping ` takes the final output sourcemap, and a ` loader ` function. For every source file pointer
@@ -55,15 +65,20 @@ const minifiedTransformedMap = JSON.stringify({
5565
5666const remapped = remapping (
5767 minifiedTransformedMap ,
58- (file ) => {
68+ (file , ctx ) => {
5969
6070 // The "transformed.js" file is an transformed file.
6171 if (file === ' transformed.js' ) {
72+ // The root importer is empty.
73+ console .assert (ctx .importer === ' ' );
74+
6275 return transformedMap ;
6376 }
6477
6578 // Loader will be called to load transformedMap's source file pointers as well.
6679 console .assert (file === ' helloworld.js' );
80+ // `transformed.js`'s sourcemap points into `helloworld.js`.
81+ console .assert (ctx .importer === ' transformed.js' );
6782 return null ;
6883 }
6984);
@@ -110,6 +125,76 @@ console.log(remapped);
110125// };
111126```
112127
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+
113198### Options
114199
115200#### excludeContent
0 commit comments