@node-minify/utils 8.0.6 → 9.0.1
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/LICENSE +1 -1
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +30 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -13
package/LICENSE
CHANGED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { OptionsPossible, Settings, MinifierOptions } from '@node-minify/types';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* node-minify
|
|
5
|
+
* Copyright(c) 2011-2024 Rodolphe Stoclin
|
|
6
|
+
* MIT Licensed
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
type Utils = {
|
|
10
|
+
readFile: (file: string) => string;
|
|
11
|
+
writeFile: ({ file, content, index }: WriteFile) => string;
|
|
12
|
+
deleteFile: (file: string) => void;
|
|
13
|
+
buildArgs: (options: Record<string, OptionsPossible>) => any;
|
|
14
|
+
clone: (obj: object) => object;
|
|
15
|
+
getFilesizeInBytes: (file: string) => string;
|
|
16
|
+
getFilesizeGzippedInBytes: (file: string) => Promise<string>;
|
|
17
|
+
prettyBytes: (num: number) => string;
|
|
18
|
+
setFileNameMin: (file: string, output: string, publicFolder?: string, replaceInPlace?: boolean) => string;
|
|
19
|
+
compressSingleFile: (settings: Settings) => string | Promise<string>;
|
|
20
|
+
getContentFromFiles: (input: string | string[]) => string;
|
|
21
|
+
runSync: ({ settings, content, index }: MinifierOptions) => string;
|
|
22
|
+
runAsync: ({ settings, content, index, }: MinifierOptions) => Promise<string>;
|
|
23
|
+
};
|
|
24
|
+
type WriteFile = {
|
|
25
|
+
file: string;
|
|
26
|
+
content: any;
|
|
27
|
+
index?: number;
|
|
28
|
+
};
|
|
29
|
+
declare const utils: Utils;
|
|
30
|
+
|
|
31
|
+
export { utils };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { OptionsPossible, Settings, MinifierOptions } from '@node-minify/types';
|
|
|
2
2
|
|
|
3
3
|
/*!
|
|
4
4
|
* node-minify
|
|
5
|
-
* Copyright(c) 2011-
|
|
5
|
+
* Copyright(c) 2011-2024 Rodolphe Stoclin
|
|
6
6
|
* MIT Licensed
|
|
7
7
|
*/
|
|
8
8
|
|
|
@@ -19,7 +19,7 @@ type Utils = {
|
|
|
19
19
|
compressSingleFile: (settings: Settings) => string | Promise<string>;
|
|
20
20
|
getContentFromFiles: (input: string | string[]) => string;
|
|
21
21
|
runSync: ({ settings, content, index }: MinifierOptions) => string;
|
|
22
|
-
runAsync: ({ settings, content, index }: MinifierOptions) => Promise<string>;
|
|
22
|
+
runAsync: ({ settings, content, index, }: MinifierOptions) => Promise<string>;
|
|
23
23
|
};
|
|
24
24
|
type WriteFile = {
|
|
25
25
|
file: string;
|
package/dist/index.js
CHANGED
|
@@ -49,7 +49,7 @@ utils.buildArgs = (options) => {
|
|
|
49
49
|
const args = [];
|
|
50
50
|
Object.keys(options).forEach((key) => {
|
|
51
51
|
if (options[key] && options[key] !== false) {
|
|
52
|
-
args.push(
|
|
52
|
+
args.push(`--${key}`);
|
|
53
53
|
}
|
|
54
54
|
if (options[key] && options[key] !== true) {
|
|
55
55
|
args.push(options[key]);
|
|
@@ -74,24 +74,32 @@ utils.getFilesizeGzippedInBytes = (file) => {
|
|
|
74
74
|
utils.prettyBytes = (num) => {
|
|
75
75
|
const UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
76
76
|
if (!Number.isFinite(num)) {
|
|
77
|
-
throw new TypeError(
|
|
77
|
+
throw new TypeError(
|
|
78
|
+
`Expected a finite number, got ${typeof num}: ${num}`
|
|
79
|
+
);
|
|
78
80
|
}
|
|
79
81
|
const neg = num < 0;
|
|
80
82
|
if (neg) {
|
|
81
83
|
num = -num;
|
|
82
84
|
}
|
|
83
85
|
if (num < 1) {
|
|
84
|
-
return (neg ? "-" : "") + num
|
|
86
|
+
return `${(neg ? "-" : "") + num} B`;
|
|
85
87
|
}
|
|
86
|
-
const exponent = Math.min(
|
|
87
|
-
|
|
88
|
+
const exponent = Math.min(
|
|
89
|
+
Math.floor(Math.log(num) / Math.log(1e3)),
|
|
90
|
+
UNITS.length - 1
|
|
91
|
+
);
|
|
92
|
+
const numStr = Number((num / 1e3 ** exponent).toPrecision(3));
|
|
88
93
|
const unit = UNITS[exponent];
|
|
89
|
-
return (neg ? "-" : "") + numStr
|
|
94
|
+
return `${(neg ? "-" : "") + numStr} ${unit}`;
|
|
90
95
|
};
|
|
91
96
|
utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
|
|
92
97
|
const filePath = file.substr(0, file.lastIndexOf("/") + 1);
|
|
93
98
|
const fileWithoutPath = file.substr(file.lastIndexOf("/") + 1);
|
|
94
|
-
let fileWithoutExtension = fileWithoutPath.substr(
|
|
99
|
+
let fileWithoutExtension = fileWithoutPath.substr(
|
|
100
|
+
0,
|
|
101
|
+
fileWithoutPath.lastIndexOf(".")
|
|
102
|
+
);
|
|
95
103
|
if (publicFolder) {
|
|
96
104
|
fileWithoutExtension = publicFolder + fileWithoutExtension;
|
|
97
105
|
}
|
|
@@ -112,10 +120,21 @@ utils.getContentFromFiles = (input) => {
|
|
|
112
120
|
(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") : ""
|
|
113
121
|
).join("\n");
|
|
114
122
|
};
|
|
115
|
-
utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? typeof settings.compressor === "function" ?
|
|
116
|
-
|
|
123
|
+
utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? typeof settings.compressor === "function" ? String(
|
|
124
|
+
settings.compressor({
|
|
125
|
+
settings,
|
|
126
|
+
content,
|
|
127
|
+
callback: null,
|
|
128
|
+
index
|
|
129
|
+
}) || ""
|
|
130
|
+
) : "" : "";
|
|
131
|
+
utils.runAsync = ({
|
|
132
|
+
settings,
|
|
133
|
+
content,
|
|
134
|
+
index
|
|
135
|
+
}) => {
|
|
117
136
|
return new Promise((resolve, reject) => {
|
|
118
|
-
settings
|
|
137
|
+
settings?.compressor && typeof settings.compressor !== "string" ? settings.compressor({
|
|
119
138
|
settings,
|
|
120
139
|
content,
|
|
121
140
|
callback: (err, result) => {
|
|
@@ -134,7 +153,7 @@ utils.runAsync = ({ settings, content, index }) => {
|
|
|
134
153
|
});
|
|
135
154
|
/*!
|
|
136
155
|
* node-minify
|
|
137
|
-
* Copyright(c) 2011-
|
|
156
|
+
* Copyright(c) 2011-2024 Rodolphe Stoclin
|
|
138
157
|
* MIT Licensed
|
|
139
158
|
*/
|
|
140
159
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2023 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 { MinifierOptions, Settings, OptionsPossible } from '@node-minify/types';\n\ntype Utils = {\n readFile: (file: string) => string;\n writeFile: ({ file, content, index }: WriteFile) => string;\n deleteFile: (file: string) => void;\n buildArgs: (options: Record<string, OptionsPossible>) => 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\ntype 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): 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): string => {\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 */\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: Record<string, OptionsPossible>): OptionsPossible[] => {\n const args: OptionsPossible[] = [];\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): object => JSON.parse(JSON.stringify(obj));\n\n/**\n * Get the file size in bytes.\n *\n * @returns {String}\n */\nutils.getFilesizeInBytes = (file: string): 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): Promise<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): string => {\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): string => {\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[]): 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;AAyBrB,IAAM,QAAQ,CAAC;AAQf,MAAM,WAAW,CAAC,aAAyB,6BAAa,MAAM,MAAM;AAUpE,MAAM,YAAY,CAAC,EAAE,MAAM,SAAS,MAAM,MAAyB;AACjE,QAAM,QAAQ,UAAU,SAAY,KAAK,KAAK,IAAI;AAClD,MAAI,KAAC,2BAAW,KAAK,SAAM,2BAAW,KAAK,KAAK,KAAC,0BAAU,KAAK,EAAE,YAAY,GAAI;AAChF,sCAAc,OAAO,SAAS,MAAM;AAAA,EACtC;AAEA,SAAO;AACT;AAOA,MAAM,aAAa,CAAC,aAAiB,2BAAW,IAAI;AAQpD,MAAM,YAAY,CAAC,YAAgE;AACjF,QAAM,OAA0B,CAAC;AACjC,SAAO,KAAK,OAAO,EAAE,QAAQ,CAAC,QAAgB;AAC5C,QAAI,QAAQ,GAAG,KAAK,QAAQ,GAAG,MAAM,OAAO;AAC1C,WAAK,KAAK,OAAO,GAAG;AAAA,IACtB;AAEA,QAAI,QAAQ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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"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport {\n createReadStream,\n existsSync,\n lstatSync,\n readFileSync,\n statSync,\n unlinkSync,\n writeFileSync,\n} from \"node:fs\";\nimport type {\n MinifierOptions,\n OptionsPossible,\n Settings,\n} from \"@node-minify/types\";\nimport gzipSize from \"gzip-size\";\n\ntype Utils = {\n readFile: (file: string) => string;\n writeFile: ({ file, content, index }: WriteFile) => string;\n deleteFile: (file: string) => void;\n buildArgs: (options: Record<string, OptionsPossible>) => any;\n clone: (obj: object) => object;\n getFilesizeInBytes: (file: string) => string;\n getFilesizeGzippedInBytes: (file: string) => Promise<string>;\n prettyBytes: (num: number) => string;\n setFileNameMin: (\n file: string,\n output: string,\n publicFolder?: string,\n replaceInPlace?: boolean\n ) => string;\n compressSingleFile: (settings: Settings) => string | Promise<string>;\n getContentFromFiles: (input: string | string[]) => string;\n runSync: ({ settings, content, index }: MinifierOptions) => string;\n runAsync: ({\n settings,\n content,\n index,\n }: MinifierOptions) => Promise<string>;\n};\n\ntype WriteFile = {\n file: string;\n content: any;\n index?: number;\n};\n\nconst utils = {} as Utils;\n\n/**\n * Read content from file.\n * @param file File name\n */\nutils.readFile = (file: string): string => readFileSync(file, \"utf8\");\n\n/**\n * Write content into file.\n * @param file File name\n * @param content Content to write\n * @param index Index of the file being processed\n */\nutils.writeFile = ({ file, content, index }: WriteFile): string => {\n const _file = index !== undefined ? file[index] : file;\n if (\n !existsSync(_file) ||\n (existsSync(_file) && !lstatSync(_file).isDirectory())\n ) {\n writeFileSync(_file, content, \"utf8\");\n }\n\n return content;\n};\n\n/**\n * Delete file.\n * @param file File name\n */\nutils.deleteFile = (file: string) => unlinkSync(file);\n\n/**\n * Builds arguments array based on an object.\n * @param options\n */\nutils.buildArgs = (\n options: Record<string, OptionsPossible>\n): OptionsPossible[] => {\n const args: OptionsPossible[] = [];\n Object.keys(options).forEach((key: string) => {\n if (options[key] && (options[key] as unknown) !== 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 * @param obj Object\n */\nutils.clone = (obj: object): object => JSON.parse(JSON.stringify(obj));\n\n/**\n * Get the file size in bytes.\n * @param file File name\n */\nutils.getFilesizeInBytes = (file: string): 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 * @param file File name\n */\nutils.getFilesizeGzippedInBytes = (file: string): Promise<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 * @param num Number\n */\nutils.prettyBytes = (num: number): string => {\n const UNITS = [\"B\", \"kB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"];\n\n if (!Number.isFinite(num)) {\n throw new TypeError(\n `Expected a finite number, got ${typeof num}: ${num}`\n );\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(\n Math.floor(Math.log(num) / Math.log(1000)),\n UNITS.length - 1\n );\n const numStr = Number((num / 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 * @param file File name\n * @param output Output file name\n * @param publicFolder Public folder\n * @param replaceInPlace Replace in place\n */\nutils.setFileNameMin = (\n file: string,\n output: string,\n publicFolder?: string,\n replaceInPlace?: boolean\n): string => {\n const filePath = file.substr(0, file.lastIndexOf(\"/\") + 1);\n const fileWithoutPath = file.substr(file.lastIndexOf(\"/\") + 1);\n let fileWithoutExtension = fileWithoutPath.substr(\n 0,\n fileWithoutPath.lastIndexOf(\".\")\n );\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 * @param settings Settings\n */\nutils.compressSingleFile = (settings: Settings): Promise<string> | string => {\n const content = settings.content\n ? settings.content\n : settings.input\n ? utils.getContentFromFiles(settings.input)\n : \"\";\n return settings.sync\n ? utils.runSync({ settings, content })\n : utils.runAsync({ settings, content });\n};\n\n/**\n * Concatenate all input files and get the data.\n * @param input Input files\n */\nutils.getContentFromFiles = (input: string | string[]): string => {\n if (!Array.isArray(input)) {\n return readFileSync(input, \"utf8\");\n }\n\n return input\n .map((path) =>\n !existsSync(path) ||\n (existsSync(path) && !lstatSync(path).isDirectory())\n ? readFileSync(path, \"utf8\")\n : \"\"\n )\n .join(\"\\n\");\n};\n\n/**\n * Run compressor in sync.\n * @param settings Settings\n * @param content Content to minify\n * @param index Index of the file being processed\n */\nutils.runSync = ({ settings, content, index }: MinifierOptions): string =>\n settings && typeof settings.compressor !== \"string\"\n ? typeof settings.compressor === \"function\"\n ? String(\n settings.compressor({\n settings,\n content,\n callback: null,\n index,\n }) || \"\"\n )\n : \"\"\n : \"\";\n\n/**\n * Run compressor in async.\n * @param settings Settings\n * @param content Content to minify\n * @param index Index of the file being processed\n */\nutils.runAsync = ({\n settings,\n content,\n index,\n}: MinifierOptions): Promise<string> => {\n return new Promise((resolve, reject) => {\n 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,qBAQO;AAMP,uBAAqB;AAiCrB,IAAM,QAAQ,CAAC;AAMf,MAAM,WAAW,CAAC,aAAyB,6BAAa,MAAM,MAAM;AAQpE,MAAM,YAAY,CAAC,EAAE,MAAM,SAAS,MAAM,MAAyB;AAC/D,QAAM,QAAQ,UAAU,SAAY,KAAK,KAAK,IAAI;AAClD,MACI,KAAC,2BAAW,KAAK,SAChB,2BAAW,KAAK,KAAK,KAAC,0BAAU,KAAK,EAAE,YAAY,GACtD;AACE,sCAAc,OAAO,SAAS,MAAM;AAAA,EACxC;AAEA,SAAO;AACX;AAMA,MAAM,aAAa,CAAC,aAAiB,2BAAW,IAAI;AAMpD,MAAM,YAAY,CACd,YACoB;AACpB,QAAM,OAA0B,CAAC;AACjC,SAAO,KAAK,OAAO,EAAE,QAAQ,CAAC,QAAgB;AAC1C,QAAI,QAAQ,GAAG,KAAM,QAAQ,GAAG,MAAkB,OAAO;AACrD,WAAK,KAAK,KAAK,GAAG,EAAE;AAAA,IACxB;AAEA,QAAI,QAAQ,GAAG,KAAK,QAAQ,GAAG,MAAM,MAAM;AACvC,WAAK,KAAK,QAAQ,GAAG,CAAC;AAAA,IAC1B;AAAA,EACJ,CAAC;AAED,SAAO;AACX;AAMA,MAAM,QAAQ,CAAC,QAAwB,KAAK,MAAM,KAAK,UAAU,GAAG,CAAC;AAMrE,MAAM,qBAAqB,CAAC,SAAyB;AACjD,QAAM,YAAQ,yBAAS,IAAI;AAC3B,QAAM,kBAAkB,MAAM;AAC9B,SAAO,MAAM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A,EACV,CAAC;AACL;","names":["gzipSize"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
createReadStream,
|
|
4
|
+
existsSync,
|
|
5
|
+
lstatSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
statSync,
|
|
8
|
+
unlinkSync,
|
|
9
|
+
writeFileSync
|
|
10
|
+
} from "node:fs";
|
|
3
11
|
import gzipSize from "gzip-size";
|
|
4
12
|
var utils = {};
|
|
5
13
|
utils.readFile = (file) => readFileSync(file, "utf8");
|
|
@@ -15,7 +23,7 @@ utils.buildArgs = (options) => {
|
|
|
15
23
|
const args = [];
|
|
16
24
|
Object.keys(options).forEach((key) => {
|
|
17
25
|
if (options[key] && options[key] !== false) {
|
|
18
|
-
args.push(
|
|
26
|
+
args.push(`--${key}`);
|
|
19
27
|
}
|
|
20
28
|
if (options[key] && options[key] !== true) {
|
|
21
29
|
args.push(options[key]);
|
|
@@ -40,24 +48,32 @@ utils.getFilesizeGzippedInBytes = (file) => {
|
|
|
40
48
|
utils.prettyBytes = (num) => {
|
|
41
49
|
const UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
42
50
|
if (!Number.isFinite(num)) {
|
|
43
|
-
throw new TypeError(
|
|
51
|
+
throw new TypeError(
|
|
52
|
+
`Expected a finite number, got ${typeof num}: ${num}`
|
|
53
|
+
);
|
|
44
54
|
}
|
|
45
55
|
const neg = num < 0;
|
|
46
56
|
if (neg) {
|
|
47
57
|
num = -num;
|
|
48
58
|
}
|
|
49
59
|
if (num < 1) {
|
|
50
|
-
return (neg ? "-" : "") + num
|
|
60
|
+
return `${(neg ? "-" : "") + num} B`;
|
|
51
61
|
}
|
|
52
|
-
const exponent = Math.min(
|
|
53
|
-
|
|
62
|
+
const exponent = Math.min(
|
|
63
|
+
Math.floor(Math.log(num) / Math.log(1e3)),
|
|
64
|
+
UNITS.length - 1
|
|
65
|
+
);
|
|
66
|
+
const numStr = Number((num / 1e3 ** exponent).toPrecision(3));
|
|
54
67
|
const unit = UNITS[exponent];
|
|
55
|
-
return (neg ? "-" : "") + numStr
|
|
68
|
+
return `${(neg ? "-" : "") + numStr} ${unit}`;
|
|
56
69
|
};
|
|
57
70
|
utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
|
|
58
71
|
const filePath = file.substr(0, file.lastIndexOf("/") + 1);
|
|
59
72
|
const fileWithoutPath = file.substr(file.lastIndexOf("/") + 1);
|
|
60
|
-
let fileWithoutExtension = fileWithoutPath.substr(
|
|
73
|
+
let fileWithoutExtension = fileWithoutPath.substr(
|
|
74
|
+
0,
|
|
75
|
+
fileWithoutPath.lastIndexOf(".")
|
|
76
|
+
);
|
|
61
77
|
if (publicFolder) {
|
|
62
78
|
fileWithoutExtension = publicFolder + fileWithoutExtension;
|
|
63
79
|
}
|
|
@@ -78,10 +94,21 @@ utils.getContentFromFiles = (input) => {
|
|
|
78
94
|
(path) => !existsSync(path) || existsSync(path) && !lstatSync(path).isDirectory() ? readFileSync(path, "utf8") : ""
|
|
79
95
|
).join("\n");
|
|
80
96
|
};
|
|
81
|
-
utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? typeof settings.compressor === "function" ?
|
|
82
|
-
|
|
97
|
+
utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? typeof settings.compressor === "function" ? String(
|
|
98
|
+
settings.compressor({
|
|
99
|
+
settings,
|
|
100
|
+
content,
|
|
101
|
+
callback: null,
|
|
102
|
+
index
|
|
103
|
+
}) || ""
|
|
104
|
+
) : "" : "";
|
|
105
|
+
utils.runAsync = ({
|
|
106
|
+
settings,
|
|
107
|
+
content,
|
|
108
|
+
index
|
|
109
|
+
}) => {
|
|
83
110
|
return new Promise((resolve, reject) => {
|
|
84
|
-
settings
|
|
111
|
+
settings?.compressor && typeof settings.compressor !== "string" ? settings.compressor({
|
|
85
112
|
settings,
|
|
86
113
|
content,
|
|
87
114
|
callback: (err, result) => {
|
|
@@ -99,7 +126,7 @@ export {
|
|
|
99
126
|
};
|
|
100
127
|
/*!
|
|
101
128
|
* node-minify
|
|
102
|
-
* Copyright(c) 2011-
|
|
129
|
+
* Copyright(c) 2011-2024 Rodolphe Stoclin
|
|
103
130
|
* MIT Licensed
|
|
104
131
|
*/
|
|
105
132
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2023 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 { MinifierOptions, Settings, OptionsPossible } from '@node-minify/types';\n\ntype Utils = {\n readFile: (file: string) => string;\n writeFile: ({ file, content, index }: WriteFile) => string;\n deleteFile: (file: string) => void;\n buildArgs: (options: Record<string, OptionsPossible>) => 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\ntype 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): 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): string => {\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 */\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: Record<string, OptionsPossible>): OptionsPossible[] => {\n const args: OptionsPossible[] = [];\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): object => JSON.parse(JSON.stringify(obj));\n\n/**\n * Get the file size in bytes.\n *\n * @returns {String}\n */\nutils.getFilesizeInBytes = (file: string): 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): Promise<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): string => {\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): string => {\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[]): 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;AAyBrB,IAAM,QAAQ,CAAC;AAQf,MAAM,WAAW,CAAC,SAAyB,aAAa,MAAM,MAAM;AAUpE,MAAM,YAAY,CAAC,EAAE,MAAM,SAAS,MAAM,MAAyB;AACjE,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;AAOA,MAAM,aAAa,CAAC,SAAiB,WAAW,IAAI;AAQpD,MAAM,YAAY,CAAC,YAAgE;AACjF,QAAM,OAA0B,CAAC;AACjC,SAAO,KAAK,OAAO,EAAE,QAAQ,CAAC,QAAgB;AAC5C,QAAI,QAAQ,GAAG,KAAK,QAAQ,GAAG,MAAM,OAAO;AAC1C,WAAK,KAAK,OAAO,GAAG;AAAA,IACtB;AAEA,QAAI,QAAQ,GAAG,KAAK,QAAQ,GAAG,MAAM,MAAM;AACzC,WAAK,KAAK,QAAQ,GAAG,CAAC;AAAA,IACxB;AAAA,EACF,CAAC;AAED,SAAO;AACT;AAQA,MAAM,QAAQ,CAAC,QAAwB,KAAK,MAAM,KAAK,UAAU,GAAG,CAAC;AAOrE,MAAM,qBAAqB,CAAC,SAAyB;AACnD,QAAM,QAAQ,SAAS,IAAI;AAC3B,QAAM,kBAAkB,MAAM;AAC9B,SAAO,MAAM,YAAY,eA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licensed\n */\n\n/**\n * Module dependencies.\n */\nimport {\n createReadStream,\n existsSync,\n lstatSync,\n readFileSync,\n statSync,\n unlinkSync,\n writeFileSync,\n} from \"node:fs\";\nimport type {\n MinifierOptions,\n OptionsPossible,\n Settings,\n} from \"@node-minify/types\";\nimport gzipSize from \"gzip-size\";\n\ntype Utils = {\n readFile: (file: string) => string;\n writeFile: ({ file, content, index }: WriteFile) => string;\n deleteFile: (file: string) => void;\n buildArgs: (options: Record<string, OptionsPossible>) => any;\n clone: (obj: object) => object;\n getFilesizeInBytes: (file: string) => string;\n getFilesizeGzippedInBytes: (file: string) => Promise<string>;\n prettyBytes: (num: number) => string;\n setFileNameMin: (\n file: string,\n output: string,\n publicFolder?: string,\n replaceInPlace?: boolean\n ) => string;\n compressSingleFile: (settings: Settings) => string | Promise<string>;\n getContentFromFiles: (input: string | string[]) => string;\n runSync: ({ settings, content, index }: MinifierOptions) => string;\n runAsync: ({\n settings,\n content,\n index,\n }: MinifierOptions) => Promise<string>;\n};\n\ntype WriteFile = {\n file: string;\n content: any;\n index?: number;\n};\n\nconst utils = {} as Utils;\n\n/**\n * Read content from file.\n * @param file File name\n */\nutils.readFile = (file: string): string => readFileSync(file, \"utf8\");\n\n/**\n * Write content into file.\n * @param file File name\n * @param content Content to write\n * @param index Index of the file being processed\n */\nutils.writeFile = ({ file, content, index }: WriteFile): string => {\n const _file = index !== undefined ? file[index] : file;\n if (\n !existsSync(_file) ||\n (existsSync(_file) && !lstatSync(_file).isDirectory())\n ) {\n writeFileSync(_file, content, \"utf8\");\n }\n\n return content;\n};\n\n/**\n * Delete file.\n * @param file File name\n */\nutils.deleteFile = (file: string) => unlinkSync(file);\n\n/**\n * Builds arguments array based on an object.\n * @param options\n */\nutils.buildArgs = (\n options: Record<string, OptionsPossible>\n): OptionsPossible[] => {\n const args: OptionsPossible[] = [];\n Object.keys(options).forEach((key: string) => {\n if (options[key] && (options[key] as unknown) !== 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 * @param obj Object\n */\nutils.clone = (obj: object): object => JSON.parse(JSON.stringify(obj));\n\n/**\n * Get the file size in bytes.\n * @param file File name\n */\nutils.getFilesizeInBytes = (file: string): 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 * @param file File name\n */\nutils.getFilesizeGzippedInBytes = (file: string): Promise<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 * @param num Number\n */\nutils.prettyBytes = (num: number): string => {\n const UNITS = [\"B\", \"kB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"];\n\n if (!Number.isFinite(num)) {\n throw new TypeError(\n `Expected a finite number, got ${typeof num}: ${num}`\n );\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(\n Math.floor(Math.log(num) / Math.log(1000)),\n UNITS.length - 1\n );\n const numStr = Number((num / 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 * @param file File name\n * @param output Output file name\n * @param publicFolder Public folder\n * @param replaceInPlace Replace in place\n */\nutils.setFileNameMin = (\n file: string,\n output: string,\n publicFolder?: string,\n replaceInPlace?: boolean\n): string => {\n const filePath = file.substr(0, file.lastIndexOf(\"/\") + 1);\n const fileWithoutPath = file.substr(file.lastIndexOf(\"/\") + 1);\n let fileWithoutExtension = fileWithoutPath.substr(\n 0,\n fileWithoutPath.lastIndexOf(\".\")\n );\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 * @param settings Settings\n */\nutils.compressSingleFile = (settings: Settings): Promise<string> | string => {\n const content = settings.content\n ? settings.content\n : settings.input\n ? utils.getContentFromFiles(settings.input)\n : \"\";\n return settings.sync\n ? utils.runSync({ settings, content })\n : utils.runAsync({ settings, content });\n};\n\n/**\n * Concatenate all input files and get the data.\n * @param input Input files\n */\nutils.getContentFromFiles = (input: string | string[]): string => {\n if (!Array.isArray(input)) {\n return readFileSync(input, \"utf8\");\n }\n\n return input\n .map((path) =>\n !existsSync(path) ||\n (existsSync(path) && !lstatSync(path).isDirectory())\n ? readFileSync(path, \"utf8\")\n : \"\"\n )\n .join(\"\\n\");\n};\n\n/**\n * Run compressor in sync.\n * @param settings Settings\n * @param content Content to minify\n * @param index Index of the file being processed\n */\nutils.runSync = ({ settings, content, index }: MinifierOptions): string =>\n settings && typeof settings.compressor !== \"string\"\n ? typeof settings.compressor === \"function\"\n ? String(\n settings.compressor({\n settings,\n content,\n callback: null,\n index,\n }) || \"\"\n )\n : \"\"\n : \"\";\n\n/**\n * Run compressor in async.\n * @param settings Settings\n * @param content Content to minify\n * @param index Index of the file being processed\n */\nutils.runAsync = ({\n settings,\n content,\n index,\n}: MinifierOptions): Promise<string> => {\n return new Promise((resolve, reject) => {\n 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;AAAA,EACI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AAMP,OAAO,cAAc;A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QAAQ,CAAC;AAC9C;AAMA,MAAM,sBAAsB,CAAC,UAAqC;AAC9D,MAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;AACvB,WAAO,aAAa,OAAO,MAAM;AAAA,EACrC;AAEA,SAAO,MACF;AAAA,IAAI,CAAC,SACF,CAAC,WAAW,IAAI,KACf,WAAW,IAAI,KAAK,CAAC,UAAU,IAAI,EAAE,YAAY,IAC5C,aAAa,MAAM,MAAM,IACzB;AAAA,EACV,EACC,KAAK,IAAI;AAClB;AAQA,MAAM,UAAU,CAAC,EAAE,UAAU,SAAS,MAAM,MACxC,YAAY,OAAO,SAAS,eAAe,WACrC,OAAO,SAAS,eAAe,aAC3B;AAAA,EACI,SAAS,WAAW;AAAA,IAChB;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV;AAAA,EACJ,CAAC,KAAK;AACV,IACA,KACJ;AAQV,MAAM,WAAW,CAAC;AAAA,EACd;AAAA,EACA;AAAA,EACA;AACJ,MAAwC;AACpC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACpC,cAAU,cAAc,OAAO,SAAS,eAAe,WACjD,SAAS,WAAW;AAAA,MAChB;AAAA,MACA;AAAA,MACA,UAAU,CAAC,KAAc,WAAoB;AACzC,YAAI,KAAK;AACL,iBAAO,OAAO,GAAG;AAAA,QACrB;AACA,gBAAQ,UAAU,EAAE;AAAA,MACxB;AAAA,MACA;AAAA,IACJ,CAAC,IACD;AAAA,EACV,CAAC;AACL;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-minify/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.1",
|
|
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": ">=18.0.0"
|
|
16
16
|
},
|
|
17
17
|
"directories": {
|
|
18
18
|
"lib": "dist",
|
|
@@ -22,9 +22,11 @@
|
|
|
22
22
|
"module": "./dist/index.mjs",
|
|
23
23
|
"types": "./dist/index.d.ts",
|
|
24
24
|
"exports": {
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.mjs",
|
|
28
|
+
"require": "./dist/index.js"
|
|
29
|
+
}
|
|
28
30
|
},
|
|
29
31
|
"files": [
|
|
30
32
|
"dist/**/*"
|
|
@@ -39,16 +41,18 @@
|
|
|
39
41
|
"bugs": {
|
|
40
42
|
"url": "https://github.com/srod/node-minify/issues"
|
|
41
43
|
},
|
|
42
|
-
"scripts": {
|
|
43
|
-
"clean": "pnpm dlx rimraf dist",
|
|
44
|
-
"build": "pnpm clean && tsup src/index.ts --format cjs,esm --dts --clean --sourcemap",
|
|
45
|
-
"prepublishOnly": "pnpm build"
|
|
46
|
-
},
|
|
47
44
|
"dependencies": {
|
|
48
45
|
"gzip-size": "6.0.0"
|
|
49
46
|
},
|
|
50
47
|
"devDependencies": {
|
|
51
|
-
"@node-minify/types": "
|
|
48
|
+
"@node-minify/types": "9.0.0"
|
|
52
49
|
},
|
|
53
|
-
"
|
|
54
|
-
|
|
50
|
+
"scripts": {
|
|
51
|
+
"clean": "pnpm dlx rimraf dist",
|
|
52
|
+
"build": "pnpm clean && tsup src/index.ts --format cjs,esm --dts --clean --sourcemap",
|
|
53
|
+
"lint": "biome lint .",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"test:ci": "vitest run --coverage",
|
|
56
|
+
"test:watch": "vitest"
|
|
57
|
+
}
|
|
58
|
+
}
|