index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var File = require('vinyl');
  3. var helpers = require('./lib/helpers');
  4. var PLUGIN_NAME = 'vinyl-sourcemap';
  5. function add(file, callback) {
  6. // Bail early an error if the file argument is not a Vinyl file
  7. if (!File.isVinyl(file)) {
  8. return callback(new Error(PLUGIN_NAME + '-add: Not a vinyl file'));
  9. }
  10. // Bail early with an error if file has streaming contents
  11. if (file.isStream()) {
  12. return callback(new Error(PLUGIN_NAME + '-add: Streaming not supported'));
  13. }
  14. // Bail early successfully if file is null or already has a sourcemap
  15. if (file.isNull() || file.sourceMap) {
  16. return callback(null, file);
  17. }
  18. var state = {
  19. path: '', // Root path for the sources in the map
  20. map: null,
  21. content: file.contents.toString(),
  22. // TODO: handle this?
  23. preExistingComment: null,
  24. };
  25. helpers.addSourceMaps(file, state, callback);
  26. }
  27. function write(file, destPath, callback) {
  28. // Check if options or a callback are passed as second argument
  29. if (typeof destPath === 'function') {
  30. callback = destPath;
  31. destPath = undefined;
  32. }
  33. // Bail early with an error if the file argument is not a Vinyl file
  34. if (!File.isVinyl(file)) {
  35. return callback(new Error(PLUGIN_NAME + '-write: Not a vinyl file'));
  36. }
  37. // Bail early with an error if file has streaming contents
  38. if (file.isStream()) {
  39. return callback(new Error(PLUGIN_NAME + '-write: Streaming not supported'));
  40. }
  41. // Bail early successfully if file is null or doesn't have sourcemap
  42. if (file.isNull() || !file.sourceMap) {
  43. return callback(null, file);
  44. }
  45. helpers.writeSourceMaps(file, destPath, callback);
  46. }
  47. module.exports = {
  48. add: add,
  49. write: write,
  50. };