@node-minify/utils 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.
@@ -0,0 +1,23 @@
1
+ /*!
2
+ * node-minify
3
+ * Copyright(c) 2011-2022 Rodolphe Stoclin
4
+ * MIT Licensed
5
+ */
6
+ interface Utils {
7
+ readFile: Function;
8
+ writeFile: Function;
9
+ deleteFile: Function;
10
+ buildArgs: Function;
11
+ clone: Function;
12
+ getFilesizeInBytes: Function;
13
+ getFilesizeGzippedInBytes: Function;
14
+ prettyBytes: Function;
15
+ setFileNameMin: Function;
16
+ compressSingleFile: Function;
17
+ getContentFromFiles: Function;
18
+ runSync: Function;
19
+ runAsync: Function;
20
+ }
21
+ declare const utils: Utils;
22
+
23
+ export { utils };
package/dist/index.js ADDED
@@ -0,0 +1,134 @@
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
+ utils: () => utils
29
+ });
30
+ module.exports = __toCommonJS(src_exports);
31
+ var import_fs = __toESM(require("fs"));
32
+ var import_gzip_size = __toESM(require("gzip-size"));
33
+ var utils = {};
34
+ utils.readFile = (file) => import_fs.default.readFileSync(file, "utf8");
35
+ utils.writeFile = ({ file, content, index }) => {
36
+ const _file = index !== void 0 ? file[index] : file;
37
+ if (!import_fs.default.existsSync(_file) || import_fs.default.existsSync(_file) && !import_fs.default.lstatSync(_file).isDirectory()) {
38
+ import_fs.default.writeFileSync(_file, content, "utf8");
39
+ }
40
+ return content;
41
+ };
42
+ utils.deleteFile = (file) => import_fs.default.unlinkSync(file);
43
+ utils.buildArgs = (options) => {
44
+ const args = [];
45
+ Object.keys(options).forEach((key) => {
46
+ if (options[key] && options[key] !== false) {
47
+ args.push("--" + key);
48
+ }
49
+ if (options[key] && options[key] !== true) {
50
+ args.push(options[key]);
51
+ }
52
+ });
53
+ return args;
54
+ };
55
+ utils.clone = (obj) => JSON.parse(JSON.stringify(obj));
56
+ utils.getFilesizeInBytes = (file) => {
57
+ const stats = import_fs.default.statSync(file);
58
+ const fileSizeInBytes = stats.size;
59
+ return utils.prettyBytes(fileSizeInBytes);
60
+ };
61
+ utils.getFilesizeGzippedInBytes = (file) => {
62
+ return new Promise((resolve) => {
63
+ const source = import_fs.default.createReadStream(file);
64
+ source.pipe(import_gzip_size.default.stream()).on("gzip-size", (size) => {
65
+ resolve(utils.prettyBytes(size));
66
+ });
67
+ });
68
+ };
69
+ utils.prettyBytes = (num) => {
70
+ const UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
71
+ if (!Number.isFinite(num)) {
72
+ throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`);
73
+ }
74
+ const neg = num < 0;
75
+ if (neg) {
76
+ num = -num;
77
+ }
78
+ if (num < 1) {
79
+ return (neg ? "-" : "") + num + " B";
80
+ }
81
+ const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1e3)), UNITS.length - 1);
82
+ const numStr = Number((num / Math.pow(1e3, exponent)).toPrecision(3));
83
+ const unit = UNITS[exponent];
84
+ return (neg ? "-" : "") + numStr + " " + unit;
85
+ };
86
+ utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
87
+ const filePath = file.substr(0, file.lastIndexOf("/") + 1);
88
+ const fileWithoutPath = file.substr(file.lastIndexOf("/") + 1);
89
+ let fileWithoutExtension = fileWithoutPath.substr(0, fileWithoutPath.lastIndexOf("."));
90
+ if (publicFolder) {
91
+ fileWithoutExtension = publicFolder + fileWithoutExtension;
92
+ }
93
+ if (replaceInPlace) {
94
+ fileWithoutExtension = filePath + fileWithoutExtension;
95
+ }
96
+ return output.replace("$1", fileWithoutExtension);
97
+ };
98
+ utils.compressSingleFile = (settings) => {
99
+ const content = settings.content ? settings.content : utils.getContentFromFiles(settings.input);
100
+ return settings.sync ? utils.runSync({ settings, content }) : utils.runAsync({ settings, content });
101
+ };
102
+ utils.getContentFromFiles = (input) => {
103
+ if (!Array.isArray(input)) {
104
+ return import_fs.default.readFileSync(input, "utf8");
105
+ }
106
+ return input.map(
107
+ (path) => !import_fs.default.existsSync(path) || import_fs.default.existsSync(path) && !import_fs.default.lstatSync(path).isDirectory() ? import_fs.default.readFileSync(path, "utf8") : ""
108
+ ).join("\n");
109
+ };
110
+ utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? settings.compressor({ settings, content, callback: null, index }) : null;
111
+ utils.runAsync = ({ settings, content, index }) => {
112
+ return new Promise((resolve, reject) => {
113
+ settings && typeof settings.compressor !== "string" ? settings.compressor({
114
+ settings,
115
+ content,
116
+ callback: (err, min) => {
117
+ if (err) {
118
+ return reject(err);
119
+ }
120
+ resolve(min);
121
+ },
122
+ index
123
+ }) : null;
124
+ });
125
+ };
126
+ // Annotate the CommonJS export names for ESM import in node:
127
+ 0 && (module.exports = {
128
+ utils
129
+ });
130
+ /*!
131
+ * node-minify
132
+ * Copyright(c) 2011-2022 Rodolphe Stoclin
133
+ * MIT Licensed
134
+ */
package/dist/index.mjs ADDED
@@ -0,0 +1,104 @@
1
+ // src/index.ts
2
+ import fs from "fs";
3
+ import gzipSize from "gzip-size";
4
+ var utils = {};
5
+ utils.readFile = (file) => fs.readFileSync(file, "utf8");
6
+ utils.writeFile = ({ file, content, index }) => {
7
+ const _file = index !== void 0 ? file[index] : file;
8
+ if (!fs.existsSync(_file) || fs.existsSync(_file) && !fs.lstatSync(_file).isDirectory()) {
9
+ fs.writeFileSync(_file, content, "utf8");
10
+ }
11
+ return content;
12
+ };
13
+ utils.deleteFile = (file) => fs.unlinkSync(file);
14
+ utils.buildArgs = (options) => {
15
+ const args = [];
16
+ Object.keys(options).forEach((key) => {
17
+ if (options[key] && options[key] !== false) {
18
+ args.push("--" + key);
19
+ }
20
+ if (options[key] && options[key] !== true) {
21
+ args.push(options[key]);
22
+ }
23
+ });
24
+ return args;
25
+ };
26
+ utils.clone = (obj) => JSON.parse(JSON.stringify(obj));
27
+ utils.getFilesizeInBytes = (file) => {
28
+ const stats = fs.statSync(file);
29
+ const fileSizeInBytes = stats.size;
30
+ return utils.prettyBytes(fileSizeInBytes);
31
+ };
32
+ utils.getFilesizeGzippedInBytes = (file) => {
33
+ return new Promise((resolve) => {
34
+ const source = fs.createReadStream(file);
35
+ source.pipe(gzipSize.stream()).on("gzip-size", (size) => {
36
+ resolve(utils.prettyBytes(size));
37
+ });
38
+ });
39
+ };
40
+ utils.prettyBytes = (num) => {
41
+ const UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
42
+ if (!Number.isFinite(num)) {
43
+ throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`);
44
+ }
45
+ const neg = num < 0;
46
+ if (neg) {
47
+ num = -num;
48
+ }
49
+ if (num < 1) {
50
+ return (neg ? "-" : "") + num + " B";
51
+ }
52
+ const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1e3)), UNITS.length - 1);
53
+ const numStr = Number((num / Math.pow(1e3, exponent)).toPrecision(3));
54
+ const unit = UNITS[exponent];
55
+ return (neg ? "-" : "") + numStr + " " + unit;
56
+ };
57
+ utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
58
+ const filePath = file.substr(0, file.lastIndexOf("/") + 1);
59
+ const fileWithoutPath = file.substr(file.lastIndexOf("/") + 1);
60
+ let fileWithoutExtension = fileWithoutPath.substr(0, fileWithoutPath.lastIndexOf("."));
61
+ if (publicFolder) {
62
+ fileWithoutExtension = publicFolder + fileWithoutExtension;
63
+ }
64
+ if (replaceInPlace) {
65
+ fileWithoutExtension = filePath + fileWithoutExtension;
66
+ }
67
+ return output.replace("$1", fileWithoutExtension);
68
+ };
69
+ utils.compressSingleFile = (settings) => {
70
+ const content = settings.content ? settings.content : utils.getContentFromFiles(settings.input);
71
+ return settings.sync ? utils.runSync({ settings, content }) : utils.runAsync({ settings, content });
72
+ };
73
+ utils.getContentFromFiles = (input) => {
74
+ if (!Array.isArray(input)) {
75
+ return fs.readFileSync(input, "utf8");
76
+ }
77
+ return input.map(
78
+ (path) => !fs.existsSync(path) || fs.existsSync(path) && !fs.lstatSync(path).isDirectory() ? fs.readFileSync(path, "utf8") : ""
79
+ ).join("\n");
80
+ };
81
+ utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? settings.compressor({ settings, content, callback: null, index }) : null;
82
+ utils.runAsync = ({ settings, content, index }) => {
83
+ return new Promise((resolve, reject) => {
84
+ settings && typeof settings.compressor !== "string" ? settings.compressor({
85
+ settings,
86
+ content,
87
+ callback: (err, min) => {
88
+ if (err) {
89
+ return reject(err);
90
+ }
91
+ resolve(min);
92
+ },
93
+ index
94
+ }) : null;
95
+ });
96
+ };
97
+ export {
98
+ utils
99
+ };
100
+ /*!
101
+ * node-minify
102
+ * Copyright(c) 2011-2022 Rodolphe Stoclin
103
+ * MIT Licensed
104
+ */
package/package.json CHANGED
@@ -1,25 +1,33 @@
1
1
  {
2
2
  "name": "@node-minify/utils",
3
- "version": "7.0.0",
3
+ "version": "8.0.0-beta.0",
4
4
  "description": "utils for @node-minify",
5
5
  "keywords": [
6
6
  "compressor",
7
7
  "minify",
8
- "minifier"
8
+ "minifier",
9
+ "utils"
9
10
  ],
10
11
  "author": "Rodolphe Stoclin <srodolphe@gmail.com>",
11
12
  "homepage": "https://github.com/srod/node-minify/tree/master/packages/utils#readme",
12
13
  "license": "MIT",
13
- "main": "lib/utils.js",
14
14
  "engines": {
15
15
  "node": ">=14.0.0"
16
16
  },
17
17
  "directories": {
18
- "lib": "lib",
18
+ "lib": "dist",
19
19
  "test": "__tests__"
20
20
  },
21
+ "main": "./dist/index.js",
22
+ "module": "./dist/index.mjs",
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ "require": "./dist/index.js",
26
+ "import": "./dist/index.mjs",
27
+ "types": "./dist/index.d.ts"
28
+ },
21
29
  "files": [
22
- "lib"
30
+ "dist/**/*"
23
31
  ],
24
32
  "publishConfig": {
25
33
  "access": "public"
@@ -31,8 +39,16 @@
31
39
  "bugs": {
32
40
  "url": "https://github.com/srod/node-minify/issues"
33
41
  },
42
+ "scripts": {
43
+ "clean": "pnpm dlx rimraf dist",
44
+ "build": "npm run clean && tsup src/index.ts --format cjs,esm --dts --clean",
45
+ "prepublishOnly": "npm run build"
46
+ },
34
47
  "dependencies": {
35
48
  "gzip-size": "6.0.0"
36
49
  },
37
- "gitHead": "8b5bda6f1ac9fe7180006f2a19ec3253e8fff4ec"
50
+ "devDependencies": {
51
+ "@node-minify/types": "8.0.0-beta.0"
52
+ },
53
+ "gitHead": "0507c6190577e1939997a8231b07952ba167a780"
38
54
  }
package/lib/utils.js DELETED
@@ -1,266 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.utils = void 0;
7
-
8
- var _fs = _interopRequireDefault(require("fs"));
9
-
10
- var _gzipSize = _interopRequireDefault(require("gzip-size"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- /*!
15
- * node-minify
16
- * Copyright(c) 2011-2022 Rodolphe Stoclin
17
- * MIT Licensed
18
- */
19
-
20
- /**
21
- * Module dependencies.
22
- */
23
- const utils = {};
24
- /**
25
- * Read content from file.
26
- *
27
- * @param {String} file
28
- * @returns {String}
29
- */
30
-
31
- exports.utils = utils;
32
-
33
- utils.readFile = file => _fs.default.readFileSync(file, 'utf8');
34
- /**
35
- * Write content into file.
36
- *
37
- * @param {String} file
38
- * @param {String} content
39
- * @param {Number} index - index of the file being processed
40
- * @returns {String}
41
- */
42
-
43
-
44
- utils.writeFile = ({
45
- file,
46
- content,
47
- index
48
- }) => {
49
- const _file = index !== undefined ? file[index] : file;
50
-
51
- if (!_fs.default.existsSync(_file) || _fs.default.existsSync(_file) && !_fs.default.lstatSync(_file).isDirectory()) {
52
- _fs.default.writeFileSync(_file, content, 'utf8');
53
- }
54
-
55
- return content;
56
- };
57
- /**
58
- * Delete file.
59
- *
60
- * @param {String} file
61
- * @returns {String}
62
- */
63
-
64
-
65
- utils.deleteFile = file => _fs.default.unlinkSync(file);
66
- /**
67
- * Builds arguments array based on an object.
68
- *
69
- * @param {Object} options
70
- * @returns {Array}
71
- */
72
-
73
-
74
- utils.buildArgs = options => {
75
- const args = [];
76
- Object.keys(options).forEach(key => {
77
- if (options[key] && options[key] !== false) {
78
- args.push('--' + key);
79
- }
80
-
81
- if (options[key] && options[key] !== true) {
82
- args.push(options[key]);
83
- }
84
- });
85
- return args;
86
- };
87
- /**
88
- * Clone an object.
89
- *
90
- * @param {Object} obj
91
- * @returns {Object}
92
- */
93
-
94
-
95
- utils.clone = obj => JSON.parse(JSON.stringify(obj));
96
- /**
97
- * Get the file size in bytes.
98
- *
99
- * @returns {String}
100
- */
101
-
102
-
103
- utils.getFilesizeInBytes = file => {
104
- const stats = _fs.default.statSync(file);
105
-
106
- const fileSizeInBytes = stats.size;
107
- return utils.prettyBytes(fileSizeInBytes);
108
- };
109
- /**
110
- * Get the gzipped file size in bytes.
111
- *
112
- * @returns {Promise.<String>}
113
- */
114
-
115
-
116
- utils.getFilesizeGzippedInBytes = file => {
117
- return new Promise(resolve => {
118
- const source = _fs.default.createReadStream(file);
119
-
120
- source.pipe(_gzipSize.default.stream()).on('gzip-size', size => {
121
- resolve(utils.prettyBytes(size));
122
- });
123
- });
124
- };
125
- /**
126
- * Get the size in human readable.
127
- * From https://github.com/sindresorhus/pretty-bytes
128
- *
129
- * @returns {String}
130
- */
131
-
132
-
133
- utils.prettyBytes = num => {
134
- const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
135
-
136
- if (!Number.isFinite(num)) {
137
- throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`);
138
- }
139
-
140
- const neg = num < 0;
141
-
142
- if (neg) {
143
- num = -num;
144
- }
145
-
146
- if (num < 1) {
147
- return (neg ? '-' : '') + num + ' B';
148
- }
149
-
150
- const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), UNITS.length - 1);
151
- const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3));
152
- const unit = UNITS[exponent];
153
- return (neg ? '-' : '') + numStr + ' ' + unit;
154
- };
155
- /**
156
- * Set the file name as minified.
157
- * eg. file.js returns file.min.js
158
- *
159
- * @param {String} file
160
- * @param {String} output
161
- * @param {String} publicFolder
162
- * @param {Boolean} replaceInPlace
163
- * @returns {String}
164
- */
165
-
166
-
167
- utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
168
- const filePath = file.substr(0, file.lastIndexOf('/') + 1);
169
- const fileWithoutPath = file.substr(file.lastIndexOf('/') + 1);
170
- let fileWithoutExtension = fileWithoutPath.substr(0, fileWithoutPath.lastIndexOf('.'));
171
-
172
- if (publicFolder) {
173
- fileWithoutExtension = publicFolder + fileWithoutExtension;
174
- }
175
-
176
- if (replaceInPlace) {
177
- fileWithoutExtension = filePath + fileWithoutExtension;
178
- }
179
-
180
- return output.replace('$1', fileWithoutExtension);
181
- };
182
- /**
183
- * Compress a single file.
184
- *
185
- * @param {Object} settings
186
- */
187
-
188
-
189
- utils.compressSingleFile = settings => {
190
- const content = settings.content ? settings.content : utils.getContentFromFiles(settings.input);
191
- return settings.sync ? utils.runSync({
192
- settings,
193
- content
194
- }) : utils.runAsync({
195
- settings,
196
- content
197
- });
198
- };
199
- /**
200
- * Concatenate all input files and get the data.
201
- *
202
- * @param {String|Array} input
203
- * @return {String}
204
- */
205
-
206
-
207
- utils.getContentFromFiles = input => {
208
- if (!Array.isArray(input)) {
209
- return _fs.default.readFileSync(input, 'utf8');
210
- }
211
-
212
- return input.map(path => !_fs.default.existsSync(path) || _fs.default.existsSync(path) && !_fs.default.lstatSync(path).isDirectory() ? _fs.default.readFileSync(path, 'utf8') : '').join('\n');
213
- };
214
- /**
215
- * Run compressor in sync.
216
- *
217
- * @param {Object} settings
218
- * @param {String} content
219
- * @param {Number} index - index of the file being processed
220
- * @return {String}
221
- */
222
-
223
-
224
- utils.runSync = ({
225
- settings,
226
- content,
227
- index
228
- }) => settings.compressor({
229
- settings,
230
- content,
231
- callback: null,
232
- index
233
- });
234
- /**
235
- * Run compressor in async.
236
- *
237
- * @param {Object} settings
238
- * @param {String} content
239
- * @param {Number} index - index of the file being processed
240
- * @return {Promise}
241
- */
242
-
243
-
244
- utils.runAsync = ({
245
- settings,
246
- content,
247
- index
248
- }) => {
249
- return new Promise((resolve, reject) => {
250
- settings.compressor({
251
- settings,
252
- content,
253
- callback: (err, min) => {
254
- if (err) {
255
- return reject(err);
256
- }
257
-
258
- resolve(min);
259
- },
260
- index
261
- });
262
- });
263
- };
264
- /**
265
- * Expose `utils`.
266
- */