1 | # gulp-transform
|
2 |
|
3 | [![version][versionBadge]][npm]
|
4 | [![build][buildBadge]][build]
|
5 | [![coverage][coverageBadge]][coverage]
|
6 | [![dependencies][dependenciesBadge]][dependencies]
|
7 |
|
8 | A [Gulp][gulp] plugin for applying custom transformations to the contents of
|
9 | files.
|
10 |
|
11 | ## Install
|
12 |
|
13 | Install via [npm][npm]:
|
14 |
|
15 | ```sh
|
16 | npm install --save-dev gulp gulp-transform
|
17 | ```
|
18 |
|
19 | ## Usage
|
20 |
|
21 | ### Synchronous usage
|
22 |
|
23 | This example adds a timestamp to the beginning of each source file. The comment
|
24 | format is inferred from the file extension. Files with unrecognized extensions
|
25 | are not modified.
|
26 |
|
27 | #### gulpfile.js
|
28 |
|
29 | ```js
|
30 | const gulp = require('gulp');
|
31 | const transform = require('gulp-transform');
|
32 | const path = require('path');
|
33 |
|
34 | const TIMESTAMP = Date();
|
35 |
|
36 | gulp.task('timestamp', () => {
|
37 | return gulp.src('src/**/*')
|
38 | .pipe(transform('utf8', timestamp))
|
39 | .pipe(gulp.dest('out'));
|
40 | });
|
41 |
|
42 | function timestamp(content, file) {
|
43 | switch (path.extname(file.path)) {
|
44 | case '.js':
|
45 | case '.ts':
|
46 | return `// ${TIMESTAMP}\n\n${content}`;
|
47 | case '.coffee':
|
48 | return `# ${TIMESTAMP}\n\n${content}`;
|
49 | default:
|
50 | return content;
|
51 | }
|
52 | }
|
53 | ```
|
54 |
|
55 | #### src/hello.js
|
56 |
|
57 | ```js
|
58 | console.log('Hello, world.');
|
59 | ```
|
60 |
|
61 | #### out/hello.js
|
62 |
|
63 | ```js
|
64 | // Thu Jul 27 2017 15:56:14 GMT-0700 (PDT)
|
65 |
|
66 | console.log('Hello, world.');
|
67 | ```
|
68 |
|
69 | ### Asynchronous usage
|
70 |
|
71 | This example uses [xml2js][xml2js] to convert XML to JSON. The callback
|
72 | returns a [Promise][promise] since the operation is asynchronous.
|
73 |
|
74 | #### gulpfile.js
|
75 |
|
76 | ```js
|
77 | const gulp = require('gulp');
|
78 | const transform = require('gulp-transform');
|
79 | const rename = require('gulp-rename');
|
80 | const xml2js = require('xml2js');
|
81 |
|
82 | gulp.task('xml-to-json', () => {
|
83 | return gulp.src('src/**/*.xml')
|
84 | .pipe(transform('utf8', xmlToJson))
|
85 | .pipe(rename({ extname: '.json' }))
|
86 | .pipe(gulp.dest('out'));
|
87 | });
|
88 |
|
89 | function xmlToJson(content) {
|
90 | return new Promise((resolve, reject) => {
|
91 | xml2js.parseString(content, (error, data) => {
|
92 | if (error) {
|
93 | reject(error);
|
94 | } else {
|
95 | resolve(JSON.stringify(data, null, 2));
|
96 | }
|
97 | });
|
98 | });
|
99 | }
|
100 | ```
|
101 |
|
102 | #### src/cities.xml
|
103 |
|
104 | ```xml
|
105 | <cities>
|
106 | <city>Amsterdam</city>
|
107 | <city>Rotterdam</city>
|
108 | <city>The Hague</city>
|
109 | </cities>
|
110 | ```
|
111 |
|
112 | #### out/cities.json
|
113 |
|
114 | ```json
|
115 | {
|
116 | "cities": {
|
117 | "city": [
|
118 | "Amsterdam",
|
119 | "Rotterdam",
|
120 | "The Hague"
|
121 | ]
|
122 | }
|
123 | }
|
124 | ```
|
125 |
|
126 | ## API
|
127 |
|
128 | ### transform([options], callback)
|
129 |
|
130 | Creates a stream that transforms the contents of [File][vinylFile] objects.
|
131 | Files in both streaming and buffer mode are accepted.
|
132 |
|
133 | To transform contents as a string, a [character encoding][encoding] must be
|
134 | specified; otherwise, contents will be passed to the callback as a
|
135 | [Buffer][nodeBuffer].
|
136 |
|
137 | The contents of each File are replaced with the return value of the callback.
|
138 | Or, to perform an asynchronous transformation, a [Promise][promise] may be
|
139 | returned.
|
140 |
|
141 | <table>
|
142 | <thead>
|
143 | <tr>
|
144 | <th>Parameter</th>
|
145 | <th>Type</th>
|
146 | <th>Description</th>
|
147 | </tr>
|
148 | </thead>
|
149 | <tbody>
|
150 | <tr>
|
151 | <td align="left">
|
152 | <strong>[options]</strong>
|
153 | </td>
|
154 | <td align="center">
|
155 | <p><code>object</code></p>
|
156 | <p><code>string</code></p>
|
157 | <p><code>null</code></p>
|
158 | </td>
|
159 | <td align="left">
|
160 | <p>
|
161 | An optional options object or a value indicating an encoding. If
|
162 | passed as an object, the following properties are are accepted as
|
163 | options:
|
164 | </p>
|
165 | <ul>
|
166 | <li>
|
167 | <strong>encoding</strong> - <code>string</code> | <code>null</code> - An
|
168 | <a href="https://nodejs.org/dist/latest/docs/api/buffer.html#buffer_buffers_and_character_encodings">
|
169 | encoding</a> supported by Node.js or <code>null</code> to indicate
|
170 | no encoding. Defaults to <code>null</code>.
|
171 | </li>
|
172 | <li>
|
173 | <strong>thisArg</strong> - <code>any</code> - The value of
|
174 | <code>this</code> within <em>callback</em>. Defaults to
|
175 | <code>undefined</code>.
|
176 | </li>
|
177 | </ul>
|
178 | <p>
|
179 | If passed as a string or <code>null</code>, it is interpreted as the
|
180 | <em>encoding</em> option.
|
181 | </p>
|
182 | <p>
|
183 | If no encoding is given, <em>callback</em> is called with a
|
184 | <code>Buffer</code> object containing the contents of the file.
|
185 | Otherwise, it is called with a string created with
|
186 | <a href="https://nodejs.org/dist/latest/docs/api/buffer.html#buffer_buf_tostring_encoding_start_end">
|
187 | <code><em>buffer</em>.toString(<em>encoding</em>)</code></a>.
|
188 | </p>
|
189 | </td>
|
190 | </tr>
|
191 | <tr>
|
192 | <td align="left">
|
193 | <strong>callback</strong>
|
194 | </td>
|
195 | <td align="center">
|
196 | <code>function</code>
|
197 | </td>
|
198 | <td align="left">
|
199 | <p>
|
200 | A function that transforms the contents of each file. It is invoked
|
201 | with two arguments:
|
202 | </p>
|
203 | <ul>
|
204 | <li>
|
205 | <strong>contents</strong> - <code>Buffer</code> | <code>string</code> - The
|
206 | contents of the file. If no encoding is given, <em>contents</em>
|
207 | will be a <code>Buffer</code>; otherwise, it will be a string.
|
208 | </li>
|
209 | <li>
|
210 | <strong>file</strong> - <code>File</code> - The
|
211 | <a href="https://github.com/gulpjs/vinyl#instance-methods">
|
212 | <code>File</code></a> object whose contents are being transformed.
|
213 | Use this to access metadata about the file, e.g., its filename.
|
214 | </li>
|
215 | </ul>
|
216 | <p>
|
217 | The contents of the file are replaced with the return value of the
|
218 | callback. For asynchronous transformations, a
|
219 | <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise">
|
220 | <code>Promise</code></a> may be returned. The return or completion
|
221 | value must have the same type as <em>contents</em>.
|
222 | </p>
|
223 | <p>
|
224 | The value of <code>this</code> within the callback may be set with the
|
225 | <em>thisArg</em> option; otherwise, <code>this</code> will be
|
226 | <code>undefined</code>.
|
227 | </p>
|
228 | </td>
|
229 | </tr>
|
230 | </tbody>
|
231 | </table>
|
232 |
|
233 | ## TypeScript
|
234 |
|
235 | [TypeScript][typescript] declarations are included in the package.
|
236 |
|
237 | ```sh
|
238 | npm i -D typescript ts-node gulp @types/gulp gulp-transform
|
239 | ```
|
240 |
|
241 | #### gulpfile.ts
|
242 |
|
243 | ```ts
|
244 | import gulp = require("gulp");
|
245 | import transform = require("gulp-transform");
|
246 |
|
247 | gulp.task("build", () => {
|
248 | gulp.src("src/**/*")
|
249 | .pipe(transform("utf8", () => { /* transform contents */ }))
|
250 | .pipe(gulp.dest("out"));
|
251 | });
|
252 | ```
|
253 |
|
254 | ## License
|
255 |
|
256 | Copyright © 2016–2017 Akim McMath. Licensed under the [MIT License][license].
|
257 |
|
258 | [gulp]: http://gulpjs.com/
|
259 | [npm]: https://npmjs.org/package/gulp-transform
|
260 | [versionBadge]: https://img.shields.io/npm/v/gulp-transform.svg?style=flat-square
|
261 | [license]: LICENSE
|
262 | [buildBadge]: https://img.shields.io/travis/mcmath/gulp-transform/master.svg?style=flat-square
|
263 | [build]: https://travis-ci.org/mcmath/gulp-transform
|
264 | [coverageBadge]: https://img.shields.io/coveralls/mcmath/gulp-transform/master.svg?style=flat-square&service=github
|
265 | [coverage]: https://coveralls.io/github/mcmath/gulp-transform?branch=master
|
266 | [dependenciesBadge]: https://img.shields.io/gemnasium/mcmath/gulp-transform.svg?style=flat-square
|
267 | [dependencies]: https://gemnasium.com/mcmath/gulp-transform
|
268 | [xml2js]: https://github.com/Leonidas-from-XIV/node-xml2js
|
269 | [vinylFile]: https://github.com/gulpjs/vinyl#instance-methods
|
270 | [encoding]: https://nodejs.org/dist/latest/docs/api/buffer.html#buffer_buffers_and_character_encodings
|
271 | [nodeBuffer]: https://nodejs.org/dist/latest-v8.x/docs/api/buffer.html
|
272 | [promise]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
|
273 | [typescript]: https://www.typescriptlang.org/
|