Gathering detailed insights and metrics for @ampproject/remapping
Gathering detailed insights and metrics for @ampproject/remapping
Gathering detailed insights and metrics for @ampproject/remapping
Gathering detailed insights and metrics for @ampproject/remapping
Remap sequential sourcemaps through transformations to point at the original source code
npm install @ampproject/remapping
99.4
Supply Chain
98.6
Quality
79
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
110 Stars
351 Commits
25 Forks
12 Watching
20 Branches
64 Contributors
Updated on 25 Nov 2024
Minified
Minified + Gzipped
TypeScript (80.14%)
JavaScript (17.08%)
Shell (2.78%)
Cumulative downloads
Total Downloads
Last day
-1.8%
7,394,961
Compared to previous day
Last week
3.7%
39,503,339
Compared to previous week
Last month
14%
161,156,273
Compared to previous month
Last year
32.5%
1,619,638,557
Compared to previous year
Remap sequential sourcemaps through transformations to point at the original source code
Remapping allows you to take the sourcemaps generated through transforming your code and "remap" them to the original source locations. Think "my minified code, transformed with babel and bundled with webpack", all pointing to the correct location in your original source code.
With remapping, none of your source code transformations need to be aware of the input's sourcemap, they only need to generate an output sourcemap. This greatly simplifies building custom transformations (think a find-and-replace).
1npm install @ampproject/remapping
1function remapping( 2 map: SourceMap | SourceMap[], 3 loader: (file: string, ctx: LoaderContext) => (SourceMap | null | undefined), 4 options?: { excludeContent: boolean, decodedMappings: boolean } 5): SourceMap; 6 7// LoaderContext gives the loader the importing sourcemap, tree depth, the ability to override the 8// "source" location (where child sources are resolved relative to, or the location of original 9// source), and the ability to override the "content" of an original source for inclusion in the 10// output sourcemap. 11type LoaderContext = { 12 readonly importer: string; 13 readonly depth: number; 14 source: string; 15 content: string | null | undefined; 16}
remapping
takes the final output sourcemap, and a loader
function. For every source file pointer
in the sourcemap, the loader
will be called with the resolved path. If the path itself represents
a transformed file (it has a sourcmap associated with it), then the loader
should return that
sourcemap. If not, the path will be treated as an original, untransformed source code.
1// Babel transformed "helloworld.js" into "transformed.js" 2const transformedMap = JSON.stringify({ 3 file: 'transformed.js', 4 // 1st column of 2nd line of output file translates into the 1st source 5 // file, line 3, column 2 6 mappings: ';CAEE', 7 sources: ['helloworld.js'], 8 version: 3, 9}); 10 11// Uglify minified "transformed.js" into "transformed.min.js" 12const minifiedTransformedMap = JSON.stringify({ 13 file: 'transformed.min.js', 14 // 0th column of 1st line of output file translates into the 1st source 15 // file, line 2, column 1. 16 mappings: 'AACC', 17 names: [], 18 sources: ['transformed.js'], 19 version: 3, 20}); 21 22const remapped = remapping( 23 minifiedTransformedMap, 24 (file, ctx) => { 25 26 // The "transformed.js" file is an transformed file. 27 if (file === 'transformed.js') { 28 // The root importer is empty. 29 console.assert(ctx.importer === ''); 30 // The depth in the sourcemap tree we're currently loading. 31 // The root `minifiedTransformedMap` is depth 0, and its source children are depth 1, etc. 32 console.assert(ctx.depth === 1); 33 34 return transformedMap; 35 } 36 37 // Loader will be called to load transformedMap's source file pointers as well. 38 console.assert(file === 'helloworld.js'); 39 // `transformed.js`'s sourcemap points into `helloworld.js`. 40 console.assert(ctx.importer === 'transformed.js'); 41 // This is a source child of `transformed`, which is a source child of `minifiedTransformedMap`. 42 console.assert(ctx.depth === 2); 43 return null; 44 } 45); 46 47console.log(remapped); 48// { 49// file: 'transpiled.min.js', 50// mappings: 'AAEE', 51// sources: ['helloworld.js'], 52// version: 3, 53// };
In this example, loader
will be called twice:
"transformed.js"
, the first source file pointer in the minifiedTransformedMap
. We return the
associated sourcemap for it (its a transformed file, after all) so that sourcemap locations can
be traced through it into the source files it represents."helloworld.js"
, our original, unmodified source code. This file does not have a sourcemap, so
we return null
.The remapped
sourcemap now points from transformed.min.js
into locations in helloworld.js
. If
you were to read the mappings
, it says "0th column of the first line output line points to the 1st
column of the 2nd line of the file helloworld.js
".
As a convenience, if you have multiple single-source transformations of a file, you may pass an
array of sourcemap files in the order of most-recent transformation sourcemap first. Note that this
changes the importer
and depth
of each call to our loader. So our above example could have been
written as:
1const remapped = remapping( 2 [minifiedTransformedMap, transformedMap], 3 () => null 4); 5 6console.log(remapped); 7// { 8// file: 'transpiled.min.js', 9// mappings: 'AAEE', 10// sources: ['helloworld.js'], 11// version: 3, 12// };
source
The source
property can overridden to any value to change the location of the current load. Eg,
for an original source file, it allows us to change the location to the original source regardless
of what the sourcemap source entry says. And for transformed files, it allows us to change the
relative resolving location for child sources of the loaded sourcemap.
1const remapped = remapping( 2 minifiedTransformedMap, 3 (file, ctx) => { 4 5 if (file === 'transformed.js') { 6 // We pretend the transformed.js file actually exists in the 'src/' directory. When the nested 7 // source files are loaded, they will now be relative to `src/`. 8 ctx.source = 'src/transformed.js'; 9 return transformedMap; 10 } 11 12 console.assert(file === 'src/helloworld.js'); 13 // We could futher change the source of this original file, eg, to be inside a nested directory 14 // itself. This will be reflected in the remapped sourcemap. 15 ctx.source = 'src/nested/transformed.js'; 16 return null; 17 } 18); 19 20console.log(remapped); 21// { 22// …, 23// sources: ['src/nested/helloworld.js'], 24// };
content
The content
property can be overridden when we encounter an original source file. Eg, this allows
you to manually provide the source content of the original file regardless of whether the
sourcesContent
field is present in the parent sourcemap. It can also be set to null
to remove
the source content.
1const remapped = remapping( 2 minifiedTransformedMap, 3 (file, ctx) => { 4 5 if (file === 'transformed.js') { 6 // transformedMap does not include a `sourcesContent` field, so usually the remapped sourcemap 7 // would not include any `sourcesContent` values. 8 return transformedMap; 9 } 10 11 console.assert(file === 'helloworld.js'); 12 // We can read the file to provide the source content. 13 ctx.content = fs.readFileSync(file, 'utf8'); 14 return null; 15 } 16); 17 18console.log(remapped); 19// { 20// …, 21// sourcesContent: [ 22// 'console.log("Hello world!")', 23// ], 24// };
By default, excludeContent
is false
. Passing { excludeContent: true }
will exclude the
sourcesContent
field from the returned sourcemap. This is mainly useful when you want to reduce
the size out the sourcemap.
By default, decodedMappings
is false
. Passing { decodedMappings: true }
will leave the
mappings
field in a decoded state instead of
encoding into a VLQ string.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More