|
'use strict'; |
|
|
|
var File = require('vinyl'); |
|
var vinylContents = require('vinyl-contents'); |
|
|
|
var helpers = require('./lib/helpers'); |
|
|
|
var PLUGIN_NAME = 'vinyl-sourcemap'; |
|
|
|
function add(file, callback) { |
|
|
|
if (!File.isVinyl(file)) { |
|
return callback(new Error(PLUGIN_NAME + '-add: Not a vinyl file')); |
|
} |
|
|
|
|
|
if (file.isNull() || file.sourceMap) { |
|
return callback(null, file); |
|
} |
|
|
|
vinylContents(file, onContents); |
|
|
|
function onContents(err, contents) { |
|
if (err) { |
|
return callback(err); |
|
} |
|
|
|
var state = { |
|
path: '', |
|
map: null, |
|
content: contents.toString(), |
|
|
|
preExistingComment: null, |
|
}; |
|
|
|
helpers.addSourceMaps(file, state, callback); |
|
} |
|
} |
|
|
|
function write(file, destPath, callback) { |
|
|
|
if (typeof destPath === 'function') { |
|
callback = destPath; |
|
destPath = undefined; |
|
} |
|
|
|
|
|
if (!File.isVinyl(file)) { |
|
return callback(new Error(PLUGIN_NAME + '-write: Not a vinyl file')); |
|
} |
|
|
|
|
|
if (file.isNull() || !file.sourceMap) { |
|
return callback(null, file); |
|
} |
|
|
|
helpers.writeSourceMaps(file, destPath, callback); |
|
} |
|
|
|
module.exports = { |
|
add: add, |
|
write: write, |
|
}; |
|
|