Skip to content

Commit 5ebde99

Browse files
deps: use @browserify/uglifyify (#42)
* deps: use @browserify/uglifyify * fall back to an older terser version on old node.js
1 parent 96e564e commit 5ebde99

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@ var collapser = require('bundle-collapser/plugin')
33
var packFlatStream = require('browser-pack-flat')
44
var commonShake = require('common-shakeify')
55
var unassertify = require('unassertify')
6-
var uglify = require('minify-stream')
6+
var minifyStream = require('minify-stream')
77
var envify = require('@browserify/envify/custom')
8-
var uglifyify = require('uglifyify')
8+
var uglifyify = require('@browserify/uglifyify')
9+
10+
function getUglify () {
11+
// For older Node.js versions, fall back to an earlier `terser` version.
12+
var uglify = null
13+
try {
14+
Function('var a = async () => {}') // eslint-disable-line no-new-func
15+
} catch (_err) {
16+
uglify = require('terser')
17+
}
18+
19+
return uglify
20+
}
921

1022
function makeUglifyOptions (debug) {
1123
var uglifyOpts = {
24+
uglify: getUglify(),
1225
output: {
1326
ascii_only: true
1427
},
@@ -42,6 +55,7 @@ module.exports = function (b, opts) {
4255
b.transform(envify(env), { global: true })
4356
// Remove dead code.
4457
b.transform(uglifyify, {
58+
uglify: getUglify(),
4559
global: true,
4660
toplevel: true,
4761
// No need to mangle here, will do that at the end.
@@ -66,7 +80,7 @@ module.exports = function (b, opts) {
6680

6781
// Minify the final output.
6882
var uglifyOpts = makeUglifyOptions(b._options.debug)
69-
b.pipeline.get('pack').push(uglify(uglifyOpts))
83+
b.pipeline.get('pack').push(minifyStream(uglifyOpts))
7084
}
7185

7286
module.exports.applyToPipeline = function applyToPipeline (pipeline, opts) {
@@ -86,5 +100,5 @@ module.exports.applyToPipeline = function applyToPipeline (pipeline, opts) {
86100

87101
// Minify the final output.
88102
var uglifyOpts = makeUglifyOptions(opts.debug)
89-
pipeline.get('pack').push(uglify(uglifyOpts))
103+
pipeline.get('pack').push(minifyStream(uglifyOpts))
90104
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
},
99
"dependencies": {
1010
"@browserify/envify": "^6.0.0",
11+
"@browserify/uglifyify": "^6.0.0",
1112
"browser-pack-flat": "^3.0.9",
1213
"bundle-collapser": "^1.3.0",
1314
"common-shakeify": "^1.1.1",
1415
"minify-stream": "^2.0.1",
1516
"multisplice": "^1.0.0",
17+
"terser": "3.16.1",
1618
"through2": "^4.0.2",
17-
"uglifyify": "^5.0.0",
1819
"unassertify": "^2.1.1"
1920
},
2021
"devDependencies": {

readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ browserify -p tinyify app.js
2222
## Included
2323

2424
- [unassertify][] - Remove `assert()` calls
25-
- [envify][] - Replace environment variables—by default, replaces `NODE_ENV` with `"production"`
26-
- [uglifyify][] - Remove dead code from modules
25+
- [@browserify/envify][] - Replace environment variables—by default, replaces `NODE_ENV` with `"production"`
26+
- [@browserify/uglifyify][] - Remove dead code from modules
2727
- [common-shakeify][] - Remove unused exports from modules
2828
- [browser-pack-flat][] - Output a "flat" bundle, with all modules in a single scope
2929
- [bundle-collapser][] - When using the `--no-flat` option, bundle-collapser replaces file paths in `require()` calls with short module IDs
@@ -38,7 +38,7 @@ Options can be provided on the command line using subarg syntax, or in a separat
3838

3939
### `env: {}`
4040

41-
Supply custom environment variables for [envify][].
41+
Supply custom environment variables for [@browserify/envify][].
4242

4343
```js
4444
b.plugin('tinyify', {
@@ -73,14 +73,14 @@ b.plugin('tinyify', { flat: false })
7373
If you need further customisation, I recommend installing the tools separately instead:
7474

7575
```bash
76-
npm install --save-dev unassertify @browserify/envify uglifyify common-shakeify browser-pack-flat uglify-js
76+
npm install --save-dev unassertify @browserify/envify @browserify/uglifyify common-shakeify browser-pack-flat terser
7777
browserify entry.js \
7878
-g unassertify \
7979
-g @browserify/envify \
80-
-g uglifyify \
80+
-g @browserify/uglifyify \
8181
-p common-shakeify \
8282
-p browser-pack-flat/plugin \
83-
| uglifyjs -cm \
83+
| terser -cm \
8484
> output.js
8585
```
8686

@@ -90,7 +90,7 @@ Or with the Node API:
9090
browserify('entry.js')
9191
.transform('unassertify', { global: true })
9292
.transform('@browserify/envify', { global: true })
93-
.transform('uglifyify', { global: true })
93+
.transform('@browserify/uglifyify', { global: true })
9494
.plugin('common-shakeify')
9595
.plugin('browser-pack-flat/plugin')
9696
.bundle()
@@ -105,8 +105,8 @@ Alternatively you can fork this repo and publish it on npm under a scope with yo
105105
[Apache-2.0](./LICENSE.md)
106106

107107
[unassertify]: https://github.com/unassert-js/unassertify
108-
[envify]: https://github.com/browserify/envify
109-
[uglifyify]: https://github.com/hughsk/uglifyify
108+
[@browserify/envify]: https://github.com/browserify/envify
109+
[@browserify/uglifyify]: https://github.com/browserify/uglifyify
110110
[common-shakeify]: https://github.com/browserify/common-shakeify
111111
[browser-pack-flat]: https://github.com/goto-bus-stop/browser-pack-flat
112112
[bundle-collapser]: https://github.com/substack/bundle-collapser

0 commit comments

Comments
 (0)