feature: minify: add support of --auto

This commit is contained in:
coderaiser
2023-05-24 18:36:23 +03:00
parent 589ea257c7
commit 260b3e82da
6 changed files with 56 additions and 3 deletions

View File

@@ -23,6 +23,17 @@ npm i minify -g
### CLI
```sh
Usage: minify [options]
Options:
-h, --help display this help and exit
-v, --version display version and exit
--js minify javascript
--css minify css
--html minify html
--auto auto detect format
```
The bash command below creates a code snippet saved as `hello.js`.
Simply copy + paste the code starting with cat, including the EOT on the last line, and press <enter>.

View File

@@ -27,7 +27,7 @@ process.on('uncaughtException', (error) => {
log(error);
});
minify();
await minify();
function readStd(callback, options) {
const {stdin} = process;
@@ -60,7 +60,7 @@ async function minify() {
if (optionsError)
return log.error(optionsError.message);
if (/^--(js|css|html)$/.test(In))
if (/^--(js|css|html|auto)$/.test(In))
return readStd(processStream, options);
await uglifyFiles(files, options);

View File

@@ -3,5 +3,6 @@
"-v, --version ": "display version and exit",
"--js ": "minify javascript",
"--css ": "minify css",
"--html ": "minify html"
"--html ": "minify html",
"--auto ": "auto detect format"
}

26
lib/auto.js Normal file
View File

@@ -0,0 +1,26 @@
import tryToCatch from 'try-to-catch';
import js from './js.js';
import html from './html.js';
import css from './css.js';
import img from './img.js';
const minifiers = [
js,
css,
html,
img,
];
export default async (data, options) => {
let error;
let result;
for (const minify of minifiers) {
[error, result] = await tryToCatch(minify, data, options);
if (result)
return result;
}
throw error;
};

View File

@@ -6,6 +6,7 @@ import js from './js.js';
import html from './html.js';
import css from './css.js';
import img from './img.js';
import auto from './auto.js';
const log = debug('minify');
@@ -14,6 +15,7 @@ const minifiers = {
html,
css,
img,
auto,
};
const {assign} = Object;

View File

@@ -21,6 +21,19 @@ test('minify: js', async (t) => {
t.end();
});
test('minify: auto', async (t) => {
const js = 'function hello(world) {\nconsole.log(world);\n}';
const code = await putoutMinify(js, {
removeUnusedVariables: false,
});
const expected = 'function hello(a){console.log(a)}';
t.equal(code, expected);
t.end();
});
test('minify: js: with alternate options', async (t) => {
const js = 'const a = 5, b = 6; console.log(a);';
const expected = 'const a=5,b=6;';