Skip to content

Commit 8b02406

Browse files
committed
standard style
1 parent 06f1f6d commit 8b02406

4 files changed

Lines changed: 87 additions & 88 deletions

File tree

index.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
var minimatch = require('minimatch').Minimatch
2-
, convert = require('convert-source-map')
3-
, through = require('through2')
4-
, path = require('path')
5-
, xtend = require('xtend')
1+
const minimatch = require('minimatch').Minimatch
2+
const convert = require('convert-source-map')
3+
const through = require('through2')
4+
const path = require('path')
5+
const xtend = require('xtend')
66

77
module.exports = uglifyify
88

9-
function uglifyify(file, opts) {
9+
function uglifyify (file, opts) {
1010
opts = xtend(opts || {})
1111

12-
var debug = opts._flags && opts._flags.debug
12+
let debug = opts._flags && opts._flags.debug
1313
// lazy require `terser` so uglifyify can be loaded on very old node.js versions
14-
var ujs = opts.uglify || require('terser')
14+
const ujs = opts.uglify || require('terser')
1515

1616
if (ignore(file, opts.ignore)) {
1717
return through()
1818
}
1919

20-
var buffer = ''
21-
var exts = []
20+
let buffer = ''
21+
const exts = []
2222
.concat(opts.exts || [])
2323
.concat(opts.x || [])
24-
.map(function(d) {
24+
.map(function (d) {
2525
if (d.charAt(0) === '.') return d
2626
return '.' + d
2727
})
2828

2929
if (
3030
/\.json$/.test(file) ||
31-
exts.length &&
32-
exts.indexOf(path.extname(file)) === -1
31+
(exts.length && exts.indexOf(path.extname(file)) === -1)
3332
) {
3433
return through()
3534
}
@@ -40,13 +39,13 @@ function uglifyify(file, opts) {
4039
delete opts.x
4140
delete opts.uglify
4241

43-
return through(function write(chunk, _enc, callback) {
42+
return through(function write (chunk, _enc, callback) {
4443
buffer += chunk
4544
callback()
46-
}, capture(function ready(callback) {
47-
var stream = this
45+
}, capture(function ready (callback) {
46+
const stream = this
4847
debug = opts.sourceMap !== false && debug
49-
opts = xtend({
48+
opts = xtend({
5049
compress: true,
5150
mangle: true,
5251
sourceMap: {
@@ -80,7 +79,7 @@ function uglifyify(file, opts) {
8079
stream.push(min.code)
8180

8281
if (min.map && min.map !== 'null') {
83-
var map = convert.fromJSON(min.map)
82+
const map = convert.fromJSON(min.map)
8483

8584
map.setProperty('sources', [path.basename(file)])
8685

@@ -92,27 +91,27 @@ function uglifyify(file, opts) {
9291
})
9392
}))
9493

95-
function capture(fn) {
96-
return function(callback) {
97-
var stream = this
94+
function capture (fn) {
95+
return function (callback) {
96+
const stream = this
9897
try {
9998
fn.apply(stream, arguments).catch(function (err) {
10099
callback(err)
101100
})
102-
} catch(err) {
101+
} catch (err) {
103102
callback(err)
104103
}
105104
}
106105
}
107106
}
108107

109-
function ignore(file, list) {
108+
function ignore (file, list) {
110109
if (!list) return
111110

112111
list = Array.isArray(list) ? list : [list]
113112

114-
return list.some(function(pattern) {
115-
var match = minimatch(pattern)
113+
return list.some(function (pattern) {
114+
const match = minimatch(pattern)
116115
return match.match(file)
117116
})
118117
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
"bl": "^5.0.0",
1515
"browserify": "^17.0.0",
1616
"from2-string": "^1.1.0",
17+
"standard": "^17.0.0",
1718
"tap-spec": "^5.0.0",
1819
"tape": "^5.6.1",
1920
"uglify-js": "^3.17.3",
2021
"wrap-stream": "^2.0.0"
2122
},
2223
"scripts": {
2324
"tests-only": "node test | tap-spec",
24-
"lint": "true",
25+
"lint": "standard",
2526
"test": "npm run lint && npm run tests-only"
2627
},
2728
"repository": {

test/fixture.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
/* eslint-disable */
12
var hello = 'world'
23
if (hello !== 'world') {
3-
throw new Error
4+
throw new Error()
45
}

test/index.js

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const convert = require('convert-source-map')
2-
const wrap = require('wrap-stream')
1+
const convert = require('convert-source-map')
2+
const wrap = require('wrap-stream')
33
const browserify = require('browserify')
4-
const from2 = require('from2-string')
5-
const test = require('tape')
6-
const path = require('path')
7-
const uglifyify = require('../')
8-
const fs = require('fs')
9-
const bl = require('bl').BufferListStream
4+
const from2 = require('from2-string')
5+
const test = require('tape')
6+
const path = require('path')
7+
const uglifyify = require('../')
8+
const fs = require('fs')
9+
const bl = require('bl').BufferListStream
1010

1111
let uglify
1212
try {
@@ -16,13 +16,13 @@ try {
1616
uglify = require('uglify-js')
1717
}
1818

19-
test('uglifyify: sanity check', function(t) {
20-
var src = path.join(__dirname, 'fixture.js')
21-
var orig = fs.readFileSync(src, 'utf8')
19+
test('uglifyify: sanity check', function (t) {
20+
const src = path.join(__dirname, 'fixture.js')
21+
const orig = fs.readFileSync(src, 'utf8')
2222

2323
fs.createReadStream(src)
24-
.pipe(uglifyify(src, { uglify: uglify }))
25-
.pipe(bl(function(err, data) {
24+
.pipe(uglifyify(src, { uglify }))
25+
.pipe(bl(function (err, data) {
2626
if (err) return t.ifError(err)
2727
data = String(data)
2828
t.notEqual(data.indexOf('var hello'), -1, 'var hello')
@@ -32,26 +32,26 @@ test('uglifyify: sanity check', function(t) {
3232
}))
3333
})
3434

35-
test('uglifyify: ignores json', function(t) {
36-
var src = path.join(__dirname, 'fixture.js')
37-
var json = path.join(__dirname, 'fixture.json')
38-
var orig = fs.readFileSync(src, 'utf8')
35+
test('uglifyify: ignores json', function (t) {
36+
const src = path.join(__dirname, 'fixture.js')
37+
const json = path.join(__dirname, 'fixture.json')
38+
const orig = fs.readFileSync(src, 'utf8')
3939

4040
fs.createReadStream(src)
41-
.pipe(uglifyify(json, { uglify: uglify }))
41+
.pipe(uglifyify(json, { uglify }))
4242
.pipe(bl(buffered))
4343

44-
function buffered(err, data) {
44+
function buffered (err, data) {
4545
if (err) return t.ifError(err)
4646
data = String(data)
4747
t.equal(data, orig, 'should not be minified')
4848
t.end()
4949
}
5050
})
5151

52-
test('uglifyify: -t [ uglifyify --exts ]', function(t) {
53-
var src = path.join(__dirname, 'fixture.js')
54-
var orig = fs.readFileSync(src, 'utf8')
52+
test('uglifyify: -t [ uglifyify --exts ]', function (t) {
53+
const src = path.join(__dirname, 'fixture.js')
54+
const orig = fs.readFileSync(src, 'utf8')
5555

5656
t.plan(5)
5757

@@ -61,12 +61,12 @@ test('uglifyify: -t [ uglifyify --exts ]', function(t) {
6161
check(path.join(__dirname, 'fixture.fbla'), true)
6262
check(src, true)
6363

64-
function check(name, ignored) {
64+
function check (name, ignored) {
6565
fs.createReadStream(src)
66-
.pipe(uglifyify(name, { exts: ['mkdn' ], x: ['.obj2'], uglify: uglify }))
66+
.pipe(uglifyify(name, { exts: ['mkdn'], x: ['.obj2'], uglify }))
6767
.pipe(bl(buffered))
6868

69-
function buffered(err, data) {
69+
function buffered (err, data) {
7070
if (err) return t.ifError(err)
7171
data = String(data)
7272
t.ok(ignored
@@ -77,28 +77,28 @@ test('uglifyify: -t [ uglifyify --exts ]', function(t) {
7777
}
7878
})
7979

80-
test('uglifyify: passes options to uglify', function(t) {
81-
var src = path.join(__dirname, 'fixture.js')
82-
var orig = fs.readFileSync(src, 'utf8')
83-
var buf1 = null
80+
test('uglifyify: passes options to uglify', function (t) {
81+
const src = path.join(__dirname, 'fixture.js')
82+
const orig = fs.readFileSync(src, 'utf8')
83+
let buf1 = null
8484

8585
fs.createReadStream(src)
8686
.pipe(closure())
87-
.pipe(uglifyify(src, { compress: { conditionals: false }, uglify: uglify }))
87+
.pipe(uglifyify(src, { compress: { conditionals: false }, uglify }))
8888
.pipe(bl(buffered1))
8989

90-
function buffered1(err, _buf1) {
90+
function buffered1 (err, _buf1) {
9191
if (err) return t.ifError(err)
9292
buf1 = String(_buf1)
9393
t.notEqual(buf1, orig, 'should be minified')
9494

9595
fs.createReadStream(src)
9696
.pipe(closure())
97-
.pipe(uglifyify(src, { uglify: uglify }))
97+
.pipe(uglifyify(src, { uglify }))
9898
.pipe(bl(buffered2))
9999
}
100100

101-
function buffered2(err, buf2) {
101+
function buffered2 (err, buf2) {
102102
if (err) return
103103
buf2 = String(buf2)
104104
t.notEqual(buf2, orig, 'should be minified')
@@ -107,73 +107,71 @@ test('uglifyify: passes options to uglify', function(t) {
107107
}
108108
})
109109

110-
111-
112-
function closure() {
110+
function closure () {
113111
return wrap('(function(){', '})()')
114112
}
115113

116-
test('uglifyify: sourcemaps', function(t) {
114+
test('uglifyify: sourcemaps', function (t) {
117115
t.plan(10)
118116

119-
var src = path.join(__dirname, 'fixture.js')
120-
var json = path.join(__dirname, 'fixture.json')
121-
var orig = fs.readFileSync(src, 'utf8')
117+
const src = path.join(__dirname, 'fixture.js')
118+
const json = path.join(__dirname, 'fixture.json')
119+
const orig = fs.readFileSync(src, 'utf8')
122120
Promise.resolve(uglify.minify(orig, {
123121
sourceMap: {
124122
url: 'out.js.map'
125123
}
126124
})).then(function (min) {
127-
var map = convert.fromJSON(min.map)
125+
const map = convert.fromJSON(min.map)
128126
map.setProperty('sources', [src])
129127
map.setProperty('sourcesContent', [orig])
130128

131-
var mapped = [orig, map.toComment()].join('\n')
129+
const mapped = [orig, map.toComment()].join('\n')
132130

133131
from2(mapped)
134-
.pipe(uglifyify(json, { uglify: uglify }))
132+
.pipe(uglifyify(json, { uglify }))
135133
.pipe(bl(doneWithMap))
136134

137135
from2(orig)
138-
.pipe(uglifyify(json, { uglify: uglify }))
136+
.pipe(uglifyify(json, { uglify }))
139137
.pipe(bl(doneWithoutMap))
140138

141139
browserify({ entries: [src], debug: true })
142-
.transform(uglifyify, { uglify: uglify })
140+
.transform(uglifyify, { uglify })
143141
.bundle()
144142
.pipe(bl(doneWithMap))
145143

146144
browserify({ entries: [src], debug: false })
147-
.transform(uglifyify, { uglify: uglify })
145+
.transform(uglifyify, { uglify })
148146
.bundle()
149147
.pipe(bl(doneWithoutDebug))
150148

151149
from2(mapped)
152-
.pipe(uglifyify(json, { _flags: { debug: false }, uglify: uglify }))
150+
.pipe(uglifyify(json, { _flags: { debug: false }, uglify }))
153151
.pipe(bl(doneWithMapAndNoDebug))
154152

155-
function doneWithMap(err, data) {
153+
function doneWithMap (err, data) {
156154
if (err) return t.ifError(err)
157155
data = String(data)
158156
t.notEqual(data, orig, 'should have changed')
159157
t.equal(data.match(/\/\/[@#]/g).length, 1, 'should have sourcemap')
160158
}
161159

162-
function doneWithoutMap(err, data) {
160+
function doneWithoutMap (err, data) {
163161
if (err) return t.ifError(err)
164162
data = String(data)
165163
t.equal(data, orig, 'should not have changed')
166164
t.equal(data.indexOf(/\/\/[@#]/g), -1, 'should not have sourcemap')
167165
}
168166

169-
function doneWithoutDebug(err, data) {
167+
function doneWithoutDebug (err, data) {
170168
if (err) return t.ifError(err)
171169
data = String(data)
172170
t.notEqual(data, orig, 'should have changed')
173171
t.equal(data.indexOf(/\/\/[@#]/g), -1, 'should not have sourcemap')
174172
}
175173

176-
function doneWithMapAndNoDebug(err, data) {
174+
function doneWithMapAndNoDebug (err, data) {
177175
if (err) return t.ifError(err)
178176
data = String(data)
179177
t.notEqual(data, orig, 'should have changed')
@@ -184,27 +182,27 @@ test('uglifyify: sourcemaps', function(t) {
184182
})
185183
})
186184

187-
test('uglifyify: transform is stable', function(t) {
185+
test('uglifyify: transform is stable', function (t) {
188186
t.plan(1)
189187

190-
var src = path.join(__dirname, 'fixture.js')
191-
var opts = {
192-
uglify: uglify,
188+
const src = path.join(__dirname, 'fixture.js')
189+
const opts = {
190+
uglify,
193191
_flags: {
194192
debug: false
195193
}
196194
}
197195

198-
var tr1 = fs.createReadStream(src).pipe(uglifyify(src, opts))
199-
var tr2 = fs.createReadStream(src).pipe(uglifyify(src, opts))
196+
const tr1 = fs.createReadStream(src).pipe(uglifyify(src, opts))
197+
const tr2 = fs.createReadStream(src).pipe(uglifyify(src, opts))
200198

201-
tr1.pipe(bl(function(err, data) {
199+
tr1.pipe(bl(function (err, data) {
202200
if (err) return t.ifError(err)
203-
var data1 = String(data)
201+
const data1 = String(data)
204202

205-
tr2.pipe(bl(function(err, data) {
203+
tr2.pipe(bl(function (err, data) {
206204
if (err) return t.ifError(err)
207-
var data2 = String(data)
205+
const data2 = String(data)
208206

209207
t.equal(data2, data1, 'repeated runs should be the same')
210208
t.end()

0 commit comments

Comments
 (0)