UNPKG

gulp-transform

Version:

A Gulp plugin for applying custom transformations to the contents of files

58 lines 2.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var stream_1 = require("stream"); var gulp_util_1 = require("gulp-util"); var FileContentStream_1 = require("./FileContentStream"); var common_1 = require("./common"); /** * A stream of File objects returned by the plugin. * * @internal */ var GulpTransformStream = (function (_super) { tslib_1.__extends(GulpTransformStream, _super); function GulpTransformStream(transform) { var _this = _super.call(this, { objectMode: true }) || this; _this.transform = transform; return _this; } GulpTransformStream.prototype._transform = function (file, _encoding, next) { if (file.isBuffer()) return void this.transformBufferFile(file, next); if (file.isStream()) return void this.transformStreamFile(file, next); next(null, file); }; GulpTransformStream.prototype.transformBufferFile = function (file, next) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, error_1; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); _a = file; return [4 /*yield*/, this.transform(file.contents, file)]; case 1: _a.contents = _b.sent(); next(null, file); return [3 /*break*/, 3]; case 2: error_1 = _b.sent(); next(new gulp_util_1.PluginError(common_1.PLUGIN_NAME, error_1)); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; GulpTransformStream.prototype.transformStreamFile = function (file, next) { var stream = new FileContentStream_1.FileContentStream(this.transform, file); stream.on("error", this.emit.bind(this, "error")); file.contents = file.contents.pipe(stream); next(null, file); }; return GulpTransformStream; }(stream_1.Transform)); exports.GulpTransformStream = GulpTransformStream; //# sourceMappingURL=GulpTransformStream.js.map