mirror of
https://github.com/S2-/minifyfromhtml.git
synced 2025-08-02 20:00:05 +02:00
use minify
This commit is contained in:
100
node_modules/minify/bin/minify.js
generated
vendored
Normal file
100
node_modules/minify/bin/minify.js
generated
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
const Pack = require('../package');
|
||||
const Version = Pack.version;
|
||||
|
||||
const log = function(...args) {
|
||||
console.log(...args);
|
||||
process.stdin.pause();
|
||||
};
|
||||
|
||||
const Argv = process.argv;
|
||||
const files = Argv.slice(2);
|
||||
const [In] = files;
|
||||
|
||||
log.error = function(e) {
|
||||
console.error(e);
|
||||
process.stdin.pause();
|
||||
};
|
||||
|
||||
process.on('uncaughtException', (error) => {
|
||||
if (error.code !== 'EPIPE')
|
||||
log(error);
|
||||
});
|
||||
|
||||
minify();
|
||||
|
||||
function readStd(callback) {
|
||||
const stdin = process.stdin;
|
||||
let chunks = '';
|
||||
const read = () => {
|
||||
const chunk = stdin.read();
|
||||
|
||||
if (chunk)
|
||||
return chunks += chunk;
|
||||
|
||||
stdin.removeListener('readable', read);
|
||||
callback(chunks);
|
||||
};
|
||||
|
||||
stdin.setEncoding('utf8');
|
||||
stdin.addListener('readable', read);
|
||||
}
|
||||
|
||||
function minify() {
|
||||
if (!In || /^(-h|--help)$/.test(In))
|
||||
return help();
|
||||
|
||||
if (/^--(js|css|html)$/.test(In))
|
||||
return readStd(processStream);
|
||||
|
||||
if (/^(-v|--version)$/.test(In))
|
||||
return log('v' + Version);
|
||||
|
||||
uglifyFiles(files);
|
||||
}
|
||||
|
||||
function processStream(chunks) {
|
||||
const minify = require('..');
|
||||
const tryCatch = require('try-catch');
|
||||
|
||||
if (!chunks || !In)
|
||||
return;
|
||||
|
||||
const name = In.replace('--', '');
|
||||
|
||||
const [e, data] = tryCatch(minify[name], chunks);
|
||||
if (e)
|
||||
return log.error(e);
|
||||
|
||||
log(data);
|
||||
}
|
||||
|
||||
function uglifyFiles(files) {
|
||||
const minify = require('..');
|
||||
const minifiers = files.map(minify);
|
||||
|
||||
Promise.all(minifiers)
|
||||
.then(logAll)
|
||||
.catch(log.error);
|
||||
}
|
||||
|
||||
function logAll(array) {
|
||||
for (const item of array)
|
||||
log(item);
|
||||
}
|
||||
|
||||
function help() {
|
||||
const bin = require('../help');
|
||||
const usage = 'Usage: minify [options]';
|
||||
|
||||
console.log(usage);
|
||||
console.log('Options:');
|
||||
|
||||
Object.keys(bin).forEach((name) => {
|
||||
console.log(' %s %s', name, bin[name]);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user