@node-minify/core 7.0.0 → 8.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +16 -0
- package/dist/index.js +230 -0
- package/dist/index.mjs +200 -0
- package/package.json +23 -7
- package/lib/compress.js +0 -108
- package/lib/compressInMemory.js +0 -37
- package/lib/core.js +0 -59
- package/lib/setup.js +0 -250
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Settings } from '@node-minify/types';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* node-minify
|
|
5
|
+
* Copyright(c) 2011-2022 Rodolphe Stoclin
|
|
6
|
+
* MIT Licensed
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Run node-minify.
|
|
11
|
+
*
|
|
12
|
+
* @param {Object} settings - Settings from user input
|
|
13
|
+
*/
|
|
14
|
+
declare const minify: (settings: Settings) => Promise<unknown>;
|
|
15
|
+
|
|
16
|
+
export { minify as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
|
|
25
|
+
// src/index.ts
|
|
26
|
+
var src_exports = {};
|
|
27
|
+
__export(src_exports, {
|
|
28
|
+
default: () => src_default
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(src_exports);
|
|
31
|
+
|
|
32
|
+
// src/setup.ts
|
|
33
|
+
var import_path = __toESM(require("path"));
|
|
34
|
+
var import_globby = __toESM(require("globby"));
|
|
35
|
+
var import_utils = require("@node-minify/utils");
|
|
36
|
+
var defaultSettings = {
|
|
37
|
+
sync: false,
|
|
38
|
+
options: {},
|
|
39
|
+
buffer: 1e3 * 1024,
|
|
40
|
+
callback: false
|
|
41
|
+
};
|
|
42
|
+
var setup = (inputSettings) => {
|
|
43
|
+
let settings = Object.assign(import_utils.utils.clone(defaultSettings), inputSettings);
|
|
44
|
+
if (settings.content) {
|
|
45
|
+
checkMandatoriesMemoryContent(inputSettings);
|
|
46
|
+
return settings;
|
|
47
|
+
}
|
|
48
|
+
checkMandatories(inputSettings);
|
|
49
|
+
settings = Object.assign(settings, wildcards(settings.input, settings.publicFolder));
|
|
50
|
+
settings = Object.assign(
|
|
51
|
+
settings,
|
|
52
|
+
checkOutput(settings.input, settings.output, settings.publicFolder, settings.replaceInPlace)
|
|
53
|
+
);
|
|
54
|
+
settings = Object.assign(settings, setPublicFolder(settings.input, settings.publicFolder));
|
|
55
|
+
return settings;
|
|
56
|
+
};
|
|
57
|
+
var checkOutput = (input, output, publicFolder, replaceInPlace) => {
|
|
58
|
+
let reg = new RegExp("\\$1");
|
|
59
|
+
if (reg.test(output)) {
|
|
60
|
+
if (Array.isArray(input)) {
|
|
61
|
+
const outputMin = input.map(
|
|
62
|
+
(file) => import_utils.utils.setFileNameMin(file, output, replaceInPlace ? null : publicFolder, replaceInPlace)
|
|
63
|
+
);
|
|
64
|
+
return { output: outputMin };
|
|
65
|
+
} else {
|
|
66
|
+
return { output: import_utils.utils.setFileNameMin(input, output, replaceInPlace ? null : publicFolder, replaceInPlace) };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var wildcards = (input, publicFolder) => {
|
|
71
|
+
if (!Array.isArray(input)) {
|
|
72
|
+
return wildcardsString(input, publicFolder);
|
|
73
|
+
}
|
|
74
|
+
return wildcardsArray(input, publicFolder);
|
|
75
|
+
};
|
|
76
|
+
var wildcardsString = (input, publicFolder) => {
|
|
77
|
+
const output = {};
|
|
78
|
+
if (input.indexOf("*") > -1) {
|
|
79
|
+
output.input = getFilesFromWildcards(input, publicFolder);
|
|
80
|
+
}
|
|
81
|
+
return output;
|
|
82
|
+
};
|
|
83
|
+
var wildcardsArray = (input, publicFolder) => {
|
|
84
|
+
let output = {};
|
|
85
|
+
let isWildcardsPresent = false;
|
|
86
|
+
output.input = input;
|
|
87
|
+
const inputWithPublicFolder = input.map((item) => {
|
|
88
|
+
if (item.indexOf("*") > -1) {
|
|
89
|
+
isWildcardsPresent = true;
|
|
90
|
+
}
|
|
91
|
+
return (publicFolder || "") + item;
|
|
92
|
+
});
|
|
93
|
+
if (isWildcardsPresent) {
|
|
94
|
+
output.input = import_globby.default.sync(inputWithPublicFolder);
|
|
95
|
+
}
|
|
96
|
+
for (let i = 0; i < output.input.length; i++) {
|
|
97
|
+
if (output.input[i].indexOf("*") > -1) {
|
|
98
|
+
output.input.splice(i, 1);
|
|
99
|
+
i--;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return output;
|
|
103
|
+
};
|
|
104
|
+
var getFilesFromWildcards = (input, publicFolder) => {
|
|
105
|
+
let output = [];
|
|
106
|
+
if (input.indexOf("*") > -1) {
|
|
107
|
+
output = import_globby.default.sync((publicFolder || "") + input);
|
|
108
|
+
}
|
|
109
|
+
return output;
|
|
110
|
+
};
|
|
111
|
+
var setPublicFolder = (input, publicFolder) => {
|
|
112
|
+
let output = {};
|
|
113
|
+
if (typeof publicFolder !== "string") {
|
|
114
|
+
return output;
|
|
115
|
+
}
|
|
116
|
+
publicFolder = import_path.default.normalize(publicFolder);
|
|
117
|
+
if (Array.isArray(input)) {
|
|
118
|
+
output.input = input.map((item) => {
|
|
119
|
+
if (import_path.default.normalize(item).indexOf(publicFolder) > -1) {
|
|
120
|
+
return item;
|
|
121
|
+
}
|
|
122
|
+
return import_path.default.normalize(publicFolder + item);
|
|
123
|
+
});
|
|
124
|
+
return output;
|
|
125
|
+
}
|
|
126
|
+
input = import_path.default.normalize(input);
|
|
127
|
+
if (input.indexOf(publicFolder) > -1) {
|
|
128
|
+
output.input = input;
|
|
129
|
+
return output;
|
|
130
|
+
}
|
|
131
|
+
output.input = import_path.default.normalize(publicFolder + input);
|
|
132
|
+
return output;
|
|
133
|
+
};
|
|
134
|
+
var checkMandatories = (settings) => {
|
|
135
|
+
["compressor", "input", "output"].forEach((item) => mandatory(item, settings));
|
|
136
|
+
};
|
|
137
|
+
var checkMandatoriesMemoryContent = (settings) => {
|
|
138
|
+
["compressor", "content"].forEach((item) => mandatory(item, settings));
|
|
139
|
+
};
|
|
140
|
+
var mandatory = (setting, settings) => {
|
|
141
|
+
if (!settings[setting]) {
|
|
142
|
+
throw new Error(setting + " is mandatory.");
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// src/compress.ts
|
|
147
|
+
var import_fs = __toESM(require("fs"));
|
|
148
|
+
var import_mkdirp = __toESM(require("mkdirp"));
|
|
149
|
+
var import_utils2 = require("@node-minify/utils");
|
|
150
|
+
var compress = (settings) => {
|
|
151
|
+
if (typeof settings.compressor !== "function") {
|
|
152
|
+
throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
|
|
153
|
+
}
|
|
154
|
+
createDirectory(settings.output);
|
|
155
|
+
if (Array.isArray(settings.output)) {
|
|
156
|
+
return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
|
|
157
|
+
} else {
|
|
158
|
+
return import_utils2.utils.compressSingleFile(settings);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
var compressArrayOfFilesSync = (settings) => {
|
|
162
|
+
return Array.isArray(settings.input) && settings.input.forEach((input, index) => {
|
|
163
|
+
const content = import_utils2.utils.getContentFromFiles(input);
|
|
164
|
+
return import_utils2.utils.runSync({ settings, content, index });
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
var compressArrayOfFilesAsync = (settings) => {
|
|
168
|
+
let sequence = Promise.resolve();
|
|
169
|
+
Array.isArray(settings.input) && settings.input.forEach((input, index) => {
|
|
170
|
+
const content = import_utils2.utils.getContentFromFiles(input);
|
|
171
|
+
sequence = sequence.then(() => import_utils2.utils.runAsync({ settings, content, index }));
|
|
172
|
+
});
|
|
173
|
+
return sequence;
|
|
174
|
+
};
|
|
175
|
+
var createDirectory = (file) => {
|
|
176
|
+
if (Array.isArray(file)) {
|
|
177
|
+
file = file[0];
|
|
178
|
+
}
|
|
179
|
+
const dir = file && file.substr(0, file.lastIndexOf("/"));
|
|
180
|
+
if (!dir) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (!import_fs.default.statSync(dir).isDirectory()) {
|
|
184
|
+
import_mkdirp.default.sync(dir);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// src/compressInMemory.ts
|
|
189
|
+
var import_utils3 = require("@node-minify/utils");
|
|
190
|
+
var compressInMemory = (settings) => {
|
|
191
|
+
if (typeof settings.compressor !== "function") {
|
|
192
|
+
throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
|
|
193
|
+
}
|
|
194
|
+
return import_utils3.utils.compressSingleFile(settings);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// src/index.ts
|
|
198
|
+
var minify = (settings) => {
|
|
199
|
+
return new Promise((resolve, reject) => {
|
|
200
|
+
const method = settings.content ? compressInMemory : compress;
|
|
201
|
+
settings = setup(settings);
|
|
202
|
+
if (!settings.sync) {
|
|
203
|
+
method(settings).then((minified) => {
|
|
204
|
+
if (settings.callback) {
|
|
205
|
+
settings.callback(null, minified);
|
|
206
|
+
}
|
|
207
|
+
resolve(minified);
|
|
208
|
+
}).catch((err) => {
|
|
209
|
+
if (settings.callback) {
|
|
210
|
+
settings.callback(err);
|
|
211
|
+
}
|
|
212
|
+
reject(err);
|
|
213
|
+
});
|
|
214
|
+
} else {
|
|
215
|
+
const minified = method(settings);
|
|
216
|
+
if (settings.callback) {
|
|
217
|
+
settings.callback(null, minified);
|
|
218
|
+
}
|
|
219
|
+
resolve(minified);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
var src_default = minify;
|
|
224
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
225
|
+
0 && (module.exports = {});
|
|
226
|
+
/*!
|
|
227
|
+
* node-minify
|
|
228
|
+
* Copyright(c) 2011-2022 Rodolphe Stoclin
|
|
229
|
+
* MIT Licensed
|
|
230
|
+
*/
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// src/setup.ts
|
|
2
|
+
import path from "path";
|
|
3
|
+
import globby from "globby";
|
|
4
|
+
import { utils } from "@node-minify/utils";
|
|
5
|
+
var defaultSettings = {
|
|
6
|
+
sync: false,
|
|
7
|
+
options: {},
|
|
8
|
+
buffer: 1e3 * 1024,
|
|
9
|
+
callback: false
|
|
10
|
+
};
|
|
11
|
+
var setup = (inputSettings) => {
|
|
12
|
+
let settings = Object.assign(utils.clone(defaultSettings), inputSettings);
|
|
13
|
+
if (settings.content) {
|
|
14
|
+
checkMandatoriesMemoryContent(inputSettings);
|
|
15
|
+
return settings;
|
|
16
|
+
}
|
|
17
|
+
checkMandatories(inputSettings);
|
|
18
|
+
settings = Object.assign(settings, wildcards(settings.input, settings.publicFolder));
|
|
19
|
+
settings = Object.assign(
|
|
20
|
+
settings,
|
|
21
|
+
checkOutput(settings.input, settings.output, settings.publicFolder, settings.replaceInPlace)
|
|
22
|
+
);
|
|
23
|
+
settings = Object.assign(settings, setPublicFolder(settings.input, settings.publicFolder));
|
|
24
|
+
return settings;
|
|
25
|
+
};
|
|
26
|
+
var checkOutput = (input, output, publicFolder, replaceInPlace) => {
|
|
27
|
+
let reg = new RegExp("\\$1");
|
|
28
|
+
if (reg.test(output)) {
|
|
29
|
+
if (Array.isArray(input)) {
|
|
30
|
+
const outputMin = input.map(
|
|
31
|
+
(file) => utils.setFileNameMin(file, output, replaceInPlace ? null : publicFolder, replaceInPlace)
|
|
32
|
+
);
|
|
33
|
+
return { output: outputMin };
|
|
34
|
+
} else {
|
|
35
|
+
return { output: utils.setFileNameMin(input, output, replaceInPlace ? null : publicFolder, replaceInPlace) };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var wildcards = (input, publicFolder) => {
|
|
40
|
+
if (!Array.isArray(input)) {
|
|
41
|
+
return wildcardsString(input, publicFolder);
|
|
42
|
+
}
|
|
43
|
+
return wildcardsArray(input, publicFolder);
|
|
44
|
+
};
|
|
45
|
+
var wildcardsString = (input, publicFolder) => {
|
|
46
|
+
const output = {};
|
|
47
|
+
if (input.indexOf("*") > -1) {
|
|
48
|
+
output.input = getFilesFromWildcards(input, publicFolder);
|
|
49
|
+
}
|
|
50
|
+
return output;
|
|
51
|
+
};
|
|
52
|
+
var wildcardsArray = (input, publicFolder) => {
|
|
53
|
+
let output = {};
|
|
54
|
+
let isWildcardsPresent = false;
|
|
55
|
+
output.input = input;
|
|
56
|
+
const inputWithPublicFolder = input.map((item) => {
|
|
57
|
+
if (item.indexOf("*") > -1) {
|
|
58
|
+
isWildcardsPresent = true;
|
|
59
|
+
}
|
|
60
|
+
return (publicFolder || "") + item;
|
|
61
|
+
});
|
|
62
|
+
if (isWildcardsPresent) {
|
|
63
|
+
output.input = globby.sync(inputWithPublicFolder);
|
|
64
|
+
}
|
|
65
|
+
for (let i = 0; i < output.input.length; i++) {
|
|
66
|
+
if (output.input[i].indexOf("*") > -1) {
|
|
67
|
+
output.input.splice(i, 1);
|
|
68
|
+
i--;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return output;
|
|
72
|
+
};
|
|
73
|
+
var getFilesFromWildcards = (input, publicFolder) => {
|
|
74
|
+
let output = [];
|
|
75
|
+
if (input.indexOf("*") > -1) {
|
|
76
|
+
output = globby.sync((publicFolder || "") + input);
|
|
77
|
+
}
|
|
78
|
+
return output;
|
|
79
|
+
};
|
|
80
|
+
var setPublicFolder = (input, publicFolder) => {
|
|
81
|
+
let output = {};
|
|
82
|
+
if (typeof publicFolder !== "string") {
|
|
83
|
+
return output;
|
|
84
|
+
}
|
|
85
|
+
publicFolder = path.normalize(publicFolder);
|
|
86
|
+
if (Array.isArray(input)) {
|
|
87
|
+
output.input = input.map((item) => {
|
|
88
|
+
if (path.normalize(item).indexOf(publicFolder) > -1) {
|
|
89
|
+
return item;
|
|
90
|
+
}
|
|
91
|
+
return path.normalize(publicFolder + item);
|
|
92
|
+
});
|
|
93
|
+
return output;
|
|
94
|
+
}
|
|
95
|
+
input = path.normalize(input);
|
|
96
|
+
if (input.indexOf(publicFolder) > -1) {
|
|
97
|
+
output.input = input;
|
|
98
|
+
return output;
|
|
99
|
+
}
|
|
100
|
+
output.input = path.normalize(publicFolder + input);
|
|
101
|
+
return output;
|
|
102
|
+
};
|
|
103
|
+
var checkMandatories = (settings) => {
|
|
104
|
+
["compressor", "input", "output"].forEach((item) => mandatory(item, settings));
|
|
105
|
+
};
|
|
106
|
+
var checkMandatoriesMemoryContent = (settings) => {
|
|
107
|
+
["compressor", "content"].forEach((item) => mandatory(item, settings));
|
|
108
|
+
};
|
|
109
|
+
var mandatory = (setting, settings) => {
|
|
110
|
+
if (!settings[setting]) {
|
|
111
|
+
throw new Error(setting + " is mandatory.");
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// src/compress.ts
|
|
116
|
+
import fs from "fs";
|
|
117
|
+
import mkdirp from "mkdirp";
|
|
118
|
+
import { utils as utils2 } from "@node-minify/utils";
|
|
119
|
+
var compress = (settings) => {
|
|
120
|
+
if (typeof settings.compressor !== "function") {
|
|
121
|
+
throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
|
|
122
|
+
}
|
|
123
|
+
createDirectory(settings.output);
|
|
124
|
+
if (Array.isArray(settings.output)) {
|
|
125
|
+
return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
|
|
126
|
+
} else {
|
|
127
|
+
return utils2.compressSingleFile(settings);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
var compressArrayOfFilesSync = (settings) => {
|
|
131
|
+
return Array.isArray(settings.input) && settings.input.forEach((input, index) => {
|
|
132
|
+
const content = utils2.getContentFromFiles(input);
|
|
133
|
+
return utils2.runSync({ settings, content, index });
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
var compressArrayOfFilesAsync = (settings) => {
|
|
137
|
+
let sequence = Promise.resolve();
|
|
138
|
+
Array.isArray(settings.input) && settings.input.forEach((input, index) => {
|
|
139
|
+
const content = utils2.getContentFromFiles(input);
|
|
140
|
+
sequence = sequence.then(() => utils2.runAsync({ settings, content, index }));
|
|
141
|
+
});
|
|
142
|
+
return sequence;
|
|
143
|
+
};
|
|
144
|
+
var createDirectory = (file) => {
|
|
145
|
+
if (Array.isArray(file)) {
|
|
146
|
+
file = file[0];
|
|
147
|
+
}
|
|
148
|
+
const dir = file && file.substr(0, file.lastIndexOf("/"));
|
|
149
|
+
if (!dir) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!fs.statSync(dir).isDirectory()) {
|
|
153
|
+
mkdirp.sync(dir);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// src/compressInMemory.ts
|
|
158
|
+
import { utils as utils3 } from "@node-minify/utils";
|
|
159
|
+
var compressInMemory = (settings) => {
|
|
160
|
+
if (typeof settings.compressor !== "function") {
|
|
161
|
+
throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
|
|
162
|
+
}
|
|
163
|
+
return utils3.compressSingleFile(settings);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// src/index.ts
|
|
167
|
+
var minify = (settings) => {
|
|
168
|
+
return new Promise((resolve, reject) => {
|
|
169
|
+
const method = settings.content ? compressInMemory : compress;
|
|
170
|
+
settings = setup(settings);
|
|
171
|
+
if (!settings.sync) {
|
|
172
|
+
method(settings).then((minified) => {
|
|
173
|
+
if (settings.callback) {
|
|
174
|
+
settings.callback(null, minified);
|
|
175
|
+
}
|
|
176
|
+
resolve(minified);
|
|
177
|
+
}).catch((err) => {
|
|
178
|
+
if (settings.callback) {
|
|
179
|
+
settings.callback(err);
|
|
180
|
+
}
|
|
181
|
+
reject(err);
|
|
182
|
+
});
|
|
183
|
+
} else {
|
|
184
|
+
const minified = method(settings);
|
|
185
|
+
if (settings.callback) {
|
|
186
|
+
settings.callback(null, minified);
|
|
187
|
+
}
|
|
188
|
+
resolve(minified);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
var src_default = minify;
|
|
193
|
+
export {
|
|
194
|
+
src_default as default
|
|
195
|
+
};
|
|
196
|
+
/*!
|
|
197
|
+
* node-minify
|
|
198
|
+
* Copyright(c) 2011-2022 Rodolphe Stoclin
|
|
199
|
+
* MIT Licensed
|
|
200
|
+
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-minify/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-beta.0",
|
|
4
4
|
"description": "core of @node-minify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"compressor",
|
|
@@ -10,16 +10,23 @@
|
|
|
10
10
|
"author": "Rodolphe Stoclin <srodolphe@gmail.com>",
|
|
11
11
|
"homepage": "https://github.com/srod/node-minify/tree/master/packages/core#readme",
|
|
12
12
|
"license": "MIT",
|
|
13
|
-
"main": "lib/core.js",
|
|
14
13
|
"engines": {
|
|
15
14
|
"node": ">=14.0.0"
|
|
16
15
|
},
|
|
17
16
|
"directories": {
|
|
18
|
-
"lib": "
|
|
17
|
+
"lib": "dist",
|
|
19
18
|
"test": "__tests__"
|
|
20
19
|
},
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"module": "./dist/index.mjs",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
"require": "./dist/index.js",
|
|
25
|
+
"import": "./dist/index.mjs",
|
|
26
|
+
"types": "./dist/index.d.ts"
|
|
27
|
+
},
|
|
21
28
|
"files": [
|
|
22
|
-
"
|
|
29
|
+
"dist/**/*"
|
|
23
30
|
],
|
|
24
31
|
"publishConfig": {
|
|
25
32
|
"access": "public"
|
|
@@ -31,10 +38,19 @@
|
|
|
31
38
|
"bugs": {
|
|
32
39
|
"url": "https://github.com/srod/node-minify/issues"
|
|
33
40
|
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"clean": "pnpm dlx rimraf dist",
|
|
43
|
+
"build": "npm run clean && tsup src/index.ts --format cjs,esm --dts --clean",
|
|
44
|
+
"prepublishOnly": "npm run build"
|
|
45
|
+
},
|
|
34
46
|
"dependencies": {
|
|
35
|
-
"@node-minify/utils": "
|
|
36
|
-
"globby": "11.0
|
|
47
|
+
"@node-minify/utils": "8.0.0-beta.0",
|
|
48
|
+
"globby": "11.1.0",
|
|
37
49
|
"mkdirp": "1.0.4"
|
|
38
50
|
},
|
|
39
|
-
"
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@node-minify/types": "8.0.0-beta.0",
|
|
53
|
+
"@types/mkdirp": "^1.0.2"
|
|
54
|
+
},
|
|
55
|
+
"gitHead": "0507c6190577e1939997a8231b07952ba167a780"
|
|
40
56
|
}
|
package/lib/compress.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.compress = void 0;
|
|
7
|
-
|
|
8
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
9
|
-
|
|
10
|
-
var _mkdirp = _interopRequireDefault(require("mkdirp"));
|
|
11
|
-
|
|
12
|
-
var _utils = require("@node-minify/utils");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
/*!
|
|
17
|
-
* node-minify
|
|
18
|
-
* Copyright(c) 2011-2022 Rodolphe Stoclin
|
|
19
|
-
* MIT Licensed
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Module dependencies.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Run compressor.
|
|
28
|
-
*
|
|
29
|
-
* @param {Object} settings
|
|
30
|
-
*/
|
|
31
|
-
const compress = settings => {
|
|
32
|
-
if (typeof settings.compressor !== 'function') {
|
|
33
|
-
throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
createDirectory(settings.output);
|
|
37
|
-
|
|
38
|
-
if (Array.isArray(settings.output)) {
|
|
39
|
-
return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
|
|
40
|
-
} else {
|
|
41
|
-
return _utils.utils.compressSingleFile(settings);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* Compress an array of files in sync.
|
|
46
|
-
*
|
|
47
|
-
* @param {Object} settings
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
exports.compress = compress;
|
|
52
|
-
|
|
53
|
-
const compressArrayOfFilesSync = settings => {
|
|
54
|
-
return settings.input.forEach((input, index) => {
|
|
55
|
-
const content = _utils.utils.getContentFromFiles(input);
|
|
56
|
-
|
|
57
|
-
return _utils.utils.runSync({
|
|
58
|
-
settings,
|
|
59
|
-
content,
|
|
60
|
-
index
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
/**
|
|
65
|
-
* Compress an array of files in async.
|
|
66
|
-
*
|
|
67
|
-
* @param {Object} settings
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const compressArrayOfFilesAsync = settings => {
|
|
72
|
-
let sequence = Promise.resolve();
|
|
73
|
-
settings.input.forEach((input, index) => {
|
|
74
|
-
const content = _utils.utils.getContentFromFiles(input);
|
|
75
|
-
|
|
76
|
-
sequence = sequence.then(() => _utils.utils.runAsync({
|
|
77
|
-
settings,
|
|
78
|
-
content,
|
|
79
|
-
index
|
|
80
|
-
}));
|
|
81
|
-
});
|
|
82
|
-
return sequence;
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* Create folder of the target file.
|
|
86
|
-
*
|
|
87
|
-
* @param {String} file - Full path of the file
|
|
88
|
-
*/
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const createDirectory = file => {
|
|
92
|
-
if (Array.isArray(file)) {
|
|
93
|
-
file = file[0];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const dir = file && file.substr(0, file.lastIndexOf('/'));
|
|
97
|
-
|
|
98
|
-
if (!dir) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (!_fs.default.statSync(dir).isDirectory()) {
|
|
103
|
-
_mkdirp.default.sync(dir);
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* Expose `compress()`.
|
|
108
|
-
*/
|
package/lib/compressInMemory.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.compressInMemory = void 0;
|
|
7
|
-
|
|
8
|
-
var _utils = require("@node-minify/utils");
|
|
9
|
-
|
|
10
|
-
/*!
|
|
11
|
-
* node-minify
|
|
12
|
-
* Copyright(c) 2011-2022 Rodolphe Stoclin
|
|
13
|
-
* MIT Licensed
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Module dependencies.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Run compressor.
|
|
22
|
-
*
|
|
23
|
-
* @param {Object} settings
|
|
24
|
-
*/
|
|
25
|
-
const compressInMemory = settings => {
|
|
26
|
-
if (typeof settings.compressor !== 'function') {
|
|
27
|
-
throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return _utils.utils.compressSingleFile(settings);
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Expose `compress()`.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
exports.compressInMemory = compressInMemory;
|
package/lib/core.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _setup = require("./setup");
|
|
4
|
-
|
|
5
|
-
var _compress = require("./compress");
|
|
6
|
-
|
|
7
|
-
var _compressInMemory = require("./compressInMemory");
|
|
8
|
-
|
|
9
|
-
/*!
|
|
10
|
-
* node-minify
|
|
11
|
-
* Copyright(c) 2011-2022 Rodolphe Stoclin
|
|
12
|
-
* MIT Licensed
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Module dependencies.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Run node-minify.
|
|
21
|
-
*
|
|
22
|
-
* @param {Object} settings - Settings from user input
|
|
23
|
-
*/
|
|
24
|
-
const minify = settings => {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
const method = settings.content ? _compressInMemory.compressInMemory : _compress.compress;
|
|
27
|
-
settings = (0, _setup.setup)(settings);
|
|
28
|
-
|
|
29
|
-
if (!settings.sync) {
|
|
30
|
-
method(settings).then(minified => {
|
|
31
|
-
if (settings.callback) {
|
|
32
|
-
settings.callback(null, minified);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
resolve(minified);
|
|
36
|
-
}).catch(err => {
|
|
37
|
-
if (settings.callback) {
|
|
38
|
-
settings.callback(err);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
reject(err);
|
|
42
|
-
});
|
|
43
|
-
} else {
|
|
44
|
-
const minified = method(settings);
|
|
45
|
-
|
|
46
|
-
if (settings.callback) {
|
|
47
|
-
settings.callback(null, minified);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
resolve(minified);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* Expose `minify()`.
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
module.exports = minify;
|
package/lib/setup.js
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.setup = void 0;
|
|
7
|
-
|
|
8
|
-
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
|
-
var _globby = _interopRequireDefault(require("globby"));
|
|
11
|
-
|
|
12
|
-
var _utils = require("@node-minify/utils");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
/*!
|
|
17
|
-
* node-minify
|
|
18
|
-
* Copyright(c) 2011-2022 Rodolphe Stoclin
|
|
19
|
-
* MIT Licensed
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Module dependencies.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Default settings.
|
|
28
|
-
*/
|
|
29
|
-
const defaultSettings = {
|
|
30
|
-
sync: false,
|
|
31
|
-
options: {},
|
|
32
|
-
buffer: 1000 * 1024,
|
|
33
|
-
callback: false
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Run setup.
|
|
37
|
-
*
|
|
38
|
-
* @param {Object} inputSettings
|
|
39
|
-
* @return {Object}
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
const setup = inputSettings => {
|
|
43
|
-
let settings = Object.assign(_utils.utils.clone(defaultSettings), inputSettings); // In memory
|
|
44
|
-
|
|
45
|
-
if (settings.content) {
|
|
46
|
-
checkMandatoriesMemoryContent(inputSettings);
|
|
47
|
-
return settings;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
checkMandatories(inputSettings);
|
|
51
|
-
settings = Object.assign(settings, wildcards(settings.input, settings.publicFolder));
|
|
52
|
-
settings = Object.assign(settings, checkOutput(settings.input, settings.output, settings.publicFolder, settings.replaceInPlace));
|
|
53
|
-
settings = Object.assign(settings, setPublicFolder(settings.input, settings.publicFolder));
|
|
54
|
-
return settings;
|
|
55
|
-
};
|
|
56
|
-
/**
|
|
57
|
-
* Check the output path, searching for $1
|
|
58
|
-
* if exist, returns the path remplacing $1 by file name
|
|
59
|
-
*
|
|
60
|
-
* @param {String|Array} input - Path file
|
|
61
|
-
* @param {String} output - Path to the output file
|
|
62
|
-
* @param {String} publicFolder - Path to the public folder
|
|
63
|
-
* @param {Boolean} replaceInPlace - True to replace file in same folder
|
|
64
|
-
* @return {Object}
|
|
65
|
-
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
exports.setup = setup;
|
|
69
|
-
|
|
70
|
-
const checkOutput = (input, output, publicFolder, replaceInPlace) => {
|
|
71
|
-
let reg = new RegExp('\\$1');
|
|
72
|
-
|
|
73
|
-
if (reg.test(output)) {
|
|
74
|
-
if (Array.isArray(input)) {
|
|
75
|
-
const outputMin = input.map(file => _utils.utils.setFileNameMin(file, output, replaceInPlace ? null : publicFolder, replaceInPlace));
|
|
76
|
-
return {
|
|
77
|
-
output: outputMin
|
|
78
|
-
};
|
|
79
|
-
} else {
|
|
80
|
-
return {
|
|
81
|
-
output: _utils.utils.setFileNameMin(input, output, replaceInPlace ? null : publicFolder, replaceInPlace)
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
/**
|
|
87
|
-
* Handle wildcards in a path, get the real path of each files.
|
|
88
|
-
*
|
|
89
|
-
* @param {String|Array} input - Path with wildcards
|
|
90
|
-
* @param {String} publicFolder - Path to the public folder
|
|
91
|
-
* @return {Object}
|
|
92
|
-
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const wildcards = (input, publicFolder) => {
|
|
96
|
-
// If it's a string
|
|
97
|
-
if (!Array.isArray(input)) {
|
|
98
|
-
return wildcardsString(input, publicFolder);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return wildcardsArray(input, publicFolder);
|
|
102
|
-
};
|
|
103
|
-
/**
|
|
104
|
-
* Handle wildcards in a path (string only), get the real path of each files.
|
|
105
|
-
*
|
|
106
|
-
* @param {String} input - Path with wildcards
|
|
107
|
-
* @param {String} publicFolder - Path to the public folder
|
|
108
|
-
* @return {Object}
|
|
109
|
-
*/
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const wildcardsString = (input, publicFolder) => {
|
|
113
|
-
const output = {};
|
|
114
|
-
|
|
115
|
-
if (input.indexOf('*') > -1) {
|
|
116
|
-
output.input = getFilesFromWildcards(input, publicFolder);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return output;
|
|
120
|
-
};
|
|
121
|
-
/**
|
|
122
|
-
* Handle wildcards in a path (array only), get the real path of each files.
|
|
123
|
-
*
|
|
124
|
-
* @param {Array} input - Path with wildcards
|
|
125
|
-
* @param {String} publicFolder - Path to the public folder
|
|
126
|
-
* @return {Object}
|
|
127
|
-
*/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const wildcardsArray = (input, publicFolder) => {
|
|
131
|
-
let output = {};
|
|
132
|
-
let isWildcardsPresent = false;
|
|
133
|
-
output.input = input; // Transform all wildcards to path file
|
|
134
|
-
|
|
135
|
-
const inputWithPublicFolder = input.map(item => {
|
|
136
|
-
if (item.indexOf('*') > -1) {
|
|
137
|
-
isWildcardsPresent = true;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return (publicFolder || '') + item;
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
if (isWildcardsPresent) {
|
|
144
|
-
output.input = _globby.default.sync(inputWithPublicFolder);
|
|
145
|
-
} // Remove all wildcards from array
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
for (let i = 0; i < output.input.length; i++) {
|
|
149
|
-
if (output.input[i].indexOf('*') > -1) {
|
|
150
|
-
output.input.splice(i, 1);
|
|
151
|
-
i--;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return output;
|
|
156
|
-
};
|
|
157
|
-
/**
|
|
158
|
-
* Get the real path of each files.
|
|
159
|
-
*
|
|
160
|
-
* @param {String} input - Path with wildcards
|
|
161
|
-
* @param {String} publicFolder - Path to the public folder
|
|
162
|
-
* @return {Object}
|
|
163
|
-
*/
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const getFilesFromWildcards = (input, publicFolder) => {
|
|
167
|
-
let output = [];
|
|
168
|
-
|
|
169
|
-
if (input.indexOf('*') > -1) {
|
|
170
|
-
output = _globby.default.sync((publicFolder || '') + input);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return output;
|
|
174
|
-
};
|
|
175
|
-
/**
|
|
176
|
-
* Prepend the public folder to each file.
|
|
177
|
-
*
|
|
178
|
-
* @param {String|Array} input - Path to file(s)
|
|
179
|
-
* @param {String} publicFolder - Path to the public folder
|
|
180
|
-
* @return {Object}
|
|
181
|
-
*/
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const setPublicFolder = (input, publicFolder) => {
|
|
185
|
-
let output = {};
|
|
186
|
-
|
|
187
|
-
if (typeof publicFolder !== 'string') {
|
|
188
|
-
return output;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
publicFolder = _path.default.normalize(publicFolder);
|
|
192
|
-
|
|
193
|
-
if (Array.isArray(input)) {
|
|
194
|
-
output.input = input.map(item => {
|
|
195
|
-
// Check if publicFolder is already in path
|
|
196
|
-
if (_path.default.normalize(item).indexOf(publicFolder) > -1) {
|
|
197
|
-
return item;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return _path.default.normalize(publicFolder + item);
|
|
201
|
-
});
|
|
202
|
-
return output;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
input = _path.default.normalize(input); // Check if publicFolder is already in path
|
|
206
|
-
|
|
207
|
-
if (input.indexOf(publicFolder) > -1) {
|
|
208
|
-
output.input = input;
|
|
209
|
-
return output;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
output.input = _path.default.normalize(publicFolder + input);
|
|
213
|
-
return output;
|
|
214
|
-
};
|
|
215
|
-
/**
|
|
216
|
-
* Check if some settings are here.
|
|
217
|
-
*
|
|
218
|
-
* @param {Object} settings
|
|
219
|
-
*/
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const checkMandatories = settings => {
|
|
223
|
-
['compressor', 'input', 'output'].forEach(item => mandatory(item, settings));
|
|
224
|
-
};
|
|
225
|
-
/**
|
|
226
|
-
* Check if some settings are here for memory content.
|
|
227
|
-
*
|
|
228
|
-
* @param {Object} settings
|
|
229
|
-
*/
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
const checkMandatoriesMemoryContent = settings => {
|
|
233
|
-
['compressor', 'content'].forEach(item => mandatory(item, settings));
|
|
234
|
-
};
|
|
235
|
-
/**
|
|
236
|
-
* Check if the setting exist.
|
|
237
|
-
*
|
|
238
|
-
* @param {String} setting
|
|
239
|
-
* @param {Object} settings
|
|
240
|
-
*/
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
const mandatory = (setting, settings) => {
|
|
244
|
-
if (!settings[setting]) {
|
|
245
|
-
throw new Error(setting + ' is mandatory.');
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
/**
|
|
249
|
-
* Expose `setup()`.
|
|
250
|
-
*/
|