Gathering detailed insights and metrics for gulp-sourcemaps
Gathering detailed insights and metrics for gulp-sourcemaps
Gathering detailed insights and metrics for gulp-sourcemaps
Gathering detailed insights and metrics for gulp-sourcemaps
@gulp-sourcemaps/map-sources
Gulp plugin for mapping sources of a sourcemap.
vinyl-sourcemaps-apply
Apply a source map to a vinyl file, merging it with preexisting source maps
@gulp-sourcemaps/identity-map
Gulp plugin for generating an identity sourcemap for a file.
@types/gulp-sourcemaps
TypeScript definitions for gulp-sourcemaps
npm install gulp-sourcemaps
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (98.82%)
HTML (0.45%)
Shell (0.42%)
Less (0.25%)
CSS (0.06%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
ISC License
1,102 Stars
272 Commits
117 Forks
21 Watchers
7 Branches
29 Contributors
Updated on Jun 25, 2025
Latest Version
3.0.0
Package Id
gulp-sourcemaps@3.0.0
Size
9.11 kB
NPM Version
6.14.8
Node Version
14.1.0
Published on
Nov 11, 2020
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
11
Sourcemap support for gulpjs.
All the examples here works with Gulp 4. To see examples related to Gulp 3, you can read them here.
Inline source maps are embedded in the source file.
Example:
1var gulp = require('gulp'); 2var plugin1 = require('gulp-plugin1'); 3var plugin2 = require('gulp-plugin2'); 4var sourcemaps = require('gulp-sourcemaps'); 5 6function javascript() { 7 gulp.src('src/**/*.js') 8 .pipe(sourcemaps.init()) 9 .pipe(plugin1()) 10 .pipe(plugin2()) 11 .pipe(sourcemaps.write()) 12 .pipe(gulp.dest('dist')); 13}; 14 15 16exports.javascript = javascript;
All plugins between sourcemaps.init()
and sourcemaps.write()
need to have support for gulp-sourcemaps
. You can find a list of such plugins in the wiki.
To write external source map files, pass a path relative to the destination to sourcemaps.write()
.
Example:
1var gulp = require('gulp'); 2var plugin1 = require('gulp-plugin1'); 3var plugin2 = require('gulp-plugin2'); 4var sourcemaps = require('gulp-sourcemaps'); 5 6function javascript() { 7 gulp.src('src/**/*.js') 8 .pipe(sourcemaps.init()) 9 .pipe(plugin1()) 10 .pipe(plugin2()) 11 .pipe(sourcemaps.write('../maps')) 12 .pipe(gulp.dest('dist')); 13}; 14 15exports.javascript = javascript;
To load existing source maps, pass the option loadMaps: true
to sourcemaps.init()
.
Example:
1var gulp = require('gulp'); 2var plugin1 = require('gulp-plugin1'); 3var plugin2 = require('gulp-plugin2'); 4var sourcemaps = require('gulp-sourcemaps'); 5 6function javascript() { 7 gulp.src('src/**/*.js') 8 .pipe(sourcemaps.init({loadMaps: true})) 9 .pipe(plugin1()) 10 .pipe(plugin2()) 11 .pipe(sourcemaps.write()) 12 .pipe(gulp.dest('dist')); 13}; 14 15exports.javascript = javascript;
To handle large files, pass the option largeFile: true
to sourcemaps.init()
.
Example:
1var gulp = require('gulp'); 2var plugin1 = require('gulp-plugin1'); 3var plugin2 = require('gulp-plugin2'); 4var sourcemaps = require('gulp-sourcemaps'); 5 6function javascript() { 7 gulp.src('src/**/*.js') 8 .pipe(sourcemaps.init({largeFile: true})) 9 .pipe(plugin1()) 10 .pipe(plugin2()) 11 .pipe(sourcemaps.write()) 12 .pipe(gulp.dest('dist')); 13}; 14 15exports.javascript = javascript;
Use the base
option on gulp.src
to make sure all files are relative to a common base directory.
Example:
1var gulp = require('gulp'); 2var plugin1 = require('gulp-plugin1'); 3var plugin2 = require('gulp-plugin2'); 4var sourcemaps = require('gulp-sourcemaps'); 5 6function javascript() { 7gulp.src(['src/test.js', 'src/testdir/test2.js'], { base: 'src' }) 8 .pipe(sourcemaps.init()) 9 .pipe(plugin1()) 10 .pipe(plugin2()) 11 .pipe(sourcemaps.write('../maps')) 12 .pipe(gulp.dest('dist')); 13}; 14 15exports.javascript = javascript;
sources
property on sourcemapsThe exported mapSources
method gives full control over the source paths. It takes a function that is called for every source and receives the default source path as a parameter and the original vinyl file.
Example:
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 .pipe(plugin1()) 5 .pipe(plugin2()) 6 // be careful with the sources returned otherwise contents might not be loaded properly 7 .pipe(sourcemaps.mapSources(function(sourcePath, file) { 8 // source paths are prefixed with '../src/' 9 return '../src/' + sourcePath; 10 })) 11 .pipe(sourcemaps.write('../maps') 12 .pipe(gulp.dest('public/scripts')); 13}; 14 15exports.javascript = javascript;
The exported identityMap
method allows you to generate a full valid source map encoding no changes (slower, only for Javascript and CSS) instead of the default empty source map (no mappings, fast). Use this option if you get missing or incorrect mappings, e.g. when debugging.
Example:
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 // An identity sourcemap will be generated at this step 5 .pipe(sourcemaps.identityMap()) 6 .pipe(plugin1()) 7 .pipe(plugin2()) 8 .pipe(sourcemaps.write('../maps') 9 .pipe(gulp.dest('public/scripts')); 10}; 11 12exports.javascript = javascript;
loadMaps
Set to true to load existing maps for source files. Supports the following:
sourceMappingURL=
commentidentityMap
This option is deprecated. Upgrade to use our sourcemap.identityMap
API.
addComment
By default a comment containing / referencing the source map is added. Set this to false
to disable the comment (e.g. if you want to load the source maps by header).
Example:
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 .pipe(plugin1()) 5 .pipe(plugin2()) 6 .pipe(sourcemaps.write('../maps', {addComment: false})) 7 .pipe(gulp.dest('dist')); 8}; 9 10exports.javascript = javascript;
includeContent
By default the source maps include the source code. Pass false
to use the original files.
Including the content is the recommended way, because it "just works". When setting this to false
you have to host the source files and set the correct sourceRoot
.
sourceRoot
Set the location where the source files are hosted (use this when includeContent
is set to false
). This is usually a URL (or an absolute URL path), not a local file system path.
By default the source root is '' or in case destPath
is set, the relative path from the source map to the source base directory (this should work for many dev environments).
If a relative path is used (empty string or one starting with a .
), it is interpreted as a path relative to the destination. The plugin rewrites it to a path relative to each source map.
Example:
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 .pipe(plugin1()) 5 .pipe(plugin2()) 6 .pipe(sourcemaps.write({includeContent: false, sourceRoot: '/src'})) 7 .pipe(gulp.dest('dist')); 8}; 9 10exports.javascript = javascript;
Example (using a function):
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 .pipe(plugin1()) 5 .pipe(plugin2()) 6 .pipe(sourcemaps.write({ 7 includeContent: false, 8 sourceRoot: function(file) { 9 return '/src'; 10 } 11 })) 12 .pipe(gulp.dest('dist')); 13}; 14 15exports.javascript = javascript;
Example (relative path):
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 .pipe(plugin1()) 5 .pipe(plugin2()) 6 .pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: '../src'})) 7 .pipe(gulp.dest('dist')); 8}; 9 10exports.javascript = javascript;
In this case for a file written to dist/subdir/example.js
, the source map is written to dist/subdir/example.js.map
and the sourceRoot will be ../../src
(resulting in the full source path ../../src/subdir/example.js
).
destPath
Set the destination path (the same you pass to gulp.dest()
). If the source map destination path is not a sub path of the destination path, this is needed to get the correct path in the file
property of the source map.
In addition, it allows to automatically set a relative sourceRoot
if none is set explicitly.
sourceMappingURLPrefix
Specify a prefix to be prepended onto the source map URL when writing external source maps. Relative paths will have their leading dots stripped.
Example:
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 .pipe(plugin1()) 5 .pipe(plugin2()) 6 .pipe(sourcemaps.write('../maps', { 7 sourceMappingURLPrefix: 'https://asset-host.example.com/assets' 8 })) 9 .pipe(gulp.dest('public/scripts')); 10}; 11 12exports.javascript = javascript;
This will result in a source mapping URL comment like sourceMappingURL=https://asset-host.example.com/assets/maps/helloworld.js.map
.
sourceMappingURL
If you need full control over the source map URL you can pass a function to this option. The output of the function must be the full URL to the source map (in function of the output file).
Example:
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 .pipe(plugin1()) 5 .pipe(plugin2()) 6 .pipe(sourcemaps.write('../maps', { 7 sourceMappingURL: function(file) { 8 return 'https://asset-host.example.com/' + file.relative + '.map'; 9 } 10 })) 11 .pipe(gulp.dest('public/scripts')); 12}; 13 14exports.javascript = javascript;
This will result in a source mapping URL comment like sourceMappingURL=https://asset-host.example.com/helloworld.js.map
.
mapFile
This option allows to rename the map file. It takes a function that is called for every map and receives the default map path as a parameter.
Example:
1function javascript() { 2 var stream = gulp.src('src/**/*.js') 3 .pipe(sourcemaps.init()) 4 .pipe(plugin1()) 5 .pipe(plugin2()) 6 .pipe(sourcemaps.write('../maps', { 7 mapFile: function(mapFilePath) { 8 // source map files are named *.map instead of *.js.map 9 return mapFilePath.replace('.js.map', '.map'); 10 } 11 })) 12 .pipe(gulp.dest('public/scripts')); 13}; 14 15exports.javascript = javascript;
mapSources
This option is deprecated. Upgrade to use our sourcemap.mapSources
API.
charset
Sets the charset for inline source maps. Default: utf8
clone
Clones the original file for creation of the map file. Could be important if file history is important. See file.clone() for possible options. Default: {deep:false, contents:false}
How to add source map support to plugins
file
and sources
) are relative to file.base
(e.g. use file.relative
).file
. E.g. by using vinyl-sourcemaps-apply.
This combines the source map of this plugin with the source maps coming from plugins further up the chain.1var through = require('through2'); 2var applySourceMap = require('vinyl-sourcemaps-apply'); 3var myTransform = require('myTransform'); 4 5module.exports = function(options) { 6 7 function transform(file, encoding, callback) { 8 // generate source maps if plugin source-map present 9 if (file.sourceMap) { 10 options.makeSourceMaps = true; 11 } 12 13 // do normal plugin logic 14 var result = myTransform(file.contents, options); 15 file.contents = new Buffer(result.code); 16 17 // apply source map to the chain 18 if (file.sourceMap) { 19 applySourceMap(file, result.map); 20 } 21 22 this.push(file); 23 callback(); 24 } 25 26 return through.obj(transform); 27};
Verify sourcemaps are working
See example below or refer to test/write.js
1var stream = plugin(); 2var init = sourcemaps.init(); 3var write = sourcemaps.write(); 4 5init.pipe(stream).pipe(write); 6 7write.on('data', function (file) { 8 assert(...); 9 cb(); 10}); 11 12init.write(new gutil.File(...)); 13init.end();
All debugging output relies on visionmedia/debug. Follow the directions to set the
environment variable $DEBUG
.
For a few examples of debug you could use:
1 DEBUG='gulp-sourcemaps:*' #everything 2 DEBUG='gulp-sourcemaps:init' #init/index.js 3 DEBUG='gulp-sourcemaps:init:*' #init/index.internals.js 4 DEBUG='gulp-sourcemaps:write:' #write/index.js 5 DEBUG='gulp-sourcemaps:write:*' #write/index.internals.js 6 DEBUG='gulp-sourcemaps:write:,gulp-sourcemaps:init:**' #write/index.internals.js and init/index.internals.js
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 8/20 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
project is not fuzzed
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
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