@node-minify/utils 10.1.0 → 10.2.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.
Files changed (48) hide show
  1. package/README.md +4 -4
  2. package/dist/buildArgs.js +1 -1
  3. package/dist/compressSingleFile.d.ts +6 -4
  4. package/dist/compressSingleFile.d.ts.map +1 -1
  5. package/dist/compressSingleFile.js +53 -9
  6. package/dist/compressSingleFile.js.map +1 -1
  7. package/dist/deleteFile.js +1 -1
  8. package/dist/ensureStringContent.d.ts +18 -0
  9. package/dist/ensureStringContent.d.ts.map +1 -0
  10. package/dist/ensureStringContent.js +23 -0
  11. package/dist/ensureStringContent.js.map +1 -0
  12. package/dist/{error-B_uK-F18.js → error-Ck87RwDD.js} +1 -1
  13. package/dist/{error-B_uK-F18.js.map → error-Ck87RwDD.js.map} +1 -1
  14. package/dist/error.js +1 -1
  15. package/dist/getContentFromFiles.d.ts +10 -1
  16. package/dist/getContentFromFiles.d.ts.map +1 -1
  17. package/dist/getContentFromFiles.js +37 -3
  18. package/dist/getContentFromFiles.js.map +1 -1
  19. package/dist/getFilesizeGzippedInBytes.js +2 -2
  20. package/dist/index.d.ts +4 -3
  21. package/dist/index.js +5 -4
  22. package/dist/isValidFile-DnWJtlKA.js +46 -0
  23. package/dist/isValidFile-DnWJtlKA.js.map +1 -0
  24. package/dist/isValidFile.d.ts +9 -1
  25. package/dist/isValidFile.d.ts.map +1 -1
  26. package/dist/isValidFile.js +2 -2
  27. package/dist/prettyBytes.js +1 -1
  28. package/dist/readFile.d.ts +12 -1
  29. package/dist/readFile.d.ts.map +1 -1
  30. package/dist/readFile.js +13 -5
  31. package/dist/readFile.js.map +1 -1
  32. package/dist/run.d.ts +10 -12
  33. package/dist/run.d.ts.map +1 -1
  34. package/dist/run.js +101 -6
  35. package/dist/run.js.map +1 -1
  36. package/dist/setFileNameMin.js +1 -1
  37. package/dist/{types-DtDQlx-T.d.ts → types-BUlX1Zbb.d.ts} +45 -4
  38. package/dist/types-BUlX1Zbb.d.ts.map +1 -0
  39. package/dist/{writeFile-CwK9ZWXr.js → writeFile-BRfs9FqY.js} +13 -11
  40. package/dist/writeFile-BRfs9FqY.js.map +1 -0
  41. package/dist/writeFile.d.ts +10 -8
  42. package/dist/writeFile.d.ts.map +1 -1
  43. package/dist/writeFile.js +1 -1
  44. package/package.json +3 -3
  45. package/dist/isValidFile-UAwceQud.js +0 -30
  46. package/dist/isValidFile-UAwceQud.js.map +0 -1
  47. package/dist/types-DtDQlx-T.d.ts.map +0 -1
  48. package/dist/writeFile-CwK9ZWXr.js.map +0 -1
package/README.md CHANGED
@@ -1,13 +1,13 @@
1
- <p align="center"><img src="/static/node-minify.svg" width="348" alt="node-minify"></p>
1
+ <p align="center"><img src="https://raw.githubusercontent.com/srod/node-minify/main/static/node-minify.svg" width="348" alt="node-minify"></p>
2
2
 
3
3
  <p align="center">A very light minifier Node.js module.</p>
4
4
 
5
5
  <p align="center">
6
6
  <br>
7
- <a href="https://npmjs.org/package/@node-minify/utils"><img src="https://img.shields.io/npm/v/@node-minify/utils.svg"></a>
8
- <a href="https://npmjs.org/package/@node-minify/utils"><img src="https://img.shields.io/npm/dm/@node-minify/utils.svg"></a>
7
+ <a href="https://npmjs.org/package/@node-minify/utils"><img src="https://img.shields.io/npm/v/@node-minify/utils.svg" alt="npm version"></a>
8
+ <a href="https://npmjs.org/package/@node-minify/utils"><img src="https://img.shields.io/npm/dm/@node-minify/utils.svg" alt="npm downloads"></a>
9
9
  <a href="https://github.com/srod/node-minify/actions"><img alt="Build Status" src="https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fsrod%2Fnode-minify%2Fbadge%3Fref%3Dmain&style=flat" /></a>
10
- <a href="https://codecov.io/gh/srod/node-minify"><img src="https://codecov.io/gh/srod/node-minify/branch/main/graph/badge.svg"></a>
10
+ <a href="https://codecov.io/gh/srod/node-minify"><img src="https://codecov.io/gh/srod/node-minify/branch/main/graph/badge.svg" alt="code coverage"></a>
11
11
  </p>
12
12
 
13
13
  # @node-minify/utils
package/dist/buildArgs.js CHANGED
@@ -1,4 +1,4 @@
1
- import { n as ValidationError } from "./error-B_uK-F18.js";
1
+ import { n as ValidationError } from "./error-Ck87RwDD.js";
2
2
 
3
3
  //#region src/buildArgs.ts
4
4
  /*!
@@ -1,12 +1,14 @@
1
- import { t as Settings } from "./types-DtDQlx-T.js";
1
+ import { r as Settings, t as CompressorOptions } from "./types-BUlX1Zbb.js";
2
2
 
3
3
  //#region src/compressSingleFile.d.ts
4
4
 
5
5
  /**
6
- * Compress a single file.
7
- * @param settings Settings
6
+ * Compress a single file using the provided settings.
7
+ *
8
+ * @param settings - Configuration that specifies input content or input path(s) and compressor options
9
+ * @returns The compressed output as a string
8
10
  */
9
- declare function compressSingleFile(settings: Settings): Promise<string>;
11
+ declare function compressSingleFile<T extends CompressorOptions = CompressorOptions>(settings: Settings<T>): Promise<string>;
10
12
  //#endregion
11
13
  export { compressSingleFile };
12
14
  //# sourceMappingURL=compressSingleFile.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"compressSingleFile.d.ts","names":[],"sources":["../src/compressSingleFile.ts"],"sourcesContent":[],"mappings":";;;;;;;;iBAcsB,kBAAA,WAA6B,WAAW"}
1
+ {"version":3,"file":"compressSingleFile.d.ts","names":[],"sources":["../src/compressSingleFile.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;iBA+CsB,6BACR,oBAAoB,6BACtB,SAAS,KAAK"}
@@ -1,25 +1,69 @@
1
- import { getContentFromFiles } from "./getContentFromFiles.js";
1
+ import { getContentFromFilesAsync } from "./getContentFromFiles.js";
2
2
  import { run } from "./run.js";
3
+ import { readFile } from "node:fs/promises";
3
4
 
4
5
  //#region src/compressSingleFile.ts
6
+ /*!
7
+ * node-minify
8
+ * Copyright(c) 2011-2025 Rodolphe Stoclin
9
+ * MIT Licensed
10
+ */
11
+ const IMAGE_EXTENSIONS = new Set([
12
+ ".png",
13
+ ".jpg",
14
+ ".jpeg",
15
+ ".gif",
16
+ ".webp",
17
+ ".avif",
18
+ ".tiff",
19
+ ".tif",
20
+ ".heif",
21
+ ".heic"
22
+ ]);
23
+ /**
24
+ * Determines whether a file path refers to a supported image file by its extension.
25
+ *
26
+ * @param filePath - The file name or path to check; may include directories.
27
+ * @returns `true` if the path ends with a recognized image extension, `false` otherwise.
28
+ */
29
+ function isImageFile(filePath) {
30
+ const lastDot = filePath.lastIndexOf(".");
31
+ if (lastDot === -1) return false;
32
+ const ext = filePath.slice(lastDot).toLowerCase();
33
+ return IMAGE_EXTENSIONS.has(ext);
34
+ }
5
35
  /**
6
- * Compress a single file.
7
- * @param settings Settings
36
+ * Compress a single file using the provided settings.
37
+ *
38
+ * @param settings - Configuration that specifies input content or input path(s) and compressor options
39
+ * @returns The compressed output as a string
8
40
  */
9
41
  async function compressSingleFile(settings) {
10
42
  return run({
11
43
  settings,
12
- content: determineContent(settings)
44
+ content: await determineContent(settings)
13
45
  });
14
46
  }
15
47
  /**
16
- * Determine the content to minify.
17
- * @param settings - Minification settings
18
- * @returns Content string to minify
48
+ * Resolve the content to be minified from the provided settings.
49
+ *
50
+ * @param settings - Settings that may contain `content` or `input` (string or string[]); `content` is used preferentially.
51
+ * @returns The resolved content: a `string` for text input, a `Buffer` for a single image file, or a `Buffer[]` for multiple image files.
52
+ * @throws Error - If `settings.input` is an array that mixes image and non-image file paths.
19
53
  */
20
- function determineContent(settings) {
54
+ async function determineContent(settings) {
21
55
  if (settings.content) return settings.content;
22
- if (settings.input) return getContentFromFiles(settings.input);
56
+ if (settings.input && Array.isArray(settings.input)) {
57
+ const imageFilesCount = settings.input.filter((file) => isImageFile(file)).length;
58
+ if (imageFilesCount > 0) {
59
+ if (imageFilesCount !== settings.input.length) throw new Error("Cannot mix image and text files in the same input array");
60
+ return await Promise.all(settings.input.map((file) => readFile(file)));
61
+ }
62
+ }
63
+ if (settings.input && typeof settings.input === "string") {
64
+ if (isImageFile(settings.input)) return await readFile(settings.input);
65
+ }
66
+ if (settings.input) return await getContentFromFilesAsync(settings.input);
23
67
  return "";
24
68
  }
25
69
 
@@ -1 +1 @@
1
- {"version":3,"file":"compressSingleFile.js","names":[],"sources":["../src/compressSingleFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport type { Settings } from \"@node-minify/types\";\nimport { getContentFromFiles } from \"./getContentFromFiles.ts\";\nimport { run } from \"./run.ts\";\n\n/**\n * Compress a single file.\n * @param settings Settings\n */\nexport async function compressSingleFile(settings: Settings): Promise<string> {\n const content = determineContent(settings);\n return run({ settings, content });\n}\n\n/**\n * Determine the content to minify.\n * @param settings - Minification settings\n * @returns Content string to minify\n */\nfunction determineContent(settings: Settings): string {\n if (settings.content) {\n return settings.content;\n }\n\n if (settings.input) {\n return getContentFromFiles(settings.input);\n }\n\n return \"\";\n}\n"],"mappings":";;;;;;;;AAcA,eAAsB,mBAAmB,UAAqC;AAE1E,QAAO,IAAI;EAAE;EAAU,SADP,iBAAiB,SAAS;EACV,CAAC;;;;;;;AAQrC,SAAS,iBAAiB,UAA4B;AAClD,KAAI,SAAS,QACT,QAAO,SAAS;AAGpB,KAAI,SAAS,MACT,QAAO,oBAAoB,SAAS,MAAM;AAG9C,QAAO"}
1
+ {"version":3,"file":"compressSingleFile.js","names":[],"sources":["../src/compressSingleFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { readFile } from \"node:fs/promises\";\nimport type {\n CompressorOptions,\n MinifierOptions,\n Settings,\n} from \"@node-minify/types\";\nimport { getContentFromFilesAsync } from \"./getContentFromFiles.ts\";\nimport { run } from \"./run.ts\";\n\nconst IMAGE_EXTENSIONS = new Set([\n \".png\",\n \".jpg\",\n \".jpeg\",\n \".gif\",\n \".webp\",\n \".avif\",\n \".tiff\",\n \".tif\",\n \".heif\",\n \".heic\",\n]);\n\n/**\n * Determines whether a file path refers to a supported image file by its extension.\n *\n * @param filePath - The file name or path to check; may include directories.\n * @returns `true` if the path ends with a recognized image extension, `false` otherwise.\n */\nfunction isImageFile(filePath: string): boolean {\n const lastDot = filePath.lastIndexOf(\".\");\n if (lastDot === -1) return false;\n const ext = filePath.slice(lastDot).toLowerCase();\n return IMAGE_EXTENSIONS.has(ext);\n}\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 return await Promise.all(\n settings.input.map((file) => readFile(file))\n );\n }\n }\n\n if (settings.input && typeof settings.input === \"string\") {\n if (isImageFile(settings.input)) {\n return await readFile(settings.input);\n }\n }\n\n if (settings.input) {\n return await getContentFromFilesAsync(settings.input);\n }\n\n return \"\";\n}\n"],"mappings":";;;;;;;;;;AAeA,MAAM,mBAAmB,IAAI,IAAI;CAC7B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACH,CAAC;;;;;;;AAQF,SAAS,YAAY,UAA2B;CAC5C,MAAM,UAAU,SAAS,YAAY,IAAI;AACzC,KAAI,YAAY,GAAI,QAAO;CAC3B,MAAM,MAAM,SAAS,MAAM,QAAQ,CAAC,aAAa;AACjD,QAAO,iBAAiB,IAAI,IAAI;;;;;;;;AASpC,eAAsB,mBAEpB,UAAwC;AAEtC,QAAO,IAAI;EAAE;EAAU,SADP,MAAM,iBAAiB,SAAS;EAChB,CAAuB;;;;;;;;;AAU3D,eAAe,iBAEb,UAA4D;AAC1D,KAAI,SAAS,QACT,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;AAEL,UAAO,MAAM,QAAQ,IACjB,SAAS,MAAM,KAAK,SAAS,SAAS,KAAK,CAAC,CAC/C;;;AAIT,KAAI,SAAS,SAAS,OAAO,SAAS,UAAU,UAC5C;MAAI,YAAY,SAAS,MAAM,CAC3B,QAAO,MAAM,SAAS,SAAS,MAAM;;AAI7C,KAAI,SAAS,MACT,QAAO,MAAM,yBAAyB,SAAS,MAAM;AAGzD,QAAO"}
@@ -1,4 +1,4 @@
1
- import { t as FileOperationError } from "./error-B_uK-F18.js";
1
+ import { t as FileOperationError } from "./error-Ck87RwDD.js";
2
2
  import { existsSync, unlinkSync } from "node:fs";
3
3
 
4
4
  //#region src/deleteFile.ts
@@ -0,0 +1,18 @@
1
+ //#region src/ensureStringContent.d.ts
2
+ /*!
3
+ * node-minify
4
+ * Copyright(c) 2011-2025 Rodolphe Stoclin
5
+ * MIT Licensed
6
+ */
7
+ /**
8
+ * Convert provided content to a string suitable for text-based compressors.
9
+ *
10
+ * @param content - The input content; a `Buffer` is converted to a string, `undefined` becomes an empty string, and a `Buffer[]` is rejected.
11
+ * @param compressorName - Name used in the error message when array content is not supported.
12
+ * @returns The content as a string; returns an empty string when `content` is `undefined`.
13
+ * @throws Error if `content` is an array with message "`<compressorName> compressor does not support array content`".
14
+ */
15
+ declare function ensureStringContent(content: string | Buffer | Buffer[] | undefined, compressorName: string): string;
16
+ //#endregion
17
+ export { ensureStringContent };
18
+ //# sourceMappingURL=ensureStringContent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ensureStringContent.d.ts","names":[],"sources":["../src/ensureStringContent.ts"],"sourcesContent":[],"mappings":";;AAcA;;;;;;;;;;;;iBAAgB,mBAAA,mBACM,SAAS"}
@@ -0,0 +1,23 @@
1
+ //#region src/ensureStringContent.ts
2
+ /*!
3
+ * node-minify
4
+ * Copyright(c) 2011-2025 Rodolphe Stoclin
5
+ * MIT Licensed
6
+ */
7
+ /**
8
+ * Convert provided content to a string suitable for text-based compressors.
9
+ *
10
+ * @param content - The input content; a `Buffer` is converted to a string, `undefined` becomes an empty string, and a `Buffer[]` is rejected.
11
+ * @param compressorName - Name used in the error message when array content is not supported.
12
+ * @returns The content as a string; returns an empty string when `content` is `undefined`.
13
+ * @throws Error if `content` is an array with message "`<compressorName> compressor does not support array content`".
14
+ */
15
+ function ensureStringContent(content, compressorName) {
16
+ if (Array.isArray(content)) throw new Error(`${compressorName} compressor does not support array content`);
17
+ if (Buffer.isBuffer(content)) return content.toString();
18
+ return content ?? "";
19
+ }
20
+
21
+ //#endregion
22
+ export { ensureStringContent };
23
+ //# sourceMappingURL=ensureStringContent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ensureStringContent.js","names":[],"sources":["../src/ensureStringContent.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Convert provided content to a string suitable for text-based compressors.\n *\n * @param content - The input content; a `Buffer` is converted to a string, `undefined` becomes an empty string, and a `Buffer[]` is rejected.\n * @param compressorName - Name used in the error message when array content is not supported.\n * @returns The content as a string; returns an empty string when `content` is `undefined`.\n * @throws Error if `content` is an array with message \"`<compressorName> compressor does not support array content`\".\n */\nexport function ensureStringContent(\n content: string | Buffer | Buffer[] | undefined,\n compressorName: string\n): string {\n if (Array.isArray(content)) {\n throw new Error(\n `${compressorName} compressor does not support array content`\n );\n }\n\n if (Buffer.isBuffer(content)) {\n return content.toString();\n }\n\n return content ?? \"\";\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAgB,oBACZ,SACA,gBACM;AACN,KAAI,MAAM,QAAQ,QAAQ,CACtB,OAAM,IAAI,MACN,GAAG,eAAe,4CACrB;AAGL,KAAI,OAAO,SAAS,QAAQ,CACxB,QAAO,QAAQ,UAAU;AAG7B,QAAO,WAAW"}
@@ -27,4 +27,4 @@ var ValidationError = class extends Error {
27
27
 
28
28
  //#endregion
29
29
  export { ValidationError as n, FileOperationError as t };
30
- //# sourceMappingURL=error-B_uK-F18.js.map
30
+ //# sourceMappingURL=error-Ck87RwDD.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"error-B_uK-F18.js","names":[],"sources":["../src/error.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 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 constructor(operation: string, path: string, originalError?: Error) {\n super(\n `Failed to ${operation} file ${path}: ${originalError?.message || \"\"}`\n );\n this.name = \"FileOperationError\";\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,YAAY,WAAmB,MAAc,eAAuB;AAChE,QACI,aAAa,UAAU,QAAQ,KAAK,IAAI,eAAe,WAAW,KACrE;AACD,OAAK,OAAO;;;;;;;AAQpB,IAAa,kBAAb,cAAqC,MAAM;CACvC,YAAY,SAAiB;AACzB,QAAM,QAAQ;AACd,OAAK,OAAO"}
1
+ {"version":3,"file":"error-Ck87RwDD.js","names":[],"sources":["../src/error.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 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 constructor(operation: string, path: string, originalError?: Error) {\n super(\n `Failed to ${operation} file ${path}: ${originalError?.message || \"\"}`\n );\n this.name = \"FileOperationError\";\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,YAAY,WAAmB,MAAc,eAAuB;AAChE,QACI,aAAa,UAAU,QAAQ,KAAK,IAAI,eAAe,WAAW,KACrE;AACD,OAAK,OAAO;;;;;;;AAQpB,IAAa,kBAAb,cAAqC,MAAM;CACvC,YAAY,SAAiB;AACzB,QAAM,QAAQ;AACd,OAAK,OAAO"}
package/dist/error.js CHANGED
@@ -1,3 +1,3 @@
1
- import { n as ValidationError, t as FileOperationError } from "./error-B_uK-F18.js";
1
+ import { n as ValidationError, t as FileOperationError } from "./error-Ck87RwDD.js";
2
2
 
3
3
  export { FileOperationError, ValidationError };
@@ -14,6 +14,15 @@
14
14
  * getContentFromFiles(['file1.js', 'file2.js'])
15
15
  */
16
16
  declare function getContentFromFiles(input: string | string[]): string;
17
+ /**
18
+ * Concatenate contents of one or more files asynchronously.
19
+ *
20
+ * @param input - A file path or an array of file paths to read
21
+ * @returns The files' contents joined with newline characters
22
+ * @throws {FileOperationError} If an underlying file operation fails for any path
23
+ * @throws {Error} If `input` is missing or processing of the provided input fails
24
+ */
25
+ declare function getContentFromFilesAsync(input: string | string[]): Promise<string>;
17
26
  //#endregion
18
- export { getContentFromFiles };
27
+ export { getContentFromFiles, getContentFromFilesAsync };
19
28
  //# sourceMappingURL=getContentFromFiles.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getContentFromFiles.d.ts","names":[],"sources":["../src/getContentFromFiles.ts"],"sourcesContent":[],"mappings":";;AAuCA;;;;;;;;;;;;;iBAAgB,mBAAA"}
1
+ {"version":3,"file":"getContentFromFiles.d.ts","names":[],"sources":["../src/getContentFromFiles.ts"],"sourcesContent":[],"mappings":";;AAuDA;AAiCA;;;;;;;;;;;;iBAjCgB,mBAAA;;;;;;;;;iBAiCM,wBAAA,4BAEnB"}
@@ -1,5 +1,6 @@
1
- import { t as FileOperationError } from "./error-B_uK-F18.js";
2
- import { t as isValidFile } from "./isValidFile-UAwceQud.js";
1
+ import { t as FileOperationError } from "./error-Ck87RwDD.js";
2
+ import { t as isValidFile } from "./isValidFile-DnWJtlKA.js";
3
+ import { readFile } from "node:fs/promises";
3
4
  import { existsSync, readFileSync } from "node:fs";
4
5
 
5
6
  //#region src/getContentFromFiles.ts
@@ -24,6 +25,20 @@ function readFileContent(path) {
24
25
  }
25
26
  }
26
27
  /**
28
+ * Read the UTF-8 content of a single file.
29
+ *
30
+ * @param path - Filesystem path to the file
31
+ * @returns The file content as a string
32
+ * @throws FileOperationError if the file does not exist, the path is a directory, or reading the file fails
33
+ */
34
+ async function readFileContentAsync(path) {
35
+ try {
36
+ return await readFile(path, "utf8");
37
+ } catch (error) {
38
+ throw new FileOperationError("read", path, error);
39
+ }
40
+ }
41
+ /**
27
42
  * Concatenate all input files and get the data.
28
43
  * @param input Single file path or array of file paths
29
44
  * @returns Concatenated content of all files
@@ -43,7 +58,26 @@ function getContentFromFiles(input) {
43
58
  throw new Error(`Failed to process input files: ${error instanceof Error ? error.message : String(error)}`);
44
59
  }
45
60
  }
61
+ /**
62
+ * Concatenate contents of one or more files asynchronously.
63
+ *
64
+ * @param input - A file path or an array of file paths to read
65
+ * @returns The files' contents joined with newline characters
66
+ * @throws {FileOperationError} If an underlying file operation fails for any path
67
+ * @throws {Error} If `input` is missing or processing of the provided input fails
68
+ */
69
+ async function getContentFromFilesAsync(input) {
70
+ try {
71
+ if (!input) throw new Error("Input must be a string or array of strings");
72
+ if (!Array.isArray(input)) return await readFileContentAsync(input);
73
+ if (input.length === 0) return "";
74
+ return (await Promise.all(input.map(readFileContentAsync))).join("\n");
75
+ } catch (error) {
76
+ if (error instanceof FileOperationError) throw error;
77
+ throw new Error(`Failed to process input files: ${error instanceof Error ? error.message : String(error)}`);
78
+ }
79
+ }
46
80
 
47
81
  //#endregion
48
- export { getContentFromFiles };
82
+ export { getContentFromFiles, getContentFromFilesAsync };
49
83
  //# sourceMappingURL=getContentFromFiles.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"getContentFromFiles.js","names":["error: unknown"],"sources":["../src/getContentFromFiles.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { existsSync, readFileSync } from \"node:fs\";\nimport { FileOperationError } from \"./error.ts\";\nimport { isValidFile } from \"./isValidFile.ts\";\n\n/**\n * Read content from a single file with error handling.\n * @param path Path to the file\n * @returns Content of the file\n * @throws {FileOperationError} If file doesn't exist or reading fails\n */\nfunction readFileContent(path: string): string {\n try {\n if (!existsSync(path)) {\n throw new Error(\"File does not exist\");\n }\n if (!isValidFile(path)) {\n throw new Error(\"Path is not a valid file\");\n }\n return readFileSync(path, \"utf8\");\n } catch (error) {\n throw new FileOperationError(\"read\", path, error as Error);\n }\n}\n\n/**\n * Concatenate all input files and get the data.\n * @param input Single file path or array of file paths\n * @returns Concatenated content of all files\n * @throws {FileOperationError} If any file operation fails\n * @example\n * getContentFromFiles('file.js')\n * getContentFromFiles(['file1.js', 'file2.js'])\n */\nexport function getContentFromFiles(input: string | string[]): string {\n try {\n if (!input) {\n throw new Error(\"Input must be a string or array of strings\");\n }\n\n if (!Array.isArray(input)) {\n return readFileContent(input);\n }\n\n if (input.length === 0) {\n return \"\";\n }\n\n return input.map(readFileContent).join(\"\\n\");\n } catch (error: unknown) {\n if (error instanceof FileOperationError) {\n throw error;\n }\n throw new Error(\n `Failed to process input files: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,SAAS,gBAAgB,MAAsB;AAC3C,KAAI;AACA,MAAI,CAAC,WAAW,KAAK,CACjB,OAAM,IAAI,MAAM,sBAAsB;AAE1C,MAAI,CAAC,YAAY,KAAK,CAClB,OAAM,IAAI,MAAM,2BAA2B;AAE/C,SAAO,aAAa,MAAM,OAAO;UAC5B,OAAO;AACZ,QAAM,IAAI,mBAAmB,QAAQ,MAAM,MAAe;;;;;;;;;;;;AAalE,SAAgB,oBAAoB,OAAkC;AAClE,KAAI;AACA,MAAI,CAAC,MACD,OAAM,IAAI,MAAM,6CAA6C;AAGjE,MAAI,CAAC,MAAM,QAAQ,MAAM,CACrB,QAAO,gBAAgB,MAAM;AAGjC,MAAI,MAAM,WAAW,EACjB,QAAO;AAGX,SAAO,MAAM,IAAI,gBAAgB,CAAC,KAAK,KAAK;UACvCA,OAAgB;AACrB,MAAI,iBAAiB,mBACjB,OAAM;AAEV,QAAM,IAAI,MACN,kCAAkC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GAC3F"}
1
+ {"version":3,"file":"getContentFromFiles.js","names":["error: unknown"],"sources":["../src/getContentFromFiles.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { existsSync, readFileSync } from \"node:fs\";\nimport { readFile } from \"node:fs/promises\";\nimport { FileOperationError } from \"./error.ts\";\nimport { isValidFile } from \"./isValidFile.ts\";\n\n/**\n * Read content from a single file with error handling.\n * @param path Path to the file\n * @returns Content of the file\n * @throws {FileOperationError} If file doesn't exist or reading fails\n */\nfunction readFileContent(path: string): string {\n try {\n if (!existsSync(path)) {\n throw new Error(\"File does not exist\");\n }\n if (!isValidFile(path)) {\n throw new Error(\"Path is not a valid file\");\n }\n return readFileSync(path, \"utf8\");\n } catch (error) {\n throw new FileOperationError(\"read\", path, error as Error);\n }\n}\n\n/**\n * Read the UTF-8 content of a single file.\n *\n * @param path - Filesystem path to the file\n * @returns The file content as a string\n * @throws FileOperationError if the file does not exist, the path is a directory, or reading the file fails\n */\nasync function readFileContentAsync(path: string): Promise<string> {\n try {\n return await readFile(path, \"utf8\");\n } catch (error) {\n throw new FileOperationError(\"read\", path, error as Error);\n }\n}\n\n/**\n * Concatenate all input files and get the data.\n * @param input Single file path or array of file paths\n * @returns Concatenated content of all files\n * @throws {FileOperationError} If any file operation fails\n * @example\n * getContentFromFiles('file.js')\n * getContentFromFiles(['file1.js', 'file2.js'])\n */\nexport function getContentFromFiles(input: string | string[]): string {\n try {\n if (!input) {\n throw new Error(\"Input must be a string or array of strings\");\n }\n\n if (!Array.isArray(input)) {\n return readFileContent(input);\n }\n\n if (input.length === 0) {\n return \"\";\n }\n\n return input.map(readFileContent).join(\"\\n\");\n } catch (error: unknown) {\n if (error instanceof FileOperationError) {\n throw error;\n }\n throw new Error(\n `Failed to process input files: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n}\n\n/**\n * Concatenate contents of one or more files asynchronously.\n *\n * @param input - A file path or an array of file paths to read\n * @returns The files' contents joined with newline characters\n * @throws {FileOperationError} If an underlying file operation fails for any path\n * @throws {Error} If `input` is missing or processing of the provided input fails\n */\nexport async function getContentFromFilesAsync(\n input: string | string[]\n): Promise<string> {\n try {\n if (!input) {\n throw new Error(\"Input must be a string or array of strings\");\n }\n\n if (!Array.isArray(input)) {\n return await readFileContentAsync(input);\n }\n\n if (input.length === 0) {\n return \"\";\n }\n\n // Read files in parallel\n const contents = await Promise.all(input.map(readFileContentAsync));\n return contents.join(\"\\n\");\n } catch (error: unknown) {\n if (error instanceof FileOperationError) {\n throw error;\n }\n throw new Error(\n `Failed to process input files: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAiBA,SAAS,gBAAgB,MAAsB;AAC3C,KAAI;AACA,MAAI,CAAC,WAAW,KAAK,CACjB,OAAM,IAAI,MAAM,sBAAsB;AAE1C,MAAI,CAAC,YAAY,KAAK,CAClB,OAAM,IAAI,MAAM,2BAA2B;AAE/C,SAAO,aAAa,MAAM,OAAO;UAC5B,OAAO;AACZ,QAAM,IAAI,mBAAmB,QAAQ,MAAM,MAAe;;;;;;;;;;AAWlE,eAAe,qBAAqB,MAA+B;AAC/D,KAAI;AACA,SAAO,MAAM,SAAS,MAAM,OAAO;UAC9B,OAAO;AACZ,QAAM,IAAI,mBAAmB,QAAQ,MAAM,MAAe;;;;;;;;;;;;AAalE,SAAgB,oBAAoB,OAAkC;AAClE,KAAI;AACA,MAAI,CAAC,MACD,OAAM,IAAI,MAAM,6CAA6C;AAGjE,MAAI,CAAC,MAAM,QAAQ,MAAM,CACrB,QAAO,gBAAgB,MAAM;AAGjC,MAAI,MAAM,WAAW,EACjB,QAAO;AAGX,SAAO,MAAM,IAAI,gBAAgB,CAAC,KAAK,KAAK;UACvCA,OAAgB;AACrB,MAAI,iBAAiB,mBACjB,OAAM;AAEV,QAAM,IAAI,MACN,kCAAkC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GAC3F;;;;;;;;;;;AAYT,eAAsB,yBAClB,OACe;AACf,KAAI;AACA,MAAI,CAAC,MACD,OAAM,IAAI,MAAM,6CAA6C;AAGjE,MAAI,CAAC,MAAM,QAAQ,MAAM,CACrB,QAAO,MAAM,qBAAqB,MAAM;AAG5C,MAAI,MAAM,WAAW,EACjB,QAAO;AAKX,UADiB,MAAM,QAAQ,IAAI,MAAM,IAAI,qBAAqB,CAAC,EACnD,KAAK,KAAK;UACrBA,OAAgB;AACrB,MAAI,iBAAiB,mBACjB,OAAM;AAEV,QAAM,IAAI,MACN,kCAAkC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GAC3F"}
@@ -1,5 +1,5 @@
1
- import { t as FileOperationError } from "./error-B_uK-F18.js";
2
- import { t as isValidFile } from "./isValidFile-UAwceQud.js";
1
+ import { t as FileOperationError } from "./error-Ck87RwDD.js";
2
+ import { t as isValidFile } from "./isValidFile-DnWJtlKA.js";
3
3
  import { prettyBytes } from "./prettyBytes.js";
4
4
  import { createReadStream, existsSync } from "node:fs";
5
5
 
package/dist/index.d.ts CHANGED
@@ -3,10 +3,11 @@ import { buildArgs, toBuildArgsOptions } from "./buildArgs.js";
3
3
  import { compressSingleFile } from "./compressSingleFile.js";
4
4
  import { deleteFile } from "./deleteFile.js";
5
5
  import { resetDeprecationWarnings, warnDeprecation } from "./deprecation.js";
6
- import { getContentFromFiles } from "./getContentFromFiles.js";
6
+ import { ensureStringContent } from "./ensureStringContent.js";
7
+ import { getContentFromFiles, getContentFromFilesAsync } from "./getContentFromFiles.js";
7
8
  import { getFilesizeGzippedInBytes } from "./getFilesizeGzippedInBytes.js";
8
9
  import { getFilesizeInBytes } from "./getFilesizeInBytes.js";
9
- import { isValidFile } from "./isValidFile.js";
10
+ import { isValidFile, isValidFileAsync } from "./isValidFile.js";
10
11
  import { prettyBytes } from "./prettyBytes.js";
11
12
  import { readFile } from "./readFile.js";
12
13
  import { run } from "./run.js";
@@ -14,4 +15,4 @@ import { setFileNameMin } from "./setFileNameMin.js";
14
15
  import { setPublicFolder } from "./setPublicFolder.js";
15
16
  import { wildcards } from "./wildcards.js";
16
17
  import { writeFile } from "./writeFile.js";
17
- export { type BuildArgsOptions, buildArgs, compressSingleFile, deleteFile, getContentFromFiles, getFilesizeGzippedInBytes, getFilesizeInBytes, isValidFile, prettyBytes, readFile, resetDeprecationWarnings, run, setFileNameMin, setPublicFolder, toBuildArgsOptions, warnDeprecation, wildcards, writeFile };
18
+ export { type BuildArgsOptions, buildArgs, compressSingleFile, deleteFile, ensureStringContent, getContentFromFiles, getContentFromFilesAsync, getFilesizeGzippedInBytes, getFilesizeInBytes, isValidFile, isValidFileAsync, prettyBytes, readFile, resetDeprecationWarnings, run, setFileNameMin, setPublicFolder, toBuildArgsOptions, warnDeprecation, wildcards, writeFile };
package/dist/index.js CHANGED
@@ -1,11 +1,12 @@
1
1
  import { buildArgs, toBuildArgsOptions } from "./buildArgs.js";
2
- import { t as isValidFile } from "./isValidFile-UAwceQud.js";
3
- import { getContentFromFiles } from "./getContentFromFiles.js";
4
- import { t as writeFile } from "./writeFile-CwK9ZWXr.js";
2
+ import { n as isValidFileAsync, t as isValidFile } from "./isValidFile-DnWJtlKA.js";
3
+ import { getContentFromFiles, getContentFromFilesAsync } from "./getContentFromFiles.js";
4
+ import { t as writeFile } from "./writeFile-BRfs9FqY.js";
5
5
  import { run } from "./run.js";
6
6
  import { compressSingleFile } from "./compressSingleFile.js";
7
7
  import { deleteFile } from "./deleteFile.js";
8
8
  import { resetDeprecationWarnings, warnDeprecation } from "./deprecation.js";
9
+ import { ensureStringContent } from "./ensureStringContent.js";
9
10
  import { prettyBytes } from "./prettyBytes.js";
10
11
  import { getFilesizeGzippedInBytes } from "./getFilesizeGzippedInBytes.js";
11
12
  import { getFilesizeInBytes } from "./getFilesizeInBytes.js";
@@ -14,4 +15,4 @@ import { setFileNameMin } from "./setFileNameMin.js";
14
15
  import { setPublicFolder } from "./setPublicFolder.js";
15
16
  import { wildcards } from "./wildcards.js";
16
17
 
17
- export { buildArgs, compressSingleFile, deleteFile, getContentFromFiles, getFilesizeGzippedInBytes, getFilesizeInBytes, isValidFile, prettyBytes, readFile, resetDeprecationWarnings, run, setFileNameMin, setPublicFolder, toBuildArgsOptions, warnDeprecation, wildcards, writeFile };
18
+ export { buildArgs, compressSingleFile, deleteFile, ensureStringContent, getContentFromFiles, getContentFromFilesAsync, getFilesizeGzippedInBytes, getFilesizeInBytes, isValidFile, isValidFileAsync, prettyBytes, readFile, resetDeprecationWarnings, run, setFileNameMin, setPublicFolder, toBuildArgsOptions, warnDeprecation, wildcards, writeFile };
@@ -0,0 +1,46 @@
1
+ import { t as FileOperationError } from "./error-Ck87RwDD.js";
2
+ import { lstat } from "node:fs/promises";
3
+ import { existsSync, lstatSync } from "node:fs";
4
+
5
+ //#region src/isValidFile.ts
6
+ /*!
7
+ * node-minify
8
+ * Copyright(c) 2011-2025 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 { isValidFileAsync as n, isValidFile as t };
46
+ //# sourceMappingURL=isValidFile-DnWJtlKA.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isValidFile-DnWJtlKA.js","names":["error: unknown"],"sources":["../src/isValidFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 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;UACtBA,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"}
@@ -15,6 +15,14 @@
15
15
  * }
16
16
  */
17
17
  declare function isValidFile(path: string): boolean;
18
+ /**
19
+ * Determine whether a filesystem path refers to an existing file (not a directory).
20
+ *
21
+ * @param path - Path to check
22
+ * @returns `true` if the path exists and is a file, `false` otherwise.
23
+ * @throws {FileOperationError} If a filesystem error other than `ENOENT` occurs while validating the path.
24
+ */
25
+ declare function isValidFileAsync(path: string): Promise<boolean>;
18
26
  //#endregion
19
- export { isValidFile };
27
+ export { isValidFile, isValidFileAsync };
20
28
  //# sourceMappingURL=isValidFile.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"isValidFile.d.ts","names":[],"sources":["../src/isValidFile.ts"],"sourcesContent":[],"mappings":";;AAmBA;;;;;;;;;;;;;;iBAAgB,WAAA"}
1
+ {"version":3,"file":"isValidFile.d.ts","names":[],"sources":["../src/isValidFile.ts"],"sourcesContent":[],"mappings":";;AAoBA;AAeA;;;;;;;;;;;;;iBAfgB,WAAA;;;;;;;;iBAeM,gBAAA,gBAAgC"}
@@ -1,3 +1,3 @@
1
- import { t as isValidFile } from "./isValidFile-UAwceQud.js";
1
+ import { n as isValidFileAsync, t as isValidFile } from "./isValidFile-DnWJtlKA.js";
2
2
 
3
- export { isValidFile };
3
+ export { isValidFile, isValidFileAsync };
@@ -1,4 +1,4 @@
1
- import { n as ValidationError } from "./error-B_uK-F18.js";
1
+ import { n as ValidationError } from "./error-Ck87RwDD.js";
2
2
 
3
3
  //#region src/prettyBytes.ts
4
4
  /*!
@@ -7,9 +7,20 @@
7
7
  /**
8
8
  * Read content from file.
9
9
  * @param file - Path to file
10
- * @returns File content as string
10
+ * @param asBuffer - If true, return Buffer; if false, return string (default: false)
11
+ * @returns File content as string or Buffer
12
+ * @throws {FileOperationError} If file doesn't exist or reading fails
13
+ * @example
14
+ * // Read as string
15
+ * const content = readFile('file.txt');
16
+ *
17
+ * // Read as Buffer (for binary files like images)
18
+ * const buffer = readFile('image.png', true);
11
19
  */
12
20
  declare function readFile(file: string): string;
21
+ declare function readFile(file: string, asBuffer: true): Buffer;
22
+ declare function readFile(file: string, asBuffer: false): string;
23
+ declare function readFile(file: string, asBuffer?: boolean): string | Buffer;
13
24
  //#endregion
14
25
  export { readFile };
15
26
  //# sourceMappingURL=readFile.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"readFile.d.ts","names":[],"sources":["../src/readFile.ts"],"sourcesContent":[],"mappings":";;AAaA;;;;;;;;;iBAAgB,QAAA"}
1
+ {"version":3,"file":"readFile.d.ts","names":[],"sources":["../src/readFile.ts"],"sourcesContent":[],"mappings":";;AAsBA;AACA;AACA;AACA;;;;;;;;;;;;;;iBAHgB,QAAA;iBACA,QAAA,gCAAwC;iBACxC,QAAA;iBACA,QAAA,6CAAqD"}
package/dist/readFile.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { t as FileOperationError } from "./error-Ck87RwDD.js";
1
2
  import { readFileSync } from "node:fs";
2
3
 
3
4
  //#region src/readFile.ts
@@ -7,12 +8,19 @@ import { readFileSync } from "node:fs";
7
8
  * MIT Licensed
8
9
  */
9
10
  /**
10
- * Read content from file.
11
- * @param file - Path to file
12
- * @returns File content as string
11
+ * Read a file from disk and return its contents as a UTF-8 string by default or as a raw Buffer.
12
+ *
13
+ * @param file - Path to the file to read.
14
+ * @param asBuffer - If `true`, return a raw `Buffer`; if `false` or omitted, return the file decoded as a UTF-8 `string`.
15
+ * @returns A `Buffer` when `asBuffer` is `true`, otherwise the file content as a UTF-8 `string`.
16
+ * @throws FileOperationError when the file cannot be read; the original error is attached as the cause.
13
17
  */
14
- function readFile(file) {
15
- return readFileSync(file, "utf8");
18
+ function readFile(file, asBuffer) {
19
+ try {
20
+ return asBuffer ? readFileSync(file) : readFileSync(file, "utf8");
21
+ } catch (error) {
22
+ throw new FileOperationError("read", file, error instanceof Error ? error : void 0);
23
+ }
16
24
  }
17
25
 
18
26
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"readFile.js","names":[],"sources":["../src/readFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { readFileSync } from \"node:fs\";\n\n/**\n * Read content from file.\n * @param file - Path to file\n * @returns File content as string\n */\nexport function readFile(file: string): string {\n return readFileSync(file, \"utf8\");\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,SAAgB,SAAS,MAAsB;AAC3C,QAAO,aAAa,MAAM,OAAO"}
1
+ {"version":3,"file":"readFile.js","names":[],"sources":["../src/readFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { readFileSync } from \"node:fs\";\nimport { FileOperationError } from \"./error.ts\";\n\n/**\n * Read content from file.\n * @param file - Path to file\n * @param asBuffer - If true, return Buffer; if false, return string (default: false)\n * @returns File content as string or Buffer\n * @throws {FileOperationError} If file doesn't exist or reading fails\n * @example\n * // Read as string\n * const content = readFile('file.txt');\n *\n * // Read as Buffer (for binary files like images)\n * const buffer = readFile('image.png', true);\n */\nexport function readFile(file: string): string;\nexport function readFile(file: string, asBuffer: true): Buffer;\nexport function readFile(file: string, asBuffer: false): string;\nexport function readFile(file: string, asBuffer?: boolean): string | Buffer;\n/**\n * Read a file from disk and return its contents as a UTF-8 string by default or as a raw Buffer.\n *\n * @param file - Path to the file to read.\n * @param asBuffer - If `true`, return a raw `Buffer`; if `false` or omitted, return the file decoded as a UTF-8 `string`.\n * @returns A `Buffer` when `asBuffer` is `true`, otherwise the file content as a UTF-8 `string`.\n * @throws FileOperationError when the file cannot be read; the original error is attached as the cause.\n */\nexport function readFile(file: string, asBuffer?: boolean): string | Buffer {\n try {\n return asBuffer ? readFileSync(file) : readFileSync(file, \"utf8\");\n } catch (error) {\n throw new FileOperationError(\n \"read\",\n file,\n error instanceof Error ? error : undefined\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkCA,SAAgB,SAAS,MAAc,UAAqC;AACxE,KAAI;AACA,SAAO,WAAW,aAAa,KAAK,GAAG,aAAa,MAAM,OAAO;UAC5D,OAAO;AACZ,QAAM,IAAI,mBACN,QACA,MACA,iBAAiB,QAAQ,QAAQ,OACpC"}
package/dist/run.d.ts CHANGED
@@ -1,23 +1,21 @@
1
- import { t as Settings } from "./types-DtDQlx-T.js";
1
+ import { n as MinifierOptions, t as CompressorOptions } from "./types-BUlX1Zbb.js";
2
2
 
3
3
  //#region src/run.d.ts
4
4
 
5
- interface RunParameters {
6
- settings: Settings;
7
- content?: string;
8
- index?: number;
9
- }
10
5
  /**
11
- * Execute the compressor and write output.
12
- * @param params - Run parameters containing settings, content, and index
13
- * @returns Minified content string
14
- * @throws {ValidationError} If settings or compressor is missing
6
+ * Run the configured compressor and persist its outputs according to the provided settings.
7
+ *
8
+ * @param settings - Compressor settings including output targets and the `compressor` implementation
9
+ * @param content - The input content to be compressed
10
+ * @param index - Optional index used when processing multiple inputs
11
+ * @returns The minified code produced by the compressor
12
+ * @throws {ValidationError} If `settings` is missing or `settings.compressor` is not provided
15
13
  */
16
- declare function run({
14
+ declare function run<T extends CompressorOptions = CompressorOptions>({
17
15
  settings,
18
16
  content,
19
17
  index
20
- }: RunParameters): Promise<string>;
18
+ }: MinifierOptions<T>): Promise<string>;
21
19
  //#endregion
22
20
  export { run };
23
21
  //# sourceMappingURL=run.d.ts.map
package/dist/run.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"run.d.ts","names":[],"sources":["../src/run.ts"],"sourcesContent":[],"mappings":";;;;UAUU,aAAA,CAcN;EACA,QAAA,EAdU,QAcV;EACD,OAAA,CAAA,EAAA,MAAA;EAAgB,KAAA,CAAA,EAAA,MAAA;;;;;;;;iBAJG,GAAA;;;;GAInB,gBAAgB"}
1
+ {"version":3,"file":"run.d.ts","names":[],"sources":["../src/run.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;iBAyBsB,cAAc,oBAAoB;;;;GAIrD,gBAAgB,KAAK"}
package/dist/run.js CHANGED
@@ -1,12 +1,21 @@
1
- import { n as ValidationError } from "./error-B_uK-F18.js";
2
- import { t as writeFile } from "./writeFile-CwK9ZWXr.js";
1
+ import { n as ValidationError } from "./error-Ck87RwDD.js";
2
+ import { t as writeFile } from "./writeFile-BRfs9FqY.js";
3
+ import { join, parse } from "node:path";
3
4
 
4
5
  //#region src/run.ts
6
+ /*!
7
+ * node-minify
8
+ * Copyright(c) 2011-2025 Rodolphe Stoclin
9
+ * MIT Licensed
10
+ */
5
11
  /**
6
- * Execute the compressor and write output.
7
- * @param params - Run parameters containing settings, content, and index
8
- * @returns Minified content string
9
- * @throws {ValidationError} If settings or compressor is missing
12
+ * Run the configured compressor and persist its outputs according to the provided settings.
13
+ *
14
+ * @param settings - Compressor settings including output targets and the `compressor` implementation
15
+ * @param content - The input content to be compressed
16
+ * @param index - Optional index used when processing multiple inputs
17
+ * @returns The minified code produced by the compressor
18
+ * @throws {ValidationError} If `settings` is missing or `settings.compressor` is not provided
10
19
  */
11
20
  async function run({ settings, content, index }) {
12
21
  if (!settings) throw new ValidationError("Settings must be provided");
@@ -19,8 +28,29 @@ async function run({ settings, content, index }) {
19
28
  writeOutput(result, settings, index);
20
29
  return result.code;
21
30
  }
31
+ /**
32
+ * Write compressor result outputs to disk unless the operation is in-memory.
33
+ *
34
+ * Writes multiple output files when `result.outputs` is provided, writes a binary `result.buffer` to the configured output when present, otherwise writes `result.code`. If a source map is available and a source map URL can be resolved from `settings`, the map is written alongside the main output.
35
+ *
36
+ * @param result - The compressor result containing `code`, optional `buffer`, `map`, or `outputs` describing one or more outputs
37
+ * @param settings - Settings that include output destination(s) and optional in-memory `content` which disables disk writes
38
+ * @param index - Optional index used when writing to multiple targets or when tracking a particular input within a batch
39
+ */
22
40
  function writeOutput(result, settings, index) {
23
41
  if (Boolean(settings.content) || !settings.output) return;
42
+ if (result.outputs && result.outputs.length > 0) {
43
+ writeMultipleOutputs(result.outputs, settings, index);
44
+ return;
45
+ }
46
+ if (result.buffer) {
47
+ writeFile({
48
+ file: settings.output,
49
+ content: result.buffer,
50
+ index
51
+ });
52
+ return;
53
+ }
24
54
  writeFile({
25
55
  file: settings.output,
26
56
  content: result.code,
@@ -35,6 +65,71 @@ function writeOutput(result, settings, index) {
35
65
  });
36
66
  }
37
67
  }
68
+ /**
69
+ * Extract the first input file path from the input configuration.
70
+ *
71
+ * @param input - A single file path, an array of paths, or undefined
72
+ * @returns The first input file path, or an empty string if none available
73
+ */
74
+ function getFirstInputFile(input) {
75
+ if (typeof input === "string") return input;
76
+ if (Array.isArray(input) && input.length > 0) return input[0] ?? "";
77
+ return "";
78
+ }
79
+ /**
80
+ * Write multiple output files produced by a compressor according to the settings' output configuration.
81
+ *
82
+ * This writes each provided output entry to a computed target path:
83
+ * - If `settings.output` is an array, a non-empty array item (not "$1") at the same index is used verbatim as the target path.
84
+ * - If `settings.output` is the string "$1", the target is generated from the first input filename and the output's `format` (or "out" if missing).
85
+ * - If `settings.output` contains "$1", every "$1" is replaced with the input base name and the output's `format` is appended.
86
+ * - If `settings.output` is a plain string, that string is used with the output's `format` appended.
87
+ * - If no usable output pattern is provided, a default path is generated from the input filename and the output's `format`.
88
+ *
89
+ * Each output's `content` is written to its resolved path using `writeFile`. The first input (if any) is used to derive base names and directories for auto-generated targets.
90
+ *
91
+ * @param outputs - Array of compressor outputs (each may include `content` and optional `format`) to write.
92
+ * @param settings - Settings used to resolve output targets (may supply `output` and `input`).
93
+ * @param index - Optional index forwarded to the file writer when writing each output.
94
+ */
95
+ function writeMultipleOutputs(outputs, settings, index) {
96
+ const output = settings.output;
97
+ const isArrayOutput = Array.isArray(output);
98
+ const outputsArray = isArrayOutput ? output : [output];
99
+ const inputFile = getFirstInputFile(settings.input);
100
+ const inputDir = parse(inputFile).dir;
101
+ const inputBase = parse(inputFile).name;
102
+ for (let i = 0; i < outputs.length; i++) {
103
+ const outputResult = outputs[i];
104
+ if (!outputResult) continue;
105
+ const format = outputResult.format || "out";
106
+ let targetFile;
107
+ const arrayItem = outputsArray[i];
108
+ if (isArrayOutput && arrayItem !== void 0 && arrayItem !== "$1" && arrayItem.trim() !== "") targetFile = arrayItem;
109
+ else if (typeof output === "string" && output === "$1") {
110
+ const baseName = inputBase || "output";
111
+ targetFile = inputDir ? join(inputDir, `${baseName}.${format}`) : `${baseName}.${format}`;
112
+ } else if (typeof output === "string" && output.includes("$1")) {
113
+ const extensionlessName = inputBase || "output";
114
+ targetFile = `${output.replace(/\$1/g, extensionlessName)}.${format}`;
115
+ } else if (typeof output === "string") targetFile = `${output}.${format}`;
116
+ else {
117
+ const baseName = inputBase || "output";
118
+ targetFile = inputDir ? join(inputDir, `${baseName}.${format}`) : `${baseName}.${format}`;
119
+ }
120
+ writeFile({
121
+ file: targetFile,
122
+ content: outputResult.content,
123
+ index
124
+ });
125
+ }
126
+ }
127
+ /**
128
+ * Resolve the configured source map path or URL from the provided settings.
129
+ *
130
+ * @param settings - Minifier settings that may include `options.sourceMap` or `options._sourceMap`
131
+ * @returns The source map URL or filename when configured, `undefined` otherwise.
132
+ */
38
133
  function getSourceMapUrl(settings) {
39
134
  const options = settings.options;
40
135
  if (!options) return;
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-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport type { CompressorResult, Settings } from \"@node-minify/types\";\nimport { ValidationError } from \"./error.ts\";\nimport { writeFile } from \"./writeFile.ts\";\n\ninterface RunParameters {\n settings: Settings;\n content?: string;\n index?: number;\n}\n\n/**\n * Execute the compressor and write output.\n * @param params - Run parameters containing settings, content, and index\n * @returns Minified content string\n * @throws {ValidationError} If settings or compressor is missing\n */\nexport async function run({\n settings,\n content,\n index,\n}: RunParameters): 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 writeOutput(result, settings, index);\n\n return result.code;\n}\n\nfunction writeOutput(\n result: CompressorResult,\n settings: Settings,\n index?: number\n): void {\n const isInMemoryMode = Boolean(settings.content);\n if (isInMemoryMode || !settings.output) {\n return;\n }\n\n writeFile({ file: settings.output, content: result.code, index });\n\n if (result.map) {\n const sourceMapUrl = getSourceMapUrl(settings);\n if (sourceMapUrl) {\n writeFile({ file: sourceMapUrl, content: result.map, index });\n }\n }\n}\n\nfunction getSourceMapUrl(settings: Settings): 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":";;;;;;;;;;AAsBA,eAAsB,IAAI,EACtB,UACA,SACA,SAC+B;AAC/B,KAAI,CAAC,SACD,OAAM,IAAI,gBAAgB,4BAA4B;AAG1D,KAAI,CAAC,SAAS,WACV,OAAM,IAAI,gBAAgB,0CAA0C;CAGxE,MAAM,SAAS,MAAM,SAAS,WAAW;EACrC;EACA;EACA;EACH,CAAC;AAEF,aAAY,QAAQ,UAAU,MAAM;AAEpC,QAAO,OAAO;;AAGlB,SAAS,YACL,QACA,UACA,OACI;AAEJ,KADuB,QAAQ,SAAS,QAAQ,IAC1B,CAAC,SAAS,OAC5B;AAGJ,WAAU;EAAE,MAAM,SAAS;EAAQ,SAAS,OAAO;EAAM;EAAO,CAAC;AAEjE,KAAI,OAAO,KAAK;EACZ,MAAM,eAAe,gBAAgB,SAAS;AAC9C,MAAI,aACA,WAAU;GAAE,MAAM;GAAc,SAAS,OAAO;GAAK;GAAO,CAAC;;;AAKzE,SAAS,gBAAgB,UAAwC;CAC7D,MAAM,UAAU,SAAS;AACzB,KAAI,CAAC,QACD;CAGJ,MAAM,YAAY,QAAQ;AAC1B,KAAI,WAAW;AACX,MAAI,OAAO,UAAU,QAAQ,SACzB,QAAO,UAAU;AAErB,MAAI,OAAO,UAAU,aAAa,SAC9B,QAAO,UAAU;;CAIzB,MAAM,aAAa,QAAQ;AAG3B,KAAI,cAAc,OAAO,WAAW,QAAQ,SACxC,QAAO,WAAW"}
1
+ {"version":3,"file":"run.js","names":["targetFile: string"],"sources":["../src/run.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 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 { writeFile } from \"./writeFile.ts\";\n\n/**\n * Run the configured compressor and persist its outputs according to the provided settings.\n *\n * @param settings - Compressor settings including output targets 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 or `settings.compressor` is not provided\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 writeOutput(result, settings, index);\n\n return result.code;\n}\n\n/**\n * Write compressor result outputs to disk unless the operation is in-memory.\n *\n * Writes multiple output files when `result.outputs` is provided, writes a binary `result.buffer` to the configured output when present, otherwise writes `result.code`. If a source map is available and a source map URL can be resolved from `settings`, the map is written alongside the main output.\n *\n * @param result - The compressor result containing `code`, optional `buffer`, `map`, or `outputs` describing one or more outputs\n * @param settings - Settings that include output destination(s) and optional in-memory `content` which disables disk writes\n * @param index - Optional index used when writing to multiple targets or when tracking a particular input within a batch\n */\nfunction writeOutput<T extends CompressorOptions = CompressorOptions>(\n result: CompressorResult,\n settings: Settings<T>,\n index?: number\n): void {\n const isInMemoryMode = Boolean(settings.content);\n if (isInMemoryMode || !settings.output) {\n return;\n }\n\n // Handle multi-output (for image conversion to multiple formats)\n if (result.outputs && result.outputs.length > 0) {\n writeMultipleOutputs(result.outputs, settings, index);\n return;\n }\n\n // Handle single buffer output (for binary images)\n if (result.buffer) {\n writeFile({ file: settings.output, content: result.buffer, index });\n return;\n }\n\n // Default: write code (string) output\n writeFile({ file: settings.output, content: result.code, index });\n\n if (result.map) {\n const sourceMapUrl = getSourceMapUrl(settings);\n if (sourceMapUrl) {\n writeFile({ file: sourceMapUrl, content: result.map, index });\n }\n }\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 multiple output files produced by a compressor according to the settings' output configuration.\n *\n * This writes each provided output entry to a computed target path:\n * - If `settings.output` is an array, a non-empty array item (not \"$1\") at the same index is used verbatim as the target path.\n * - If `settings.output` is the string \"$1\", the target is generated from the first input filename and the output's `format` (or \"out\" if missing).\n * - If `settings.output` contains \"$1\", every \"$1\" is replaced with the input base name and the output's `format` is appended.\n * - If `settings.output` is a plain string, that string is used with the output's `format` appended.\n * - If no usable output pattern is provided, a default path is generated from the input filename and the output's `format`.\n *\n * Each output's `content` is written to its resolved path using `writeFile`. The first input (if any) is used to derive base names and directories for auto-generated targets.\n *\n * @param outputs - Array of compressor outputs (each may include `content` and optional `format`) to write.\n * @param settings - Settings used to resolve output targets (may supply `output` and `input`).\n * @param index - Optional index forwarded to the file writer when writing each output.\n */\nfunction writeMultipleOutputs<T extends CompressorOptions = CompressorOptions>(\n outputs: NonNullable<CompressorResult[\"outputs\"]>,\n settings: Settings<T>,\n index?: number\n): 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 for (let i = 0; i < outputs.length; i++) {\n const outputResult = outputs[i];\n if (!outputResult) {\n continue;\n }\n\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 writeFile({ file: targetFile, content: outputResult.content, index });\n }\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":";;;;;;;;;;;;;;;;;;;AAyBA,eAAsB,IAAqD,EACvE,UACA,SACA,SACoC;AACpC,KAAI,CAAC,SACD,OAAM,IAAI,gBAAgB,4BAA4B;AAG1D,KAAI,CAAC,SAAS,WACV,OAAM,IAAI,gBAAgB,0CAA0C;CAGxE,MAAM,SAAS,MAAM,SAAS,WAAW;EACrC;EACA;EACA;EACH,CAAC;AAEF,aAAY,QAAQ,UAAU,MAAM;AAEpC,QAAO,OAAO;;;;;;;;;;;AAYlB,SAAS,YACL,QACA,UACA,OACI;AAEJ,KADuB,QAAQ,SAAS,QAAQ,IAC1B,CAAC,SAAS,OAC5B;AAIJ,KAAI,OAAO,WAAW,OAAO,QAAQ,SAAS,GAAG;AAC7C,uBAAqB,OAAO,SAAS,UAAU,MAAM;AACrD;;AAIJ,KAAI,OAAO,QAAQ;AACf,YAAU;GAAE,MAAM,SAAS;GAAQ,SAAS,OAAO;GAAQ;GAAO,CAAC;AACnE;;AAIJ,WAAU;EAAE,MAAM,SAAS;EAAQ,SAAS,OAAO;EAAM;EAAO,CAAC;AAEjE,KAAI,OAAO,KAAK;EACZ,MAAM,eAAe,gBAAgB,SAAS;AAC9C,MAAI,aACA,WAAU;GAAE,MAAM;GAAc,SAAS,OAAO;GAAK;GAAO,CAAC;;;;;;;;;AAWzE,SAAS,kBAAkB,OAA8C;AACrE,KAAI,OAAO,UAAU,SACjB,QAAO;AAEX,KAAI,MAAM,QAAQ,MAAM,IAAI,MAAM,SAAS,EACvC,QAAO,MAAM,MAAM;AAEvB,QAAO;;;;;;;;;;;;;;;;;;AAmBX,SAAS,qBACL,SACA,UACA,OACI;CACJ,MAAM,SAAS,SAAS;CACxB,MAAM,gBAAgB,MAAM,QAAQ,OAAO;CAC3C,MAAM,eAAe,gBAAgB,SAAS,CAAC,OAAO;CACtD,MAAM,YAAY,kBAAkB,SAAS,MAAM;CACnD,MAAM,WAAW,MAAM,UAAU,CAAC;CAClC,MAAM,YAAY,MAAM,UAAU,CAAC;AAEnC,MAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACrC,MAAM,eAAe,QAAQ;AAC7B,MAAI,CAAC,aACD;EAGJ,MAAM,SAAS,aAAa,UAAU;EACtC,IAAIA;EAEJ,MAAM,YAAY,aAAa;AAE/B,MACI,iBACA,cAAc,UACd,cAAc,QACd,UAAU,MAAM,KAAK,GAGrB,cAAa;WACN,OAAO,WAAW,YAAY,WAAW,MAAM;GAEtD,MAAM,WAAW,aAAa;AAC9B,gBAAa,WACP,KAAK,UAAU,GAAG,SAAS,GAAG,SAAS,GACvC,GAAG,SAAS,GAAG;aACd,OAAO,WAAW,YAAY,OAAO,SAAS,KAAK,EAAE;GAE5D,MAAM,oBAAoB,aAAa;AAEvC,gBAAa,GADM,OAAO,QAAQ,QAAQ,kBAAkB,CACjC,GAAG;aACvB,OAAO,WAAW,SAEzB,cAAa,GAAG,OAAO,GAAG;OACvB;GAEH,MAAM,WAAW,aAAa;AAC9B,gBAAa,WACP,KAAK,UAAU,GAAG,SAAS,GAAG,SAAS,GACvC,GAAG,SAAS,GAAG;;AAGzB,YAAU;GAAE,MAAM;GAAY,SAAS,aAAa;GAAS;GAAO,CAAC;;;;;;;;;AAU7E,SAAS,gBACL,UACkB;CAClB,MAAM,UAAU,SAAS;AACzB,KAAI,CAAC,QACD;CAGJ,MAAM,YAAY,QAAQ;AAC1B,KAAI,WAAW;AACX,MAAI,OAAO,UAAU,QAAQ,SACzB,QAAO,UAAU;AAErB,MAAI,OAAO,UAAU,aAAa,SAC9B,QAAO,UAAU;;CAIzB,MAAM,aAAa,QAAQ;AAG3B,KAAI,cAAc,OAAO,WAAW,QAAQ,SACxC,QAAO,WAAW"}
@@ -1,4 +1,4 @@
1
- import { n as ValidationError } from "./error-B_uK-F18.js";
1
+ import { n as ValidationError } from "./error-Ck87RwDD.js";
2
2
 
3
3
  //#region src/setFileNameMin.ts
4
4
  /*!
@@ -1,11 +1,47 @@
1
1
  //#region ../types/src/types.d.ts
2
2
 
3
+ /**
4
+ * Output result for multi-format image compression.
5
+ */
6
+ type CompressorOutput = {
7
+ /**
8
+ * Format of the output (e.g., 'webp', 'avif').
9
+ */
10
+ format?: string;
11
+
12
+ /**
13
+ * Output content as string or Buffer.
14
+ */
15
+ content: string | Buffer;
16
+ };
3
17
  /**
4
18
  * Result returned by a compressor function.
5
19
  */
6
20
  type CompressorResult = {
21
+ /**
22
+ * Minified content as string (for text-based formats like JS, CSS, HTML, SVG).
23
+ */
7
24
  code: string;
25
+
26
+ /**
27
+ * Source map (for JS/CSS compressors).
28
+ */
8
29
  map?: string;
30
+
31
+ /**
32
+ * Minified content as Buffer (for binary formats like images).
33
+ * @example
34
+ * When using sharp for PNG/WebP compression
35
+ */
36
+ buffer?: Buffer;
37
+
38
+ /**
39
+ * Multiple outputs for multi-format image compression.
40
+ * Used when converting to multiple formats simultaneously.
41
+ * @example
42
+ * [{ format: 'webp', content: <Buffer> }, { format: 'avif', content: <Buffer> }]
43
+ */
44
+ outputs?: CompressorOutput[];
9
45
  };
10
46
  /**
11
47
  * Base options that all compressors can accept.
@@ -53,8 +89,10 @@ type Settings<TOptions extends CompressorOptions = CompressorOptions> = {
53
89
  /**
54
90
  * Content to minify (for in-memory minification).
55
91
  * If provided, input/output are not required.
92
+ * For text-based formats (JS, CSS, HTML, SVG): string
93
+ * For binary formats (images): Buffer (handled internally by image compressors)
56
94
  */
57
- content?: string;
95
+ content?: string | Buffer;
58
96
 
59
97
  /**
60
98
  * Input file path(s) or glob pattern.
@@ -136,8 +174,11 @@ type MinifierOptions<TOptions extends CompressorOptions = CompressorOptions> = {
136
174
 
137
175
  /**
138
176
  * The content to minify.
177
+ * For text-based formats (JS, CSS, HTML, SVG): string
178
+ * For binary formats (images): Buffer
179
+ * For multiple binary files: Buffer[]
139
180
  */
140
- content?: string;
181
+ content?: string | Buffer | Buffer[];
141
182
 
142
183
  /**
143
184
  * Index of current file when processing multiple files.
@@ -145,5 +186,5 @@ type MinifierOptions<TOptions extends CompressorOptions = CompressorOptions> = {
145
186
  index?: number;
146
187
  };
147
188
  //#endregion
148
- export { Settings as t };
149
- //# sourceMappingURL=types-DtDQlx-T.d.ts.map
189
+ export { MinifierOptions as n, Settings as r, CompressorOptions as t };
190
+ //# sourceMappingURL=types-BUlX1Zbb.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types-BUlX1Zbb.d.ts","names":["CompressorReturnType","ImageFormat","CompressorOutput","Buffer","CompressorResult","CompressorOptions","Record","Compressor","TOptions","MinifierOptions","Promise","FileType","Settings","Result","MinifyOptions"],"sources":["../../types/src/types.d.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * The return type of a compressor function.\n * @deprecated Use `CompressorResult` instead. Will be removed in v11.\n */\nexport type CompressorReturnType = string;\n\n/**\n * Supported image formats for image compression.\n */\nexport type ImageFormat =\n | \"webp\"\n | \"avif\"\n | \"png\"\n | \"jpeg\"\n | \"jpg\"\n | \"gif\"\n | \"tiff\"\n | \"heif\"\n | \"heic\";\n\n/**\n * Output result for multi-format image compression.\n */\nexport type CompressorOutput = {\n /**\n * Format of the output (e.g., 'webp', 'avif').\n */\n format?: string;\n\n /**\n * Output content as string or Buffer.\n */\n content: string | Buffer;\n};\n\n/**\n * Result returned by a compressor function.\n */\nexport type CompressorResult = {\n /**\n * Minified content as string (for text-based formats like JS, CSS, HTML, SVG).\n */\n code: string;\n\n /**\n * Source map (for JS/CSS compressors).\n */\n map?: string;\n\n /**\n * Minified content as Buffer (for binary formats like images).\n * @example\n * When using sharp for PNG/WebP compression\n */\n buffer?: Buffer;\n\n /**\n * Multiple outputs for multi-format image compression.\n * Used when converting to multiple formats simultaneously.\n * @example\n * [{ format: 'webp', content: <Buffer> }, { format: 'avif', content: <Buffer> }]\n */\n outputs?: CompressorOutput[];\n};\n\n/**\n * Base options that all compressors can accept.\n * Specific compressors may extend this with their own options.\n */\nexport type CompressorOptions = Record<string, unknown>;\n\n/**\n * A compressor function that minifies content.\n * @param args - The minifier options including settings and content\n * @returns A promise resolving to the compression result\n */\nexport type Compressor<TOptions extends CompressorOptions = CompressorOptions> =\n (args: MinifierOptions<TOptions>) => Promise<CompressorResult>;\n\n/**\n * File type for compressors that support multiple types (e.g., YUI).\n */\nexport type FileType = \"js\" | \"css\";\n\n/**\n * User-facing settings for the minify function.\n * This is what users pass when calling minify().\n *\n * @example\n * ```ts\n * import { minify } from '@node-minify/core';\n * import { terser } from '@node-minify/terser';\n *\n * await minify({\n * compressor: terser,\n * input: 'src/*.js',\n * output: 'dist/bundle.min.js',\n * options: { mangle: true }\n * });\n * ```\n */\nexport type Settings<TOptions extends CompressorOptions = CompressorOptions> = {\n /**\n * The compressor function to use for minification.\n */\n compressor: Compressor<TOptions>;\n\n /**\n * Optional label for the compressor (used in logging).\n */\n compressorLabel?: string;\n\n /**\n * Content to minify (for in-memory minification).\n * If provided, input/output are not required.\n * For text-based formats (JS, CSS, HTML, SVG): string\n * For binary formats (images): Buffer (handled internally by image compressors)\n */\n content?: string | Buffer;\n\n /**\n * Input file path(s) or glob pattern.\n * Can be a single file, array of files, or wildcard pattern.\n *\n * @example\n * - 'src/app.js'\n * - ['src/a.js', 'src/b.js']\n * - 'src/**\\/*.js'\n */\n input?: string | string[];\n\n /**\n * Output file path.\n * Use $1 as placeholder for input filename in multi-file scenarios.\n * Can be a single file, array of files, or pattern with $1.\n *\n * @example\n * - 'dist/bundle.min.js'\n * - ['file1.min.js', 'file2.min.js']\n * - '$1.min.js' (creates app.min.js from app.js)\n */\n output?: string | string[];\n\n /**\n * Compressor-specific options.\n * See individual compressor documentation for available options.\n */\n options?: TOptions;\n\n /**\n * CLI option string (used by CLI only).\n * @internal\n */\n option?: string;\n\n /**\n * Buffer size for file operations (in bytes).\n * @default 1024000 (1MB)\n */\n buffer?: number;\n\n /**\n * File type for compressors that support multiple types.\n * Required for YUI compressor.\n */\n type?: FileType;\n\n /**\n * Suppress console output.\n * @default false\n */\n silence?: boolean;\n\n /**\n * Public folder to prepend to input paths.\n *\n * @example\n * With publicFolder: 'public/js/' and input: 'app.js',\n * the actual path becomes 'public/js/app.js'\n */\n publicFolder?: string;\n\n /**\n * Replace files in place instead of creating new output files.\n * @default false\n */\n replaceInPlace?: boolean;\n};\n\n/**\n * Options passed to compressor functions internally.\n * This is what compressors receive, not what users pass.\n */\nexport type MinifierOptions<\n TOptions extends CompressorOptions = CompressorOptions,\n> = {\n /**\n * The full settings object.\n */\n settings: Settings<TOptions>;\n\n /**\n * The content to minify.\n * For text-based formats (JS, CSS, HTML, SVG): string\n * For binary formats (images): Buffer\n * For multiple binary files: Buffer[]\n */\n content?: string | Buffer | Buffer[];\n\n /**\n * Index of current file when processing multiple files.\n */\n index?: number;\n};\n\n/**\n * Result returned after compression (used by CLI).\n */\nexport type Result = {\n /**\n * Label of the compressor used.\n */\n compressorLabel: string;\n\n /**\n * Size of minified content (formatted string, e.g., \"1.5 KB\").\n */\n size: string;\n\n /**\n * Gzipped size of minified content (formatted string).\n */\n sizeGzip: string;\n};\n\n/**\n * Type alias for user convenience.\n * @deprecated Use `Settings` instead. Will be removed in v11.\n */\nexport type MinifyOptions<\n TOptions extends CompressorOptions = CompressorOptions,\n> = Settings<TOptions>;\n"],"mappings":";;;;;AAqNsC,KAxL1BE,gBAAAA,GAwL0B;;;;;;;;;oBA/KhBC;;;;;KAMVC,gBAAAA;;;;;;;;;;;;;;;;WAgBCD;;;;;;;;YAQCD;;;;;;KAOFG,iBAAAA,GAAoBC;;;;;;KAOpBC,4BAA4BF,oBAAoBA,4BACjDI,gBAAgBD,cAAcE,QAAQN;;;;KAKrCO,QAAAA;;;;;;;;;;;;;;;;;;KAmBAC,0BAA0BP,oBAAoBA;;;;cAI1CE,WAAWC;;;;;;;;;;;;;qBAaJL;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YA6BTK;;;;;;;;;;;;;;;;;;SAkBHG;;;;;;;;;;;;;;;;;;;;;;;;;;;KA4BCF,iCACSJ,oBAAoBA;;;;YAK3BO,SAASJ;;;;;;;;qBAQAL,SAASA"}
@@ -1,4 +1,4 @@
1
- import { n as ValidationError, t as FileOperationError } from "./error-B_uK-F18.js";
1
+ import { n as ValidationError, t as FileOperationError } from "./error-Ck87RwDD.js";
2
2
  import { existsSync, lstatSync, writeFileSync } from "node:fs";
3
3
 
4
4
  //#region src/writeFile.ts
@@ -8,14 +8,16 @@ import { existsSync, lstatSync, writeFileSync } from "node:fs";
8
8
  * MIT Licensed
9
9
  */
10
10
  /**
11
- * Write content into file.
12
- * @param params Object containing file path, content and optional index
13
- * @returns Written content
14
- * @throws {ValidationError} If no target file is provided
15
- * @throws {FileOperationError} If file operations fail
16
- * @example
17
- * writeFile({ file: 'output.js', content: 'console.log("Hello")' })
18
- * writeFile({ file: ['file1.js', 'file2.js'], content: 'shared content', index: 0 })
11
+ * Write provided content to a target file.
12
+ *
13
+ * When `file` is an array and `index` is provided, the file at that index is used; otherwise `file` is used directly.
14
+ *
15
+ * @param file - Target path or array of target paths
16
+ * @param content - Content to write; may be a `string` or `Buffer`
17
+ * @param index - Optional index to select a file when `file` is an array
18
+ * @returns The same `content` value that was written
19
+ * @throws ValidationError If no target file, no content, or the resolved target path is invalid
20
+ * @throws FileOperationError If the underlying filesystem write fails (wraps the original error)
19
21
  */
20
22
  function writeFile({ file, content, index }) {
21
23
  try {
@@ -24,7 +26,7 @@ function writeFile({ file, content, index }) {
24
26
  const targetFile = index !== void 0 ? Array.isArray(file) ? file[index] : file : file;
25
27
  if (typeof targetFile !== "string") throw new ValidationError("Invalid target file path");
26
28
  if (!(!existsSync(targetFile) || !lstatSync(targetFile).isDirectory())) throw new Error("Target path exists and is a directory");
27
- writeFileSync(targetFile, content, "utf8");
29
+ writeFileSync(targetFile, content, Buffer.isBuffer(content) ? void 0 : "utf8");
28
30
  return content;
29
31
  } catch (error) {
30
32
  if (error instanceof ValidationError) throw error;
@@ -34,4 +36,4 @@ function writeFile({ file, content, index }) {
34
36
 
35
37
  //#endregion
36
38
  export { writeFile as t };
37
- //# sourceMappingURL=writeFile-CwK9ZWXr.js.map
39
+ //# sourceMappingURL=writeFile-BRfs9FqY.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writeFile-BRfs9FqY.js","names":[],"sources":["../src/writeFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { existsSync, lstatSync, writeFileSync } from \"node:fs\";\nimport { FileOperationError, ValidationError } from \"./error.ts\";\n\ninterface WriteFileParams {\n file: string | string[];\n content: string | Buffer;\n index?: number;\n}\n\n/**\n * Write provided content to a target file.\n *\n * When `file` is an array and `index` is provided, the file at that index is used; otherwise `file` is used directly.\n *\n * @param file - Target path or array of target paths\n * @param content - Content to write; may be a `string` or `Buffer`\n * @param index - Optional index to select a file when `file` is an array\n * @returns The same `content` value that was written\n * @throws ValidationError If no target file, no content, or the resolved target path is invalid\n * @throws FileOperationError If the underlying filesystem write fails (wraps the original error)\n */\nexport function writeFile({\n file,\n content,\n index,\n}: WriteFileParams): string | Buffer {\n try {\n if (!file) {\n throw new ValidationError(\"No target file provided\");\n }\n\n if (!content) {\n throw new ValidationError(\"No content provided\");\n }\n\n const targetFile =\n index !== undefined\n ? Array.isArray(file)\n ? file[index]\n : file\n : file;\n\n if (typeof targetFile !== \"string\") {\n throw new ValidationError(\"Invalid target file path\");\n }\n\n const shouldWrite =\n !existsSync(targetFile) || !lstatSync(targetFile).isDirectory();\n\n if (!shouldWrite) {\n throw new Error(\"Target path exists and is a directory\");\n }\n\n writeFileSync(\n targetFile,\n content,\n Buffer.isBuffer(content) ? undefined : \"utf8\"\n );\n return content;\n } catch (error) {\n if (error instanceof ValidationError) {\n throw error;\n }\n throw new FileOperationError(\n \"write to\",\n typeof file === \"string\" ? file : \"multiple files\",\n error as Error\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,UAAU,EACtB,MACA,SACA,SACiC;AACjC,KAAI;AACA,MAAI,CAAC,KACD,OAAM,IAAI,gBAAgB,0BAA0B;AAGxD,MAAI,CAAC,QACD,OAAM,IAAI,gBAAgB,sBAAsB;EAGpD,MAAM,aACF,UAAU,SACJ,MAAM,QAAQ,KAAK,GACf,KAAK,SACL,OACJ;AAEV,MAAI,OAAO,eAAe,SACtB,OAAM,IAAI,gBAAgB,2BAA2B;AAMzD,MAAI,EAFA,CAAC,WAAW,WAAW,IAAI,CAAC,UAAU,WAAW,CAAC,aAAa,EAG/D,OAAM,IAAI,MAAM,wCAAwC;AAG5D,gBACI,YACA,SACA,OAAO,SAAS,QAAQ,GAAG,SAAY,OAC1C;AACD,SAAO;UACF,OAAO;AACZ,MAAI,iBAAiB,gBACjB,OAAM;AAEV,QAAM,IAAI,mBACN,YACA,OAAO,SAAS,WAAW,OAAO,kBAClC,MACH"}
@@ -10,14 +10,16 @@ interface WriteFileParams {
10
10
  index?: number;
11
11
  }
12
12
  /**
13
- * Write content into file.
14
- * @param params Object containing file path, content and optional index
15
- * @returns Written content
16
- * @throws {ValidationError} If no target file is provided
17
- * @throws {FileOperationError} If file operations fail
18
- * @example
19
- * writeFile({ file: 'output.js', content: 'console.log("Hello")' })
20
- * writeFile({ file: ['file1.js', 'file2.js'], content: 'shared content', index: 0 })
13
+ * Write provided content to a target file.
14
+ *
15
+ * When `file` is an array and `index` is provided, the file at that index is used; otherwise `file` is used directly.
16
+ *
17
+ * @param file - Target path or array of target paths
18
+ * @param content - Content to write; may be a `string` or `Buffer`
19
+ * @param index - Optional index to select a file when `file` is an array
20
+ * @returns The same `content` value that was written
21
+ * @throws ValidationError If no target file, no content, or the resolved target path is invalid
22
+ * @throws FileOperationError If the underlying filesystem write fails (wraps the original error)
21
23
  */
22
24
  declare function writeFile({
23
25
  file,
@@ -1 +1 @@
1
- {"version":3,"file":"writeFile.d.ts","names":[],"sources":["../src/writeFile.ts"],"sourcesContent":[],"mappings":";;;AAyBA;;;UAhBU,eAAA,CAmBN;EACD,IAAA,EAAA,MAAA,GAAA,MAAA,EAAA;EAA2B,OAAA,EAAA,MAAA,GAlBR,MAkBQ;EAAM,KAAA,CAAA,EAAA,MAAA;;;;;;;;;;;;iBAJpB,SAAA;;;;GAIb,2BAA2B"}
1
+ {"version":3,"file":"writeFile.d.ts","names":[],"sources":["../src/writeFile.ts"],"sourcesContent":[],"mappings":";;;AA2BA;;;UAlBU,eAAA,CAqBN;EACD,IAAA,EAAA,MAAA,GAAA,MAAA,EAAA;EAA2B,OAAA,EAAA,MAAA,GApBR,MAoBQ;EAAM,KAAA,CAAA,EAAA,MAAA;;;;;;;;;;;;;;iBAJpB,SAAA;;;;GAIb,2BAA2B"}
package/dist/writeFile.js CHANGED
@@ -1,3 +1,3 @@
1
- import { t as writeFile } from "./writeFile-CwK9ZWXr.js";
1
+ import { t as writeFile } from "./writeFile-BRfs9FqY.js";
2
2
 
3
3
  export { writeFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-minify/utils",
3
- "version": "10.1.0",
3
+ "version": "10.2.0",
4
4
  "description": "utils for @node-minify",
5
5
  "keywords": [
6
6
  "compressor",
@@ -46,7 +46,7 @@
46
46
  "lint": "biome lint .",
47
47
  "prepublishOnly": "bun run build",
48
48
  "test": "vitest run",
49
- "test:ci": "vitest run --coverage",
49
+ "test:coverage": "vitest run --coverage",
50
50
  "test:watch": "vitest",
51
51
  "typecheck": "tsc --noEmit",
52
52
  "dev": "tsdown src/index.ts --watch"
@@ -56,6 +56,6 @@
56
56
  "gzip-size": "7.0.0"
57
57
  },
58
58
  "devDependencies": {
59
- "@node-minify/types": "workspace:*"
59
+ "@node-minify/types": "10.2.0"
60
60
  }
61
61
  }
@@ -1,30 +0,0 @@
1
- import { t as FileOperationError } from "./error-B_uK-F18.js";
2
- import { existsSync, lstatSync } from "node:fs";
3
-
4
- //#region src/isValidFile.ts
5
- /*!
6
- * node-minify
7
- * Copyright(c) 2011-2025 Rodolphe Stoclin
8
- * MIT Licensed
9
- */
10
- /**
11
- * Check if the path is a valid file.
12
- * @param path Path to check
13
- * @returns true if path exists and is a file, false otherwise
14
- * @throws {FileOperationError} If filesystem operations fail
15
- * @example
16
- * if (isValidFile('path/to/file.js')) {
17
- * // do something
18
- * }
19
- */
20
- function isValidFile(path) {
21
- try {
22
- return existsSync(path) && !lstatSync(path).isDirectory();
23
- } catch (error) {
24
- throw new FileOperationError("validate", path, error);
25
- }
26
- }
27
-
28
- //#endregion
29
- export { isValidFile as t };
30
- //# sourceMappingURL=isValidFile-UAwceQud.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"isValidFile-UAwceQud.js","names":[],"sources":["../src/isValidFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { existsSync, lstatSync } from \"node:fs\";\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"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,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"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types-DtDQlx-T.d.ts","names":["CompressorReturnType","CompressorResult","CompressorOptions","Record","Compressor","TOptions","MinifierOptions","Promise","FileType","Settings","Result","MinifyOptions"],"sources":["../../types/src/types.d.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * The return type of a compressor function.\n * @deprecated Use `CompressorResult` instead. Will be removed in v11.\n */\nexport type CompressorReturnType = string;\n\n/**\n * Result returned by a compressor function.\n */\nexport type CompressorResult = {\n code: string;\n map?: string;\n};\n\n/**\n * Base options that all compressors can accept.\n * Specific compressors may extend this with their own options.\n */\nexport type CompressorOptions = Record<string, unknown>;\n\n/**\n * A compressor function that minifies content.\n * @param args - The minifier options including settings and content\n * @returns A promise resolving to the compression result\n */\nexport type Compressor<TOptions extends CompressorOptions = CompressorOptions> =\n (args: MinifierOptions<TOptions>) => Promise<CompressorResult>;\n\n/**\n * File type for compressors that support multiple types (e.g., YUI).\n */\nexport type FileType = \"js\" | \"css\";\n\n/**\n * User-facing settings for the minify function.\n * This is what users pass when calling minify().\n *\n * @example\n * ```ts\n * import { minify } from '@node-minify/core';\n * import { terser } from '@node-minify/terser';\n *\n * await minify({\n * compressor: terser,\n * input: 'src/*.js',\n * output: 'dist/bundle.min.js',\n * options: { mangle: true }\n * });\n * ```\n */\nexport type Settings<TOptions extends CompressorOptions = CompressorOptions> = {\n /**\n * The compressor function to use for minification.\n */\n compressor: Compressor<TOptions>;\n\n /**\n * Optional label for the compressor (used in logging).\n */\n compressorLabel?: string;\n\n /**\n * Content to minify (for in-memory minification).\n * If provided, input/output are not required.\n */\n content?: string;\n\n /**\n * Input file path(s) or glob pattern.\n * Can be a single file, array of files, or wildcard pattern.\n *\n * @example\n * - 'src/app.js'\n * - ['src/a.js', 'src/b.js']\n * - 'src/**\\/*.js'\n */\n input?: string | string[];\n\n /**\n * Output file path.\n * Use $1 as placeholder for input filename in multi-file scenarios.\n * Can be a single file, array of files, or pattern with $1.\n *\n * @example\n * - 'dist/bundle.min.js'\n * - ['file1.min.js', 'file2.min.js']\n * - '$1.min.js' (creates app.min.js from app.js)\n */\n output?: string | string[];\n\n /**\n * Compressor-specific options.\n * See individual compressor documentation for available options.\n */\n options?: TOptions;\n\n /**\n * CLI option string (used by CLI only).\n * @internal\n */\n option?: string;\n\n /**\n * Buffer size for file operations (in bytes).\n * @default 1024000 (1MB)\n */\n buffer?: number;\n\n /**\n * File type for compressors that support multiple types.\n * Required for YUI compressor.\n */\n type?: FileType;\n\n /**\n * Suppress console output.\n * @default false\n */\n silence?: boolean;\n\n /**\n * Public folder to prepend to input paths.\n *\n * @example\n * With publicFolder: 'public/js/' and input: 'app.js',\n * the actual path becomes 'public/js/app.js'\n */\n publicFolder?: string;\n\n /**\n * Replace files in place instead of creating new output files.\n * @default false\n */\n replaceInPlace?: boolean;\n};\n\n/**\n * Options passed to compressor functions internally.\n * This is what compressors receive, not what users pass.\n */\nexport type MinifierOptions<\n TOptions extends CompressorOptions = CompressorOptions,\n> = {\n /**\n * The full settings object.\n */\n settings: Settings<TOptions>;\n\n /**\n * The content to minify.\n */\n content?: string;\n\n /**\n * Index of current file when processing multiple files.\n */\n index?: number;\n};\n\n/**\n * Result returned after compression (used by CLI).\n */\nexport type Result = {\n /**\n * Label of the compressor used.\n */\n compressorLabel: string;\n\n /**\n * Size of minified content (formatted string, e.g., \"1.5 KB\").\n */\n size: string;\n\n /**\n * Gzipped size of minified content (formatted string).\n */\n sizeGzip: string;\n};\n\n/**\n * Type alias for user convenience.\n * @deprecated Use `Settings` instead. Will be removed in v11.\n */\nexport type MinifyOptions<\n TOptions extends CompressorOptions = CompressorOptions,\n> = Settings<TOptions>;\n"],"mappings":";;AAwDA;;;AAI2BK,KA7CfJ,gBAAAA,GA6CeI;EAAXD,IAAAA,EAAAA,MAAAA;EAwCFC,GAAAA,CAAAA,EAAAA,MAAAA;CAkBHG;AA4BX;;;;AAMcC,KAhIFP,iBAAAA,GAAoBC,MAgIlBM,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA;;;;;;KAzHFL,4BAA4BF,oBAAoBA,4BACjDI,gBAAgBD,cAAcE,QAAQN;;;;KAKrCO,QAAAA;;;;;;;;;;;;;;;;;;KAmBAC,0BAA0BP,oBAAoBA;;;;cAI1CE,WAAWC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YAwCbA;;;;;;;;;;;;;;;;;;SAkBHG;;;;;;;;;;;;;;;;;;;;;;;;;;;KA4BCF,iCACSJ,oBAAoBA;;;;YAK3BO,SAASJ"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"writeFile-CwK9ZWXr.js","names":[],"sources":["../src/writeFile.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\nimport { existsSync, lstatSync, writeFileSync } from \"node:fs\";\nimport { FileOperationError, ValidationError } from \"./error.ts\";\n\ninterface WriteFileParams {\n file: string | string[];\n content: string | Buffer;\n index?: number;\n}\n\n/**\n * Write content into file.\n * @param params Object containing file path, content and optional index\n * @returns Written content\n * @throws {ValidationError} If no target file is provided\n * @throws {FileOperationError} If file operations fail\n * @example\n * writeFile({ file: 'output.js', content: 'console.log(\"Hello\")' })\n * writeFile({ file: ['file1.js', 'file2.js'], content: 'shared content', index: 0 })\n */\nexport function writeFile({\n file,\n content,\n index,\n}: WriteFileParams): string | Buffer {\n try {\n if (!file) {\n throw new ValidationError(\"No target file provided\");\n }\n\n if (!content) {\n throw new ValidationError(\"No content provided\");\n }\n\n const targetFile =\n index !== undefined\n ? Array.isArray(file)\n ? file[index]\n : file\n : file;\n\n if (typeof targetFile !== \"string\") {\n throw new ValidationError(\"Invalid target file path\");\n }\n\n const shouldWrite =\n !existsSync(targetFile) || !lstatSync(targetFile).isDirectory();\n\n if (!shouldWrite) {\n throw new Error(\"Target path exists and is a directory\");\n }\n\n writeFileSync(targetFile, content, \"utf8\");\n return content;\n } catch (error) {\n if (error instanceof ValidationError) {\n throw error;\n }\n throw new FileOperationError(\n \"write to\",\n typeof file === \"string\" ? file : \"multiple files\",\n error as Error\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,UAAU,EACtB,MACA,SACA,SACiC;AACjC,KAAI;AACA,MAAI,CAAC,KACD,OAAM,IAAI,gBAAgB,0BAA0B;AAGxD,MAAI,CAAC,QACD,OAAM,IAAI,gBAAgB,sBAAsB;EAGpD,MAAM,aACF,UAAU,SACJ,MAAM,QAAQ,KAAK,GACf,KAAK,SACL,OACJ;AAEV,MAAI,OAAO,eAAe,SACtB,OAAM,IAAI,gBAAgB,2BAA2B;AAMzD,MAAI,EAFA,CAAC,WAAW,WAAW,IAAI,CAAC,UAAU,WAAW,CAAC,aAAa,EAG/D,OAAM,IAAI,MAAM,wCAAwC;AAG5D,gBAAc,YAAY,SAAS,OAAO;AAC1C,SAAO;UACF,OAAO;AACZ,MAAI,iBAAiB,gBACjB,OAAM;AAEV,QAAM,IAAI,mBACN,YACA,OAAO,SAAS,WAAW,OAAO,kBAClC,MACH"}