@node-minify/utils 10.4.0 → 10.5.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/buildArgs.d.ts +0 -1
- package/dist/buildArgs.d.ts.map +1 -1
- package/dist/buildArgs.js +1 -1
- package/dist/compressSingleFile.d.ts +1 -2
- package/dist/compressSingleFile.d.ts.map +1 -1
- package/dist/compressSingleFile.js +1 -1
- package/dist/compressSingleFile.js.map +1 -1
- package/dist/compressor-resolver.d.ts +1 -2
- package/dist/compressor-resolver.d.ts.map +1 -1
- package/dist/deleteFile.d.ts.map +1 -1
- package/dist/deleteFile.js +1 -1
- package/dist/deprecation.d.ts.map +1 -1
- package/dist/ensureStringContent.d.ts.map +1 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +31 -2
- package/dist/error.js.map +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/getContentFromFiles.d.ts.map +1 -1
- package/dist/getContentFromFiles.js +2 -2
- package/dist/getFilesizeBrotliInBytes.d.ts.map +1 -1
- package/dist/getFilesizeBrotliInBytes.js +2 -2
- package/dist/getFilesizeGzippedInBytes.d.ts.map +1 -1
- package/dist/getFilesizeGzippedInBytes.js +2 -2
- package/dist/getFilesizeInBytes.d.ts.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +5 -4
- package/dist/isImageFile.d.ts.map +1 -1
- package/dist/isValidFile.d.ts.map +1 -1
- package/dist/isValidFile.js +45 -2
- package/dist/isValidFile.js.map +1 -0
- package/dist/prettyBytes.d.ts.map +1 -1
- package/dist/prettyBytes.js +1 -1
- package/dist/readFile.d.ts.map +1 -1
- package/dist/readFile.js +1 -1
- package/dist/run.d.ts +1 -2
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +25 -24
- package/dist/run.js.map +1 -1
- package/dist/setFileNameMin.d.ts.map +1 -1
- package/dist/setFileNameMin.js +15 -9
- package/dist/setFileNameMin.js.map +1 -1
- package/dist/setPublicFolder.d.ts +1 -0
- package/dist/setPublicFolder.d.ts.map +1 -1
- package/dist/setPublicFolder.js +6 -1
- package/dist/setPublicFolder.js.map +1 -1
- package/dist/sourceMap.d.ts +27 -0
- package/dist/sourceMap.d.ts.map +1 -0
- package/dist/sourceMap.js +37 -0
- package/dist/sourceMap.js.map +1 -0
- package/dist/{types-CWBFD3au.d.ts → types-D10QejTN.d.ts} +1 -21
- package/dist/types-D10QejTN.d.ts.map +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/wildcards.d.ts +21 -3
- package/dist/wildcards.d.ts.map +1 -1
- package/dist/wildcards.js +40 -16
- package/dist/wildcards.js.map +1 -1
- package/dist/writeFile.d.ts.map +1 -1
- package/dist/writeFile.js +95 -2
- package/dist/writeFile.js.map +1 -0
- package/package.json +2 -2
- package/dist/error-CUgKxOvI.js +0 -32
- package/dist/error-CUgKxOvI.js.map +0 -1
- package/dist/isValidFile-COstpeyW.js +0 -46
- package/dist/isValidFile-COstpeyW.js.map +0 -1
- package/dist/types-CWBFD3au.d.ts.map +0 -1
- package/dist/writeFile-mfUS3rMz.js +0 -96
- package/dist/writeFile-mfUS3rMz.js.map +0 -1
package/dist/buildArgs.d.ts
CHANGED
package/dist/buildArgs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildArgs.d.ts","names":[],"sources":["../src/buildArgs.ts"],"
|
|
1
|
+
{"version":3,"file":"buildArgs.d.ts","names":[],"sources":["../src/buildArgs.ts"],"mappings":";;;;;;;;;iBAegB,kBAAA,CACZ,OAAA,EAAS,MAAA,oBACV,gBAAA;;;;;;;;;;iBAwBa,SAAA,CAAU,OAAA,EAAS,gBAAA"}
|
package/dist/buildArgs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compressSingleFile.d.ts","names":[],"sources":["../src/compressSingleFile.ts"],"
|
|
1
|
+
{"version":3,"file":"compressSingleFile.d.ts","names":[],"sources":["../src/compressSingleFile.ts"],"mappings":";;;;;;;;;iBAsBsB,kBAAA,WACR,iBAAA,GAAoB,iBAAA,CAAA,CAChC,QAAA,EAAU,QAAA,CAAS,CAAA,IAAK,OAAA"}
|
|
@@ -24,7 +24,7 @@ async function compressSingleFile(settings) {
|
|
|
24
24
|
* @throws Error - If `settings.input` is an array that mixes image and non-image file paths.
|
|
25
25
|
*/
|
|
26
26
|
async function determineContent(settings) {
|
|
27
|
-
if (settings.content) return settings.content;
|
|
27
|
+
if (settings.content !== void 0) return settings.content;
|
|
28
28
|
if (settings.input && Array.isArray(settings.input)) {
|
|
29
29
|
const imageFilesCount = settings.input.filter((file) => isImageFile(file)).length;
|
|
30
30
|
if (imageFilesCount > 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compressSingleFile.js","names":[],"sources":["../src/compressSingleFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright (c) 2011-2026 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport type {\n CompressorOptions,\n MinifierOptions,\n Settings,\n} from \"@node-minify/types\";\nimport { getContentFromFilesAsync } from \"./getContentFromFiles.ts\";\nimport { isImageFile } from \"./isImageFile.ts\";\nimport { readFileAsync } from \"./readFile.ts\";\nimport { run } from \"./run.ts\";\n\n/**\n * Compress a single file using the provided settings.\n *\n * @param settings - Configuration that specifies input content or input path(s) and compressor options\n * @returns The compressed output as a string\n */\nexport async function compressSingleFile<\n T extends CompressorOptions = CompressorOptions,\n>(settings: Settings<T>): Promise<string> {\n const content = await determineContent(settings);\n return run({ settings, content } as MinifierOptions<T>);\n}\n\n/**\n * Resolve the content to be minified from the provided settings.\n *\n * @param settings - Settings that may contain `content` or `input` (string or string[]); `content` is used preferentially.\n * @returns The resolved content: a `string` for text input, a `Buffer` for a single image file, or a `Buffer[]` for multiple image files.\n * @throws Error - If `settings.input` is an array that mixes image and non-image file paths.\n */\nasync function determineContent<\n T extends CompressorOptions = CompressorOptions,\n>(settings: Settings<T>): Promise<string | Buffer | Buffer[]> {\n if (settings.content) {\n return settings.content;\n }\n\n if (settings.input && Array.isArray(settings.input)) {\n const imageFilesCount = settings.input.filter((file) =>\n isImageFile(file)\n ).length;\n if (imageFilesCount > 0) {\n if (imageFilesCount !== settings.input.length) {\n throw new Error(\n \"Cannot mix image and text files in the same input array\"\n );\n }\n const firstInput = settings.input[0];\n if (settings.input.length === 1 && firstInput) {\n return await readFileAsync(firstInput, true);\n }\n return await Promise.all(\n settings.input.map((file) => readFileAsync(file, true))\n );\n }\n }\n\n if (settings.input && typeof settings.input === \"string\") {\n if (isImageFile(settings.input)) {\n return await readFileAsync(settings.input, true);\n }\n }\n\n if (settings.input) {\n return await getContentFromFilesAsync(settings.input);\n }\n\n return \"\";\n}\n"],"mappings":";;;;;;;;;;;;AAsBA,eAAsB,mBAEpB,UAAwC;AAEtC,QAAO,IAAI;EAAE;EAAU,SADP,MAAM,iBAAiB,SAAS;EAChB,CAAuB;;;;;;;;;AAU3D,eAAe,iBAEb,UAA4D;AAC1D,KAAI,SAAS,
|
|
1
|
+
{"version":3,"file":"compressSingleFile.js","names":[],"sources":["../src/compressSingleFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright (c) 2011-2026 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport type {\n CompressorOptions,\n MinifierOptions,\n Settings,\n} from \"@node-minify/types\";\nimport { getContentFromFilesAsync } from \"./getContentFromFiles.ts\";\nimport { isImageFile } from \"./isImageFile.ts\";\nimport { readFileAsync } from \"./readFile.ts\";\nimport { run } from \"./run.ts\";\n\n/**\n * Compress a single file using the provided settings.\n *\n * @param settings - Configuration that specifies input content or input path(s) and compressor options\n * @returns The compressed output as a string\n */\nexport async function compressSingleFile<\n T extends CompressorOptions = CompressorOptions,\n>(settings: Settings<T>): Promise<string> {\n const content = await determineContent(settings);\n return run({ settings, content } as MinifierOptions<T>);\n}\n\n/**\n * Resolve the content to be minified from the provided settings.\n *\n * @param settings - Settings that may contain `content` or `input` (string or string[]); `content` is used preferentially.\n * @returns The resolved content: a `string` for text input, a `Buffer` for a single image file, or a `Buffer[]` for multiple image files.\n * @throws Error - If `settings.input` is an array that mixes image and non-image file paths.\n */\nasync function determineContent<\n T extends CompressorOptions = CompressorOptions,\n>(settings: Settings<T>): Promise<string | Buffer | Buffer[]> {\n if (settings.content !== undefined) {\n return settings.content;\n }\n\n if (settings.input && Array.isArray(settings.input)) {\n const imageFilesCount = settings.input.filter((file) =>\n isImageFile(file)\n ).length;\n if (imageFilesCount > 0) {\n if (imageFilesCount !== settings.input.length) {\n throw new Error(\n \"Cannot mix image and text files in the same input array\"\n );\n }\n const firstInput = settings.input[0];\n if (settings.input.length === 1 && firstInput) {\n return await readFileAsync(firstInput, true);\n }\n return await Promise.all(\n settings.input.map((file) => readFileAsync(file, true))\n );\n }\n }\n\n if (settings.input && typeof settings.input === \"string\") {\n if (isImageFile(settings.input)) {\n return await readFileAsync(settings.input, true);\n }\n }\n\n if (settings.input) {\n return await getContentFromFilesAsync(settings.input);\n }\n\n return \"\";\n}\n"],"mappings":";;;;;;;;;;;;AAsBA,eAAsB,mBAEpB,UAAwC;AAEtC,QAAO,IAAI;EAAE;EAAU,SADP,MAAM,iBAAiB,SAAS;EAChB,CAAuB;;;;;;;;;AAU3D,eAAe,iBAEb,UAA4D;AAC1D,KAAI,SAAS,YAAY,OACrB,QAAO,SAAS;AAGpB,KAAI,SAAS,SAAS,MAAM,QAAQ,SAAS,MAAM,EAAE;EACjD,MAAM,kBAAkB,SAAS,MAAM,QAAQ,SAC3C,YAAY,KAAK,CACpB,CAAC;AACF,MAAI,kBAAkB,GAAG;AACrB,OAAI,oBAAoB,SAAS,MAAM,OACnC,OAAM,IAAI,MACN,0DACH;GAEL,MAAM,aAAa,SAAS,MAAM;AAClC,OAAI,SAAS,MAAM,WAAW,KAAK,WAC/B,QAAO,MAAM,cAAc,YAAY,KAAK;AAEhD,UAAO,MAAM,QAAQ,IACjB,SAAS,MAAM,KAAK,SAAS,cAAc,MAAM,KAAK,CAAC,CAC1D;;;AAIT,KAAI,SAAS,SAAS,OAAO,SAAS,UAAU,UAC5C;MAAI,YAAY,SAAS,MAAM,CAC3B,QAAO,MAAM,cAAc,SAAS,OAAO,KAAK;;AAIxD,KAAI,SAAS,MACT,QAAO,MAAM,yBAAyB,SAAS,MAAM;AAGzD,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compressor-resolver.d.ts","names":[],"sources":["../src/compressor-resolver.ts"],"
|
|
1
|
+
{"version":3,"file":"compressor-resolver.d.ts","names":[],"sources":["../src/compressor-resolver.ts"],"mappings":";;;;;;KA0CY,oBAAA;EAcR;;;EAVA,UAAA,EAAY,UAAA;EAmBW;;;EAdvB,KAAA;EA6FkB;;;EAxFlB,SAAA;AAAA;;;;;AA2HJ;;iBAlHgB,WAAA,CAAY,IAAA;;;;;;;iBA+EN,iBAAA,CAClB,IAAA,WACD,OAAA,CAAQ,oBAAA;;;;;;;;iBAiCW,oBAAA,CAClB,IAAA,WACD,OAAA,CAAQ,oBAAA;AAkFX;;;;;;;AAAA,iBA9CsB,mBAAA,CAClB,IAAA,WACD,OAAA,CAAQ,oBAAA;;AAkFX;;;;;AAUA;iBAhDsB,iBAAA,CAClB,IAAA,WACD,OAAA,CAAQ,oBAAA;;;;;;;iBAoCK,mBAAA,CAAoB,IAAA;;;;;;;iBAUpB,kBAAA,CAAmB,IAAA"}
|
package/dist/deleteFile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deleteFile.d.ts","names":[],"sources":["../src/deleteFile.ts"],"
|
|
1
|
+
{"version":3,"file":"deleteFile.d.ts","names":[],"sources":["../src/deleteFile.ts"],"mappings":";;AAgBA;;;;;;;;;;;iBAAgB,UAAA,CAAW,IAAA"}
|
package/dist/deleteFile.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deprecation.d.ts","names":[],"sources":["../src/deprecation.ts"],"
|
|
1
|
+
{"version":3,"file":"deprecation.d.ts","names":[],"sources":["../src/deprecation.ts"],"mappings":";;AAwBA;;;;;AAaA;;;;;;;;;;;;;;;iBAbgB,eAAA,CAAgB,WAAA,UAAqB,OAAA;;;;;iBAarC,wBAAA,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ensureStringContent.d.ts","names":[],"sources":["../src/ensureStringContent.ts"],"
|
|
1
|
+
{"version":3,"file":"ensureStringContent.d.ts","names":[],"sources":["../src/ensureStringContent.ts"],"mappings":";;AAcA;;;;;;;;;;;;iBAAgB,mBAAA,CACZ,OAAA,WAAkB,MAAA,GAAS,MAAA,gBAC3B,cAAA"}
|
package/dist/error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","names":[],"sources":["../src/error.ts"],"
|
|
1
|
+
{"version":3,"file":"error.d.ts","names":[],"sources":["../src/error.ts"],"mappings":";;AAUA;;;;;;;;cAAa,kBAAA,SAA2B,KAAA;EAC3B,KAAA,GAAQ,KAAA;cAEL,SAAA,UAAmB,IAAA,UAAc,aAAA,GAAgB,KAAA;AAAA;;;;;cAiBpD,eAAA,SAAwB,KAAA;cACrB,OAAA;AAAA"}
|
package/dist/error.js
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/error.ts
|
|
2
|
+
/*!
|
|
3
|
+
* node-minify
|
|
4
|
+
* Copyright (c) 2011-2026 Rodolphe Stoclin
|
|
5
|
+
* MIT Licensed
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Error class for file operation failures.
|
|
9
|
+
* @extends Error
|
|
10
|
+
*/
|
|
11
|
+
var FileOperationError = class extends Error {
|
|
12
|
+
cause;
|
|
13
|
+
constructor(operation, path, originalError) {
|
|
14
|
+
super(`Failed to ${operation} file ${path}: ${originalError?.message || ""}`, originalError ? { cause: originalError } : void 0);
|
|
15
|
+
this.name = "FileOperationError";
|
|
16
|
+
if (originalError && !this.cause) this.cause = originalError;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Error class for validation failures.
|
|
21
|
+
* @extends Error
|
|
22
|
+
*/
|
|
23
|
+
var ValidationError = class extends Error {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "ValidationError";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
2
29
|
|
|
3
|
-
|
|
30
|
+
//#endregion
|
|
31
|
+
export { FileOperationError, ValidationError };
|
|
32
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","names":[],"sources":["../src/error.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright (c) 2011-2026 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Error class for file operation failures.\n * @extends Error\n */\nexport class FileOperationError extends Error {\n override cause?: Error;\n\n constructor(operation: string, path: string, originalError?: Error) {\n super(\n `Failed to ${operation} file ${path}: ${originalError?.message || \"\"}`,\n originalError ? { cause: originalError } : undefined\n );\n this.name = \"FileOperationError\";\n // For older runtimes that don't support the cause option\n if (originalError && !this.cause) {\n this.cause = originalError;\n }\n }\n}\n\n/**\n * Error class for validation failures.\n * @extends Error\n */\nexport class ValidationError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"ValidationError\";\n }\n}\n"],"mappings":";;;;;;;;;;AAUA,IAAa,qBAAb,cAAwC,MAAM;CAC1C,AAAS;CAET,YAAY,WAAmB,MAAc,eAAuB;AAChE,QACI,aAAa,UAAU,QAAQ,KAAK,IAAI,eAAe,WAAW,MAClE,gBAAgB,EAAE,OAAO,eAAe,GAAG,OAC9C;AACD,OAAK,OAAO;AAEZ,MAAI,iBAAiB,CAAC,KAAK,MACvB,MAAK,QAAQ;;;;;;;AASzB,IAAa,kBAAb,cAAqC,MAAM;CACvC,YAAY,SAAiB;AACzB,QAAM,QAAQ;AACd,OAAK,OAAO"}
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","names":[],"sources":["../src/errors.ts"],"
|
|
1
|
+
{"version":3,"file":"errors.d.ts","names":[],"sources":["../src/errors.ts"],"mappings":";;AAaA;;;;;;;;;AAkBA;;iBAlBgB,qBAAA,CACZ,cAAA,UACA,KAAA,YACD,KAAA;;;;;;;;iBAea,oBAAA,CACZ,MAAA,WACA,cAAA,mBACO,MAAA;EAAY,IAAA;AAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getContentFromFiles.d.ts","names":[],"sources":["../src/getContentFromFiles.ts"],"
|
|
1
|
+
{"version":3,"file":"getContentFromFiles.d.ts","names":[],"sources":["../src/getContentFromFiles.ts"],"mappings":";;AAuDA;;;;;AAiCA;;;;;;;;iBAjCgB,mBAAA,CAAoB,KAAA;;;;;;;;;iBAiCd,wBAAA,CAClB,KAAA,sBACD,OAAA"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { FileOperationError } from "./error.js";
|
|
2
|
+
import { isValidFile } from "./isValidFile.js";
|
|
3
3
|
import { existsSync, readFileSync } from "node:fs";
|
|
4
4
|
import { readFile } from "node:fs/promises";
|
|
5
5
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getFilesizeBrotliInBytes.d.ts","names":[],"sources":["../src/getFilesizeBrotliInBytes.ts"],"
|
|
1
|
+
{"version":3,"file":"getFilesizeBrotliInBytes.d.ts","names":[],"sources":["../src/getFilesizeBrotliInBytes.ts"],"mappings":";;AAyDA;;;;;AAyBA;;;;;iBAzBsB,wBAAA,CAAyB,IAAA,WAAe,OAAA;;;;;;;;;;iBAyBxC,oBAAA,CAAqB,IAAA,WAAe,OAAA"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { FileOperationError } from "./error.js";
|
|
2
|
+
import { isValidFile } from "./isValidFile.js";
|
|
3
3
|
import { prettyBytes } from "./prettyBytes.js";
|
|
4
4
|
import { existsSync } from "node:fs";
|
|
5
5
|
import { readFile } from "node:fs/promises";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getFilesizeGzippedInBytes.d.ts","names":[],"sources":["../src/getFilesizeGzippedInBytes.ts"],"
|
|
1
|
+
{"version":3,"file":"getFilesizeGzippedInBytes.d.ts","names":[],"sources":["../src/getFilesizeGzippedInBytes.ts"],"mappings":";;AAgDA;;;;;AAwBA;;;;;iBAxBsB,yBAAA,CAA0B,IAAA,WAAe,OAAA;;;;;;;;;iBAwBzC,qBAAA,CAAsB,IAAA,WAAe,OAAA"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { FileOperationError } from "./error.js";
|
|
2
|
+
import { isValidFile } from "./isValidFile.js";
|
|
3
3
|
import { prettyBytes } from "./prettyBytes.js";
|
|
4
4
|
import { existsSync } from "node:fs";
|
|
5
5
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getFilesizeInBytes.d.ts","names":[],"sources":["../src/getFilesizeInBytes.ts"],"
|
|
1
|
+
{"version":3,"file":"getFilesizeInBytes.d.ts","names":[],"sources":["../src/getFilesizeInBytes.ts"],"mappings":";;AAgBA;;;;;;;;;;;cAAa,kBAAA,GAAsB,IAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { readFile, readFileAsync } from "./readFile.js";
|
|
|
17
17
|
import { run } from "./run.js";
|
|
18
18
|
import { setFileNameMin } from "./setFileNameMin.js";
|
|
19
19
|
import { setPublicFolder } from "./setPublicFolder.js";
|
|
20
|
-
import {
|
|
20
|
+
import { extractSourceMapOption, getSourceMapBoolean } from "./sourceMap.js";
|
|
21
|
+
import { DEFAULT_IGNORES, WildcardOptions, wildcards } from "./wildcards.js";
|
|
21
22
|
import { writeFile, writeFileAsync } from "./writeFile.js";
|
|
22
|
-
export { type BuildArgsOptions, type CompressorResolution, buildArgs, compressSingleFile, deleteFile, ensureStringContent, getContentFromFiles, getContentFromFilesAsync, getFilesizeBrotliInBytes, getFilesizeBrotliRaw, getFilesizeGzippedInBytes, getFilesizeGzippedRaw, getFilesizeInBytes, getKnownExportName, isBuiltInCompressor, isImageFile, isLocalPath, isValidFile, isValidFileAsync, prettyBytes, readFile, readFileAsync, resetDeprecationWarnings, resolveCompressor, run, setFileNameMin, setPublicFolder, toBuildArgsOptions, tryResolveBuiltIn, tryResolveLocalFile, tryResolveNpmPackage, validateMinifyResult, warnDeprecation, wildcards, wrapMinificationError, writeFile, writeFileAsync };
|
|
23
|
+
export { type BuildArgsOptions, type CompressorResolution, DEFAULT_IGNORES, type WildcardOptions, buildArgs, compressSingleFile, deleteFile, ensureStringContent, extractSourceMapOption, getContentFromFiles, getContentFromFilesAsync, getFilesizeBrotliInBytes, getFilesizeBrotliRaw, getFilesizeGzippedInBytes, getFilesizeGzippedRaw, getFilesizeInBytes, getKnownExportName, getSourceMapBoolean, isBuiltInCompressor, isImageFile, isLocalPath, isValidFile, isValidFileAsync, prettyBytes, readFile, readFileAsync, resetDeprecationWarnings, resolveCompressor, run, setFileNameMin, setPublicFolder, toBuildArgsOptions, tryResolveBuiltIn, tryResolveLocalFile, tryResolveNpmPackage, validateMinifyResult, warnDeprecation, wildcards, wrapMinificationError, writeFile, writeFileAsync };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { buildArgs, toBuildArgsOptions } from "./buildArgs.js";
|
|
2
|
-
import {
|
|
2
|
+
import { isValidFile, isValidFileAsync } from "./isValidFile.js";
|
|
3
3
|
import { getContentFromFiles, getContentFromFilesAsync } from "./getContentFromFiles.js";
|
|
4
4
|
import { isImageFile } from "./isImageFile.js";
|
|
5
5
|
import { readFile, readFileAsync } from "./readFile.js";
|
|
6
|
-
import {
|
|
6
|
+
import { writeFile, writeFileAsync } from "./writeFile.js";
|
|
7
7
|
import { run } from "./run.js";
|
|
8
8
|
import { compressSingleFile } from "./compressSingleFile.js";
|
|
9
9
|
import { getKnownExportName, isBuiltInCompressor, isLocalPath, resolveCompressor, tryResolveBuiltIn, tryResolveLocalFile, tryResolveNpmPackage } from "./compressor-resolver.js";
|
|
@@ -17,6 +17,7 @@ import { getFilesizeGzippedInBytes, getFilesizeGzippedRaw } from "./getFilesizeG
|
|
|
17
17
|
import { getFilesizeInBytes } from "./getFilesizeInBytes.js";
|
|
18
18
|
import { setFileNameMin } from "./setFileNameMin.js";
|
|
19
19
|
import { setPublicFolder } from "./setPublicFolder.js";
|
|
20
|
-
import {
|
|
20
|
+
import { extractSourceMapOption, getSourceMapBoolean } from "./sourceMap.js";
|
|
21
|
+
import { DEFAULT_IGNORES, wildcards } from "./wildcards.js";
|
|
21
22
|
|
|
22
|
-
export { buildArgs, compressSingleFile, deleteFile, ensureStringContent, getContentFromFiles, getContentFromFilesAsync, getFilesizeBrotliInBytes, getFilesizeBrotliRaw, getFilesizeGzippedInBytes, getFilesizeGzippedRaw, getFilesizeInBytes, getKnownExportName, isBuiltInCompressor, isImageFile, isLocalPath, isValidFile, isValidFileAsync, prettyBytes, readFile, readFileAsync, resetDeprecationWarnings, resolveCompressor, run, setFileNameMin, setPublicFolder, toBuildArgsOptions, tryResolveBuiltIn, tryResolveLocalFile, tryResolveNpmPackage, validateMinifyResult, warnDeprecation, wildcards, wrapMinificationError, writeFile, writeFileAsync };
|
|
23
|
+
export { DEFAULT_IGNORES, buildArgs, compressSingleFile, deleteFile, ensureStringContent, extractSourceMapOption, getContentFromFiles, getContentFromFilesAsync, getFilesizeBrotliInBytes, getFilesizeBrotliRaw, getFilesizeGzippedInBytes, getFilesizeGzippedRaw, getFilesizeInBytes, getKnownExportName, getSourceMapBoolean, isBuiltInCompressor, isImageFile, isLocalPath, isValidFile, isValidFileAsync, prettyBytes, readFile, readFileAsync, resetDeprecationWarnings, resolveCompressor, run, setFileNameMin, setPublicFolder, toBuildArgsOptions, tryResolveBuiltIn, tryResolveLocalFile, tryResolveNpmPackage, validateMinifyResult, warnDeprecation, wildcards, wrapMinificationError, writeFile, writeFileAsync };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isImageFile.d.ts","names":[],"sources":["../src/isImageFile.ts"],"
|
|
1
|
+
{"version":3,"file":"isImageFile.d.ts","names":[],"sources":["../src/isImageFile.ts"],"mappings":";;AA0BA;;;;;;;;;;iBAAgB,WAAA,CAAY,QAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isValidFile.d.ts","names":[],"sources":["../src/isValidFile.ts"],"
|
|
1
|
+
{"version":3,"file":"isValidFile.d.ts","names":[],"sources":["../src/isValidFile.ts"],"mappings":";;AAoBA;;;;;AAeA;;;;;;;;;iBAfgB,WAAA,CAAY,IAAA;;;;;;;;iBAeN,gBAAA,CAAiB,IAAA,WAAe,OAAA"}
|
package/dist/isValidFile.js
CHANGED
|
@@ -1,3 +1,46 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FileOperationError } from "./error.js";
|
|
2
|
+
import { existsSync, lstatSync } from "node:fs";
|
|
3
|
+
import { lstat } from "node:fs/promises";
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
//#region src/isValidFile.ts
|
|
6
|
+
/*!
|
|
7
|
+
* node-minify
|
|
8
|
+
* Copyright (c) 2011-2026 Rodolphe Stoclin
|
|
9
|
+
* MIT Licensed
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Check if the path is a valid file.
|
|
13
|
+
* @param path Path to check
|
|
14
|
+
* @returns true if path exists and is a file, false otherwise
|
|
15
|
+
* @throws {FileOperationError} If filesystem operations fail
|
|
16
|
+
* @example
|
|
17
|
+
* if (isValidFile('path/to/file.js')) {
|
|
18
|
+
* // do something
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
function isValidFile(path) {
|
|
22
|
+
try {
|
|
23
|
+
return existsSync(path) && !lstatSync(path).isDirectory();
|
|
24
|
+
} catch (error) {
|
|
25
|
+
throw new FileOperationError("validate", path, error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Determine whether a filesystem path refers to an existing file (not a directory).
|
|
30
|
+
*
|
|
31
|
+
* @param path - Path to check
|
|
32
|
+
* @returns `true` if the path exists and is a file, `false` otherwise.
|
|
33
|
+
* @throws {FileOperationError} If a filesystem error other than `ENOENT` occurs while validating the path.
|
|
34
|
+
*/
|
|
35
|
+
async function isValidFileAsync(path) {
|
|
36
|
+
try {
|
|
37
|
+
return !(await lstat(path)).isDirectory();
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return false;
|
|
40
|
+
throw new FileOperationError("validate", path, error instanceof Error ? error : new Error(String(error)));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
export { isValidFile, isValidFileAsync };
|
|
46
|
+
//# sourceMappingURL=isValidFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isValidFile.js","names":[],"sources":["../src/isValidFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright (c) 2011-2026 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { existsSync, lstatSync } from \"node:fs\";\nimport { lstat } from \"node:fs/promises\";\nimport { FileOperationError } from \"./error.ts\";\n\n/**\n * Check if the path is a valid file.\n * @param path Path to check\n * @returns true if path exists and is a file, false otherwise\n * @throws {FileOperationError} If filesystem operations fail\n * @example\n * if (isValidFile('path/to/file.js')) {\n * // do something\n * }\n */\nexport function isValidFile(path: string): boolean {\n try {\n return existsSync(path) && !lstatSync(path).isDirectory();\n } catch (error) {\n throw new FileOperationError(\"validate\", path, error as Error);\n }\n}\n\n/**\n * Determine whether a filesystem path refers to an existing file (not a directory).\n *\n * @param path - Path to check\n * @returns `true` if the path exists and is a file, `false` otherwise.\n * @throws {FileOperationError} If a filesystem error other than `ENOENT` occurs while validating the path.\n */\nexport async function isValidFileAsync(path: string): Promise<boolean> {\n try {\n const stats = await lstat(path);\n return !stats.isDirectory();\n } catch (error: unknown) {\n if (\n error &&\n typeof error === \"object\" &&\n \"code\" in error &&\n error.code === \"ENOENT\"\n ) {\n return false;\n }\n throw new FileOperationError(\n \"validate\",\n path,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,YAAY,MAAuB;AAC/C,KAAI;AACA,SAAO,WAAW,KAAK,IAAI,CAAC,UAAU,KAAK,CAAC,aAAa;UACpD,OAAO;AACZ,QAAM,IAAI,mBAAmB,YAAY,MAAM,MAAe;;;;;;;;;;AAWtE,eAAsB,iBAAiB,MAAgC;AACnE,KAAI;AAEA,SAAO,EADO,MAAM,MAAM,KAAK,EACjB,aAAa;UACtB,OAAgB;AACrB,MACI,SACA,OAAO,UAAU,YACjB,UAAU,SACV,MAAM,SAAS,SAEf,QAAO;AAEX,QAAM,IAAI,mBACN,YACA,MACA,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,MAAM,CAAC,CAC5D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prettyBytes.d.ts","names":[],"sources":["../src/prettyBytes.ts"],"
|
|
1
|
+
{"version":3,"file":"prettyBytes.d.ts","names":[],"sources":["../src/prettyBytes.ts"],"mappings":";;AAoBA;;;;;;;;;;;;;;iBAAgB,WAAA,CAAY,GAAA"}
|
package/dist/prettyBytes.js
CHANGED
package/dist/readFile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readFile.d.ts","names":[],"sources":["../src/readFile.ts"],"
|
|
1
|
+
{"version":3,"file":"readFile.d.ts","names":[],"sources":["../src/readFile.ts"],"mappings":";;AAuBA;;;;;AACA;;;;;;;;;AACA;;;iBAFgB,QAAA,CAAS,IAAA;AAAA,iBACT,QAAA,CAAS,IAAA,UAAc,QAAA,SAAiB,MAAA;AAAA,iBACxC,QAAA,CAAS,IAAA,UAAc,QAAA;AAAA,iBACvB,QAAA,CAAS,IAAA,UAAc,QAAA,sBAA8B,MAAA;;;;;;;;iBA4B/C,aAAA,CAAc,IAAA,WAAe,OAAA;AAAA,iBAC7B,aAAA,CAClB,IAAA,UACA,QAAA,SACD,OAAA,CAAQ,MAAA;AAAA,iBACW,aAAA,CAClB,IAAA,UACA,QAAA,UACD,OAAA;AAAA,iBACmB,aAAA,CAClB,IAAA,UACA,QAAA,aACD,OAAA,UAAiB,MAAA"}
|
package/dist/readFile.js
CHANGED
package/dist/run.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { n as CompressorOptions, r as MinifierOptions } from "./types-
|
|
1
|
+
import { n as CompressorOptions, r as MinifierOptions } from "./types-D10QejTN.js";
|
|
2
2
|
|
|
3
3
|
//#region src/run.d.ts
|
|
4
|
-
|
|
5
4
|
/**
|
|
6
5
|
* Execute the configured compressor and persist its outputs according to the provided settings.
|
|
7
6
|
*
|
package/dist/run.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.ts","names":[],"sources":["../src/run.ts"],"
|
|
1
|
+
{"version":3,"file":"run.d.ts","names":[],"sources":["../src/run.ts"],"mappings":";;;;;;;;;;;;iBAyBsB,GAAA,WAAc,iBAAA,GAAoB,iBAAA,CAAA,CAAA;EACpD,QAAA;EACA,OAAA;EACA;AAAA,GACD,eAAA,CAAgB,CAAA,IAAK,OAAA"}
|
package/dist/run.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ValidationError } from "./error.js";
|
|
2
|
+
import { writeFileAsync } from "./writeFile.js";
|
|
3
3
|
import { join, parse } from "node:path";
|
|
4
4
|
|
|
5
5
|
//#region src/run.ts
|
|
@@ -51,7 +51,7 @@ function validateCompressorResult(result, settings) {
|
|
|
51
51
|
* @param index - Optional batch/index marker forwarded to underlying write operations
|
|
52
52
|
*/
|
|
53
53
|
async function writeOutput(result, settings, index) {
|
|
54
|
-
if (
|
|
54
|
+
if (settings.content !== void 0 || !settings.output) return;
|
|
55
55
|
const hasBuffer = result.buffer && result.buffer.length > 0;
|
|
56
56
|
const hasOutputs = result.outputs && result.outputs.length > 0;
|
|
57
57
|
const isCompletelyEmpty = !(result.code !== "") && !hasBuffer && !hasOutputs;
|
|
@@ -84,14 +84,18 @@ async function writeOutput(result, settings, index) {
|
|
|
84
84
|
await Promise.all(writePromises);
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
|
-
* Extract
|
|
87
|
+
* Extract an input file path from the input configuration.
|
|
88
88
|
*
|
|
89
89
|
* @param input - A single file path, an array of paths, or undefined
|
|
90
|
-
* @
|
|
90
|
+
* @param index - Optional index of the current input being processed when `input` is an array
|
|
91
|
+
* @returns The indexed input file path when available, otherwise the first input file path or an empty string
|
|
91
92
|
*/
|
|
92
|
-
function
|
|
93
|
+
function getInputFile(input, index) {
|
|
93
94
|
if (typeof input === "string") return input;
|
|
94
|
-
if (Array.isArray(input) && input.length > 0)
|
|
95
|
+
if (Array.isArray(input) && input.length > 0) {
|
|
96
|
+
if (index !== void 0) return input[index] ?? "";
|
|
97
|
+
return input[0] ?? "";
|
|
98
|
+
}
|
|
95
99
|
return "";
|
|
96
100
|
}
|
|
97
101
|
/**
|
|
@@ -105,28 +109,25 @@ function getFirstInputFile(input) {
|
|
|
105
109
|
*/
|
|
106
110
|
async function writeMultipleOutputs(outputs, settings, index) {
|
|
107
111
|
const output = settings.output;
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
const inputFile = getFirstInputFile(settings.input);
|
|
112
|
+
const perFormatOutputs = Array.isArray(output) && index === void 0;
|
|
113
|
+
const inputFile = getInputFile(settings.input, index);
|
|
111
114
|
const inputDir = parse(inputFile).dir;
|
|
112
|
-
const
|
|
115
|
+
const baseName = parse(inputFile).name || "output";
|
|
116
|
+
const currentOutput = Array.isArray(output) && index !== void 0 ? output[index] : output;
|
|
117
|
+
const withFormatExtension = (file, format) => file.endsWith(`.${format}`) ? file : `${file}.${format}`;
|
|
113
118
|
const promises = outputs.map(async (outputResult, i) => {
|
|
114
119
|
if (!outputResult) return;
|
|
115
120
|
const format = outputResult.format || "out";
|
|
116
121
|
let targetFile;
|
|
117
|
-
const arrayItem =
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
else {
|
|
127
|
-
const baseName = inputBase || "output";
|
|
128
|
-
targetFile = inputDir ? join(inputDir, `${baseName}.${format}`) : `${baseName}.${format}`;
|
|
129
|
-
}
|
|
122
|
+
const arrayItem = perFormatOutputs ? output[i] : void 0;
|
|
123
|
+
if (perFormatOutputs && arrayItem !== void 0 && arrayItem !== "$1" && arrayItem.trim() !== "") if (arrayItem.includes("$1")) {
|
|
124
|
+
const outputFile = arrayItem.replace(/\$1/g, baseName);
|
|
125
|
+
targetFile = parse(outputFile).ext ? outputFile : withFormatExtension(outputFile, format);
|
|
126
|
+
} else targetFile = parse(arrayItem).ext ? arrayItem : withFormatExtension(arrayItem, format);
|
|
127
|
+
else if (typeof currentOutput === "string" && currentOutput === "$1") targetFile = inputDir ? join(inputDir, `${baseName}.${format}`) : `${baseName}.${format}`;
|
|
128
|
+
else if (typeof currentOutput === "string" && currentOutput.includes("$1")) targetFile = withFormatExtension(currentOutput.replace(/\$1/g, baseName), format);
|
|
129
|
+
else if (typeof currentOutput === "string" && currentOutput.trim() !== "") targetFile = withFormatExtension(currentOutput, format);
|
|
130
|
+
else targetFile = inputDir ? join(inputDir, `${baseName}.${format}`) : `${baseName}.${format}`;
|
|
130
131
|
await writeFileAsync({
|
|
131
132
|
file: targetFile,
|
|
132
133
|
content: outputResult.content,
|
package/dist/run.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.js","names":[],"sources":["../src/run.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright (c) 2011-2026 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { join, parse } from \"node:path\";\nimport type {\n CompressorOptions,\n CompressorResult,\n MinifierOptions,\n Settings,\n} from \"@node-minify/types\";\nimport { ValidationError } from \"./error.ts\";\nimport { writeFileAsync } from \"./writeFile.ts\";\n\n/**\n * Execute the configured compressor and persist its outputs according to the provided settings.\n *\n * @param settings - Compressor settings including output targets, options, and the `compressor` implementation\n * @param content - The input content to be compressed\n * @param index - Optional index used when processing multiple inputs\n * @returns The minified code produced by the compressor\n * @throws ValidationError If `settings` is missing, `settings.compressor` is not provided, or the compressor returns an invalid result\n */\nexport async function run<T extends CompressorOptions = CompressorOptions>({\n settings,\n content,\n index,\n}: MinifierOptions<T>): Promise<string> {\n if (!settings) {\n throw new ValidationError(\"Settings must be provided\");\n }\n\n if (!settings.compressor) {\n throw new ValidationError(\"Compressor must be provided in settings\");\n }\n\n const result = await settings.compressor({\n settings,\n content,\n index,\n });\n\n validateCompressorResult(result, settings);\n\n await writeOutput(result, settings, index);\n\n return result.code;\n}\n\n/**\n * Verify that a compressor result is an object containing a string `code` property and narrow its type to `CompressorResult`.\n *\n * @param result - The value returned by the compressor to validate.\n * @param settings - Minifier settings (used to derive the compressor label for error messages).\n * @throws ValidationError - If `result` is not an object with a string `code` property.\n */\nfunction validateCompressorResult<\n T extends CompressorOptions = CompressorOptions,\n>(result: unknown, settings: Settings<T>): asserts result is CompressorResult {\n if (!result || typeof result !== \"object\") {\n const label = settings.compressorLabel || \"compressor\";\n throw new ValidationError(\n `Compressor '${label}' returned invalid result. Expected an object with { code: string }.`\n );\n }\n\n const obj = result as Record<string, unknown>;\n if (!(\"code\" in obj) || typeof obj.code !== \"string\") {\n const label = settings.compressorLabel || \"compressor\";\n throw new ValidationError(\n `Compressor '${label}' must return { code: string }. Got: ${JSON.stringify(Object.keys(obj))}`\n );\n }\n}\n\n/**\n * Write compressor outputs to configured output paths unless execution is in-memory.\n *\n * Writes multiple outputs when `result.outputs` is present, writes `result.buffer` if provided, otherwise writes `result.code`. If `result.map` exists and a source map URL can be resolved from `settings`, the map file is written alongside the main output. The function returns without writing when `settings.output` is not set, when `settings.content` is provided (in-memory mode), or when `settings.allowEmptyOutput` is true and `result.code` is an empty string.\n *\n * @param result - Compressor result containing `code` and optional `buffer`, `map`, or `outputs`\n * @param settings - Settings that determine output destinations and in-memory mode\n * @param index - Optional batch/index marker forwarded to underlying write operations\n */\nasync function writeOutput<T extends CompressorOptions = CompressorOptions>(\n result: CompressorResult,\n settings: Settings<T>,\n index?: number\n): Promise<void> {\n const isInMemoryMode = Boolean(settings.content);\n if (isInMemoryMode || !settings.output) {\n return;\n }\n\n const hasBuffer = result.buffer && result.buffer.length > 0;\n const hasOutputs = result.outputs && result.outputs.length > 0;\n const hasCode = result.code !== \"\";\n const isCompletelyEmpty = !hasCode && !hasBuffer && !hasOutputs;\n if (settings.allowEmptyOutput && isCompletelyEmpty) {\n return;\n }\n\n // Handle multi-output (for image conversion to multiple formats)\n if (result.outputs && result.outputs.length > 0) {\n await writeMultipleOutputs(result.outputs, settings, index);\n return;\n }\n\n // Handle single buffer output (for binary images)\n if (result.buffer) {\n await writeFileAsync({\n file: settings.output,\n content: result.buffer,\n index,\n });\n return;\n }\n\n // Default: write code (string) output\n const writePromises = [\n writeFileAsync({\n file: settings.output,\n content: result.code,\n index,\n }),\n ];\n\n if (result.map) {\n const sourceMapUrl = getSourceMapUrl(settings);\n if (sourceMapUrl) {\n writePromises.push(\n writeFileAsync({\n file: sourceMapUrl,\n content: result.map,\n index,\n })\n );\n }\n }\n\n await Promise.all(writePromises);\n}\n\n/**\n * Extract the first input file path from the input configuration.\n *\n * @param input - A single file path, an array of paths, or undefined\n * @returns The first input file path, or an empty string if none available\n */\nfunction getFirstInputFile(input: string | string[] | undefined): string {\n if (typeof input === \"string\") {\n return input;\n }\n if (Array.isArray(input) && input.length > 0) {\n return input[0] ?? \"\";\n }\n return \"\";\n}\n\n/**\n * Write compressor outputs to files resolved from the provided settings.\n *\n * Resolves a target path for each output entry based on settings.output and settings.input, then writes each entry's content to its resolved file location.\n *\n * @param outputs - Array of compressor output entries (each entry typically contains `content` and optional `format`) to be written.\n * @param settings - Settings used to resolve target paths (may supply `output` pattern/array and `input` for deriving names).\n * @param index - Optional numeric index forwarded to the file writer for each write operation.\n */\nasync function writeMultipleOutputs<\n T extends CompressorOptions = CompressorOptions,\n>(\n outputs: NonNullable<CompressorResult[\"outputs\"]>,\n settings: Settings<T>,\n index?: number\n): Promise<void> {\n const output = settings.output;\n const isArrayOutput = Array.isArray(output);\n const outputsArray = isArrayOutput ? output : [output];\n const inputFile = getFirstInputFile(settings.input);\n const inputDir = parse(inputFile).dir;\n const inputBase = parse(inputFile).name;\n\n const promises = outputs.map(async (outputResult, i) => {\n if (!outputResult) {\n return;\n }\n\n // Write outputs in parallel to improve performance\n const format = outputResult.format || \"out\";\n let targetFile: string;\n\n const arrayItem = outputsArray[i];\n\n if (\n isArrayOutput &&\n arrayItem !== undefined &&\n arrayItem !== \"$1\" &&\n arrayItem.trim() !== \"\"\n ) {\n // Explicit output path provided in array - use as-is\n targetFile = arrayItem;\n } else if (typeof output === \"string\" && output === \"$1\") {\n // $1 only: auto-generate from input filename in input directory\n const baseName = inputBase || \"output\";\n targetFile = inputDir\n ? join(inputDir, `${baseName}.${format}`)\n : `${baseName}.${format}`;\n } else if (typeof output === \"string\" && output.includes(\"$1\")) {\n // $1 pattern in path: replace and append format\n const extensionlessName = inputBase || \"output\";\n const outputFile = output.replace(/\\$1/g, extensionlessName);\n targetFile = `${outputFile}.${format}`;\n } else if (typeof output === \"string\") {\n // Single string output: append format extension\n targetFile = `${output}.${format}`;\n } else {\n // Fallback\n const baseName = inputBase || \"output\";\n targetFile = inputDir\n ? join(inputDir, `${baseName}.${format}`)\n : `${baseName}.${format}`;\n }\n\n await writeFileAsync({\n file: targetFile,\n content: outputResult.content,\n index,\n });\n });\n\n await Promise.all(promises);\n}\n\n/**\n * Resolve the configured source map path or URL from the provided settings.\n *\n * @param settings - Minifier settings that may include `options.sourceMap` or `options._sourceMap`\n * @returns The source map URL or filename when configured, `undefined` otherwise.\n */\nfunction getSourceMapUrl<T extends CompressorOptions = CompressorOptions>(\n settings: Settings<T>\n): string | undefined {\n const options = settings.options as Record<string, unknown> | undefined;\n if (!options) {\n return undefined;\n }\n\n const sourceMap = options.sourceMap as Record<string, unknown> | undefined;\n if (sourceMap) {\n if (typeof sourceMap.url === \"string\") {\n return sourceMap.url;\n }\n if (typeof sourceMap.filename === \"string\") {\n return sourceMap.filename;\n }\n }\n\n const _sourceMap = options._sourceMap as\n | Record<string, unknown>\n | undefined;\n if (_sourceMap && typeof _sourceMap.url === \"string\") {\n return _sourceMap.url;\n }\n\n return undefined;\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"run.js","names":[],"sources":["../src/run.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright (c) 2011-2026 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { join, parse } from \"node:path\";\nimport type {\n CompressorOptions,\n CompressorResult,\n MinifierOptions,\n Settings,\n} from \"@node-minify/types\";\nimport { ValidationError } from \"./error.ts\";\nimport { writeFileAsync } from \"./writeFile.ts\";\n\n/**\n * Execute the configured compressor and persist its outputs according to the provided settings.\n *\n * @param settings - Compressor settings including output targets, options, and the `compressor` implementation\n * @param content - The input content to be compressed\n * @param index - Optional index used when processing multiple inputs\n * @returns The minified code produced by the compressor\n * @throws ValidationError If `settings` is missing, `settings.compressor` is not provided, or the compressor returns an invalid result\n */\nexport async function run<T extends CompressorOptions = CompressorOptions>({\n settings,\n content,\n index,\n}: MinifierOptions<T>): Promise<string> {\n if (!settings) {\n throw new ValidationError(\"Settings must be provided\");\n }\n\n if (!settings.compressor) {\n throw new ValidationError(\"Compressor must be provided in settings\");\n }\n\n const result = await settings.compressor({\n settings,\n content,\n index,\n });\n\n validateCompressorResult(result, settings);\n\n await writeOutput(result, settings, index);\n\n return result.code;\n}\n\n/**\n * Verify that a compressor result is an object containing a string `code` property and narrow its type to `CompressorResult`.\n *\n * @param result - The value returned by the compressor to validate.\n * @param settings - Minifier settings (used to derive the compressor label for error messages).\n * @throws ValidationError - If `result` is not an object with a string `code` property.\n */\nfunction validateCompressorResult<\n T extends CompressorOptions = CompressorOptions,\n>(result: unknown, settings: Settings<T>): asserts result is CompressorResult {\n if (!result || typeof result !== \"object\") {\n const label = settings.compressorLabel || \"compressor\";\n throw new ValidationError(\n `Compressor '${label}' returned invalid result. Expected an object with { code: string }.`\n );\n }\n\n const obj = result as Record<string, unknown>;\n if (!(\"code\" in obj) || typeof obj.code !== \"string\") {\n const label = settings.compressorLabel || \"compressor\";\n throw new ValidationError(\n `Compressor '${label}' must return { code: string }. Got: ${JSON.stringify(Object.keys(obj))}`\n );\n }\n}\n\n/**\n * Write compressor outputs to configured output paths unless execution is in-memory.\n *\n * Writes multiple outputs when `result.outputs` is present, writes `result.buffer` if provided, otherwise writes `result.code`. If `result.map` exists and a source map URL can be resolved from `settings`, the map file is written alongside the main output. The function returns without writing when `settings.output` is not set, when `settings.content` is provided (in-memory mode), or when `settings.allowEmptyOutput` is true and `result.code` is an empty string.\n *\n * @param result - Compressor result containing `code` and optional `buffer`, `map`, or `outputs`\n * @param settings - Settings that determine output destinations and in-memory mode\n * @param index - Optional batch/index marker forwarded to underlying write operations\n */\nasync function writeOutput<T extends CompressorOptions = CompressorOptions>(\n result: CompressorResult,\n settings: Settings<T>,\n index?: number\n): Promise<void> {\n const isInMemoryMode = settings.content !== undefined;\n if (isInMemoryMode || !settings.output) {\n return;\n }\n\n const hasBuffer = result.buffer && result.buffer.length > 0;\n const hasOutputs = result.outputs && result.outputs.length > 0;\n const hasCode = result.code !== \"\";\n const isCompletelyEmpty = !hasCode && !hasBuffer && !hasOutputs;\n if (settings.allowEmptyOutput && isCompletelyEmpty) {\n return;\n }\n\n // Handle multi-output (for image conversion to multiple formats)\n if (result.outputs && result.outputs.length > 0) {\n await writeMultipleOutputs(result.outputs, settings, index);\n return;\n }\n\n // Handle single buffer output (for binary images)\n if (result.buffer) {\n await writeFileAsync({\n file: settings.output,\n content: result.buffer,\n index,\n });\n return;\n }\n\n // Default: write code (string) output\n const writePromises = [\n writeFileAsync({\n file: settings.output,\n content: result.code,\n index,\n }),\n ];\n\n if (result.map) {\n const sourceMapUrl = getSourceMapUrl(settings);\n if (sourceMapUrl) {\n writePromises.push(\n writeFileAsync({\n file: sourceMapUrl,\n content: result.map,\n index,\n })\n );\n }\n }\n\n await Promise.all(writePromises);\n}\n\n/**\n * Extract an input file path from the input configuration.\n *\n * @param input - A single file path, an array of paths, or undefined\n * @param index - Optional index of the current input being processed when `input` is an array\n * @returns The indexed input file path when available, otherwise the first input file path or an empty string\n */\nfunction getInputFile(\n input: string | string[] | undefined,\n index?: number\n): string {\n if (typeof input === \"string\") {\n return input;\n }\n if (Array.isArray(input) && input.length > 0) {\n if (index !== undefined) {\n return input[index] ?? \"\";\n }\n return input[0] ?? \"\";\n }\n return \"\";\n}\n\n/**\n * Write compressor outputs to files resolved from the provided settings.\n *\n * Resolves a target path for each output entry based on settings.output and settings.input, then writes each entry's content to its resolved file location.\n *\n * @param outputs - Array of compressor output entries (each entry typically contains `content` and optional `format`) to be written.\n * @param settings - Settings used to resolve target paths (may supply `output` pattern/array and `input` for deriving names).\n * @param index - Optional numeric index forwarded to the file writer for each write operation.\n */\nasync function writeMultipleOutputs<\n T extends CompressorOptions = CompressorOptions,\n>(\n outputs: NonNullable<CompressorResult[\"outputs\"]>,\n settings: Settings<T>,\n index?: number\n): Promise<void> {\n const output = settings.output;\n const perFormatOutputs = Array.isArray(output) && index === undefined;\n const inputFile = getInputFile(settings.input, index);\n const inputDir = parse(inputFile).dir;\n const inputBase = parse(inputFile).name;\n const baseName = inputBase || \"output\";\n const currentOutput =\n Array.isArray(output) && index !== undefined ? output[index] : output;\n const withFormatExtension = (file: string, format: string): string =>\n file.endsWith(`.${format}`) ? file : `${file}.${format}`;\n\n const promises = outputs.map(async (outputResult, i) => {\n if (!outputResult) {\n return;\n }\n\n // Write outputs in parallel to improve performance\n const format = outputResult.format || \"out\";\n let targetFile: string;\n\n const arrayItem = perFormatOutputs ? output[i] : undefined;\n\n if (\n perFormatOutputs &&\n arrayItem !== undefined &&\n arrayItem !== \"$1\" &&\n arrayItem.trim() !== \"\"\n ) {\n if (arrayItem.includes(\"$1\")) {\n const outputFile = arrayItem.replace(/\\$1/g, baseName);\n targetFile = parse(outputFile).ext\n ? outputFile\n : withFormatExtension(outputFile, format);\n } else {\n targetFile = parse(arrayItem).ext\n ? arrayItem\n : withFormatExtension(arrayItem, format);\n }\n } else if (\n typeof currentOutput === \"string\" &&\n currentOutput === \"$1\"\n ) {\n // $1 only: auto-generate from input filename in input directory\n targetFile = inputDir\n ? join(inputDir, `${baseName}.${format}`)\n : `${baseName}.${format}`;\n } else if (\n typeof currentOutput === \"string\" &&\n currentOutput.includes(\"$1\")\n ) {\n // $1 pattern in path: replace and append format\n const outputFile = currentOutput.replace(/\\$1/g, baseName);\n targetFile = withFormatExtension(outputFile, format);\n } else if (\n typeof currentOutput === \"string\" &&\n currentOutput.trim() !== \"\"\n ) {\n // Single string output: append format extension\n targetFile = withFormatExtension(currentOutput, format);\n } else {\n // Fallback\n targetFile = inputDir\n ? join(inputDir, `${baseName}.${format}`)\n : `${baseName}.${format}`;\n }\n\n await writeFileAsync({\n file: targetFile,\n content: outputResult.content,\n index,\n });\n });\n\n await Promise.all(promises);\n}\n\n/**\n * Resolve the configured source map path or URL from the provided settings.\n *\n * @param settings - Minifier settings that may include `options.sourceMap` or `options._sourceMap`\n * @returns The source map URL or filename when configured, `undefined` otherwise.\n */\nfunction getSourceMapUrl<T extends CompressorOptions = CompressorOptions>(\n settings: Settings<T>\n): string | undefined {\n const options = settings.options as Record<string, unknown> | undefined;\n if (!options) {\n return undefined;\n }\n\n const sourceMap = options.sourceMap as Record<string, unknown> | undefined;\n if (sourceMap) {\n if (typeof sourceMap.url === \"string\") {\n return sourceMap.url;\n }\n if (typeof sourceMap.filename === \"string\") {\n return sourceMap.filename;\n }\n }\n\n const _sourceMap = options._sourceMap as\n | Record<string, unknown>\n | undefined;\n if (_sourceMap && typeof _sourceMap.url === \"string\") {\n return _sourceMap.url;\n }\n\n return undefined;\n}\n"],"mappings":"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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setFileNameMin.d.ts","names":[],"sources":["../src/setFileNameMin.ts"],"
|
|
1
|
+
{"version":3,"file":"setFileNameMin.d.ts","names":[],"sources":["../src/setFileNameMin.ts"],"mappings":";;AA8BA;;;;;;;;;;;;;;;;iBAAgB,cAAA,CACZ,IAAA,UACA,MAAA,UACA,YAAA,WACA,cAAA"}
|
package/dist/setFileNameMin.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ValidationError } from "./error.js";
|
|
2
|
+
import path from "node:path";
|
|
2
3
|
|
|
3
4
|
//#region src/setFileNameMin.ts
|
|
4
5
|
/*!
|
|
@@ -7,6 +8,14 @@ import { n as ValidationError } from "./error-CUgKxOvI.js";
|
|
|
7
8
|
* MIT Licensed
|
|
8
9
|
*/
|
|
9
10
|
/**
|
|
11
|
+
* Normalize any Windows path separators to POSIX separators.
|
|
12
|
+
* @param value Path-like string
|
|
13
|
+
* @returns Path string using `/` separators
|
|
14
|
+
*/
|
|
15
|
+
function toPosixPath(value) {
|
|
16
|
+
return value.replaceAll("\\", "/");
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
10
19
|
* Set the file name as minified.
|
|
11
20
|
* @param file Original file path
|
|
12
21
|
* @param output Output pattern (use $1 as placeholder for filename)
|
|
@@ -22,17 +31,14 @@ function setFileNameMin(file, output, publicFolder, replaceInPlace) {
|
|
|
22
31
|
if (!file || typeof file !== "string") throw new ValidationError("File path must be a non-empty string");
|
|
23
32
|
if (!output || typeof output !== "string" || !output.includes("$1")) throw new ValidationError("Output pattern must be a string containing \"$1\"");
|
|
24
33
|
try {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const lastDotIndex = fileWithoutPath.lastIndexOf(".");
|
|
29
|
-
if (lastDotIndex === -1) throw new ValidationError("File must have an extension");
|
|
30
|
-
let fileWithoutExtension = fileWithoutPath.substring(0, lastDotIndex);
|
|
34
|
+
const parsedFile = path.posix.parse(toPosixPath(file));
|
|
35
|
+
if (!parsedFile.ext) throw new ValidationError("File must have an extension");
|
|
36
|
+
let fileWithoutExtension = parsedFile.name;
|
|
31
37
|
if (publicFolder) {
|
|
32
38
|
if (typeof publicFolder !== "string") throw new ValidationError("Public folder must be a string");
|
|
33
|
-
fileWithoutExtension = publicFolder
|
|
39
|
+
fileWithoutExtension = path.posix.join(toPosixPath(publicFolder), fileWithoutExtension);
|
|
34
40
|
}
|
|
35
|
-
if (replaceInPlace) fileWithoutExtension =
|
|
41
|
+
if (replaceInPlace) fileWithoutExtension = path.posix.join(parsedFile.dir, fileWithoutExtension);
|
|
36
42
|
return output.replace("$1", fileWithoutExtension);
|
|
37
43
|
} catch (error) {
|
|
38
44
|
if (error instanceof ValidationError) throw error;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setFileNameMin.js","names":[],"sources":["../src/setFileNameMin.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright (c) 2011-2026 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { ValidationError } from \"./error.ts\";\n\n/**\n * Set the file name as minified.\n * @param file Original file path\n * @param output Output pattern (use $1 as placeholder for filename)\n * @param publicFolder Optional public folder prefix\n * @param replaceInPlace Whether to keep original path\n * @returns Minified file path\n * @throws {ValidationError} If input parameters are invalid\n * @example\n * setFileNameMin('src/file.js', '$1.min.js') // 'src/file.min.js'\n * setFileNameMin('file.js', '$1.min.js', 'public/') // 'public/file.min.js'\n */\nexport function setFileNameMin(\n file: string,\n output: string,\n publicFolder?: string,\n replaceInPlace?: boolean\n): string {\n if (!file || typeof file !== \"string\") {\n throw new ValidationError(\"File path must be a non-empty string\");\n }\n\n if (!output || typeof output !== \"string\" || !output.includes(\"$1\")) {\n throw new ValidationError(\n 'Output pattern must be a string containing \"$1\"'\n );\n }\n\n try {\n const
|
|
1
|
+
{"version":3,"file":"setFileNameMin.js","names":[],"sources":["../src/setFileNameMin.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright (c) 2011-2026 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport path from \"node:path\";\nimport { ValidationError } from \"./error.ts\";\n\n/**\n * Normalize any Windows path separators to POSIX separators.\n * @param value Path-like string\n * @returns Path string using `/` separators\n */\nfunction toPosixPath(value: string): string {\n return value.replaceAll(\"\\\\\", \"/\");\n}\n\n/**\n * Set the file name as minified.\n * @param file Original file path\n * @param output Output pattern (use $1 as placeholder for filename)\n * @param publicFolder Optional public folder prefix\n * @param replaceInPlace Whether to keep original path\n * @returns Minified file path\n * @throws {ValidationError} If input parameters are invalid\n * @example\n * setFileNameMin('src/file.js', '$1.min.js') // 'src/file.min.js'\n * setFileNameMin('file.js', '$1.min.js', 'public/') // 'public/file.min.js'\n */\nexport function setFileNameMin(\n file: string,\n output: string,\n publicFolder?: string,\n replaceInPlace?: boolean\n): string {\n if (!file || typeof file !== \"string\") {\n throw new ValidationError(\"File path must be a non-empty string\");\n }\n\n if (!output || typeof output !== \"string\" || !output.includes(\"$1\")) {\n throw new ValidationError(\n 'Output pattern must be a string containing \"$1\"'\n );\n }\n\n try {\n const parsedFile = path.posix.parse(toPosixPath(file));\n\n if (!parsedFile.ext) {\n throw new ValidationError(\"File must have an extension\");\n }\n\n let fileWithoutExtension = parsedFile.name;\n\n if (publicFolder) {\n if (typeof publicFolder !== \"string\") {\n throw new ValidationError(\"Public folder must be a string\");\n }\n fileWithoutExtension = path.posix.join(\n toPosixPath(publicFolder),\n fileWithoutExtension\n );\n }\n\n if (replaceInPlace) {\n fileWithoutExtension = path.posix.join(\n parsedFile.dir,\n fileWithoutExtension\n );\n }\n\n return output.replace(\"$1\", fileWithoutExtension);\n } catch (error) {\n if (error instanceof ValidationError) {\n throw error;\n }\n throw new ValidationError(\n `Failed to process file name: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAS,YAAY,OAAuB;AACxC,QAAO,MAAM,WAAW,MAAM,IAAI;;;;;;;;;;;;;;AAetC,SAAgB,eACZ,MACA,QACA,cACA,gBACM;AACN,KAAI,CAAC,QAAQ,OAAO,SAAS,SACzB,OAAM,IAAI,gBAAgB,uCAAuC;AAGrE,KAAI,CAAC,UAAU,OAAO,WAAW,YAAY,CAAC,OAAO,SAAS,KAAK,CAC/D,OAAM,IAAI,gBACN,oDACH;AAGL,KAAI;EACA,MAAM,aAAa,KAAK,MAAM,MAAM,YAAY,KAAK,CAAC;AAEtD,MAAI,CAAC,WAAW,IACZ,OAAM,IAAI,gBAAgB,8BAA8B;EAG5D,IAAI,uBAAuB,WAAW;AAEtC,MAAI,cAAc;AACd,OAAI,OAAO,iBAAiB,SACxB,OAAM,IAAI,gBAAgB,iCAAiC;AAE/D,0BAAuB,KAAK,MAAM,KAC9B,YAAY,aAAa,EACzB,qBACH;;AAGL,MAAI,eACA,wBAAuB,KAAK,MAAM,KAC9B,WAAW,KACX,qBACH;AAGL,SAAO,OAAO,QAAQ,MAAM,qBAAqB;UAC5C,OAAO;AACZ,MAAI,iBAAiB,gBACjB,OAAM;AAEV,QAAM,IAAI,gBACN,gCAAgC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GACzF"}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* Prepend the public folder to each file.
|
|
9
9
|
* @param input Path to file(s)
|
|
10
10
|
* @param publicFolder Path to the public folder
|
|
11
|
+
* @returns Object containing the updated input path(s), or an empty object if `publicFolder` is invalid
|
|
11
12
|
*/
|
|
12
13
|
declare function setPublicFolder(input: string | string[], publicFolder: string): {
|
|
13
14
|
input?: undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPublicFolder.d.ts","names":[],"sources":["../src/setPublicFolder.ts"],"
|
|
1
|
+
{"version":3,"file":"setPublicFolder.d.ts","names":[],"sources":["../src/setPublicFolder.ts"],"mappings":";;AAcA;;;;;;;;;;iBAAgB,eAAA,CACZ,KAAA,qBACA,YAAA"}
|