@node-minify/core 10.0.0-next.0 → 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 +1 -1
- package/README.md +8 -9
- package/dist/index.d.ts +151 -10
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +155 -238
- package/dist/index.js.map +1 -0
- package/package.json +13 -14
- package/dist/index.cjs +0 -283
- package/dist/index.d.cts +0 -15
package/LICENSE
CHANGED
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%
|
|
10
|
-
<a href="https://codecov.io/gh/srod/node-minify"><img src="https://codecov.io/gh/srod/node-minify/branch/
|
|
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
|
-
|
|
25
|
-
|
|
24
|
+
import { minify } from '@node-minify/core';
|
|
25
|
+
import { uglifyJs } from '@node-minify/uglify-js';
|
|
26
26
|
|
|
27
|
-
minify({
|
|
28
|
-
compressor:
|
|
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/
|
|
40
|
+
[MIT](https://github.com/srod/node-minify/blob/main/LICENSE)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,156 @@
|
|
|
1
|
-
|
|
1
|
+
//#region ../types/src/types.d.ts
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
*
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
15
|
-
|
|
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,243 +1,160 @@
|
|
|
1
|
-
|
|
1
|
+
import { compressSingleFile, getContentFromFiles, run, setFileNameMin, setPublicFolder, wildcards } from "@node-minify/utils";
|
|
2
2
|
import fs from "node:fs";
|
|
3
|
-
import { utils } from "@node-minify/utils";
|
|
4
3
|
import { mkdirp } from "mkdirp";
|
|
5
|
-
var compress = (settings) => {
|
|
6
|
-
if (typeof settings.compressor !== "function") {
|
|
7
|
-
throw new Error(
|
|
8
|
-
"compressor should be a function, maybe you forgot to install the compressor"
|
|
9
|
-
);
|
|
10
|
-
}
|
|
11
|
-
if (settings.output) {
|
|
12
|
-
createDirectory(settings.output);
|
|
13
|
-
}
|
|
14
|
-
if (Array.isArray(settings.output)) {
|
|
15
|
-
return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
|
|
16
|
-
}
|
|
17
|
-
return utils.compressSingleFile(settings);
|
|
18
|
-
};
|
|
19
|
-
var compressArrayOfFilesSync = (settings) => {
|
|
20
|
-
return Array.isArray(settings.input) && settings.input.forEach((input, index) => {
|
|
21
|
-
const content = utils.getContentFromFiles(input);
|
|
22
|
-
return utils.runSync({ settings, content, index });
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
|
-
var compressArrayOfFilesAsync = (settings) => {
|
|
26
|
-
let sequence = Promise.resolve();
|
|
27
|
-
Array.isArray(settings.input) && settings.input.forEach((input, index) => {
|
|
28
|
-
const content = utils.getContentFromFiles(input);
|
|
29
|
-
sequence = sequence.then(
|
|
30
|
-
() => utils.runAsync({ settings, content, index })
|
|
31
|
-
);
|
|
32
|
-
});
|
|
33
|
-
return sequence;
|
|
34
|
-
};
|
|
35
|
-
var createDirectory = (file) => {
|
|
36
|
-
if (Array.isArray(file)) {
|
|
37
|
-
file = file[0];
|
|
38
|
-
}
|
|
39
|
-
const dir = file?.substr(0, file.lastIndexOf("/"));
|
|
40
|
-
if (!dir) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
if (!fs.statSync(dir).isDirectory()) {
|
|
44
|
-
mkdirp.sync(dir);
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
4
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
+
}
|
|
58
64
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
};
|
|
141
|
-
var wildcardsArray = (input, publicFolder) => {
|
|
142
|
-
const output = {};
|
|
143
|
-
let isWildcardsPresent = false;
|
|
144
|
-
output.input = input;
|
|
145
|
-
const inputWithPublicFolder = input.map((item) => {
|
|
146
|
-
if (item.indexOf("*") > -1) {
|
|
147
|
-
isWildcardsPresent = true;
|
|
148
|
-
}
|
|
149
|
-
return (publicFolder || "") + item;
|
|
150
|
-
});
|
|
151
|
-
if (isWildcardsPresent) {
|
|
152
|
-
output.input = fg.globSync(inputWithPublicFolder);
|
|
153
|
-
}
|
|
154
|
-
for (let i = 0; i < output.input.length; i++) {
|
|
155
|
-
if (output.input[i].indexOf("*") > -1) {
|
|
156
|
-
output.input.splice(i, 1);
|
|
157
|
-
i--;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return output;
|
|
161
|
-
};
|
|
162
|
-
var getFilesFromWildcards = (input, publicFolder) => {
|
|
163
|
-
let output = [];
|
|
164
|
-
if (input.indexOf("*") > -1) {
|
|
165
|
-
output = fg.globSync((publicFolder || "") + input);
|
|
166
|
-
}
|
|
167
|
-
return output;
|
|
168
|
-
};
|
|
169
|
-
var setPublicFolder = (input, publicFolder) => {
|
|
170
|
-
const output = {};
|
|
171
|
-
if (typeof publicFolder !== "string") {
|
|
172
|
-
return output;
|
|
173
|
-
}
|
|
174
|
-
publicFolder = path.normalize(publicFolder);
|
|
175
|
-
if (Array.isArray(input)) {
|
|
176
|
-
output.input = input.map((item) => {
|
|
177
|
-
if (path.normalize(item).indexOf(publicFolder) > -1) {
|
|
178
|
-
return item;
|
|
179
|
-
}
|
|
180
|
-
return path.normalize(publicFolder + item);
|
|
181
|
-
});
|
|
182
|
-
return output;
|
|
183
|
-
}
|
|
184
|
-
input = path.normalize(input);
|
|
185
|
-
if (input.indexOf(publicFolder) > -1) {
|
|
186
|
-
output.input = input;
|
|
187
|
-
return output;
|
|
188
|
-
}
|
|
189
|
-
output.input = path.normalize(publicFolder + input);
|
|
190
|
-
return output;
|
|
191
|
-
};
|
|
192
|
-
var checkMandatories = (settings) => {
|
|
193
|
-
["compressor", "input", "output"].forEach(
|
|
194
|
-
(item) => mandatory(item, settings)
|
|
195
|
-
);
|
|
196
|
-
};
|
|
197
|
-
var checkMandatoriesMemoryContent = (settings) => {
|
|
198
|
-
["compressor", "content"].forEach(
|
|
199
|
-
(item) => mandatory(item, settings)
|
|
200
|
-
);
|
|
201
|
-
};
|
|
202
|
-
var mandatory = (setting, settings) => {
|
|
203
|
-
if (!settings[setting]) {
|
|
204
|
-
throw new Error(`${setting} is mandatory.`);
|
|
205
|
-
}
|
|
206
|
-
};
|
|
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
|
+
}
|
|
207
146
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
reject(err);
|
|
224
|
-
});
|
|
225
|
-
} else {
|
|
226
|
-
const minified = method(settings);
|
|
227
|
-
if (settings.callback) {
|
|
228
|
-
settings.callback(null, minified);
|
|
229
|
-
}
|
|
230
|
-
resolve(minified);
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
};
|
|
234
|
-
minify.default = minify;
|
|
235
|
-
var src_default = minify;
|
|
236
|
-
export {
|
|
237
|
-
src_default as default
|
|
238
|
-
};
|
|
239
|
-
/*!
|
|
240
|
-
* node-minify
|
|
241
|
-
* Copyright(c) 2011-2024 Rodolphe Stoclin
|
|
242
|
-
* MIT Licensed
|
|
243
|
-
*/
|
|
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
|
+
}
|
|
157
|
+
|
|
158
|
+
//#endregion
|
|
159
|
+
export { minify };
|
|
160
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-minify/core",
|
|
3
|
-
"version": "10.0.0
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"description": "core of @node-minify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"compressor",
|
|
@@ -12,20 +12,19 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"type": "module",
|
|
14
14
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
15
|
+
"node": ">=20.0.0"
|
|
16
16
|
},
|
|
17
17
|
"directories": {
|
|
18
18
|
"lib": "dist",
|
|
19
19
|
"test": "__tests__"
|
|
20
20
|
},
|
|
21
|
-
"
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"main": "./dist/index.js",
|
|
22
23
|
"exports": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"import": "./dist/index.js",
|
|
26
|
-
"default": "./dist/index.cjs"
|
|
27
|
-
}
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
28
26
|
},
|
|
27
|
+
"sideEffects": false,
|
|
29
28
|
"files": [
|
|
30
29
|
"dist/**/*"
|
|
31
30
|
],
|
|
@@ -40,22 +39,22 @@
|
|
|
40
39
|
"url": "https://github.com/srod/node-minify/issues"
|
|
41
40
|
},
|
|
42
41
|
"scripts": {
|
|
43
|
-
"build": "
|
|
44
|
-
"check-exports": "attw --pack .",
|
|
42
|
+
"build": "tsdown src/index.ts",
|
|
43
|
+
"check-exports": "attw --pack . --profile esm-only",
|
|
45
44
|
"format:check": "biome check .",
|
|
46
45
|
"lint": "biome lint .",
|
|
47
46
|
"prepublishOnly": "bun run build",
|
|
48
47
|
"test": "vitest run",
|
|
49
48
|
"test:ci": "vitest run --coverage",
|
|
50
|
-
"test:watch": "vitest"
|
|
49
|
+
"test:watch": "vitest",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
51
|
+
"dev": "tsdown src/index.ts --watch"
|
|
51
52
|
},
|
|
52
53
|
"dependencies": {
|
|
53
54
|
"@node-minify/utils": "workspace:*",
|
|
54
|
-
"fast-glob": "^3.3.2",
|
|
55
55
|
"mkdirp": "3.0.1"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@node-minify/types": "workspace:*"
|
|
59
|
-
"@types/mkdirp": "^2.0.0"
|
|
58
|
+
"@node-minify/types": "workspace:*"
|
|
60
59
|
}
|
|
61
60
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,283 +0,0 @@
|
|
|
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 __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
default: () => src_default
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(src_exports);
|
|
36
|
-
|
|
37
|
-
// src/compress.ts
|
|
38
|
-
var import_node_fs = __toESM(require("fs"), 1);
|
|
39
|
-
var import_utils = require("@node-minify/utils");
|
|
40
|
-
var import_mkdirp = require("mkdirp");
|
|
41
|
-
var compress = (settings) => {
|
|
42
|
-
if (typeof settings.compressor !== "function") {
|
|
43
|
-
throw new Error(
|
|
44
|
-
"compressor should be a function, maybe you forgot to install the compressor"
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
if (settings.output) {
|
|
48
|
-
createDirectory(settings.output);
|
|
49
|
-
}
|
|
50
|
-
if (Array.isArray(settings.output)) {
|
|
51
|
-
return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
|
|
52
|
-
}
|
|
53
|
-
return import_utils.utils.compressSingleFile(settings);
|
|
54
|
-
};
|
|
55
|
-
var compressArrayOfFilesSync = (settings) => {
|
|
56
|
-
return Array.isArray(settings.input) && settings.input.forEach((input, index) => {
|
|
57
|
-
const content = import_utils.utils.getContentFromFiles(input);
|
|
58
|
-
return import_utils.utils.runSync({ settings, content, index });
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
var compressArrayOfFilesAsync = (settings) => {
|
|
62
|
-
let sequence = Promise.resolve();
|
|
63
|
-
Array.isArray(settings.input) && settings.input.forEach((input, index) => {
|
|
64
|
-
const content = import_utils.utils.getContentFromFiles(input);
|
|
65
|
-
sequence = sequence.then(
|
|
66
|
-
() => import_utils.utils.runAsync({ settings, content, index })
|
|
67
|
-
);
|
|
68
|
-
});
|
|
69
|
-
return sequence;
|
|
70
|
-
};
|
|
71
|
-
var createDirectory = (file) => {
|
|
72
|
-
if (Array.isArray(file)) {
|
|
73
|
-
file = file[0];
|
|
74
|
-
}
|
|
75
|
-
const dir = file?.substr(0, file.lastIndexOf("/"));
|
|
76
|
-
if (!dir) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
if (!import_node_fs.default.statSync(dir).isDirectory()) {
|
|
80
|
-
import_mkdirp.mkdirp.sync(dir);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
// src/compressInMemory.ts
|
|
85
|
-
var import_utils2 = require("@node-minify/utils");
|
|
86
|
-
var compressInMemory = (settings) => {
|
|
87
|
-
if (typeof settings.compressor !== "function") {
|
|
88
|
-
throw new Error(
|
|
89
|
-
"compressor should be a function, maybe you forgot to install the compressor"
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
return import_utils2.utils.compressSingleFile(settings);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
// src/setup.ts
|
|
96
|
-
var import_node_path = __toESM(require("path"), 1);
|
|
97
|
-
var import_utils3 = require("@node-minify/utils");
|
|
98
|
-
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
99
|
-
var defaultSettings = {
|
|
100
|
-
sync: false,
|
|
101
|
-
options: {},
|
|
102
|
-
buffer: 1e3 * 1024,
|
|
103
|
-
callback: false
|
|
104
|
-
};
|
|
105
|
-
var setup = (inputSettings) => {
|
|
106
|
-
let settings = Object.assign(
|
|
107
|
-
import_utils3.utils.clone(defaultSettings),
|
|
108
|
-
inputSettings
|
|
109
|
-
);
|
|
110
|
-
if (settings.content) {
|
|
111
|
-
checkMandatoriesMemoryContent(inputSettings);
|
|
112
|
-
return settings;
|
|
113
|
-
}
|
|
114
|
-
checkMandatories(inputSettings);
|
|
115
|
-
if (settings.input) {
|
|
116
|
-
settings = Object.assign(
|
|
117
|
-
settings,
|
|
118
|
-
wildcards(settings.input, settings.publicFolder)
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
if (settings.input && settings.output) {
|
|
122
|
-
settings = Object.assign(
|
|
123
|
-
settings,
|
|
124
|
-
checkOutput(
|
|
125
|
-
settings.input,
|
|
126
|
-
settings.output,
|
|
127
|
-
settings.publicFolder,
|
|
128
|
-
settings.replaceInPlace
|
|
129
|
-
)
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
if (settings.input && settings.publicFolder) {
|
|
133
|
-
settings = Object.assign(
|
|
134
|
-
settings,
|
|
135
|
-
setPublicFolder(settings.input, settings.publicFolder)
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
return settings;
|
|
139
|
-
};
|
|
140
|
-
var checkOutput = (input, output, publicFolder, replaceInPlace) => {
|
|
141
|
-
const reg = /\$1/;
|
|
142
|
-
if (reg.test(output)) {
|
|
143
|
-
if (Array.isArray(input)) {
|
|
144
|
-
const outputMin = input.map(
|
|
145
|
-
(file) => import_utils3.utils.setFileNameMin(
|
|
146
|
-
file,
|
|
147
|
-
output,
|
|
148
|
-
replaceInPlace ? void 0 : publicFolder,
|
|
149
|
-
replaceInPlace
|
|
150
|
-
)
|
|
151
|
-
);
|
|
152
|
-
return { output: outputMin };
|
|
153
|
-
}
|
|
154
|
-
return {
|
|
155
|
-
output: import_utils3.utils.setFileNameMin(
|
|
156
|
-
input,
|
|
157
|
-
output,
|
|
158
|
-
replaceInPlace ? void 0 : publicFolder,
|
|
159
|
-
replaceInPlace
|
|
160
|
-
)
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
var wildcards = (input, publicFolder) => {
|
|
165
|
-
if (!Array.isArray(input)) {
|
|
166
|
-
return wildcardsString(input, publicFolder);
|
|
167
|
-
}
|
|
168
|
-
return wildcardsArray(input, publicFolder);
|
|
169
|
-
};
|
|
170
|
-
var wildcardsString = (input, publicFolder) => {
|
|
171
|
-
const output = {};
|
|
172
|
-
if (input.indexOf("*") > -1) {
|
|
173
|
-
output.input = getFilesFromWildcards(input, publicFolder);
|
|
174
|
-
}
|
|
175
|
-
return output;
|
|
176
|
-
};
|
|
177
|
-
var wildcardsArray = (input, publicFolder) => {
|
|
178
|
-
const output = {};
|
|
179
|
-
let isWildcardsPresent = false;
|
|
180
|
-
output.input = input;
|
|
181
|
-
const inputWithPublicFolder = input.map((item) => {
|
|
182
|
-
if (item.indexOf("*") > -1) {
|
|
183
|
-
isWildcardsPresent = true;
|
|
184
|
-
}
|
|
185
|
-
return (publicFolder || "") + item;
|
|
186
|
-
});
|
|
187
|
-
if (isWildcardsPresent) {
|
|
188
|
-
output.input = import_fast_glob.default.globSync(inputWithPublicFolder);
|
|
189
|
-
}
|
|
190
|
-
for (let i = 0; i < output.input.length; i++) {
|
|
191
|
-
if (output.input[i].indexOf("*") > -1) {
|
|
192
|
-
output.input.splice(i, 1);
|
|
193
|
-
i--;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return output;
|
|
197
|
-
};
|
|
198
|
-
var getFilesFromWildcards = (input, publicFolder) => {
|
|
199
|
-
let output = [];
|
|
200
|
-
if (input.indexOf("*") > -1) {
|
|
201
|
-
output = import_fast_glob.default.globSync((publicFolder || "") + input);
|
|
202
|
-
}
|
|
203
|
-
return output;
|
|
204
|
-
};
|
|
205
|
-
var setPublicFolder = (input, publicFolder) => {
|
|
206
|
-
const output = {};
|
|
207
|
-
if (typeof publicFolder !== "string") {
|
|
208
|
-
return output;
|
|
209
|
-
}
|
|
210
|
-
publicFolder = import_node_path.default.normalize(publicFolder);
|
|
211
|
-
if (Array.isArray(input)) {
|
|
212
|
-
output.input = input.map((item) => {
|
|
213
|
-
if (import_node_path.default.normalize(item).indexOf(publicFolder) > -1) {
|
|
214
|
-
return item;
|
|
215
|
-
}
|
|
216
|
-
return import_node_path.default.normalize(publicFolder + item);
|
|
217
|
-
});
|
|
218
|
-
return output;
|
|
219
|
-
}
|
|
220
|
-
input = import_node_path.default.normalize(input);
|
|
221
|
-
if (input.indexOf(publicFolder) > -1) {
|
|
222
|
-
output.input = input;
|
|
223
|
-
return output;
|
|
224
|
-
}
|
|
225
|
-
output.input = import_node_path.default.normalize(publicFolder + input);
|
|
226
|
-
return output;
|
|
227
|
-
};
|
|
228
|
-
var checkMandatories = (settings) => {
|
|
229
|
-
["compressor", "input", "output"].forEach(
|
|
230
|
-
(item) => mandatory(item, settings)
|
|
231
|
-
);
|
|
232
|
-
};
|
|
233
|
-
var checkMandatoriesMemoryContent = (settings) => {
|
|
234
|
-
["compressor", "content"].forEach(
|
|
235
|
-
(item) => mandatory(item, settings)
|
|
236
|
-
);
|
|
237
|
-
};
|
|
238
|
-
var mandatory = (setting, settings) => {
|
|
239
|
-
if (!settings[setting]) {
|
|
240
|
-
throw new Error(`${setting} is mandatory.`);
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
// src/index.ts
|
|
245
|
-
var minify = (settings) => {
|
|
246
|
-
return new Promise((resolve, reject) => {
|
|
247
|
-
const method = settings.content ? compressInMemory : compress;
|
|
248
|
-
settings = setup(settings);
|
|
249
|
-
if (!settings.sync) {
|
|
250
|
-
method(settings).then((minified) => {
|
|
251
|
-
if (settings.callback) {
|
|
252
|
-
settings.callback(null, minified);
|
|
253
|
-
}
|
|
254
|
-
resolve(minified);
|
|
255
|
-
}).catch((err) => {
|
|
256
|
-
if (settings.callback) {
|
|
257
|
-
settings.callback(err);
|
|
258
|
-
}
|
|
259
|
-
reject(err);
|
|
260
|
-
});
|
|
261
|
-
} else {
|
|
262
|
-
const minified = method(settings);
|
|
263
|
-
if (settings.callback) {
|
|
264
|
-
settings.callback(null, minified);
|
|
265
|
-
}
|
|
266
|
-
resolve(minified);
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
};
|
|
270
|
-
minify.default = minify;
|
|
271
|
-
var src_default = minify;
|
|
272
|
-
/*!
|
|
273
|
-
* node-minify
|
|
274
|
-
* Copyright(c) 2011-2024 Rodolphe Stoclin
|
|
275
|
-
* MIT Licensed
|
|
276
|
-
*/
|
|
277
|
-
|
|
278
|
-
// fix-cjs-exports
|
|
279
|
-
if (module.exports.default) {
|
|
280
|
-
Object.assign(module.exports.default, module.exports);
|
|
281
|
-
module.exports = module.exports.default;
|
|
282
|
-
delete module.exports.default;
|
|
283
|
-
}
|
package/dist/index.d.cts
DELETED
|
@@ -1,15 +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 };
|
|
15
|
-
export = minify
|