Skip to content

Commit

Permalink
feature: minify: add ability to use terser to minify JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Mar 4, 2024
1 parent 6c39c70 commit 1d6ad78
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ For cli use these options can be provided in a JSON file named `.minify.json` li
**Minify** walking up parent directories to locate and read it’s configuration file `.minify.json`.
### `js`
In section related to `js` you can choose `type` of minifier:
- `putout` (default);
- [`terser`](https://github.com/terser/terser);
When you want to pass [options](https://github.com/terser/terser#minify-options) to `terser`, use section with the same name, `.minify.json` will look this way:
```json
{
"js": {
"type": "terser",
"terser": {
"mangle": false
}
}
}
```
## License
MIT
8 changes: 8 additions & 0 deletions lib/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ export default async (data, userOptions) => {

const options = userOptions?.js || {};

if (options.type === 'terser') {
const {terser} = options;
const {minify} = await import('terser');
const {code} = await minify(data, terser);

return code;
}

return await minify(data, options);
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"html-minifier-terser": "^7.1.0",
"readjson": "^2.2.2",
"simport": "^1.2.0",
"terser": "^5.28.1",
"try-catch": "^3.0.0",
"try-to-catch": "^3.0.0"
},
Expand Down
15 changes: 15 additions & 0 deletions test/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {dirname} from 'node:path';
import tryToCatch from 'try-to-catch';
import test from 'supertape';
import CleanCSS from 'clean-css';
import {minify as terserMinify} from 'terser';
import {minify as putoutMinify} from '@putout/minify';
import htmlMinifier from 'html-minifier-terser';
import {minify} from '../lib/minify.js';
Expand All @@ -21,6 +22,20 @@ test('minify: js', async (t) => {
t.end();
});

test('minify: js: terser', async (t) => {
const js = 'hello(`x`); function hello(world) {\nconsole.log(world);\n}';

const {code} = await terserMinify(js);
const result = await minify.js(js, {
js: {
type: 'terser',
},
});

t.equal(code, result);
t.end();
});

test('minify: auto', async (t) => {
const js = 'function hello(world) {\nconsole.log(world);\n}';

Expand Down

0 comments on commit 1d6ad78

Please sign in to comment.