-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
47 lines (39 loc) · 1.12 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
var duplexify = require('duplexify')
var concat = require('concat-stream')
var fromString = require('from2-string')
var defaultUglify = require('terser')
var convert = require('convert-source-map')
var xtend = require('xtend')
module.exports = uglifyStream
function defaultOpts () {
return {
sourceMap: { content: 'inline' }
}
}
function uglifyStream (opts) {
opts = xtend(defaultOpts(), opts || {})
var uglify = opts.uglify || defaultUglify
delete opts.uglify
var stream = duplexify()
var writer = concat({ encoding: 'string' }, function (source) {
var result = uglify.minify(source, opts)
if (result.then) {
result.then(onsuccess, onerror)
} else if (result.error) {
return onerror(result.error)
} else onsuccess(result)
function onsuccess (minified) {
var final = minified.code
if (minified.map) {
final += '\n' + convert.fromJSON(minified.map).toComment()
}
var reader = fromString(final)
stream.setReadable(reader)
}
function onerror (error) {
stream.emit('error', error)
}
})
stream.setWritable(writer)
return stream
}