debug = function (){}
n/a
function identityMap() { function transform(file, _, cb) { if (!file.sourceMap || !file.isBuffer()) { return cb(null, file); } var sourcePath = normalizePath(file.relative); var contents = file.contents.toString(); switch (file.extname) { case '.js': { file.sourceMap = generate.js(sourcePath, contents); break; } case '.css': { file.sourceMap = generate.css(sourcePath, contents); break; } } cb(null, file); } return through.obj(transform); }
...
Example:
```javascript
gulp.task('javascript', function() {
var stream = 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'));
});
```
...
function init(options) {
var debug = require('../debug').spawn('init');
function sourceMapInit(file, encoding, callback) {
/*jshint validthis:true */
// pass through if file is null or already has a source map
if (file.isNull() || file.sourceMap) {
this.push(file);
return callback();
}
if (file.isStream()) {
return callback(new Error(utils.PLUGIN_NAME + '-init: Streaming not supported'));
}
if (options === undefined) {
options = {};
}
debug(function() {
return options;
});
var fileContent = file.contents.toString();
var sourceMap, preExistingComment;
var internals = initInternals(options, file, fileContent);
if (options.loadMaps) {
var result = internals.loadMaps();
sourceMap = result.map;
fileContent = result.content;
preExistingComment = result.preExistingComment;
}
if (!sourceMap && options.identityMap) {
debug(function() { return '**identityMap option is deprecated, update to use sourcemap.identityMap stream**'; });
debug(function() {
return 'identityMap';
});
var fileType = path.extname(file.path);
var source = unixStylePath(file.relative);
var generator = new SourceMapGenerator({file: source});
if (fileType === '.js') {
var tokenizer = acorn.tokenizer(fileContent, {locations: true});
while (true) {
var token = tokenizer.getToken();
if (token.type.label === "eof")
break;
var mapping = {
original: token.loc.start,
generated: token.loc.start,
source: source
};
if (token.type.label === 'name') {
mapping.name = token.value;
}
generator.addMapping(mapping);
}
generator.setSourceContent(source, fileContent);
sourceMap = generator.toJSON();
} else if (fileType === '.css') {
debug('css');
var ast = css.parse(fileContent, {silent: true});
debug(function() {
return ast;
});
var registerTokens = function(ast) {
if (ast.position) {
generator.addMapping({original: ast.position.start, generated: ast.position.start, source: source});
}
function logAst(key, ast) {
debug(function() {
return 'key: ' + key;
});
debug(function() {
return ast[key];
});
}
for (var key in ast) {
logAst(key, ast);
if (key !== "position") {
if (Object.prototype.toString.call(ast[key]) === '[object Object]') {
registerTokens(ast[key]);
} else if (Array.isArray(ast[key])) {
debug(function() {
return "@@@@ ast[key] isArray @@@@";
});
for (var i = 0; i < ast[key].length; i++) {
registerTokens(ast[key][i]);
}
}
}
}
};
registerTokens(ast);
generator.setSourceContent(source, fileContent);
sourceMap = generator.toJSON();
}
}
if (!sourceMap) {
// Make an empty source map
sourceMap = {
version: 3,
names: [],
mappings: '',
sources: [unixStylePath(file.relative)],
sourcesContent: [fileContent]
};
}
else if(preExistingComment !== null && typeof preExistingComment !== 'undefined')
sourceMap.preExistingComment = preExistingComment;
sourceMap.file = unixStylePath(file.relative);
file.sourceMap = sourceMap;
this.push(file);
callback();
}
return through.obj(sourceMapInit);
}
...
var gulp = require('gulp');
var plugin1 = require('gulp-plugin1');
var plugin2 = require('gulp-plugin2');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('javascript', function() {
gulp.src('src/**/*.js')
.pipe(sourcemaps.init())
.pipe(plugin1())
.pipe(plugin2())
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist'));
});
```
...
function mapSources(mapFn) { function transform(file, _, cb) { if (!file.sourceMap || !file.sourceMap.sources) { return cb(null, file); } function mapper(sourcePath) { var result = sourcePath; if (typeof mapFn === 'function') { result = mapFn(sourcePath, file); } return normalize(result); } file.sourceMap.sources = file.sourceMap.sources.map(mapper); cb(null, file); } return through.obj(transform); }
...
```javascript
gulp.task('javascript', function() {
var stream = 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'));
});
```
...
function write(destPath, options) {
var debug = require('../debug').spawn('write');
debug(function() { return "destPath"; });
debug(function() { return destPath; });
debug(function() { return "original options";});
debug(function() { return options; });
if (options === undefined && typeof destPath !== 'string') {
options = destPath;
destPath = undefined;
}
options = options || {};
// set defaults for options if unset
if (options.includeContent === undefined)
options.includeContent = true;
if (options.addComment === undefined)
options.addComment = true;
if (options.charset === undefined)
options.charset = "utf8";
debug(function() { return "derrived options"; });
debug(function() { return options; });
var internals = internalsInit(destPath, options);
function sourceMapWrite(file, encoding, callback) {
/*jshint validthis:true */
if (file.isNull() || !file.sourceMap) {
this.push(file);
return callback();
}
if (file.isStream()) {
return callback(new Error(utils.PLUGIN_NAME + '-write: Streaming not supported'));
}
// fix paths if Windows style paths
file.sourceMap.file = unixStylePath(file.relative);
internals.setSourceRoot(file);
internals.loadContent(file);
internals.mapSources(file);
internals.mapDestPath(file, this);
this.push(file);
callback();
}
return through.obj(sourceMapWrite);
}
...
var sourcemaps = require('gulp-sourcemaps');
gulp.task('javascript', function() {
gulp.src('src/**/*.js')
.pipe(sourcemaps.init())
.pipe(plugin1())
.pipe(plugin2())
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist'));
});
```
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](https://github.com/gulp-sourcemaps/gulp-sourcemaps/wiki/Plugins-with-gulp-sourcemaps-support).
...
debug = function (){}
n/a
spawn = function (ns) { var dbg = new Debugger(this.namespace, ns); return dbg.debug; }
...
return filePath.split(path.sep).join('/');
}
var PLUGIN_NAME = require('../package.json').name;
var urlRegex = /^(https?|webpack(-[^:]+)?):\/\//;
var debug = require('./debug').spawn('utils');
/*
So reusing the same ref for a regex (with global (g)) is from a poor decision in js.
See http://stackoverflow.com/questions/10229144/bug-with-regexp-in-javascript-when-do-global-search
So we either need to use a new instance of a regex everywhere.
*/
var sourceMapUrlRegEx = function(){ return /\/\/\# sourceMappingURL\=.*/g;};
...
getCommentFormatter = function (file) { var extension = file.relative.split('.').pop(), fileContents = file.contents.toString(), newline = detectNewline.graceful(fileContents || ''), commentFormatter = function(url) { return ''; }; if (file.sourceMap.preExistingComment){ debug(function() { return 'preExistingComment commentFormatter'; }); commentFormatter = function(url) { return "//# sourceMappingURL=" + url + newline; }; return commentFormatter; } switch (extension) { case 'css': debug(function() { return 'css commentFormatter';}); commentFormatter = function(url) { return newline + "/*# sourceMappingURL=" + url + " */" + newline; }; break; case 'js': debug(function() { return 'js commentFormatter'; }); commentFormatter = function(url) { return newline + "//# sourceMappingURL=" + url + newline; }; break; default: debug(function() { return 'unknown commentFormatter'; }); } return commentFormatter; }
n/a
getInlinePreExisting = function (fileContent){ if(sourceMapUrlRegEx().test(fileContent)){ debug(function() { return 'has preExisting'; }); return fileContent.match(sourceMapUrlRegEx())[0]; } }
n/a
sourceMapUrlRegEx = function (){ return /\/\/\# sourceMappingURL\=.*/g;}
n/a
function unixStylePath(filePath) { return filePath.split(path.sep).join('/'); }
n/a