@node-minify/utils 8.0.2-beta.0 → 8.0.3-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +25 -13
- package/dist/index.js +18 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
|
+
import { Options, Dictionary, Settings, MinifierOptions } from '@node-minify/types';
|
|
2
|
+
|
|
1
3
|
/*!
|
|
2
4
|
* node-minify
|
|
3
5
|
* Copyright(c) 2011-2022 Rodolphe Stoclin
|
|
4
6
|
* MIT Licensed
|
|
5
7
|
*/
|
|
8
|
+
|
|
6
9
|
interface Utils {
|
|
7
|
-
readFile:
|
|
8
|
-
writeFile:
|
|
9
|
-
deleteFile:
|
|
10
|
-
buildArgs:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
readFile: (file: string) => string;
|
|
11
|
+
writeFile: ({ file, content, index }: WriteFile) => string;
|
|
12
|
+
deleteFile: (file: string) => void;
|
|
13
|
+
buildArgs: (options: Options & Dictionary<string | boolean | [] | {
|
|
14
|
+
url: string;
|
|
15
|
+
} | {
|
|
16
|
+
filename: string;
|
|
17
|
+
} | undefined>) => any;
|
|
18
|
+
clone: (obj: object) => object;
|
|
19
|
+
getFilesizeInBytes: (file: string) => string;
|
|
20
|
+
getFilesizeGzippedInBytes: (file: string) => Promise<string>;
|
|
21
|
+
prettyBytes: (num: number) => string;
|
|
22
|
+
setFileNameMin: (file: string, output: string, publicFolder: string, replaceInPlace: boolean) => string;
|
|
23
|
+
compressSingleFile: (settings: Settings) => string | Promise<string>;
|
|
24
|
+
getContentFromFiles: (input: string | string[]) => string;
|
|
25
|
+
runSync: ({ settings, content, index }: MinifierOptions) => string;
|
|
26
|
+
runAsync: ({ settings, content, index }: MinifierOptions) => Promise<string>;
|
|
27
|
+
}
|
|
28
|
+
interface WriteFile {
|
|
29
|
+
file: string;
|
|
30
|
+
content: any;
|
|
31
|
+
index?: number;
|
|
20
32
|
}
|
|
21
33
|
declare const utils: Utils;
|
|
22
34
|
|
package/dist/index.js
CHANGED
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
20
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -29,18 +33,18 @@ __export(src_exports, {
|
|
|
29
33
|
utils: () => utils
|
|
30
34
|
});
|
|
31
35
|
module.exports = __toCommonJS(src_exports);
|
|
32
|
-
var
|
|
36
|
+
var import_node_fs = require("fs");
|
|
33
37
|
var import_gzip_size = __toESM(require("gzip-size"));
|
|
34
38
|
var utils = {};
|
|
35
|
-
utils.readFile = (file) =>
|
|
39
|
+
utils.readFile = (file) => (0, import_node_fs.readFileSync)(file, "utf8");
|
|
36
40
|
utils.writeFile = ({ file, content, index }) => {
|
|
37
41
|
const _file = index !== void 0 ? file[index] : file;
|
|
38
|
-
if (!
|
|
39
|
-
|
|
42
|
+
if (!(0, import_node_fs.existsSync)(_file) || (0, import_node_fs.existsSync)(_file) && !(0, import_node_fs.lstatSync)(_file).isDirectory()) {
|
|
43
|
+
(0, import_node_fs.writeFileSync)(_file, content, "utf8");
|
|
40
44
|
}
|
|
41
45
|
return content;
|
|
42
46
|
};
|
|
43
|
-
utils.deleteFile = (file) =>
|
|
47
|
+
utils.deleteFile = (file) => (0, import_node_fs.unlinkSync)(file);
|
|
44
48
|
utils.buildArgs = (options) => {
|
|
45
49
|
const args = [];
|
|
46
50
|
Object.keys(options).forEach((key) => {
|
|
@@ -55,13 +59,13 @@ utils.buildArgs = (options) => {
|
|
|
55
59
|
};
|
|
56
60
|
utils.clone = (obj) => JSON.parse(JSON.stringify(obj));
|
|
57
61
|
utils.getFilesizeInBytes = (file) => {
|
|
58
|
-
const stats =
|
|
62
|
+
const stats = (0, import_node_fs.statSync)(file);
|
|
59
63
|
const fileSizeInBytes = stats.size;
|
|
60
64
|
return utils.prettyBytes(fileSizeInBytes);
|
|
61
65
|
};
|
|
62
66
|
utils.getFilesizeGzippedInBytes = (file) => {
|
|
63
67
|
return new Promise((resolve) => {
|
|
64
|
-
const source =
|
|
68
|
+
const source = (0, import_node_fs.createReadStream)(file);
|
|
65
69
|
source.pipe(import_gzip_size.default.stream()).on("gzip-size", (size) => {
|
|
66
70
|
resolve(utils.prettyBytes(size));
|
|
67
71
|
});
|
|
@@ -97,28 +101,28 @@ utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
|
|
|
97
101
|
return output.replace("$1", fileWithoutExtension);
|
|
98
102
|
};
|
|
99
103
|
utils.compressSingleFile = (settings) => {
|
|
100
|
-
const content = settings.content ? settings.content : utils.getContentFromFiles(settings.input);
|
|
104
|
+
const content = settings.content ? settings.content : settings.input ? utils.getContentFromFiles(settings.input) : "";
|
|
101
105
|
return settings.sync ? utils.runSync({ settings, content }) : utils.runAsync({ settings, content });
|
|
102
106
|
};
|
|
103
107
|
utils.getContentFromFiles = (input) => {
|
|
104
108
|
if (!Array.isArray(input)) {
|
|
105
|
-
return
|
|
109
|
+
return (0, import_node_fs.readFileSync)(input, "utf8");
|
|
106
110
|
}
|
|
107
111
|
return input.map(
|
|
108
|
-
(path) => !
|
|
112
|
+
(path) => !(0, import_node_fs.existsSync)(path) || (0, import_node_fs.existsSync)(path) && !(0, import_node_fs.lstatSync)(path).isDirectory() ? (0, import_node_fs.readFileSync)(path, "utf8") : ""
|
|
109
113
|
).join("\n");
|
|
110
114
|
};
|
|
111
|
-
utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? settings.compressor({ settings, content, callback: null, index }) :
|
|
115
|
+
utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? typeof settings.compressor === "function" ? settings.compressor({ settings, content, callback: null, index }) : "" : "";
|
|
112
116
|
utils.runAsync = ({ settings, content, index }) => {
|
|
113
117
|
return new Promise((resolve, reject) => {
|
|
114
|
-
settings && typeof settings.compressor !== "string" ? settings.compressor({
|
|
118
|
+
settings && settings.compressor && typeof settings.compressor !== "string" ? settings.compressor({
|
|
115
119
|
settings,
|
|
116
120
|
content,
|
|
117
|
-
callback: (err,
|
|
121
|
+
callback: (err, result) => {
|
|
118
122
|
if (err) {
|
|
119
123
|
return reject(err);
|
|
120
124
|
}
|
|
121
|
-
resolve(
|
|
125
|
+
resolve(result || "");
|
|
122
126
|
},
|
|
123
127
|
index
|
|
124
128
|
}) : null;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport { readFileSync, lstatSync, statSync, existsSync, writeFileSync, unlinkSync, createReadStream } from 'node:fs';\nimport gzipSize from 'gzip-size';\nimport { Dictionary, MinifierOptions, Settings, Options } from '@node-minify/types';\n\ninterface Utils {\n readFile: (file: string) => string;\n writeFile: ({ file, content, index }: WriteFile) => string;\n deleteFile: (file: string) => void;\n buildArgs: (\n options: Options & Dictionary<string | boolean | [] | { url: string } | { filename: string } | undefined>\n ) => any;\n clone: (obj: object) => object;\n getFilesizeInBytes: (file: string) => string;\n getFilesizeGzippedInBytes: (file: string) => Promise<string>;\n prettyBytes: (num: number) => string;\n setFileNameMin: (file: string, output: string, publicFolder: string, replaceInPlace: boolean) => string;\n compressSingleFile: (settings: Settings) => string | Promise<string>;\n getContentFromFiles: (input: string | string[]) => string;\n runSync: ({ settings, content, index }: MinifierOptions) => string;\n runAsync: ({ settings, content, index }: MinifierOptions) => Promise<string>;\n}\n\ninterface WriteFile {\n file: string;\n content: any;\n index?: number;\n}\n\nconst utils = {} as Utils;\n\n/**\n * Read content from file.\n *\n * @param {String} file\n * @returns {String}\n */\nutils.readFile = (file: string) => readFileSync(file, 'utf8');\n\n/**\n * Write content into file.\n *\n * @param {String} file\n * @param {String} content\n * @param {Number} index - index of the file being processed\n * @returns {String}\n */\nutils.writeFile = ({ file, content, index }: WriteFile) => {\n const _file = index !== undefined ? file[index] : file;\n if (!existsSync(_file) || (existsSync(_file) && !lstatSync(_file).isDirectory())) {\n writeFileSync(_file, content, 'utf8');\n }\n\n return content;\n};\n\n/**\n * Delete file.\n *\n * @param {String} file\n * @returns {String}\n */\nutils.deleteFile = (file: string) => unlinkSync(file);\n\n/**\n * Builds arguments array based on an object.\n *\n * @param {Object} options\n * @returns {Array}\n */\nutils.buildArgs = (options: Dictionary<string | boolean | [] | { url: string } | { filename: string } | undefined>) => {\n const args: (string | boolean | [] | { url: string } | { filename: string } | undefined)[] = [];\n Object.keys(options).forEach((key: string) => {\n if (options[key] && options[key] !== false) {\n args.push('--' + key);\n }\n\n if (options[key] && options[key] !== true) {\n args.push(options[key]);\n }\n });\n\n return args;\n};\n\n/**\n * Clone an object.\n *\n * @param {Object} obj\n * @returns {Object}\n */\nutils.clone = (obj: object) => JSON.parse(JSON.stringify(obj));\n\n/**\n * Get the file size in bytes.\n *\n * @returns {String}\n */\nutils.getFilesizeInBytes = (file: string) => {\n const stats = statSync(file);\n const fileSizeInBytes = stats.size;\n return utils.prettyBytes(fileSizeInBytes);\n};\n\n/**\n * Get the gzipped file size in bytes.\n *\n * @returns {Promise.<String>}\n */\nutils.getFilesizeGzippedInBytes = (file: string) => {\n return new Promise(resolve => {\n const source = createReadStream(file);\n source.pipe(gzipSize.stream()).on('gzip-size', size => {\n resolve(utils.prettyBytes(size));\n });\n });\n};\n\n/**\n * Get the size in human readable.\n * From https://github.com/sindresorhus/pretty-bytes\n *\n * @returns {String}\n */\nutils.prettyBytes = (num: number) => {\n const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];\n\n if (!Number.isFinite(num)) {\n throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`);\n }\n\n const neg = num < 0;\n\n if (neg) {\n num = -num;\n }\n\n if (num < 1) {\n return (neg ? '-' : '') + num + ' B';\n }\n\n const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), UNITS.length - 1);\n const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3));\n const unit = UNITS[exponent];\n\n return (neg ? '-' : '') + numStr + ' ' + unit;\n};\n\n/**\n * Set the file name as minified.\n * eg. file.js returns file.min.js\n *\n * @param {String} file\n * @param {String} output\n * @param {String} publicFolder\n * @param {Boolean} replaceInPlace\n * @returns {String}\n */\nutils.setFileNameMin = (file: string, output: string, publicFolder: string, replaceInPlace: boolean) => {\n const filePath = file.substr(0, file.lastIndexOf('/') + 1);\n const fileWithoutPath = file.substr(file.lastIndexOf('/') + 1);\n let fileWithoutExtension = fileWithoutPath.substr(0, fileWithoutPath.lastIndexOf('.'));\n if (publicFolder) {\n fileWithoutExtension = publicFolder + fileWithoutExtension;\n }\n if (replaceInPlace) {\n fileWithoutExtension = filePath + fileWithoutExtension;\n }\n return output.replace('$1', fileWithoutExtension);\n};\n\n/**\n * Compress a single file.\n *\n * @param {Object} settings\n */\nutils.compressSingleFile = (settings: Settings): Promise<string> | string => {\n const content = settings.content ? settings.content : settings.input ? utils.getContentFromFiles(settings.input) : '';\n return settings.sync ? utils.runSync({ settings, content }) : utils.runAsync({ settings, content });\n};\n\n/**\n * Concatenate all input files and get the data.\n *\n * @param {String|Array} input\n * @return {String}\n */\nutils.getContentFromFiles = (input: string | string[]) => {\n if (!Array.isArray(input)) {\n return readFileSync(input, 'utf8');\n }\n\n return input\n .map(path =>\n !existsSync(path) || (existsSync(path) && !lstatSync(path).isDirectory()) ? readFileSync(path, 'utf8') : ''\n )\n .join('\\n');\n};\n\n/**\n * Run compressor in sync.\n *\n * @param {Object} settings\n * @param {String} content\n * @param {Number} index - index of the file being processed\n * @return {String}\n */\nutils.runSync = ({ settings, content, index }: MinifierOptions): string =>\n settings && typeof settings.compressor !== 'string'\n ? typeof settings.compressor === 'function'\n ? settings.compressor({ settings, content, callback: null, index })\n : ''\n : '';\n\n/**\n * Run compressor in async.\n *\n * @param {Object} settings\n * @param {String} content\n * @param {Number} index - index of the file being processed\n * @return {Promise}\n */\nutils.runAsync = ({ settings, content, index }: MinifierOptions): Promise<string> => {\n return new Promise((resolve, reject) => {\n settings && settings.compressor && typeof settings.compressor !== 'string'\n ? settings.compressor({\n settings,\n content,\n callback: (err: unknown, result?: string) => {\n if (err) {\n return reject(err);\n }\n resolve(result || '');\n },\n index\n })\n : null;\n });\n};\n\n/**\n * Expose `utils`.\n */\nexport { utils };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,qBAA2G;AAC3G,uBAAqB;AA2BrB,IAAM,QAAQ,CAAC;AAQf,MAAM,WAAW,CAAC,aAAiB,6BAAa,MAAM,MAAM;AAU5D,MAAM,YAAY,CAAC,EAAE,MAAM,SAAS,MAAM,MAAiB;AACzD,QAAM,QAAQ,UAAU,SAAY,KAAK,KAAK,IAAI;AAClD,MAAI,KAAC,2BAAW,KAAK,SAAM,2BAAW,KAAK,KAAK,KAAC,0BAAU,KAAK,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3G,EACC,KAAK,IAAI;AACd;AAUA,MAAM,UAAU,CAAC,EAAE,UAAU,SAAS,MAAM,MAC1C,YAAY,OAAO,SAAS,eAAe,WACvC,OAAO,SAAS,eAAe,aAC7B,SAAS,WAAW,EAAE,UAAU,SAAS,UAAU,MAAM,MAAM,CAAC,IAChE,KACF;AAUN,MAAM,WAAW,CAAC,EAAE,UAAU,SAAS,MAAM,MAAwC;AACnF,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,gBAAY,SAAS,cAAc,OAAO,SAAS,eAAe,WAC9D,SAAS,WAAW;AAAA,MAClB;AAAA,MACA;AAAA,MACA,UAAU,CAAC,KAAc,WAAoB;AAC3C,YAAI,KAAK;AACP,iBAAO,OAAO,GAAG;AAAA,QACnB;AACA,gBAAQ,UAAU,EAAE;AAAA,MACtB;AAAA,MACA;AAAA,IACF,CAAC,IACD;AAAA,EACN,CAAC;AACH;","names":["gzipSize"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import
|
|
2
|
+
import { readFileSync, lstatSync, statSync, existsSync, writeFileSync, unlinkSync, createReadStream } from "fs";
|
|
3
3
|
import gzipSize from "gzip-size";
|
|
4
4
|
var utils = {};
|
|
5
|
-
utils.readFile = (file) =>
|
|
5
|
+
utils.readFile = (file) => readFileSync(file, "utf8");
|
|
6
6
|
utils.writeFile = ({ file, content, index }) => {
|
|
7
7
|
const _file = index !== void 0 ? file[index] : file;
|
|
8
|
-
if (!
|
|
9
|
-
|
|
8
|
+
if (!existsSync(_file) || existsSync(_file) && !lstatSync(_file).isDirectory()) {
|
|
9
|
+
writeFileSync(_file, content, "utf8");
|
|
10
10
|
}
|
|
11
11
|
return content;
|
|
12
12
|
};
|
|
13
|
-
utils.deleteFile = (file) =>
|
|
13
|
+
utils.deleteFile = (file) => unlinkSync(file);
|
|
14
14
|
utils.buildArgs = (options) => {
|
|
15
15
|
const args = [];
|
|
16
16
|
Object.keys(options).forEach((key) => {
|
|
@@ -25,13 +25,13 @@ utils.buildArgs = (options) => {
|
|
|
25
25
|
};
|
|
26
26
|
utils.clone = (obj) => JSON.parse(JSON.stringify(obj));
|
|
27
27
|
utils.getFilesizeInBytes = (file) => {
|
|
28
|
-
const stats =
|
|
28
|
+
const stats = statSync(file);
|
|
29
29
|
const fileSizeInBytes = stats.size;
|
|
30
30
|
return utils.prettyBytes(fileSizeInBytes);
|
|
31
31
|
};
|
|
32
32
|
utils.getFilesizeGzippedInBytes = (file) => {
|
|
33
33
|
return new Promise((resolve) => {
|
|
34
|
-
const source =
|
|
34
|
+
const source = createReadStream(file);
|
|
35
35
|
source.pipe(gzipSize.stream()).on("gzip-size", (size) => {
|
|
36
36
|
resolve(utils.prettyBytes(size));
|
|
37
37
|
});
|
|
@@ -67,28 +67,28 @@ utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
|
|
|
67
67
|
return output.replace("$1", fileWithoutExtension);
|
|
68
68
|
};
|
|
69
69
|
utils.compressSingleFile = (settings) => {
|
|
70
|
-
const content = settings.content ? settings.content : utils.getContentFromFiles(settings.input);
|
|
70
|
+
const content = settings.content ? settings.content : settings.input ? utils.getContentFromFiles(settings.input) : "";
|
|
71
71
|
return settings.sync ? utils.runSync({ settings, content }) : utils.runAsync({ settings, content });
|
|
72
72
|
};
|
|
73
73
|
utils.getContentFromFiles = (input) => {
|
|
74
74
|
if (!Array.isArray(input)) {
|
|
75
|
-
return
|
|
75
|
+
return readFileSync(input, "utf8");
|
|
76
76
|
}
|
|
77
77
|
return input.map(
|
|
78
|
-
(path) => !
|
|
78
|
+
(path) => !existsSync(path) || existsSync(path) && !lstatSync(path).isDirectory() ? readFileSync(path, "utf8") : ""
|
|
79
79
|
).join("\n");
|
|
80
80
|
};
|
|
81
|
-
utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? settings.compressor({ settings, content, callback: null, index }) :
|
|
81
|
+
utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? typeof settings.compressor === "function" ? settings.compressor({ settings, content, callback: null, index }) : "" : "";
|
|
82
82
|
utils.runAsync = ({ settings, content, index }) => {
|
|
83
83
|
return new Promise((resolve, reject) => {
|
|
84
|
-
settings && typeof settings.compressor !== "string" ? settings.compressor({
|
|
84
|
+
settings && settings.compressor && typeof settings.compressor !== "string" ? settings.compressor({
|
|
85
85
|
settings,
|
|
86
86
|
content,
|
|
87
|
-
callback: (err,
|
|
87
|
+
callback: (err, result) => {
|
|
88
88
|
if (err) {
|
|
89
89
|
return reject(err);
|
|
90
90
|
}
|
|
91
|
-
resolve(
|
|
91
|
+
resolve(result || "");
|
|
92
92
|
},
|
|
93
93
|
index
|
|
94
94
|
}) : null;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport { readFileSync, lstatSync, statSync, existsSync, writeFileSync, unlinkSync, createReadStream } from 'node:fs';\nimport gzipSize from 'gzip-size';\nimport { Dictionary, MinifierOptions, Settings, Options } from '@node-minify/types';\n\ninterface Utils {\n readFile: (file: string) => string;\n writeFile: ({ file, content, index }: WriteFile) => string;\n deleteFile: (file: string) => void;\n buildArgs: (\n options: Options & Dictionary<string | boolean | [] | { url: string } | { filename: string } | undefined>\n ) => any;\n clone: (obj: object) => object;\n getFilesizeInBytes: (file: string) => string;\n getFilesizeGzippedInBytes: (file: string) => Promise<string>;\n prettyBytes: (num: number) => string;\n setFileNameMin: (file: string, output: string, publicFolder: string, replaceInPlace: boolean) => string;\n compressSingleFile: (settings: Settings) => string | Promise<string>;\n getContentFromFiles: (input: string | string[]) => string;\n runSync: ({ settings, content, index }: MinifierOptions) => string;\n runAsync: ({ settings, content, index }: MinifierOptions) => Promise<string>;\n}\n\ninterface WriteFile {\n file: string;\n content: any;\n index?: number;\n}\n\nconst utils = {} as Utils;\n\n/**\n * Read content from file.\n *\n * @param {String} file\n * @returns {String}\n */\nutils.readFile = (file: string) => readFileSync(file, 'utf8');\n\n/**\n * Write content into file.\n *\n * @param {String} file\n * @param {String} content\n * @param {Number} index - index of the file being processed\n * @returns {String}\n */\nutils.writeFile = ({ file, content, index }: WriteFile) => {\n const _file = index !== undefined ? file[index] : file;\n if (!existsSync(_file) || (existsSync(_file) && !lstatSync(_file).isDirectory())) {\n writeFileSync(_file, content, 'utf8');\n }\n\n return content;\n};\n\n/**\n * Delete file.\n *\n * @param {String} file\n * @returns {String}\n */\nutils.deleteFile = (file: string) => unlinkSync(file);\n\n/**\n * Builds arguments array based on an object.\n *\n * @param {Object} options\n * @returns {Array}\n */\nutils.buildArgs = (options: Dictionary<string | boolean | [] | { url: string } | { filename: string } | undefined>) => {\n const args: (string | boolean | [] | { url: string } | { filename: string } | undefined)[] = [];\n Object.keys(options).forEach((key: string) => {\n if (options[key] && options[key] !== false) {\n args.push('--' + key);\n }\n\n if (options[key] && options[key] !== true) {\n args.push(options[key]);\n }\n });\n\n return args;\n};\n\n/**\n * Clone an object.\n *\n * @param {Object} obj\n * @returns {Object}\n */\nutils.clone = (obj: object) => JSON.parse(JSON.stringify(obj));\n\n/**\n * Get the file size in bytes.\n *\n * @returns {String}\n */\nutils.getFilesizeInBytes = (file: string) => {\n const stats = statSync(file);\n const fileSizeInBytes = stats.size;\n return utils.prettyBytes(fileSizeInBytes);\n};\n\n/**\n * Get the gzipped file size in bytes.\n *\n * @returns {Promise.<String>}\n */\nutils.getFilesizeGzippedInBytes = (file: string) => {\n return new Promise(resolve => {\n const source = createReadStream(file);\n source.pipe(gzipSize.stream()).on('gzip-size', size => {\n resolve(utils.prettyBytes(size));\n });\n });\n};\n\n/**\n * Get the size in human readable.\n * From https://github.com/sindresorhus/pretty-bytes\n *\n * @returns {String}\n */\nutils.prettyBytes = (num: number) => {\n const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];\n\n if (!Number.isFinite(num)) {\n throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`);\n }\n\n const neg = num < 0;\n\n if (neg) {\n num = -num;\n }\n\n if (num < 1) {\n return (neg ? '-' : '') + num + ' B';\n }\n\n const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), UNITS.length - 1);\n const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3));\n const unit = UNITS[exponent];\n\n return (neg ? '-' : '') + numStr + ' ' + unit;\n};\n\n/**\n * Set the file name as minified.\n * eg. file.js returns file.min.js\n *\n * @param {String} file\n * @param {String} output\n * @param {String} publicFolder\n * @param {Boolean} replaceInPlace\n * @returns {String}\n */\nutils.setFileNameMin = (file: string, output: string, publicFolder: string, replaceInPlace: boolean) => {\n const filePath = file.substr(0, file.lastIndexOf('/') + 1);\n const fileWithoutPath = file.substr(file.lastIndexOf('/') + 1);\n let fileWithoutExtension = fileWithoutPath.substr(0, fileWithoutPath.lastIndexOf('.'));\n if (publicFolder) {\n fileWithoutExtension = publicFolder + fileWithoutExtension;\n }\n if (replaceInPlace) {\n fileWithoutExtension = filePath + fileWithoutExtension;\n }\n return output.replace('$1', fileWithoutExtension);\n};\n\n/**\n * Compress a single file.\n *\n * @param {Object} settings\n */\nutils.compressSingleFile = (settings: Settings): Promise<string> | string => {\n const content = settings.content ? settings.content : settings.input ? utils.getContentFromFiles(settings.input) : '';\n return settings.sync ? utils.runSync({ settings, content }) : utils.runAsync({ settings, content });\n};\n\n/**\n * Concatenate all input files and get the data.\n *\n * @param {String|Array} input\n * @return {String}\n */\nutils.getContentFromFiles = (input: string | string[]) => {\n if (!Array.isArray(input)) {\n return readFileSync(input, 'utf8');\n }\n\n return input\n .map(path =>\n !existsSync(path) || (existsSync(path) && !lstatSync(path).isDirectory()) ? readFileSync(path, 'utf8') : ''\n )\n .join('\\n');\n};\n\n/**\n * Run compressor in sync.\n *\n * @param {Object} settings\n * @param {String} content\n * @param {Number} index - index of the file being processed\n * @return {String}\n */\nutils.runSync = ({ settings, content, index }: MinifierOptions): string =>\n settings && typeof settings.compressor !== 'string'\n ? typeof settings.compressor === 'function'\n ? settings.compressor({ settings, content, callback: null, index })\n : ''\n : '';\n\n/**\n * Run compressor in async.\n *\n * @param {Object} settings\n * @param {String} content\n * @param {Number} index - index of the file being processed\n * @return {Promise}\n */\nutils.runAsync = ({ settings, content, index }: MinifierOptions): Promise<string> => {\n return new Promise((resolve, reject) => {\n settings && settings.compressor && typeof settings.compressor !== 'string'\n ? settings.compressor({\n settings,\n content,\n callback: (err: unknown, result?: string) => {\n if (err) {\n return reject(err);\n }\n resolve(result || '');\n },\n index\n })\n : null;\n });\n};\n\n/**\n * Expose `utils`.\n */\nexport { utils };\n"],"mappings":";AASA,SAAS,cAAc,WAAW,UAAU,YAAY,eAAe,YAAY,wBAAwB;AAC3G,OAAO,cAAc;AA2BrB,IAAM,QAAQ,CAAC;AAQf,MAAM,WAAW,CAAC,SAAiB,aAAa,MAAM,MAAM;AAU5D,MAAM,YAAY,CAAC,EAAE,MAAM,SAAS,MAAM,MAAiB;AACzD,QAAM,QAAQ,UAAU,SAAY,KAAK,KAAK,IAAI;AAClD,MAAI,CAAC,WAAW,KAAK,KAAM,WAAW,KAAK,KAAK,CAAC,UAAU,KAAK,EAAE,YAAY,GAAI;AAChF,kBAAc,OAAO,SAAS,MAAM;AAAA,EACtC;AAEA,SAAO;AACT;AAQA,MAAM,aAAa,CAAC,SAAiB,WAAW,IAAI;AAQpD,MAAM,YAAY,CAAC,YAAoG;AACrH,QAAM,OAAuF,CAAC;AAC9F,SAAO,KAAK,OAAO,EAAE,QAAQ,CAAC,QAAgB;AAC5C,QAAI,QAAQ,GAAG,KAAK,QAAQ,GAAG,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WAAW;AACtC,gBAAY,SAAS,cAAc,OAAO,SAAS,eAAe,WAC9D,SAAS,WAAW;AAAA,MAClB;AAAA,MACA;AAAA,MACA,UAAU,CAAC,KAAc,WAAoB;AAC3C,YAAI,KAAK;AACP,iBAAO,OAAO,GAAG;AAAA,QACnB;AACA,gBAAQ,UAAU,EAAE;AAAA,MACtB;AAAA,MACA;AAAA,IACF,CAAC,IACD;AAAA,EACN,CAAC;AACH;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-minify/utils",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.3-beta.0",
|
|
4
4
|
"description": "utils for @node-minify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"compressor",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"homepage": "https://github.com/srod/node-minify/tree/master/packages/utils#readme",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
15
|
+
"node": ">=16.0.0"
|
|
16
16
|
},
|
|
17
17
|
"directories": {
|
|
18
18
|
"lib": "dist",
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"clean": "pnpm dlx rimraf dist",
|
|
44
|
-
"build": "
|
|
45
|
-
"prepublishOnly": "
|
|
44
|
+
"build": "pnpm clean && tsup src/index.ts --format cjs,esm --dts --clean --sourcemap",
|
|
45
|
+
"prepublishOnly": "pnpm build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"gzip-size": "6.0.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@node-minify/types": "8.0.
|
|
51
|
+
"@node-minify/types": "8.0.3-beta.0"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "f79e146eed24ca117756cf029eb5005631f12892"
|
|
54
54
|
}
|