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