@node-minify/core 9.0.2 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 Rodolphe Stoclin
3
+ Copyright (c) 2025 Rodolphe Stoclin
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -6,8 +6,8 @@
6
6
  <br>
7
7
  <a href="https://npmjs.org/package/@node-minify/core"><img src="https://img.shields.io/npm/v/@node-minify/core.svg"></a>
8
8
  <a href="https://npmjs.org/package/@node-minify/core"><img src="https://img.shields.io/npm/dm/@node-minify/core.svg"></a>
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%3Ddevelop&style=flat" /></a>
10
- <a href="https://codecov.io/gh/srod/node-minify"><img src="https://codecov.io/gh/srod/node-minify/branch/develop/graph/badge.svg"></a>
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>
11
11
  </p>
12
12
 
13
13
  # node-minify
@@ -21,14 +21,13 @@ npm install @node-minify/core @node-minify/uglify-js
21
21
  ## Usage
22
22
 
23
23
  ```js
24
- const minify = require('@node-minify/core');
25
- const uglifyJS = require('@node-minify/uglify-js');
24
+ import { minify } from '@node-minify/core';
25
+ import { uglifyJs } from '@node-minify/uglify-js';
26
26
 
27
- minify({
28
- compressor: uglifyJS,
27
+ await minify({
28
+ compressor: uglifyJs,
29
29
  input: 'foo.js',
30
- output: 'bar.js',
31
- callback: function (err, min) {}
30
+ output: 'bar.js'
32
31
  });
33
32
  ```
34
33
 
@@ -38,4 +37,4 @@ Visit https://node-minify.2clics.net for full documentation
38
37
 
39
38
  ## License
40
39
 
41
- [MIT](https://github.com/srod/node-minify/blob/develop/LICENSE)
40
+ [MIT](https://github.com/srod/node-minify/blob/main/LICENSE)
package/dist/index.d.ts CHANGED
@@ -1,14 +1,156 @@
1
- import { Settings } from '@node-minify/types';
1
+ //#region ../types/src/types.d.ts
2
2
 
3
- /*!
4
- * node-minify
5
- * Copyright(c) 2011-2024 Rodolphe Stoclin
6
- * MIT Licensed
3
+ /**
4
+ * Result returned by a compressor function.
7
5
  */
6
+ type CompressorResult = {
7
+ code: string;
8
+ map?: string;
9
+ };
10
+ /**
11
+ * Base options that all compressors can accept.
12
+ * Specific compressors may extend this with their own options.
13
+ */
14
+ type CompressorOptions = Record<string, unknown>;
15
+ /**
16
+ * A compressor function that minifies content.
17
+ * @param args - The minifier options including settings and content
18
+ * @returns A promise resolving to the compression result
19
+ */
20
+ type Compressor<TOptions extends CompressorOptions = CompressorOptions> = (args: MinifierOptions<TOptions>) => Promise<CompressorResult>;
21
+ /**
22
+ * File type for compressors that support multiple types (e.g., YUI).
23
+ */
24
+ type FileType = "js" | "css";
25
+ /**
26
+ * User-facing settings for the minify function.
27
+ * This is what users pass when calling minify().
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { minify } from '@node-minify/core';
32
+ * import { terser } from '@node-minify/terser';
33
+ *
34
+ * await minify({
35
+ * compressor: terser,
36
+ * input: 'src/*.js',
37
+ * output: 'dist/bundle.min.js',
38
+ * options: { mangle: true }
39
+ * });
40
+ * ```
41
+ */
42
+ type Settings<TOptions extends CompressorOptions = CompressorOptions> = {
43
+ /**
44
+ * The compressor function to use for minification.
45
+ */
46
+ compressor: Compressor<TOptions>;
47
+
48
+ /**
49
+ * Optional label for the compressor (used in logging).
50
+ */
51
+ compressorLabel?: string;
52
+
53
+ /**
54
+ * Content to minify (for in-memory minification).
55
+ * If provided, input/output are not required.
56
+ */
57
+ content?: string;
58
+
59
+ /**
60
+ * Input file path(s) or glob pattern.
61
+ * Can be a single file, array of files, or wildcard pattern.
62
+ *
63
+ * @example
64
+ * - 'src/app.js'
65
+ * - ['src/a.js', 'src/b.js']
66
+ * - 'src/**\/*.js'
67
+ */
68
+ input?: string | string[];
69
+
70
+ /**
71
+ * Output file path.
72
+ * Use $1 as placeholder for input filename in multi-file scenarios.
73
+ * Can be a single file, array of files, or pattern with $1.
74
+ *
75
+ * @example
76
+ * - 'dist/bundle.min.js'
77
+ * - ['file1.min.js', 'file2.min.js']
78
+ * - '$1.min.js' (creates app.min.js from app.js)
79
+ */
80
+ output?: string | string[];
81
+
82
+ /**
83
+ * Compressor-specific options.
84
+ * See individual compressor documentation for available options.
85
+ */
86
+ options?: TOptions;
8
87
 
9
- declare const minify: {
10
- (settings: Settings): Promise<unknown>;
11
- default: any;
88
+ /**
89
+ * CLI option string (used by CLI only).
90
+ * @internal
91
+ */
92
+ option?: string;
93
+
94
+ /**
95
+ * Buffer size for file operations (in bytes).
96
+ * @default 1024000 (1MB)
97
+ */
98
+ buffer?: number;
99
+
100
+ /**
101
+ * File type for compressors that support multiple types.
102
+ * Required for YUI compressor.
103
+ */
104
+ type?: FileType;
105
+
106
+ /**
107
+ * Suppress console output.
108
+ * @default false
109
+ */
110
+ silence?: boolean;
111
+
112
+ /**
113
+ * Public folder to prepend to input paths.
114
+ *
115
+ * @example
116
+ * With publicFolder: 'public/js/' and input: 'app.js',
117
+ * the actual path becomes 'public/js/app.js'
118
+ */
119
+ publicFolder?: string;
120
+
121
+ /**
122
+ * Replace files in place instead of creating new output files.
123
+ * @default false
124
+ */
125
+ replaceInPlace?: boolean;
12
126
  };
127
+ /**
128
+ * Options passed to compressor functions internally.
129
+ * This is what compressors receive, not what users pass.
130
+ */
131
+ type MinifierOptions<TOptions extends CompressorOptions = CompressorOptions> = {
132
+ /**
133
+ * The full settings object.
134
+ */
135
+ settings: Settings<TOptions>;
136
+
137
+ /**
138
+ * The content to minify.
139
+ */
140
+ content?: string;
13
141
 
14
- export { minify as default };
142
+ /**
143
+ * Index of current file when processing multiple files.
144
+ */
145
+ index?: number;
146
+ };
147
+ //#endregion
148
+ //#region src/index.d.ts
149
+ /**
150
+ * Run node-minify.
151
+ * @param settings Settings from user input
152
+ */
153
+ declare function minify(settings: Settings): Promise<string>;
154
+ //#endregion
155
+ export { minify };
156
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":["CompressorReturnType","CompressorResult","CompressorOptions","Record","Compressor","TOptions","MinifierOptions","Promise","FileType","Settings","Result","MinifyOptions"],"sources":["../../types/src/types.d.ts","../src/index.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;;;;ACtId;;KDaYL,4BAA4BF,oBAAoBA,4BACjDI,gBAAgBD,cAAcE,QAAQN;;;;KAKrCO,QAAAA;;;;;;;;;;;;;;;;;;KAmBAC,0BAA0BP,oBAAoBA;;;;cAI1CE,WAAWC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YAwCbA;;;;;;;;;;;;;;;;;;SAkBHG;;;;;;;;;;;;;;;;;;;;;;;;;;;KA4BCF,iCACSJ,oBAAoBA;;;;YAK3BO,SAASJ;;;;;;;;;;;;;;;;AAnHvB;AAmBA;AAAsCH,iBCtChB,MAAA,CDsCgBA,QAAAA,ECtCC,QDsCDA,CAAAA,ECtCY,ODsCZA,CAAAA,MAAAA,CAAAA"}
package/dist/index.js CHANGED
@@ -1,265 +1,160 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
1
+ import { compressSingleFile, getContentFromFiles, run, setFileNameMin, setPublicFolder, wildcards } from "@node-minify/utils";
2
+ import fs from "node:fs";
3
+ import { mkdirp } from "mkdirp";
24
4
 
25
- // src/compress.ts
26
- var import_node_fs = __toESM(require("fs"));
27
- var import_utils = require("@node-minify/utils");
28
- var import_mkdirp = require("mkdirp");
29
- var compress = (settings) => {
30
- if (typeof settings.compressor !== "function") {
31
- throw new Error(
32
- "compressor should be a function, maybe you forgot to install the compressor"
33
- );
34
- }
35
- if (settings.output) {
36
- createDirectory(settings.output);
37
- }
38
- if (Array.isArray(settings.output)) {
39
- return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
40
- }
41
- return import_utils.utils.compressSingleFile(settings);
42
- };
43
- var compressArrayOfFilesSync = (settings) => {
44
- return Array.isArray(settings.input) && settings.input.forEach((input, index) => {
45
- const content = import_utils.utils.getContentFromFiles(input);
46
- return import_utils.utils.runSync({ settings, content, index });
47
- });
48
- };
49
- var compressArrayOfFilesAsync = (settings) => {
50
- let sequence = Promise.resolve();
51
- Array.isArray(settings.input) && settings.input.forEach((input, index) => {
52
- const content = import_utils.utils.getContentFromFiles(input);
53
- sequence = sequence.then(
54
- () => import_utils.utils.runAsync({ settings, content, index })
55
- );
56
- });
57
- return sequence;
58
- };
59
- var createDirectory = (file) => {
60
- if (Array.isArray(file)) {
61
- file = file[0];
62
- }
63
- const dir = file?.substr(0, file.lastIndexOf("/"));
64
- if (!dir) {
65
- return;
66
- }
67
- if (!import_node_fs.default.statSync(dir).isDirectory()) {
68
- import_mkdirp.mkdirp.sync(dir);
69
- }
70
- };
5
+ //#region src/compress.ts
6
+ /*!
7
+ * node-minify
8
+ * Copyright(c) 2011-2025 Rodolphe Stoclin
9
+ * MIT Licensed
10
+ */
11
+ /**
12
+ * Module dependencies.
13
+ */
14
+ /**
15
+ * Run compressor.
16
+ * @param settings Settings
17
+ */
18
+ async function compress(settings) {
19
+ if (Array.isArray(settings.output)) {
20
+ if (!Array.isArray(settings.input)) throw new Error("When output is an array, input must also be an array");
21
+ if (settings.input.length !== settings.output.length) throw new Error(`Input and output arrays must have the same length (input: ${settings.input.length}, output: ${settings.output.length})`);
22
+ }
23
+ if (settings.output) createDirectory(settings.output);
24
+ if (Array.isArray(settings.output)) return compressArrayOfFiles(settings);
25
+ return compressSingleFile(settings);
26
+ }
27
+ /**
28
+ * Compress an array of files.
29
+ * @param settings Settings
30
+ */
31
+ async function compressArrayOfFiles(settings) {
32
+ let result = "";
33
+ if (Array.isArray(settings.input)) for (let index = 0; index < settings.input.length; index++) {
34
+ const input = settings.input[index];
35
+ if (input) result = await run({
36
+ settings,
37
+ content: getContentFromFiles(input),
38
+ index
39
+ });
40
+ }
41
+ return result;
42
+ }
43
+ /**
44
+ * Create folder of the target file.
45
+ * @param filePath Full path of the file (can be string or array when $1 pattern is used)
46
+ */
47
+ function createDirectory(filePath) {
48
+ if (!filePath) return;
49
+ const paths = Array.isArray(filePath) ? filePath : [filePath];
50
+ for (const path of paths) {
51
+ if (typeof path !== "string") continue;
52
+ const dirPath = path.substring(0, path.lastIndexOf("/"));
53
+ if (!dirPath) continue;
54
+ if (!directoryExists(dirPath)) mkdirp.sync(dirPath);
55
+ }
56
+ }
57
+ function directoryExists(path) {
58
+ try {
59
+ return fs.statSync(path).isDirectory();
60
+ } catch {
61
+ return false;
62
+ }
63
+ }
71
64
 
72
- // src/compressInMemory.ts
73
- var import_utils2 = require("@node-minify/utils");
74
- var compressInMemory = (settings) => {
75
- if (typeof settings.compressor !== "function") {
76
- throw new Error(
77
- "compressor should be a function, maybe you forgot to install the compressor"
78
- );
79
- }
80
- return import_utils2.utils.compressSingleFile(settings);
81
- };
65
+ //#endregion
66
+ //#region src/setup.ts
67
+ /**
68
+ * Default settings.
69
+ */
70
+ const defaultSettings = {
71
+ options: {},
72
+ buffer: 1e3 * 1024
73
+ };
74
+ /**
75
+ * Run setup.
76
+ * @param inputSettings Settings from user input
77
+ */
78
+ function setup(inputSettings) {
79
+ const settings = {
80
+ ...structuredClone(defaultSettings),
81
+ ...inputSettings
82
+ };
83
+ if (settings.content) {
84
+ validateMandatoryFields(inputSettings, ["compressor", "content"]);
85
+ return settings;
86
+ }
87
+ validateMandatoryFields(inputSettings, [
88
+ "compressor",
89
+ "input",
90
+ "output"
91
+ ]);
92
+ return enhanceSettings(settings);
93
+ }
94
+ /**
95
+ * Enhance settings.
96
+ */
97
+ function enhanceSettings(settings) {
98
+ let enhancedSettings = settings;
99
+ if (enhancedSettings.input) enhancedSettings = {
100
+ ...enhancedSettings,
101
+ ...wildcards(enhancedSettings.input, enhancedSettings.publicFolder)
102
+ };
103
+ if (enhancedSettings.input && enhancedSettings.output && !Array.isArray(enhancedSettings.output)) enhancedSettings = {
104
+ ...enhancedSettings,
105
+ ...checkOutput(enhancedSettings.input, enhancedSettings.output, enhancedSettings.publicFolder, enhancedSettings.replaceInPlace)
106
+ };
107
+ if (enhancedSettings.input && enhancedSettings.publicFolder) enhancedSettings = {
108
+ ...enhancedSettings,
109
+ ...setPublicFolder(enhancedSettings.input, enhancedSettings.publicFolder)
110
+ };
111
+ return enhancedSettings;
112
+ }
113
+ /**
114
+ * Check the output path, searching for $1
115
+ * if exist, returns the path replacing $1 by file name
116
+ * @param input Path file
117
+ * @param output Path to the output file
118
+ * @param publicFolder Path to the public folder
119
+ * @param replaceInPlace True to replace file in same folder
120
+ * @returns Enhanced settings with processed output, or undefined if no processing needed
121
+ */
122
+ function checkOutput(input, output, publicFolder, replaceInPlace) {
123
+ if (Array.isArray(output)) return;
124
+ if (!/\$1/.test(output)) return;
125
+ const effectivePublicFolder = replaceInPlace ? void 0 : publicFolder;
126
+ if (Array.isArray(input)) return { output: input.map((file) => setFileNameMin(file, output, effectivePublicFolder, replaceInPlace)) };
127
+ return { output: setFileNameMin(input, output, effectivePublicFolder, replaceInPlace) };
128
+ }
129
+ /**
130
+ * Validate that mandatory fields are present in settings.
131
+ * @param settings - Settings object to validate
132
+ * @param fields - Array of required field names
133
+ */
134
+ function validateMandatoryFields(settings, fields) {
135
+ for (const field of fields) mandatory(field, settings);
136
+ if (typeof settings.compressor !== "function") throw new Error("compressor should be a function, maybe you forgot to install the compressor");
137
+ }
138
+ /**
139
+ * Check if the setting exists.
140
+ * @param setting - Setting key to check
141
+ * @param settings - Settings object
142
+ */
143
+ function mandatory(setting, settings) {
144
+ if (!settings[setting]) throw new Error(`${setting} is mandatory.`);
145
+ }
82
146
 
83
- // src/setup.ts
84
- var import_node_path = __toESM(require("path"));
85
- var import_utils3 = require("@node-minify/utils");
86
- var import_glob = require("glob");
87
- var defaultSettings = {
88
- sync: false,
89
- options: {},
90
- buffer: 1e3 * 1024,
91
- callback: false
92
- };
93
- var setup = (inputSettings) => {
94
- let settings = Object.assign(
95
- import_utils3.utils.clone(defaultSettings),
96
- inputSettings
97
- );
98
- if (settings.content) {
99
- checkMandatoriesMemoryContent(inputSettings);
100
- return settings;
101
- }
102
- checkMandatories(inputSettings);
103
- if (settings.input) {
104
- settings = Object.assign(
105
- settings,
106
- wildcards(settings.input, settings.publicFolder)
107
- );
108
- }
109
- if (settings.input && settings.output) {
110
- settings = Object.assign(
111
- settings,
112
- checkOutput(
113
- settings.input,
114
- settings.output,
115
- settings.publicFolder,
116
- settings.replaceInPlace
117
- )
118
- );
119
- }
120
- if (settings.input && settings.publicFolder) {
121
- settings = Object.assign(
122
- settings,
123
- setPublicFolder(settings.input, settings.publicFolder)
124
- );
125
- }
126
- return settings;
127
- };
128
- var checkOutput = (input, output, publicFolder, replaceInPlace) => {
129
- const reg = /\$1/;
130
- if (reg.test(output)) {
131
- if (Array.isArray(input)) {
132
- const outputMin = input.map(
133
- (file) => import_utils3.utils.setFileNameMin(
134
- file,
135
- output,
136
- replaceInPlace ? void 0 : publicFolder,
137
- replaceInPlace
138
- )
139
- );
140
- return { output: outputMin };
141
- }
142
- return {
143
- output: import_utils3.utils.setFileNameMin(
144
- input,
145
- output,
146
- replaceInPlace ? void 0 : publicFolder,
147
- replaceInPlace
148
- )
149
- };
150
- }
151
- };
152
- var wildcards = (input, publicFolder) => {
153
- if (!Array.isArray(input)) {
154
- return wildcardsString(input, publicFolder);
155
- }
156
- return wildcardsArray(input, publicFolder);
157
- };
158
- var wildcardsString = (input, publicFolder) => {
159
- const output = {};
160
- if (input.indexOf("*") > -1) {
161
- output.input = getFilesFromWildcards(input, publicFolder);
162
- }
163
- return output;
164
- };
165
- var wildcardsArray = (input, publicFolder) => {
166
- const output = {};
167
- let isWildcardsPresent = false;
168
- output.input = input;
169
- const inputWithPublicFolder = input.map((item) => {
170
- if (item.indexOf("*") > -1) {
171
- isWildcardsPresent = true;
172
- }
173
- return (publicFolder || "") + item;
174
- });
175
- if (isWildcardsPresent) {
176
- output.input = (0, import_glob.globSync)(inputWithPublicFolder);
177
- }
178
- for (let i = 0; i < output.input.length; i++) {
179
- if (output.input[i].indexOf("*") > -1) {
180
- output.input.splice(i, 1);
181
- i--;
182
- }
183
- }
184
- return output;
185
- };
186
- var getFilesFromWildcards = (input, publicFolder) => {
187
- let output = [];
188
- if (input.indexOf("*") > -1) {
189
- output = (0, import_glob.globSync)((publicFolder || "") + input);
190
- }
191
- return output;
192
- };
193
- var setPublicFolder = (input, publicFolder) => {
194
- const output = {};
195
- if (typeof publicFolder !== "string") {
196
- return output;
197
- }
198
- publicFolder = import_node_path.default.normalize(publicFolder);
199
- if (Array.isArray(input)) {
200
- output.input = input.map((item) => {
201
- if (import_node_path.default.normalize(item).indexOf(publicFolder) > -1) {
202
- return item;
203
- }
204
- return import_node_path.default.normalize(publicFolder + item);
205
- });
206
- return output;
207
- }
208
- input = import_node_path.default.normalize(input);
209
- if (input.indexOf(publicFolder) > -1) {
210
- output.input = input;
211
- return output;
212
- }
213
- output.input = import_node_path.default.normalize(publicFolder + input);
214
- return output;
215
- };
216
- var checkMandatories = (settings) => {
217
- ["compressor", "input", "output"].forEach(
218
- (item) => mandatory(item, settings)
219
- );
220
- };
221
- var checkMandatoriesMemoryContent = (settings) => {
222
- ["compressor", "content"].forEach(
223
- (item) => mandatory(item, settings)
224
- );
225
- };
226
- var mandatory = (setting, settings) => {
227
- if (!settings[setting]) {
228
- throw new Error(`${setting} is mandatory.`);
229
- }
230
- };
147
+ //#endregion
148
+ //#region src/index.ts
149
+ /**
150
+ * Run node-minify.
151
+ * @param settings Settings from user input
152
+ */
153
+ async function minify(settings) {
154
+ const compressorSettings = setup(settings);
155
+ return await (settings.content ? compressSingleFile : compress)(compressorSettings);
156
+ }
231
157
 
232
- // src/index.ts
233
- var minify = (settings) => {
234
- return new Promise((resolve, reject) => {
235
- const method = settings.content ? compressInMemory : compress;
236
- settings = setup(settings);
237
- if (!settings.sync) {
238
- method(settings).then((minified) => {
239
- if (settings.callback) {
240
- settings.callback(null, minified);
241
- }
242
- resolve(minified);
243
- }).catch((err) => {
244
- if (settings.callback) {
245
- settings.callback(err);
246
- }
247
- reject(err);
248
- });
249
- } else {
250
- const minified = method(settings);
251
- if (settings.callback) {
252
- settings.callback(null, minified);
253
- }
254
- resolve(minified);
255
- }
256
- });
257
- };
258
- minify.default = minify;
259
- module.exports = minify;
260
- /*!
261
- * node-minify
262
- * Copyright(c) 2011-2024 Rodolphe Stoclin
263
- * MIT Licensed
264
- */
158
+ //#endregion
159
+ export { minify };
265
160
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/compress.ts","../src/compressInMemory.ts","../src/setup.ts","../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport fs from \"node:fs\";\nimport type { Settings } from \"@node-minify/types\";\nimport { utils } from \"@node-minify/utils\";\nimport { mkdirp } from \"mkdirp\";\n\n/**\n * Run compressor.\n * @param settings Settings\n */\nconst compress = (settings: Settings): Promise<string> | string => {\n if (typeof settings.compressor !== \"function\") {\n throw new Error(\n \"compressor should be a function, maybe you forgot to install the compressor\"\n );\n }\n\n if (settings.output) {\n createDirectory(settings.output);\n }\n\n if (Array.isArray(settings.output)) {\n return settings.sync\n ? compressArrayOfFilesSync(settings)\n : compressArrayOfFilesAsync(settings);\n }\n return utils.compressSingleFile(settings);\n};\n\n/**\n * Compress an array of files in sync.\n * @param settings Settings\n */\nconst compressArrayOfFilesSync = (settings: Settings): any => {\n return (\n Array.isArray(settings.input) &&\n settings.input.forEach((input, index) => {\n const content = utils.getContentFromFiles(input);\n return utils.runSync({ settings, content, index });\n })\n );\n};\n\n/**\n * Compress an array of files in async.\n * @param settings Settings\n */\nconst compressArrayOfFilesAsync = (\n settings: Settings\n): Promise<string | void> => {\n let sequence: Promise<string | void> = Promise.resolve();\n Array.isArray(settings.input) &&\n settings.input.forEach((input, index) => {\n const content = utils.getContentFromFiles(input);\n sequence = sequence.then(() =>\n utils.runAsync({ settings, content, index })\n );\n });\n return sequence;\n};\n\n/**\n * Create folder of the target file.\n * @param file Full path of the file\n */\nconst createDirectory = (file: string) => {\n if (Array.isArray(file)) {\n file = file[0];\n }\n const dir = file?.substr(0, file.lastIndexOf(\"/\"));\n if (!dir) {\n return;\n }\n if (!fs.statSync(dir).isDirectory()) {\n mkdirp.sync(dir);\n }\n};\n\n/**\n * Expose `compress()`.\n */\nexport { compress };\n","/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport type { Settings } from \"@node-minify/types\";\nimport { utils } from \"@node-minify/utils\";\n\n/**\n * Run compressor.\n * @param settings Settings\n */\nconst compressInMemory = (settings: Settings): Promise<string> | string => {\n if (typeof settings.compressor !== \"function\") {\n throw new Error(\n \"compressor should be a function, maybe you forgot to install the compressor\"\n );\n }\n\n return utils.compressSingleFile(settings);\n};\n\n/**\n * Expose `compress()`.\n */\nexport { compressInMemory };\n","/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport path from \"node:path\";\nimport type { Settings } from \"@node-minify/types\";\nimport { utils } from \"@node-minify/utils\";\nimport { globSync } from \"glob\";\n\n/**\n * Default settings.\n */\nconst defaultSettings = {\n sync: false,\n options: {},\n buffer: 1000 * 1024,\n callback: false,\n};\n\n/**\n * Run setup.\n * @param inputSettings Settings from user input\n */\nconst setup = (inputSettings: Settings) => {\n let settings: Settings = Object.assign(\n utils.clone(defaultSettings),\n inputSettings\n );\n\n // In memory\n if (settings.content) {\n checkMandatoriesMemoryContent(inputSettings);\n return settings;\n }\n\n checkMandatories(inputSettings);\n\n if (settings.input) {\n settings = Object.assign(\n settings,\n wildcards(settings.input, settings.publicFolder)\n );\n }\n if (settings.input && settings.output) {\n settings = Object.assign(\n settings,\n checkOutput(\n settings.input,\n settings.output,\n settings.publicFolder,\n settings.replaceInPlace\n )\n );\n }\n if (settings.input && settings.publicFolder) {\n settings = Object.assign(\n settings,\n setPublicFolder(settings.input, settings.publicFolder)\n );\n }\n\n return settings;\n};\n\n/**\n * Check the output path, searching for $1\n * if exist, returns the path replacing $1 by file name\n * @param input Path file\n * @param output Path to the output file\n * @param publicFolder Path to the public folder\n * @param replaceInPlace True to replace file in same folder\n */\nconst checkOutput = (\n input: string | string[],\n output: string,\n publicFolder?: string,\n replaceInPlace?: boolean\n) => {\n const reg = /\\$1/;\n if (reg.test(output)) {\n if (Array.isArray(input)) {\n const outputMin = input.map((file) =>\n utils.setFileNameMin(\n file,\n output,\n replaceInPlace ? undefined : publicFolder,\n replaceInPlace\n )\n );\n return { output: outputMin };\n }\n return {\n output: utils.setFileNameMin(\n input,\n output,\n replaceInPlace ? undefined : publicFolder,\n replaceInPlace\n ),\n };\n }\n};\n\n/**\n * Handle wildcards in a path, get the real path of each files.\n * @param input Path with wildcards\n * @param publicFolder Path to the public folder\n */\nconst wildcards = (input: string | string[], publicFolder?: string) => {\n // If it's a string\n if (!Array.isArray(input)) {\n return wildcardsString(input, publicFolder);\n }\n\n return wildcardsArray(input, publicFolder);\n};\n\n/**\n * Handle wildcards in a path (string only), get the real path of each files.\n * @param input Path with wildcards\n * @param publicFolder Path to the public folder\n */\nconst wildcardsString = (input: string, publicFolder?: string) => {\n const output: { input?: string[] } = {};\n\n if (input.indexOf(\"*\") > -1) {\n output.input = getFilesFromWildcards(input, publicFolder);\n }\n\n return output;\n};\n\n/**\n * Handle wildcards in a path (array only), get the real path of each files.\n * @param input Path with wildcards\n * @param publicFolder Path to the public folder\n */\nconst wildcardsArray = (input: string[], publicFolder?: string) => {\n const output: { input?: string[] } = {};\n let isWildcardsPresent = false;\n\n output.input = input;\n\n // Transform all wildcards to path file\n const inputWithPublicFolder = input.map((item) => {\n if (item.indexOf(\"*\") > -1) {\n isWildcardsPresent = true;\n }\n return (publicFolder || \"\") + item;\n });\n\n if (isWildcardsPresent) {\n output.input = globSync(inputWithPublicFolder);\n }\n\n // Remove all wildcards from array\n for (let i = 0; i < output.input.length; i++) {\n if (output.input[i].indexOf(\"*\") > -1) {\n output.input.splice(i, 1);\n\n i--;\n }\n }\n\n return output;\n};\n\n/**\n * Get the real path of each files.\n * @param input Path with wildcards\n * @param publicFolder Path to the public folder\n */\nconst getFilesFromWildcards = (input: string, publicFolder?: string) => {\n let output: string[] = [];\n\n if (input.indexOf(\"*\") > -1) {\n output = globSync((publicFolder || \"\") + input);\n }\n\n return output;\n};\n\n/**\n * Prepend the public folder to each file.\n * @param input Path to file(s)\n * @param publicFolder Path to the public folder\n */\nconst setPublicFolder = (input: string | string[], publicFolder: string) => {\n const output: { input?: string | string[] } = {};\n\n if (typeof publicFolder !== \"string\") {\n return output;\n }\n\n publicFolder = path.normalize(publicFolder);\n\n if (Array.isArray(input)) {\n output.input = input.map((item) => {\n // Check if publicFolder is already in path\n if (path.normalize(item).indexOf(publicFolder) > -1) {\n return item;\n }\n return path.normalize(publicFolder + item);\n });\n return output;\n }\n\n input = path.normalize(input);\n\n // Check if publicFolder is already in path\n if (input.indexOf(publicFolder) > -1) {\n output.input = input;\n return output;\n }\n\n output.input = path.normalize(publicFolder + input);\n\n return output;\n};\n\n/**\n * Check if some settings are here.\n * @param settings Settings\n */\nconst checkMandatories = (settings: Settings) => {\n [\"compressor\", \"input\", \"output\"].forEach((item: string) =>\n mandatory(item, settings)\n );\n};\n\n/**\n * Check if some settings are here for memory content.\n * @param settings Settings\n */\nconst checkMandatoriesMemoryContent = (settings: Settings) => {\n [\"compressor\", \"content\"].forEach((item: string) =>\n mandatory(item, settings)\n );\n};\n\n/**\n * Check if the setting exist.\n * @param setting Setting\n * @param settings Settings\n */\nconst mandatory = (setting: string, settings: { [key: string]: any }) => {\n if (!settings[setting]) {\n throw new Error(`${setting} is mandatory.`);\n }\n};\n\n/**\n * Expose `setup()`.\n */\nexport { setup };\n","/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport type { Settings } from \"@node-minify/types\";\nimport { compress } from \"./compress\";\nimport { compressInMemory } from \"./compressInMemory\";\nimport { setup } from \"./setup\";\n\n/**\n * Run node-minify.\n * @param settings Settings from user input\n */\nconst minify = (settings: Settings) => {\n return new Promise((resolve, reject) => {\n const method: any = settings.content ? compressInMemory : compress;\n settings = setup(settings);\n if (!settings.sync) {\n method(settings)\n .then((minified: string) => {\n if (settings.callback) {\n settings.callback(null, minified);\n }\n resolve(minified);\n })\n .catch((err: Error) => {\n if (settings.callback) {\n settings.callback(err);\n }\n reject(err);\n });\n } else {\n const minified: string = method(settings);\n if (settings.callback) {\n settings.callback(null, minified);\n }\n resolve(minified);\n }\n });\n};\n\n/**\n * Expose `minify()`.\n */\nminify.default = minify;\nexport = minify;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AASA,qBAAe;AAEf,mBAAsB;AACtB,oBAAuB;AAMvB,IAAM,WAAW,CAAC,aAAiD;AAC/D,MAAI,OAAO,SAAS,eAAe,YAAY;AAC3C,UAAM,IAAI;AAAA,MACN;AAAA,IACJ;AAAA,EACJ;AAEA,MAAI,SAAS,QAAQ;AACjB,oBAAgB,SAAS,MAAM;AAAA,EACnC;AAEA,MAAI,MAAM,QAAQ,SAAS,MAAM,GAAG;AAChC,WAAO,SAAS,OACV,yBAAyB,QAAQ,IACjC,0BAA0B,QAAQ;AAAA,EAC5C;AACA,SAAO,mBAAM,mBAAmB,QAAQ;AAC5C;AAMA,IAAM,2BAA2B,CAAC,aAA4B;AAC1D,SACI,MAAM,QAAQ,SAAS,KAAK,KAC5B,SAAS,MAAM,QAAQ,CAAC,OAAO,UAAU;AACrC,UAAM,UAAU,mBAAM,oBAAoB,KAAK;AAC/C,WAAO,mBAAM,QAAQ,EAAE,UAAU,SAAS,MAAM,CAAC;AAAA,EACrD,CAAC;AAET;AAMA,IAAM,4BAA4B,CAC9B,aACyB;AACzB,MAAI,WAAmC,QAAQ,QAAQ;AACvD,QAAM,QAAQ,SAAS,KAAK,KACxB,SAAS,MAAM,QAAQ,CAAC,OAAO,UAAU;AACrC,UAAM,UAAU,mBAAM,oBAAoB,KAAK;AAC/C,eAAW,SAAS;AAAA,MAAK,MACrB,mBAAM,SAAS,EAAE,UAAU,SAAS,MAAM,CAAC;AAAA,IAC/C;AAAA,EACJ,CAAC;AACL,SAAO;AACX;AAMA,IAAM,kBAAkB,CAAC,SAAiB;AACtC,MAAI,MAAM,QAAQ,IAAI,GAAG;AACrB,WAAO,KAAK,CAAC;AAAA,EACjB;AACA,QAAM,M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AM,GAAG,OAAO,gBAAgB;AAAA,EAC9C;AACJ;;;AC3OA,IAAM,SAAS,CAAC,aAAuB;AACnC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACpC,UAAM,SAAc,SAAS,UAAU,mBAAmB;AAC1D,eAAW,MAAM,QAAQ;AACzB,QAAI,CAAC,SAAS,MAAM;AAChB,aAAO,QAAQ,EACV,KAAK,CAAC,aAAqB;AACxB,YAAI,SAAS,UAAU;AACnB,mBAAS,SAAS,MAAM,QAAQ;AAAA,QACpC;AACA,gBAAQ,QAAQ;AAAA,MACpB,CAAC,EACA,MAAM,CAAC,QAAe;AACnB,YAAI,SAAS,UAAU;AACnB,mBAAS,SAAS,GAAG;AAAA,QACzB;AACA,eAAO,GAAG;AAAA,MACd,CAAC;AAAA,IACT,OAAO;AACH,YAAM,WAAmB,OAAO,QAAQ;AACxC,UAAI,SAAS,UAAU;AACnB,iBAAS,SAAS,MAAM,QAAQ;AAAA,MACpC;AACA,cAAQ,QAAQ;AAAA,IACpB;AAAA,EACJ,CAAC;AACL;AAKA,OAAO,UAAU;AACjB,iBAAS;","names":["fs","import_utils","import_utils","path"]}
1
+ {"version":3,"file":"index.js","names":["settings: Settings"],"sources":["../src/compress.ts","../src/setup.ts","../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport fs from \"node:fs\";\nimport type { Settings } from \"@node-minify/types\";\nimport {\n compressSingleFile,\n getContentFromFiles,\n run,\n} from \"@node-minify/utils\";\nimport { mkdirp } from \"mkdirp\";\n\n/**\n * Run compressor.\n * @param settings Settings\n */\nexport async function compress(settings: Settings): Promise<string> {\n if (Array.isArray(settings.output)) {\n if (!Array.isArray(settings.input)) {\n throw new Error(\n \"When output is an array, input must also be an array\"\n );\n }\n if (settings.input.length !== settings.output.length) {\n throw new Error(\n `Input and output arrays must have the same length (input: ${settings.input.length}, output: ${settings.output.length})`\n );\n }\n }\n\n if (settings.output) {\n createDirectory(settings.output);\n }\n\n // Handle array outputs (from user input or created internally by checkOutput when processing $1 pattern)\n if (Array.isArray(settings.output)) {\n return compressArrayOfFiles(settings);\n }\n\n return compressSingleFile(settings);\n}\n\n/**\n * Compress an array of files.\n * @param settings Settings\n */\nasync function compressArrayOfFiles(settings: Settings): Promise<string> {\n let result = \"\";\n if (Array.isArray(settings.input)) {\n for (let index = 0; index < settings.input.length; index++) {\n const input = settings.input[index];\n if (input) {\n const content = getContentFromFiles(input);\n result = await run({ settings, content, index });\n }\n }\n }\n return result;\n}\n\n/**\n * Create folder of the target file.\n * @param filePath Full path of the file (can be string or array when $1 pattern is used)\n */\nfunction createDirectory(filePath: string | string[]) {\n // Early return if no file path provided\n if (!filePath) {\n return;\n }\n\n // Handle array (created internally by checkOutput when processing $1 pattern)\n const paths = Array.isArray(filePath) ? filePath : [filePath];\n\n for (const path of paths) {\n if (typeof path !== \"string\") {\n continue;\n }\n\n // Extract directory path\n const dirPath = path.substring(0, path.lastIndexOf(\"/\"));\n\n // Early return if no directory path\n if (!dirPath) {\n continue;\n }\n\n // Create directory if it doesn't exist\n if (!directoryExists(dirPath)) {\n mkdirp.sync(dirPath);\n }\n }\n}\n\n// Helper function to check if directory exists\nfunction directoryExists(path: string): boolean {\n try {\n return fs.statSync(path).isDirectory();\n } catch {\n return false;\n }\n}\n","/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport type { Settings } from \"@node-minify/types\";\nimport { setFileNameMin, setPublicFolder, wildcards } from \"@node-minify/utils\";\n\n/**\n * Default settings.\n */\nconst defaultSettings = {\n options: {},\n buffer: 1000 * 1024,\n};\n\n/**\n * Run setup.\n * @param inputSettings Settings from user input\n */\nfunction setup(inputSettings: Settings) {\n const settings: Settings = {\n ...structuredClone(defaultSettings),\n ...inputSettings,\n };\n\n // In memory\n if (settings.content) {\n validateMandatoryFields(inputSettings, [\"compressor\", \"content\"]);\n return settings;\n }\n\n validateMandatoryFields(inputSettings, [\"compressor\", \"input\", \"output\"]);\n\n return enhanceSettings(settings);\n}\n\n/**\n * Enhance settings.\n */\nfunction enhanceSettings(settings: Settings): Settings {\n let enhancedSettings = settings;\n\n if (enhancedSettings.input) {\n enhancedSettings = {\n ...enhancedSettings,\n ...wildcards(enhancedSettings.input, enhancedSettings.publicFolder),\n };\n }\n if (\n enhancedSettings.input &&\n enhancedSettings.output &&\n !Array.isArray(enhancedSettings.output)\n ) {\n enhancedSettings = {\n ...enhancedSettings,\n ...checkOutput(\n enhancedSettings.input,\n enhancedSettings.output,\n enhancedSettings.publicFolder,\n enhancedSettings.replaceInPlace\n ),\n };\n }\n if (enhancedSettings.input && enhancedSettings.publicFolder) {\n enhancedSettings = {\n ...enhancedSettings,\n ...setPublicFolder(\n enhancedSettings.input,\n enhancedSettings.publicFolder\n ),\n };\n }\n\n return enhancedSettings;\n}\n\n/**\n * Check the output path, searching for $1\n * if exist, returns the path replacing $1 by file name\n * @param input Path file\n * @param output Path to the output file\n * @param publicFolder Path to the public folder\n * @param replaceInPlace True to replace file in same folder\n * @returns Enhanced settings with processed output, or undefined if no processing needed\n */\nfunction checkOutput(\n input: string | string[],\n output: string | string[],\n publicFolder?: string,\n replaceInPlace?: boolean\n): { output: string | string[] } | undefined {\n // Arrays don't use the $1 placeholder pattern - they're handled directly in compress()\n if (Array.isArray(output)) {\n return undefined;\n }\n\n const PLACEHOLDER_PATTERN = /\\$1/;\n\n if (!PLACEHOLDER_PATTERN.test(output)) {\n return undefined;\n }\n\n const effectivePublicFolder = replaceInPlace ? undefined : publicFolder;\n\n // If array of files\n if (Array.isArray(input)) {\n const outputMin = input.map((file) =>\n setFileNameMin(file, output, effectivePublicFolder, replaceInPlace)\n );\n return { output: outputMin };\n }\n\n // Single file\n return {\n output: setFileNameMin(\n input,\n output,\n effectivePublicFolder,\n replaceInPlace\n ),\n };\n}\n\n/**\n * Validate that mandatory fields are present in settings.\n * @param settings - Settings object to validate\n * @param fields - Array of required field names\n */\nfunction validateMandatoryFields(settings: Settings, fields: string[]) {\n for (const field of fields) {\n mandatory(field, settings);\n }\n\n if (typeof settings.compressor !== \"function\") {\n throw new Error(\n \"compressor should be a function, maybe you forgot to install the compressor\"\n );\n }\n}\n\n/**\n * Check if the setting exists.\n * @param setting - Setting key to check\n * @param settings - Settings object\n */\nfunction mandatory(setting: string, settings: Record<string, unknown>) {\n if (!settings[setting]) {\n throw new Error(`${setting} is mandatory.`);\n }\n}\n\nexport { setup };\n","/*!\n * node-minify\n * Copyright(c) 2011-2025 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport type { Settings } from \"@node-minify/types\";\nimport { compressSingleFile } from \"@node-minify/utils\";\nimport { compress } from \"./compress.ts\";\nimport { setup } from \"./setup.ts\";\n\n/**\n * Run node-minify.\n * @param settings Settings from user input\n */\nexport async function minify(settings: Settings): Promise<string> {\n const compressorSettings = setup(settings);\n const method = settings.content ? compressSingleFile : compress;\n return await method(compressorSettings);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAsBA,eAAsB,SAAS,UAAqC;AAChE,KAAI,MAAM,QAAQ,SAAS,OAAO,EAAE;AAChC,MAAI,CAAC,MAAM,QAAQ,SAAS,MAAM,CAC9B,OAAM,IAAI,MACN,uDACH;AAEL,MAAI,SAAS,MAAM,WAAW,SAAS,OAAO,OAC1C,OAAM,IAAI,MACN,6DAA6D,SAAS,MAAM,OAAO,YAAY,SAAS,OAAO,OAAO,GACzH;;AAIT,KAAI,SAAS,OACT,iBAAgB,SAAS,OAAO;AAIpC,KAAI,MAAM,QAAQ,SAAS,OAAO,CAC9B,QAAO,qBAAqB,SAAS;AAGzC,QAAO,mBAAmB,SAAS;;;;;;AAOvC,eAAe,qBAAqB,UAAqC;CACrE,IAAI,SAAS;AACb,KAAI,MAAM,QAAQ,SAAS,MAAM,CAC7B,MAAK,IAAI,QAAQ,GAAG,QAAQ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iBAAiB,eACpB;EACJ;AAEL,KAAI,iBAAiB,SAAS,iBAAiB,aAC3C,oBAAmB;EACf,GAAG;EACH,GAAG,gBACC,iBAAiB,OACjB,iBAAiB,aACpB;EACJ;AAGL,QAAO;;;;;;;;;;;AAYX,SAAS,YACL,OACA,QACA,cACA,gBACyC;AAEzC,KAAI,MAAM,QAAQ,OAAO,CACrB;AAKJ,KAAI,CAFwB,MAEH,KAAK,OAAO,CACjC;CAGJ,MAAM,wBAAwB,iBAAiB,SAAY;AAG3D,KAAI,MAAM,QAAQ,MAAM,CAIpB,QAAO,EAAE,QAHS,MAAM,KAAK,SACzB,eAAe,MAAM,QAAQ,uBAAuB,eAAe,CACtE,EAC2B;AAIhC,QAAO,EACH,QAAQ,eACJ,OACA,QACA,uBACA,eACH,EACJ;;;;;;;AAQL,SAAS,wBAAwB,UAAoB,QAAkB;AACnE,MAAK,MAAM,SAAS,OAChB,WAAU,OAAO,SAAS;AAG9B,KAAI,OAAO,SAAS,eAAe,WAC/B,OAAM,IAAI,MACN,8EACH;;;;;;;AAST,SAAS,UAAU,SAAiB,UAAmC;AACnE,KAAI,CAAC,SAAS,SACV,OAAM,IAAI,MAAM,GAAG,QAAQ,gBAAgB;;;;;;;;;ACtInD,eAAsB,OAAO,UAAqC;CAC9D,MAAM,qBAAqB,MAAM,SAAS;AAE1C,QAAO,OADQ,SAAS,UAAU,qBAAqB,UACnC,mBAAmB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-minify/core",
3
- "version": "9.0.2",
3
+ "version": "10.0.0",
4
4
  "description": "core of @node-minify",
5
5
  "keywords": [
6
6
  "compressor",
@@ -8,25 +8,23 @@
8
8
  "minifier"
9
9
  ],
10
10
  "author": "Rodolphe Stoclin <srodolphe@gmail.com>",
11
- "homepage": "https://github.com/srod/node-minify/tree/master/packages/core#readme",
11
+ "homepage": "https://github.com/srod/node-minify/tree/main/packages/core#readme",
12
12
  "license": "MIT",
13
+ "type": "module",
13
14
  "engines": {
14
- "node": ">=18.0.0"
15
+ "node": ">=20.0.0"
15
16
  },
16
17
  "directories": {
17
18
  "lib": "dist",
18
19
  "test": "__tests__"
19
20
  },
20
- "main": "./dist/index.js",
21
- "module": "./dist/index.mjs",
22
21
  "types": "./dist/index.d.ts",
22
+ "main": "./dist/index.js",
23
23
  "exports": {
24
- ".": {
25
- "types": "./dist/index.d.ts",
26
- "import": "./dist/index.mjs",
27
- "require": "./dist/index.js"
28
- }
24
+ "types": "./dist/index.d.ts",
25
+ "default": "./dist/index.js"
29
26
  },
27
+ "sideEffects": false,
30
28
  "files": [
31
29
  "dist/**/*"
32
30
  ],
@@ -40,21 +38,23 @@
40
38
  "bugs": {
41
39
  "url": "https://github.com/srod/node-minify/issues"
42
40
  },
43
- "dependencies": {
44
- "glob": "10.3.3",
45
- "mkdirp": "3.0.1",
46
- "@node-minify/utils": "9.0.1"
47
- },
48
- "devDependencies": {
49
- "@types/mkdirp": "^1.0.2",
50
- "@node-minify/types": "9.0.0"
51
- },
52
41
  "scripts": {
53
- "clean": "pnpm dlx rimraf dist",
54
- "build": "pnpm clean && tsup src/index.ts --format cjs,esm --dts --clean --sourcemap",
42
+ "build": "tsdown src/index.ts",
43
+ "check-exports": "attw --pack . --profile esm-only",
44
+ "format:check": "biome check .",
55
45
  "lint": "biome lint .",
46
+ "prepublishOnly": "bun run build",
56
47
  "test": "vitest run",
57
48
  "test:ci": "vitest run --coverage",
58
- "test:watch": "vitest"
49
+ "test:watch": "vitest",
50
+ "typecheck": "tsc --noEmit",
51
+ "dev": "tsdown src/index.ts --watch"
52
+ },
53
+ "dependencies": {
54
+ "@node-minify/utils": "workspace:*",
55
+ "mkdirp": "3.0.1"
56
+ },
57
+ "devDependencies": {
58
+ "@node-minify/types": "workspace:*"
59
59
  }
60
- }
60
+ }
package/dist/index.d.mts DELETED
@@ -1,14 +0,0 @@
1
- import { Settings } from '@node-minify/types';
2
-
3
- /*!
4
- * node-minify
5
- * Copyright(c) 2011-2024 Rodolphe Stoclin
6
- * MIT Licensed
7
- */
8
-
9
- declare const minify: {
10
- (settings: Settings): Promise<unknown>;
11
- default: any;
12
- };
13
-
14
- export { minify as default };
package/dist/index.mjs DELETED
@@ -1,275 +0,0 @@
1
- var __getOwnPropNames = Object.getOwnPropertyNames;
2
- var __esm = (fn, res) => function __init() {
3
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
4
- };
5
- var __commonJS = (cb, mod) => function __require() {
6
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
7
- };
8
-
9
- // src/compress.ts
10
- import fs from "node:fs";
11
- import { utils } from "@node-minify/utils";
12
- import { mkdirp } from "mkdirp";
13
- var compress, compressArrayOfFilesSync, compressArrayOfFilesAsync, createDirectory;
14
- var init_compress = __esm({
15
- "src/compress.ts"() {
16
- "use strict";
17
- compress = (settings) => {
18
- if (typeof settings.compressor !== "function") {
19
- throw new Error(
20
- "compressor should be a function, maybe you forgot to install the compressor"
21
- );
22
- }
23
- if (settings.output) {
24
- createDirectory(settings.output);
25
- }
26
- if (Array.isArray(settings.output)) {
27
- return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
28
- }
29
- return utils.compressSingleFile(settings);
30
- };
31
- compressArrayOfFilesSync = (settings) => {
32
- return Array.isArray(settings.input) && settings.input.forEach((input, index) => {
33
- const content = utils.getContentFromFiles(input);
34
- return utils.runSync({ settings, content, index });
35
- });
36
- };
37
- compressArrayOfFilesAsync = (settings) => {
38
- let sequence = Promise.resolve();
39
- Array.isArray(settings.input) && settings.input.forEach((input, index) => {
40
- const content = utils.getContentFromFiles(input);
41
- sequence = sequence.then(
42
- () => utils.runAsync({ settings, content, index })
43
- );
44
- });
45
- return sequence;
46
- };
47
- createDirectory = (file) => {
48
- if (Array.isArray(file)) {
49
- file = file[0];
50
- }
51
- const dir = file?.substr(0, file.lastIndexOf("/"));
52
- if (!dir) {
53
- return;
54
- }
55
- if (!fs.statSync(dir).isDirectory()) {
56
- mkdirp.sync(dir);
57
- }
58
- };
59
- }
60
- });
61
-
62
- // src/compressInMemory.ts
63
- import { utils as utils2 } from "@node-minify/utils";
64
- var compressInMemory;
65
- var init_compressInMemory = __esm({
66
- "src/compressInMemory.ts"() {
67
- "use strict";
68
- compressInMemory = (settings) => {
69
- if (typeof settings.compressor !== "function") {
70
- throw new Error(
71
- "compressor should be a function, maybe you forgot to install the compressor"
72
- );
73
- }
74
- return utils2.compressSingleFile(settings);
75
- };
76
- }
77
- });
78
-
79
- // src/setup.ts
80
- import path from "node:path";
81
- import { utils as utils3 } from "@node-minify/utils";
82
- import { globSync } from "glob";
83
- var defaultSettings, setup, checkOutput, wildcards, wildcardsString, wildcardsArray, getFilesFromWildcards, setPublicFolder, checkMandatories, checkMandatoriesMemoryContent, mandatory;
84
- var init_setup = __esm({
85
- "src/setup.ts"() {
86
- "use strict";
87
- defaultSettings = {
88
- sync: false,
89
- options: {},
90
- buffer: 1e3 * 1024,
91
- callback: false
92
- };
93
- setup = (inputSettings) => {
94
- let settings = Object.assign(
95
- utils3.clone(defaultSettings),
96
- inputSettings
97
- );
98
- if (settings.content) {
99
- checkMandatoriesMemoryContent(inputSettings);
100
- return settings;
101
- }
102
- checkMandatories(inputSettings);
103
- if (settings.input) {
104
- settings = Object.assign(
105
- settings,
106
- wildcards(settings.input, settings.publicFolder)
107
- );
108
- }
109
- if (settings.input && settings.output) {
110
- settings = Object.assign(
111
- settings,
112
- checkOutput(
113
- settings.input,
114
- settings.output,
115
- settings.publicFolder,
116
- settings.replaceInPlace
117
- )
118
- );
119
- }
120
- if (settings.input && settings.publicFolder) {
121
- settings = Object.assign(
122
- settings,
123
- setPublicFolder(settings.input, settings.publicFolder)
124
- );
125
- }
126
- return settings;
127
- };
128
- checkOutput = (input, output, publicFolder, replaceInPlace) => {
129
- const reg = /\$1/;
130
- if (reg.test(output)) {
131
- if (Array.isArray(input)) {
132
- const outputMin = input.map(
133
- (file) => utils3.setFileNameMin(
134
- file,
135
- output,
136
- replaceInPlace ? void 0 : publicFolder,
137
- replaceInPlace
138
- )
139
- );
140
- return { output: outputMin };
141
- }
142
- return {
143
- output: utils3.setFileNameMin(
144
- input,
145
- output,
146
- replaceInPlace ? void 0 : publicFolder,
147
- replaceInPlace
148
- )
149
- };
150
- }
151
- };
152
- wildcards = (input, publicFolder) => {
153
- if (!Array.isArray(input)) {
154
- return wildcardsString(input, publicFolder);
155
- }
156
- return wildcardsArray(input, publicFolder);
157
- };
158
- wildcardsString = (input, publicFolder) => {
159
- const output = {};
160
- if (input.indexOf("*") > -1) {
161
- output.input = getFilesFromWildcards(input, publicFolder);
162
- }
163
- return output;
164
- };
165
- wildcardsArray = (input, publicFolder) => {
166
- const output = {};
167
- let isWildcardsPresent = false;
168
- output.input = input;
169
- const inputWithPublicFolder = input.map((item) => {
170
- if (item.indexOf("*") > -1) {
171
- isWildcardsPresent = true;
172
- }
173
- return (publicFolder || "") + item;
174
- });
175
- if (isWildcardsPresent) {
176
- output.input = globSync(inputWithPublicFolder);
177
- }
178
- for (let i = 0; i < output.input.length; i++) {
179
- if (output.input[i].indexOf("*") > -1) {
180
- output.input.splice(i, 1);
181
- i--;
182
- }
183
- }
184
- return output;
185
- };
186
- getFilesFromWildcards = (input, publicFolder) => {
187
- let output = [];
188
- if (input.indexOf("*") > -1) {
189
- output = globSync((publicFolder || "") + input);
190
- }
191
- return output;
192
- };
193
- setPublicFolder = (input, publicFolder) => {
194
- const output = {};
195
- if (typeof publicFolder !== "string") {
196
- return output;
197
- }
198
- publicFolder = path.normalize(publicFolder);
199
- if (Array.isArray(input)) {
200
- output.input = input.map((item) => {
201
- if (path.normalize(item).indexOf(publicFolder) > -1) {
202
- return item;
203
- }
204
- return path.normalize(publicFolder + item);
205
- });
206
- return output;
207
- }
208
- input = path.normalize(input);
209
- if (input.indexOf(publicFolder) > -1) {
210
- output.input = input;
211
- return output;
212
- }
213
- output.input = path.normalize(publicFolder + input);
214
- return output;
215
- };
216
- checkMandatories = (settings) => {
217
- ["compressor", "input", "output"].forEach(
218
- (item) => mandatory(item, settings)
219
- );
220
- };
221
- checkMandatoriesMemoryContent = (settings) => {
222
- ["compressor", "content"].forEach(
223
- (item) => mandatory(item, settings)
224
- );
225
- };
226
- mandatory = (setting, settings) => {
227
- if (!settings[setting]) {
228
- throw new Error(`${setting} is mandatory.`);
229
- }
230
- };
231
- }
232
- });
233
-
234
- // src/index.ts
235
- var require_src = __commonJS({
236
- "src/index.ts"(exports, module) {
237
- init_compress();
238
- init_compressInMemory();
239
- init_setup();
240
- var minify = (settings) => {
241
- return new Promise((resolve, reject) => {
242
- const method = settings.content ? compressInMemory : compress;
243
- settings = setup(settings);
244
- if (!settings.sync) {
245
- method(settings).then((minified) => {
246
- if (settings.callback) {
247
- settings.callback(null, minified);
248
- }
249
- resolve(minified);
250
- }).catch((err) => {
251
- if (settings.callback) {
252
- settings.callback(err);
253
- }
254
- reject(err);
255
- });
256
- } else {
257
- const minified = method(settings);
258
- if (settings.callback) {
259
- settings.callback(null, minified);
260
- }
261
- resolve(minified);
262
- }
263
- });
264
- };
265
- minify.default = minify;
266
- module.exports = minify;
267
- }
268
- });
269
- export default require_src();
270
- /*!
271
- * node-minify
272
- * Copyright(c) 2011-2024 Rodolphe Stoclin
273
- * MIT Licensed
274
- */
275
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/compress.ts","../src/compressInMemory.ts","../src/setup.ts","../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport fs from \"node:fs\";\nimport type { Settings } from \"@node-minify/types\";\nimport { utils } from \"@node-minify/utils\";\nimport { mkdirp } from \"mkdirp\";\n\n/**\n * Run compressor.\n * @param settings Settings\n */\nconst compress = (settings: Settings): Promise<string> | string => {\n if (typeof settings.compressor !== \"function\") {\n throw new Error(\n \"compressor should be a function, maybe you forgot to install the compressor\"\n );\n }\n\n if (settings.output) {\n createDirectory(settings.output);\n }\n\n if (Array.isArray(settings.output)) {\n return settings.sync\n ? compressArrayOfFilesSync(settings)\n : compressArrayOfFilesAsync(settings);\n }\n return utils.compressSingleFile(settings);\n};\n\n/**\n * Compress an array of files in sync.\n * @param settings Settings\n */\nconst compressArrayOfFilesSync = (settings: Settings): any => {\n return (\n Array.isArray(settings.input) &&\n settings.input.forEach((input, index) => {\n const content = utils.getContentFromFiles(input);\n return utils.runSync({ settings, content, index });\n })\n );\n};\n\n/**\n * Compress an array of files in async.\n * @param settings Settings\n */\nconst compressArrayOfFilesAsync = (\n settings: Settings\n): Promise<string | void> => {\n let sequence: Promise<string | void> = Promise.resolve();\n Array.isArray(settings.input) &&\n settings.input.forEach((input, index) => {\n const content = utils.getContentFromFiles(input);\n sequence = sequence.then(() =>\n utils.runAsync({ settings, content, index })\n );\n });\n return sequence;\n};\n\n/**\n * Create folder of the target file.\n * @param file Full path of the file\n */\nconst createDirectory = (file: string) => {\n if (Array.isArray(file)) {\n file = file[0];\n }\n const dir = file?.substr(0, file.lastIndexOf(\"/\"));\n if (!dir) {\n return;\n }\n if (!fs.statSync(dir).isDirectory()) {\n mkdirp.sync(dir);\n }\n};\n\n/**\n * Expose `compress()`.\n */\nexport { compress };\n","/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport type { Settings } from \"@node-minify/types\";\nimport { utils } from \"@node-minify/utils\";\n\n/**\n * Run compressor.\n * @param settings Settings\n */\nconst compressInMemory = (settings: Settings): Promise<string> | string => {\n if (typeof settings.compressor !== \"function\") {\n throw new Error(\n \"compressor should be a function, maybe you forgot to install the compressor\"\n );\n }\n\n return utils.compressSingleFile(settings);\n};\n\n/**\n * Expose `compress()`.\n */\nexport { compressInMemory };\n","/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport path from \"node:path\";\nimport type { Settings } from \"@node-minify/types\";\nimport { utils } from \"@node-minify/utils\";\nimport { globSync } from \"glob\";\n\n/**\n * Default settings.\n */\nconst defaultSettings = {\n sync: false,\n options: {},\n buffer: 1000 * 1024,\n callback: false,\n};\n\n/**\n * Run setup.\n * @param inputSettings Settings from user input\n */\nconst setup = (inputSettings: Settings) => {\n let settings: Settings = Object.assign(\n utils.clone(defaultSettings),\n inputSettings\n );\n\n // In memory\n if (settings.content) {\n checkMandatoriesMemoryContent(inputSettings);\n return settings;\n }\n\n checkMandatories(inputSettings);\n\n if (settings.input) {\n settings = Object.assign(\n settings,\n wildcards(settings.input, settings.publicFolder)\n );\n }\n if (settings.input && settings.output) {\n settings = Object.assign(\n settings,\n checkOutput(\n settings.input,\n settings.output,\n settings.publicFolder,\n settings.replaceInPlace\n )\n );\n }\n if (settings.input && settings.publicFolder) {\n settings = Object.assign(\n settings,\n setPublicFolder(settings.input, settings.publicFolder)\n );\n }\n\n return settings;\n};\n\n/**\n * Check the output path, searching for $1\n * if exist, returns the path replacing $1 by file name\n * @param input Path file\n * @param output Path to the output file\n * @param publicFolder Path to the public folder\n * @param replaceInPlace True to replace file in same folder\n */\nconst checkOutput = (\n input: string | string[],\n output: string,\n publicFolder?: string,\n replaceInPlace?: boolean\n) => {\n const reg = /\\$1/;\n if (reg.test(output)) {\n if (Array.isArray(input)) {\n const outputMin = input.map((file) =>\n utils.setFileNameMin(\n file,\n output,\n replaceInPlace ? undefined : publicFolder,\n replaceInPlace\n )\n );\n return { output: outputMin };\n }\n return {\n output: utils.setFileNameMin(\n input,\n output,\n replaceInPlace ? undefined : publicFolder,\n replaceInPlace\n ),\n };\n }\n};\n\n/**\n * Handle wildcards in a path, get the real path of each files.\n * @param input Path with wildcards\n * @param publicFolder Path to the public folder\n */\nconst wildcards = (input: string | string[], publicFolder?: string) => {\n // If it's a string\n if (!Array.isArray(input)) {\n return wildcardsString(input, publicFolder);\n }\n\n return wildcardsArray(input, publicFolder);\n};\n\n/**\n * Handle wildcards in a path (string only), get the real path of each files.\n * @param input Path with wildcards\n * @param publicFolder Path to the public folder\n */\nconst wildcardsString = (input: string, publicFolder?: string) => {\n const output: { input?: string[] } = {};\n\n if (input.indexOf(\"*\") > -1) {\n output.input = getFilesFromWildcards(input, publicFolder);\n }\n\n return output;\n};\n\n/**\n * Handle wildcards in a path (array only), get the real path of each files.\n * @param input Path with wildcards\n * @param publicFolder Path to the public folder\n */\nconst wildcardsArray = (input: string[], publicFolder?: string) => {\n const output: { input?: string[] } = {};\n let isWildcardsPresent = false;\n\n output.input = input;\n\n // Transform all wildcards to path file\n const inputWithPublicFolder = input.map((item) => {\n if (item.indexOf(\"*\") > -1) {\n isWildcardsPresent = true;\n }\n return (publicFolder || \"\") + item;\n });\n\n if (isWildcardsPresent) {\n output.input = globSync(inputWithPublicFolder);\n }\n\n // Remove all wildcards from array\n for (let i = 0; i < output.input.length; i++) {\n if (output.input[i].indexOf(\"*\") > -1) {\n output.input.splice(i, 1);\n\n i--;\n }\n }\n\n return output;\n};\n\n/**\n * Get the real path of each files.\n * @param input Path with wildcards\n * @param publicFolder Path to the public folder\n */\nconst getFilesFromWildcards = (input: string, publicFolder?: string) => {\n let output: string[] = [];\n\n if (input.indexOf(\"*\") > -1) {\n output = globSync((publicFolder || \"\") + input);\n }\n\n return output;\n};\n\n/**\n * Prepend the public folder to each file.\n * @param input Path to file(s)\n * @param publicFolder Path to the public folder\n */\nconst setPublicFolder = (input: string | string[], publicFolder: string) => {\n const output: { input?: string | string[] } = {};\n\n if (typeof publicFolder !== \"string\") {\n return output;\n }\n\n publicFolder = path.normalize(publicFolder);\n\n if (Array.isArray(input)) {\n output.input = input.map((item) => {\n // Check if publicFolder is already in path\n if (path.normalize(item).indexOf(publicFolder) > -1) {\n return item;\n }\n return path.normalize(publicFolder + item);\n });\n return output;\n }\n\n input = path.normalize(input);\n\n // Check if publicFolder is already in path\n if (input.indexOf(publicFolder) > -1) {\n output.input = input;\n return output;\n }\n\n output.input = path.normalize(publicFolder + input);\n\n return output;\n};\n\n/**\n * Check if some settings are here.\n * @param settings Settings\n */\nconst checkMandatories = (settings: Settings) => {\n [\"compressor\", \"input\", \"output\"].forEach((item: string) =>\n mandatory(item, settings)\n );\n};\n\n/**\n * Check if some settings are here for memory content.\n * @param settings Settings\n */\nconst checkMandatoriesMemoryContent = (settings: Settings) => {\n [\"compressor\", \"content\"].forEach((item: string) =>\n mandatory(item, settings)\n );\n};\n\n/**\n * Check if the setting exist.\n * @param setting Setting\n * @param settings Settings\n */\nconst mandatory = (setting: string, settings: { [key: string]: any }) => {\n if (!settings[setting]) {\n throw new Error(`${setting} is mandatory.`);\n }\n};\n\n/**\n * Expose `setup()`.\n */\nexport { setup };\n","/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport type { Settings } from \"@node-minify/types\";\nimport { compress } from \"./compress\";\nimport { compressInMemory } from \"./compressInMemory\";\nimport { setup } from \"./setup\";\n\n/**\n * Run node-minify.\n * @param settings Settings from user input\n */\nconst minify = (settings: Settings) => {\n return new Promise((resolve, reject) => {\n const method: any = settings.content ? compressInMemory : compress;\n settings = setup(settings);\n if (!settings.sync) {\n method(settings)\n .then((minified: string) => {\n if (settings.callback) {\n settings.callback(null, minified);\n }\n resolve(minified);\n })\n .catch((err: Error) => {\n if (settings.callback) {\n settings.callback(err);\n }\n reject(err);\n });\n } else {\n const minified: string = method(settings);\n if (settings.callback) {\n settings.callback(null, minified);\n }\n resolve(minified);\n }\n });\n};\n\n/**\n * Expose `minify()`.\n */\nminify.default = minify;\nexport = minify;\n"],"mappings":";;;;;;;;;AASA,OAAO,QAAQ;AAEf,SAAS,aAAa;AACtB,SAAS,cAAc;AAZvB,IAkBM,UAuBA,0BAcA,2BAkBA;AAzEN;AAAA;AAAA;AAkBA,IAAM,WAAW,CAAC,aAAiD;AAC/D,UAAI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