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