-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
79 lines (69 loc) · 1.93 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
69
70
71
72
73
74
75
76
77
78
79
"use strict";
var through = require("through2");
var ngAnnotate = require("ng-annotate");
var applySourceMap = require("vinyl-sourcemaps-apply");
var merge = require("merge");
var BufferStreams = require("bufferstreams");
var PluginError = require("plugin-error");
var PLUGIN_NAME = "gulp-ng-annotate";
// Function which handle logic for both stream and buffer modes.
function transform(file, input, opts) {
var res = ngAnnotate(input.toString(), opts);
if (res.errors) {
var filename = "";
if (file.path) {
filename = file.relative + ": ";
}
throw new PluginError(PLUGIN_NAME, filename + res.errors.join("\n"));
}
if (opts.map && file.sourceMap) {
var sourceMap = JSON.parse(res.map);
sourceMap.file = file.relative;
applySourceMap(file, sourceMap);
}
return new Buffer(res.src);
}
module.exports = function (options) {
options = options || {};
if (!options.remove) {
options = merge({add: true}, options);
};
return through.obj(function (file, enc, done) {
// When null just pass through.
if (file.isNull()) {
this.push(file);
return done();
}
var opts = merge({map: !!file.sourceMap}, options);
if (opts.map) {
if (typeof opts.map === "boolean") {
opts.map = {};
}
if (file.path) {
opts.map.inFile = file.relative;
}
}
// Buffer input.
if (file.isBuffer()) {
try {
file.contents = transform(file, file.contents, opts);
} catch (e) {
this.emit("error", e);
return done();
}
// Dealing with stream input.
} else {
file.contents = file.contents.pipe(new BufferStreams(function(err, buf, cb) {
if (err) return cb(new PluginError(PLUGIN_NAME, err));
try {
var transformed = transform(file, buf, opts)
} catch (e) {
return cb(e);
}
cb(null, transformed);
}));
}
this.push(file);
done();
});
};