Skip to content

Commit

Permalink
deps: use @browserify/uglifyify (#42)
Browse files Browse the repository at this point in the history
* deps: use @browserify/uglifyify

* fall back to an older terser version on old node.js
  • Loading branch information
goto-bus-stop authored Oct 16, 2022
1 parent 96e564e commit 5ebde99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@ var collapser = require('bundle-collapser/plugin')
var packFlatStream = require('browser-pack-flat')
var commonShake = require('common-shakeify')
var unassertify = require('unassertify')
var uglify = require('minify-stream')
var minifyStream = require('minify-stream')
var envify = require('@browserify/envify/custom')
var uglifyify = require('uglifyify')
var uglifyify = require('@browserify/uglifyify')

function getUglify () {
// For older Node.js versions, fall back to an earlier `terser` version.
var uglify = null
try {
Function('var a = async () => {}') // eslint-disable-line no-new-func
} catch (_err) {
uglify = require('terser')
}

return uglify
}

function makeUglifyOptions (debug) {
var uglifyOpts = {
uglify: getUglify(),
output: {
ascii_only: true
},
Expand Down Expand Up @@ -42,6 +55,7 @@ module.exports = function (b, opts) {
b.transform(envify(env), { global: true })
// Remove dead code.
b.transform(uglifyify, {
uglify: getUglify(),
global: true,
toplevel: true,
// No need to mangle here, will do that at the end.
Expand All @@ -66,7 +80,7 @@ module.exports = function (b, opts) {

// Minify the final output.
var uglifyOpts = makeUglifyOptions(b._options.debug)
b.pipeline.get('pack').push(uglify(uglifyOpts))
b.pipeline.get('pack').push(minifyStream(uglifyOpts))
}

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

// Minify the final output.
var uglifyOpts = makeUglifyOptions(opts.debug)
pipeline.get('pack').push(uglify(uglifyOpts))
pipeline.get('pack').push(minifyStream(uglifyOpts))
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
},
"dependencies": {
"@browserify/envify": "^6.0.0",
"@browserify/uglifyify": "^6.0.0",
"browser-pack-flat": "^3.0.9",
"bundle-collapser": "^1.3.0",
"common-shakeify": "^1.1.1",
"minify-stream": "^2.0.1",
"multisplice": "^1.0.0",
"terser": "3.16.1",
"through2": "^4.0.2",
"uglifyify": "^5.0.0",
"unassertify": "^2.1.1"
},
"devDependencies": {
Expand Down
18 changes: 9 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ browserify -p tinyify app.js
## Included

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

### `env: {}`

Supply custom environment variables for [envify][].
Supply custom environment variables for [@browserify/envify][].

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

```bash
npm install --save-dev unassertify @browserify/envify uglifyify common-shakeify browser-pack-flat uglify-js
npm install --save-dev unassertify @browserify/envify @browserify/uglifyify common-shakeify browser-pack-flat terser
browserify entry.js \
-g unassertify \
-g @browserify/envify \
-g uglifyify \
-g @browserify/uglifyify \
-p common-shakeify \
-p browser-pack-flat/plugin \
| uglifyjs -cm \
| terser -cm \
> output.js
```

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

[unassertify]: https://github.com/unassert-js/unassertify
[envify]: https://github.com/browserify/envify
[uglifyify]: https://github.com/hughsk/uglifyify
[@browserify/envify]: https://github.com/browserify/envify
[@browserify/uglifyify]: https://github.com/browserify/uglifyify
[common-shakeify]: https://github.com/browserify/common-shakeify
[browser-pack-flat]: https://github.com/goto-bus-stop/browser-pack-flat
[bundle-collapser]: https://github.com/substack/bundle-collapser
Expand Down

0 comments on commit 5ebde99

Please sign in to comment.