Skip to content

Commit 90f2128

Browse files
committed
doc(issue 25): readme
1 parent 657f09b commit 90f2128

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed

README.md

+52-29
Original file line numberDiff line numberDiff line change
@@ -300,45 +300,68 @@ gulp.src(['src/test.js', 'src/testdir/test2.js'], { base: 'src' })
300300

301301
Sets the charset for inline source maps. Default: `utf8`
302302

303-
### Plugin developers only: How to add source map support to plugins
303+
### Plugin developers only:
304304

305-
- Generate a source map for the transformation the plugin is applying
306-
- **Important**: Make sure the paths in the generated source map (`file` and `sources`) are relative to `file.base` (e.g. use `file.relative`).
307-
- Apply this source map to the vinyl `file`. E.g. by using [vinyl-sourcemaps-apply](https://github.com/floridoo/vinyl-sourcemaps-apply).
308-
This combines the source map of this plugin with the source maps coming from plugins further up the chain.
309-
- Add your plugin to the [wiki page](https://github.com/floridoo/gulp-sourcemaps/wiki/Plugins-with-gulp-sourcemaps-support)
305+
- **How to add source map support to plugins**
310306

311-
#### Example:
307+
- Generate a source map for the transformation the plugin is applying
308+
- **Important**: Make sure the paths in the generated source map (`file` and `sources`) are relative to `file.base` (e.g. use `file.relative`).
309+
- Apply this source map to the vinyl `file`. E.g. by using [vinyl-sourcemaps-apply](https://github.com/floridoo/vinyl-sourcemaps-apply).
310+
This combines the source map of this plugin with the source maps coming from plugins further up the chain.
311+
- Add your plugin to the [wiki page](https://github.com/floridoo/gulp-sourcemaps/wiki/Plugins-with-gulp-sourcemaps-support)
312312

313-
```javascript
314-
var through = require('through2');
315-
var applySourceMap = require('vinyl-sourcemaps-apply');
316-
var myTransform = require('myTransform');
313+
#### Example:
317314

318-
module.exports = function(options) {
315+
```js
316+
var through = require('through2');
317+
var applySourceMap = require('vinyl-sourcemaps-apply');
318+
var myTransform = require('myTransform');
319319

320-
function transform(file, encoding, callback) {
321-
// generate source maps if plugin source-map present
322-
if (file.sourceMap) {
323-
options.makeSourceMaps = true;
324-
}
320+
module.exports = function(options) {
321+
322+
function transform(file, encoding, callback) {
323+
// generate source maps if plugin source-map present
324+
if (file.sourceMap) {
325+
options.makeSourceMaps = true;
326+
}
327+
328+
// do normal plugin logic
329+
var result = myTransform(file.contents, options);
330+
file.contents = new Buffer(result.code);
325331

326-
// do normal plugin logic
327-
var result = myTransform(file.contents, options);
328-
file.contents = new Buffer(result.code);
332+
// apply source map to the chain
333+
if (file.sourceMap) {
334+
applySourceMap(file, result.map);
335+
}
329336

330-
// apply source map to the chain
331-
if (file.sourceMap) {
332-
applySourceMap(file, result.map);
337+
this.push(file);
338+
callback();
333339
}
334340

335-
this.push(file);
336-
callback();
337-
}
341+
return through.obj(transform);
342+
};
343+
```
338344

339-
return through.obj(transform);
340-
};
341-
```
345+
- **Very sourcemaps is working**
346+
347+
See example below or refer to [test/write.js](./test/write.js)
348+
349+
#### Example:
350+
```js
351+
var stream = plugin();
352+
var init = sourcemaps.init();
353+
var write = sourcemaps.write();
354+
355+
init.pipe(stream).pipe(write);
356+
357+
write.on('data', function (file) {
358+
assert(...);
359+
cb();
360+
});
361+
362+
init.write(new gutil.File(...));
363+
init.end();
364+
```
342365

343366
[npm-image]: https://img.shields.io/npm/v/gulp-sourcemaps.svg
344367
[npm-url]: https://www.npmjs.com/package/gulp-sourcemaps

0 commit comments

Comments
 (0)