-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
57 lines (44 loc) · 1.08 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
'use strict';
var ngAnnotate = require('ng-annotate'),
through = require('through2'),
defaults = require('defaults'),
path = require('path'),
clone = require('clone');
module.exports = function (file, opts) {
opts = defaults(clone(opts), {
add: true,
x: [],
ext: [],
sourcemap: opts && opts._flags ? opts._flags.debug : false
});
opts.x = Array.isArray(opts.x) && opts.x || [opts.x];
opts.ext = Array.isArray(opts.ext) && opts.ext || [opts.ext];
var exts = [].concat(opts.x, opts.ext, ['.js']);
if (exts.indexOf(path.extname(file)) == -1) {
return through();
}
if (opts.sourcemap) {
opts.map = {
inFile: file,
inline: true
};
opts.sourcemap = undefined;
}
var data = '';
return through(transform, flush);
function transform (chunk, enc, cb) {
data += chunk;
cb();
}
function flush (cb) {
// jshint validthis: true
var annotateResult = ngAnnotate(data, opts);
if (annotateResult.errors) {
annotateResult.errors.unshift(file);
cb(new Error(annotateResult.errors.join('\n')));
return;
}
this.push(annotateResult.src);
cb();
}
};