mirror of
https://github.com/S2-/minifyfromhtml.git
synced 2025-08-03 04:10:04 +02:00
update minify
This commit is contained in:
34
node_modules/minify/lib/minify.js
generated
vendored
34
node_modules/minify/lib/minify.js
generated
vendored
@@ -2,20 +2,16 @@
|
||||
|
||||
const DIR = __dirname + '/';
|
||||
|
||||
const fs = require('fs');
|
||||
const {readFile} = require('fs').promises;
|
||||
const path = require('path');
|
||||
const {
|
||||
promisify,
|
||||
} = require('util');
|
||||
|
||||
const tryToCatch = require('try-to-catch');
|
||||
const readFile = promisify(fs.readFile);
|
||||
|
||||
const log = require('debug')('minify');
|
||||
|
||||
['js', 'html', 'css', 'img'].forEach((name) => {
|
||||
for (const name of ['js', 'html', 'css', 'img']) {
|
||||
minify[name] = require(DIR + name);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = minify;
|
||||
|
||||
@@ -24,7 +20,7 @@ function check(name) {
|
||||
throw Error('name could not be empty!');
|
||||
}
|
||||
|
||||
async function minify(name) {
|
||||
async function minify(name, userOptions) {
|
||||
const EXT = ['js', 'html', 'css'];
|
||||
|
||||
check(name);
|
||||
@@ -36,7 +32,7 @@ async function minify(name) {
|
||||
throw Error(`File type "${ext}" not supported.`);
|
||||
|
||||
log('optimizing ' + path.basename(name));
|
||||
return optimize(name);
|
||||
return optimize(name, userOptions);
|
||||
}
|
||||
|
||||
function getName(file) {
|
||||
@@ -51,9 +47,10 @@ function getName(file) {
|
||||
/**
|
||||
* function minificate js,css and html files
|
||||
*
|
||||
* @param files - js, css or html file path
|
||||
* @param {string} file - js, css or html file path
|
||||
* @param {object} userOptions - object with optional `html`, `css, `js`, and `img` keys, which each can contain options to be combined with defaults and passed to the respective minifier
|
||||
*/
|
||||
async function optimize(file) {
|
||||
async function optimize(file, userOptions) {
|
||||
check(file);
|
||||
|
||||
const name = getName(file);
|
||||
@@ -61,23 +58,26 @@ async function optimize(file) {
|
||||
log('reading file ' + path.basename(name));
|
||||
|
||||
const data = await readFile(name, 'utf8');
|
||||
return onDataRead(file, data);
|
||||
return onDataRead(file, data, userOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processing of files
|
||||
* @param fileData {name, data}
|
||||
* Processing of files
|
||||
* @param {string} filename
|
||||
* @param {string} data - the contents of the file
|
||||
* @param {object} userOptions - object with optional `html`, `css, `js`, and `img` keys, which each can contain options to be combined with defaults and passed to the respective minifier
|
||||
*/
|
||||
async function onDataRead(filename, data) {
|
||||
async function onDataRead(filename, data, userOptions) {
|
||||
log('file ' + path.basename(filename) + ' read');
|
||||
|
||||
const ext = path.extname(filename).replace(/^\./, '');
|
||||
|
||||
const optimizedData = await minify[ext](data);
|
||||
const optimizedData = await minify[ext](data, userOptions);
|
||||
|
||||
let b64Optimize;
|
||||
|
||||
if (ext === 'css')
|
||||
[, b64Optimize] = await tryToCatch(minify.img, filename, optimizedData);
|
||||
[, b64Optimize] = await tryToCatch(minify.img, filename, optimizedData, userOptions);
|
||||
|
||||
return b64Optimize || optimizedData;
|
||||
}
|
||||
|
Reference in New Issue
Block a user