This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
forked from hughsk/uglifyify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
147 lines (124 loc) · 3.14 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var minimatch = require('minimatch').Minimatch
, convert = require('convert-source-map')
, through = require('through')
, path = require('path')
, ujs = require('terser')
module.exports = uglifyify
function uglifyify(file, opts) {
opts = opts || {}
var debug = opts._flags && opts._flags.debug
if (ignore(file, opts.ignore)) {
return through()
}
var buffer = ''
var exts = []
.concat(opts.exts || [])
.concat(opts.x || [])
.map(function(d) {
if (d.charAt(0) === '.') return d
return '.' + d
})
if (
/\.json$/.test(file) ||
exts.length &&
exts.indexOf(path.extname(file)) === -1
) {
return through()
}
return through(function write(chunk) {
buffer += chunk
}, capture(function ready() {
debug = opts.sourceMap !== false && debug
var _opts = Object.assign({}, {
compress: true,
mangle: true,
sourceMap: {
filename: file
}
}, opts)
// map out command line options to uglify compatible ones
mapArgv(opts)
// remove exts before passing opts to uglify
delete _opts.global
delete _opts.ignore
delete _opts.exts
delete _opts.x
delete _opts._
delete _opts._flags
if (typeof _opts.compress === 'object') {
delete _opts.compress._
}
if (debug) _opts.sourceMap.url = 'out.js.map'
// Check if incoming source code already has source map comment.
// If so, send it in to ujs.minify as the inSourceMap parameter
if (debug) {
_opts.sourceMap.content = 'inline'
}
var min = ujs.minify(buffer, _opts)
// we should catcch the min error if it comes back and end the stream
if (min.error) {
const err = min.error instanceof Error ? min.error : new Error(min.error.message)
// will be emitted by `capture()`
throw err
}
// Uglify leaves a source map comment pointing back to "out.js.map",
// which we want to get rid of because it confuses browserify.
min.code = min.code.replace(/\/\/[#@] ?sourceMappingURL=out.js.map$/, '')
this.queue(min.code)
if (min.map && min.map !== 'null') {
var map = convert.fromJSON(min.map)
this.queue('\n')
this.queue(map.toComment())
}
this.queue(null)
}))
function capture(fn) {
return function() {
try {
fn.apply(this, arguments)
} catch(err) {
return this.emit('error', err)
}
}
}
}
function ignore(file, list) {
if (!list) return
list = Array.isArray(list) ? list : [list]
return list.some(function(pattern) {
var match = minimatch(pattern)
return match.match(file)
})
}
// uglify-es doesn't allow for command line options in javascript api, this
// remaps it
function mapArgv (opts) {
if (opts._flags) {
delete opts._flags
}
if (opts.c) {
opts.compress = opts.c
delete opts.c
}
if (opts.m) {
opts.mangle = opts.m
delete opts.m
}
if (opts.p) {
opts.parse = opts.p
delete opts.p
}
if (opts.b) {
opts.beautify = opts.b
delete opts.b
}
if (opts.o) {
opts.output = opts.o
delete opts.o
}
if (opts.d) {
opts.define = opts.d
delete opts.d
}
delete opts._
}