gulp-transform
A Gulp plugin for applying custom transformations to the contents of files.
Install
Install via npm:
npm install --save-dev gulp gulp-transform
Usage
Synchronous usage
This example adds a timestamp to the beginning of each source file. The comment format is inferred from the file extension. Files with unrecognized extensions are not modified.
gulpfile.js
const gulp = ;const transform = ;const path = ; const TIMESTAMP = Date; gulp; { }
src/hello.js
console;
out/hello.js
// Thu Jul 27 2017 15:56:14 GMT-0700 (PDT) console;
Asynchronous usage
This example uses xml2js to convert XML to JSON. The callback returns a Promise since the operation is asynchronous.
gulpfile.js
const gulp = ;const transform = ;const rename = ;const xml2js = ; gulp; { return { xml2js; };}
src/cities.xml
Amsterdam Rotterdam The Hague
out/cities.json
API
transform([options], callback)
Creates a stream that transforms the contents of File objects. Files in both streaming and buffer mode are accepted.
To transform contents as a string, a character encoding must be specified; otherwise, contents will be passed to the callback as a Buffer.
The contents of each File are replaced with the return value of the callback. Or, to perform an asynchronous transformation, a Promise may be returned.
Parameter | Type | Description |
---|---|---|
[options] |
|
An optional options object or a value indicating an encoding. If passed as an object, the following properties are are accepted as options:
If passed as a string or
If no encoding is given, callback is called with a
|
callback |
function
|
A function that transforms the contents of each file. It is invoked with two arguments:
The contents of the file are replaced with the return value of the
callback. For asynchronous transformations, a
The value of |
TypeScript
TypeScript declarations are included in the package.
npm i -D typescript ts-node gulp @types/gulp gulp-transform
gulpfile.ts
;; gulp.task"build",;
License
Copyright © 2016–2017 Akim McMath. Licensed under the MIT License.