Skip to content

Commit

Permalink
Prefer uglify-es
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Jan 20, 2018
1 parent 3b4fa3c commit 466adc2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 56 deletions.
25 changes: 1 addition & 24 deletions README.md
Expand Up @@ -33,30 +33,7 @@ uglify(options, minifier)

`options` – default: `{}`, type: `object`. [UglifyJS API options](https://github.com/mishoo/UglifyJS2#api-reference)

`minifier` – default: `require('uglify-js').minify`, type: `function`. Module to use as a minifier. You can use other versions (or forks) of UglifyJS instead default one.

## Warning

[UglifyJS](https://github.com/mishoo/UglifyJS2), which this plugin is based on, does not support the ES2015 module syntax. Thus using this plugin with Rollup's default bundle format (`'es'`) will not work and error out.
To work around this you can tell `rollup-plugin-uglify` to use the UglifyJS [unstable es version](https://github.com/mishoo/UglifyJS2) by passing its `minify` function to minify your code.
```js
import { rollup } from 'rollup';
import uglify from 'rollup-plugin-uglify';
import { minify } from 'uglify-es';

rollup({
entry: 'main.js',
plugins: [
uglify({}, minify)
]
});
```

To install the experimental version of UglifyJS:

```
npm i uglify-es -D
```
`minifier` – default: `require('uglify-es').minify`, type: `function`. Module to use as a minifier. You can use other versions (or forks) of UglifyJS instead default one.

## Examples

Expand Down
31 changes: 16 additions & 15 deletions index.js
@@ -1,21 +1,22 @@
const minify = require('uglify-js').minify;
const minify = require("uglify-es").minify;

function uglify(userOptions, minifier) {
if (minifier === undefined) {
minifier = minify;
}
const options = Object.assign({ sourceMap: true }, userOptions);
return {
name: 'uglify',
if (minifier === undefined) {
minifier = minify;
}
const options = Object.assign({ sourceMap: true }, userOptions);

return {
name: "uglify",

transformBundle(code) {
const result = minifier(code, options);
if (result.error) {
throw result.error;
}
return result;
}
};
transformBundle(code) {
const result = minifier(code, options);
if (result.error) {
throw result.error;
}
return result;
}
};
}

module.exports = uglify;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -23,7 +23,7 @@
"author": "Bogdan Chadkin <trysound@yandex.ru>",
"license": "MIT",
"dependencies": {
"uglify-js": "^3.3.7"
"uglify-es": "^3.3.7"
},
"devDependencies": {
"jest": "^22.1.4",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/unminified.js
Expand Up @@ -2,5 +2,5 @@
var a = 5;

if (a < 3) {
console.log(4);
console.log(4);
}
16 changes: 8 additions & 8 deletions test/test.js
Expand Up @@ -62,15 +62,15 @@ test("allow passing minifier", async () => {
});

test("throw error on uglify fail", async () => {
const bundle = await rollup({
input: "test/fixtures/failed.js",
plugins: [
uglify({}, () => ({
error: Error("some error")
}))
]
});
try {
const bundle = await rollup({
input: "test/fixtures/failed.js",
plugins: [
uglify({}, () => ({
error: Error("some error")
}))
]
});
await bundle.generate({ format: "es" });
expect(true).toBeFalsy();
} catch (err) {
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Expand Up @@ -2556,6 +2556,13 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

uglify-es@^3.3.7:
version "3.3.7"
resolved "https://registry.yarnpkg.com/uglify-es/-/uglify-es-3.3.7.tgz#d1249af668666aba7cb1163e277455be9eb393cf"
dependencies:
commander "~2.13.0"
source-map "~0.6.1"

uglify-js@^2.6:
version "2.8.27"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.27.tgz#47787f912b0f242e5b984343be8e35e95f694c9c"
Expand All @@ -2565,13 +2572,6 @@ uglify-js@^2.6:
optionalDependencies:
uglify-to-browserify "~1.0.0"

uglify-js@^3.3.7:
version "3.3.7"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.3.7.tgz#28463e7c7451f89061d2b235e30925bf5625e14d"
dependencies:
commander "~2.13.0"
source-map "~0.6.1"

uglify-to-browserify@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"
Expand Down

1 comment on commit 466adc2

@tunnckoCore
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alleluah, finally! 🙏

It was guaranteed that 99% of the devs manually used uglifyEs.minify...

Please sign in to comment.