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.
Write external source map files
To write external source map files, pass a path relative to the destination to sourcemaps.write().
The 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:
functionjavascript(){varstream=gulp.src('src/**/*.js').pipe(sourcemaps.init()).pipe(plugin1()).pipe(plugin2())// be careful with the sources returned otherwise contents might not be loaded properly.pipe(sourcemaps.mapSources(function(sourcePath,file){// source paths are prefixed with '../src/'return'../src/'+sourcePath;})).pipe(sourcemaps.write('../maps').pipe(gulp.dest('public/scripts'));};exports.javascript=javascript;
Generate Identity Sourcemap
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:
functionjavascript(){varstream=gulp.src('src/**/*.js').pipe(sourcemaps.init())// An identity sourcemap will be generated at this step.pipe(sourcemaps.identityMap()).pipe(plugin1()).pipe(plugin2()).pipe(sourcemaps.write('../maps').pipe(gulp.dest('public/scripts'));};exports.javascript=javascript;
Init Options
loadMaps
Set to true to load existing maps for source files. Supports the following:
inline source maps
source map files referenced by a sourceMappingURL= comment
source map files with the same name (plus .map) in the same directory
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).
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.
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.
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).
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:
functionjavascript(){varstream=gulp.src('src/**/*.js').pipe(sourcemaps.init()).pipe(plugin1()).pipe(plugin2()).pipe(sourcemaps.write('../maps',{mapFile: function(mapFilePath){// source map files are named *.map instead of *.js.mapreturnmapFilePath.replace('.js.map','.map');}})).pipe(gulp.dest('public/scripts'));};exports.javascript=javascript;
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}
Plugin developers only:
How to add source map support to plugins
Generate a source map for the transformation the plugin is applying
Important: Make sure the paths in the generated source map (file and sources) are relative to file.base (e.g. use file.relative).
Apply this source map to the vinyl 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.
请发表评论