* transformation * @param {import("vinyl")} file * @param {BufferEncoding} enc * @param {(error?: Error | null, data?: any) => void} callback
(file, enc, callback)
| 24 | * @param {(error?: Error | null, data?: any) => void} callback |
| 25 | */ |
| 26 | transform(file, enc, callback) { |
| 27 | if (file.isNull()) { |
| 28 | return callback(null, file); |
| 29 | } |
| 30 | |
| 31 | let replacement = _replacement; |
| 32 | if (typeof _replacement === 'function') { |
| 33 | // Pass the vinyl file object as this.file |
| 34 | replacement = _replacement.bind({ file: file }); |
| 35 | } |
| 36 | |
| 37 | function doReplace() { |
| 38 | if (file.isStream()) { |
| 39 | file.contents = file.contents.pipe(rs(search, replacement)); |
| 40 | return callback(null, file); |
| 41 | } |
| 42 | |
| 43 | if (file.isBuffer()) { |
| 44 | if (search instanceof RegExp) { |
| 45 | file.contents = Buffer.from(String(file.contents).replace(search, replacement)); |
| 46 | } else { |
| 47 | const chunks = String(file.contents).split(search); |
| 48 | |
| 49 | let result; |
| 50 | if (typeof replacement === 'function') { |
| 51 | // Start with the first chunk already in the result |
| 52 | // Replacements will be added thereafter |
| 53 | // This is done to avoid checking the value of i in the loop |
| 54 | result = [ chunks[0] ]; |
| 55 | |
| 56 | // The replacement function should be called once for each match |
| 57 | for (let i = 1; i < chunks.length; i++) { |
| 58 | // Add the replacement value |
| 59 | result.push(replacement(search)); |
| 60 | |
| 61 | // Add the next chunk |
| 62 | result.push(chunks[i]); |
| 63 | } |
| 64 | |
| 65 | result = result.join(''); |
| 66 | } |
| 67 | else { |
| 68 | result = chunks.join(replacement); |
| 69 | } |
| 70 | |
| 71 | file.contents = Buffer.from(result); |
| 72 | } |
| 73 | return callback(null, file); |
| 74 | } |
| 75 | |
| 76 | callback(null, file); |
| 77 | } |
| 78 | |
| 79 | if (options.skipBinary) { |
| 80 | if (!istextorbinary.isText(file.path, file.contents)) { |
| 81 | callback(null, file); |
| 82 | } else { |
| 83 | doReplace(); |
nothing calls this directly
no test coverage detected
searching dependent graphs…