forked from mcasimir/gulp-rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (59 loc) · 1.81 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
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
/**
* @usage
*
* gulp.src('app.js', {read: false})
* .pipe(rollup(options))
* .pipe(gulp.dest('dist'));
*/
var through = require('through2'),
gutil = require('gulp-util'),
PluginError = gutil.PluginError,
fs = require('fs'),
path = require('path'),
PLUGIN_NAME = 'gulp-rollup';
module.exports = function(options) {
options = options || {};
return through.obj(function(file, enc, callback) {
if (!file.path) { return callback(); }
if (file.isStream()) {
return callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
try {
var stats = fs.lstatSync(file.path);
if (stats.isFile()) {
options.entry = file.path;
var rollup = options.rollup || require('rollup');
if (options.rollup) {
delete options.rollup;
}
rollup.rollup(options).then(function(bundle) {
var res = bundle.generate(options);
file.contents = new Buffer(res.code);
var map = res.map;
if (map) {
// This makes sure the paths in the generated source map (file and
// sources) are relative to file.base:
map.file = unixStylePath(file.relative);
map.sources = map.sources.map(function(fileName) {
return unixStylePath(path.relative(file.base, fileName));
});
file.sourceMap = map;
}
callback(null, file);
}, function(err) {
setImmediate(function() {
callback(new PluginError(PLUGIN_NAME, err));
});
});
}
} catch (err) {
callback(new PluginError(PLUGIN_NAME, err));
}
}, function() {
this.emit('end');
});
};
function unixStylePath(filePath) {
return filePath.split(path.sep).join('/');
}