mirror of
https://github.com/coderaiser/minify.git
synced 2025-12-05 18:55:58 -06:00
feature: minify: add support of --auto
This commit is contained in:
11
README.md
11
README.md
@@ -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>.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
26
lib/auto.js
Normal 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;
|
||||
};
|
||||
@@ -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;
|
||||
|
||||
@@ -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;';
|
||||
|
||||
Reference in New Issue
Block a user