forked from sindresorhus/gulp-rework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
55 lines (46 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var _ = require('lodash');
var rework = require('rework');
var applySourceMap = require('vinyl-sourcemaps-apply');
var lastIsObject = _.flowRight(_.isPlainObject, _.last);
module.exports = function () {
var args = [].slice.call(arguments);
var options = lastIsObject(args) ? args.pop() : {};
var plugins = args;
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-rework', 'Streaming not supported'));
return;
}
try {
var ret = rework(file.contents.toString(), {source: file.path});
ret.sourcemap(file.sourceMap);
plugins.forEach(ret.use.bind(ret));
if (file.sourceMap) {
options.sourcemap = true;
options.inputSourcemap = false;
options.sourcemapAsObject = true;
}
var result = ret.toString(options);
if (file.sourceMap) {
file.contents = new Buffer(result.code);
result.map.file = file.relative;
result.map.sources = result.map.sources.map(function (src) {
return file.relative;
});
applySourceMap(file, result.map);
} else {
file.contents = new Buffer(result);
}
cb(null, file);
} catch (err) {
cb(new gutil.PluginError('gulp-rework', err, {fileName: err.filename || file.path}));
}
});
};