@node-minify/core 7.1.0 → 8.0.1-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,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,231 @@
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
+ */
231
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/setup.ts","../src/compress.ts","../src/compressInMemory.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport { setup } from './setup';\nimport { compress } from './compress';\nimport { compressInMemory } from './compressInMemory';\nimport { Settings } from '@node-minify/types';\n\n// export interface Settings {\n// compressorLabel: string | Function;\n// compressor?: string | Function;\n// sync?: boolean;\n// callback?: Function;\n// content?: string;\n// input: string | string[];\n// output: string;\n// options?: string;\n// }\n\n/**\n * Run node-minify.\n *\n * @param {Object} settings - Settings from user input\n */\nconst minify = (settings: Settings) => {\n return new Promise((resolve, reject) => {\n const method = settings.content ? compressInMemory : compress;\n settings = setup(settings);\n if (!settings.sync) {\n method(settings)\n .then((minified: string) => {\n if (settings.callback) {\n settings.callback(null, minified);\n }\n resolve(minified);\n })\n .catch((err: Error) => {\n if (settings.callback) {\n settings.callback(err);\n }\n reject(err);\n });\n } else {\n const minified = method(settings);\n if (settings.callback) {\n settings.callback(null, minified);\n }\n resolve(minified);\n }\n });\n};\n\n/**\n * Expose `minify()`.\n */\nexport default minify;\n","/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport path from 'path';\nimport globby from 'globby';\nimport { utils } from '@node-minify/utils';\n\n/**\n * Default settings.\n */\nconst defaultSettings = {\n sync: false,\n options: {},\n buffer: 1000 * 1024,\n callback: false\n};\n\n/**\n * Run setup.\n *\n * @param {Object} inputSettings\n * @return {Object}\n */\nconst setup = (inputSettings: {}) => {\n let settings = Object.assign(utils.clone(defaultSettings), inputSettings);\n\n // In memory\n if (settings.content) {\n checkMandatoriesMemoryContent(inputSettings);\n return settings;\n }\n\n checkMandatories(inputSettings);\n\n settings = Object.assign(settings, wildcards(settings.input, settings.publicFolder));\n settings = Object.assign(\n settings,\n checkOutput(settings.input, settings.output, settings.publicFolder, settings.replaceInPlace)\n );\n settings = Object.assign(settings, setPublicFolder(settings.input, settings.publicFolder));\n\n return settings;\n};\n\n/**\n * Check the output path, searching for $1\n * if exist, returns the path remplacing $1 by file name\n *\n * @param {String|Array} input - Path file\n * @param {String} output - Path to the output file\n * @param {String} publicFolder - Path to the public folder\n * @param {Boolean} replaceInPlace - True to replace file in same folder\n * @return {Object}\n */\nconst checkOutput = (input: string | string[], output: string, publicFolder: string, replaceInPlace: boolean) => {\n let reg = new RegExp('\\\\$1');\n if (reg.test(output)) {\n if (Array.isArray(input)) {\n const outputMin = input.map(file =>\n utils.setFileNameMin(file, output, replaceInPlace ? null : publicFolder, replaceInPlace)\n );\n return { output: outputMin };\n } else {\n return { output: utils.setFileNameMin(input, output, replaceInPlace ? null : publicFolder, replaceInPlace) };\n }\n }\n};\n\n/**\n * Handle wildcards in a path, get the real path of each files.\n *\n * @param {String|Array} input - Path with wildcards\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst wildcards = (input: string | string[], publicFolder: string) => {\n // If it's a string\n if (!Array.isArray(input)) {\n return wildcardsString(input, publicFolder);\n }\n\n return wildcardsArray(input, publicFolder);\n};\n\n/**\n * Handle wildcards in a path (string only), get the real path of each files.\n *\n * @param {String} input - Path with wildcards\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst wildcardsString = (input: string, publicFolder: string) => {\n const output: { input?: string[] } = {};\n\n if (input.indexOf('*') > -1) {\n output.input = getFilesFromWildcards(input, publicFolder);\n }\n\n return output;\n};\n\n/**\n * Handle wildcards in a path (array only), get the real path of each files.\n *\n * @param {Array} input - Path with wildcards\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst wildcardsArray = (input: string[], publicFolder: string) => {\n let output: { input?: string[] } = {};\n let isWildcardsPresent = false;\n\n output.input = input;\n\n // Transform all wildcards to path file\n const inputWithPublicFolder = input.map(item => {\n if (item.indexOf('*') > -1) {\n isWildcardsPresent = true;\n }\n return (publicFolder || '') + item;\n });\n\n if (isWildcardsPresent) {\n output.input = globby.sync(inputWithPublicFolder);\n }\n\n // Remove all wildcards from array\n for (let i = 0; i < output.input.length; i++) {\n if (output.input[i].indexOf('*') > -1) {\n output.input.splice(i, 1);\n\n i--;\n }\n }\n\n return output;\n};\n\n/**\n * Get the real path of each files.\n *\n * @param {String} input - Path with wildcards\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst getFilesFromWildcards = (input: string, publicFolder: string) => {\n let output: string[] = [];\n\n if (input.indexOf('*') > -1) {\n output = globby.sync((publicFolder || '') + input);\n }\n\n return output;\n};\n\n/**\n * Prepend the public folder to each file.\n *\n * @param {String|Array} input - Path to file(s)\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst setPublicFolder = (input: string | string[], publicFolder: string) => {\n let output: { input?: string | string[] } = {};\n\n if (typeof publicFolder !== 'string') {\n return output;\n }\n\n publicFolder = path.normalize(publicFolder);\n\n if (Array.isArray(input)) {\n output.input = input.map(item => {\n // Check if publicFolder is already in path\n if (path.normalize(item).indexOf(publicFolder) > -1) {\n return item;\n }\n return path.normalize(publicFolder + item);\n });\n return output;\n }\n\n input = path.normalize(input);\n\n // Check if publicFolder is already in path\n if (input.indexOf(publicFolder) > -1) {\n output.input = input;\n return output;\n }\n\n output.input = path.normalize(publicFolder + input);\n\n return output;\n};\n\n/**\n * Check if some settings are here.\n *\n * @param {Object} settings\n */\nconst checkMandatories = (settings: {}) => {\n ['compressor', 'input', 'output'].forEach(item => mandatory(item, settings));\n};\n\n/**\n * Check if some settings are here for memory content.\n *\n * @param {Object} settings\n */\nconst checkMandatoriesMemoryContent = (settings: {}) => {\n ['compressor', 'content'].forEach(item => mandatory(item, settings));\n};\n\n/**\n * Check if the setting exist.\n *\n * @param {String} setting\n * @param {Object} settings\n */\ninterface Dictionary<T> {\n [Key: string]: T;\n}\nconst mandatory = (setting: string, settings: Dictionary<string>) => {\n if (!settings[setting]) {\n throw new Error(setting + ' is mandatory.');\n }\n};\n\n/**\n * Expose `setup()`.\n */\nexport { setup };\n","/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport fs from 'fs';\nimport mkdirp from 'mkdirp';\nimport { utils } from '@node-minify/utils';\nimport { Settings } from '@node-minify/types';\n\n/**\n * Run compressor.\n *\n * @param {Object} settings\n */\nconst compress = (settings: Settings) => {\n if (typeof settings.compressor !== 'function') {\n throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);\n }\n\n createDirectory(settings.output);\n\n if (Array.isArray(settings.output)) {\n return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);\n } else {\n return utils.compressSingleFile(settings);\n }\n};\n\n/**\n * Compress an array of files in sync.\n *\n * @param {Object} settings\n */\nconst compressArrayOfFilesSync = (settings: Settings) => {\n return (\n Array.isArray(settings.input) &&\n settings.input.forEach((input, index) => {\n const content = utils.getContentFromFiles(input);\n return utils.runSync({ settings, content, index });\n })\n );\n};\n\n/**\n * Compress an array of files in async.\n *\n * @param {Object} settings\n */\nconst compressArrayOfFilesAsync = (settings: Settings) => {\n let sequence = Promise.resolve();\n Array.isArray(settings.input) &&\n settings.input.forEach((input, index) => {\n const content = utils.getContentFromFiles(input);\n sequence = sequence.then(() => utils.runAsync({ settings, content, index }));\n });\n return sequence;\n};\n\n/**\n * Create folder of the target file.\n *\n * @param {String} file - Full path of the file\n */\nconst createDirectory = (file: string) => {\n if (Array.isArray(file)) {\n file = file[0];\n }\n const dir = file && file.substr(0, file.lastIndexOf('/'));\n if (!dir) {\n return;\n }\n if (!fs.statSync(dir).isDirectory()) {\n mkdirp.sync(dir);\n }\n};\n\n/**\n * Expose `compress()`.\n */\nexport { compress };\n","/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport { utils } from '@node-minify/utils';\nimport { Settings } from '@node-minify/types';\n\n/**\n * Run compressor.\n *\n * @param {Object} settings\n */\nconst compressInMemory = (settings: Settings) => {\n if (typeof settings.compressor !== 'function') {\n throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);\n }\n\n return utils.compressSingleFile(settings);\n};\n\n/**\n * Expose `compress()`.\n */\nexport { compressInMemory };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACSA,kBAAiB;AACjB,oBAAmB;AACnB,mBAAsB;AAKtB,IAAM,kBAAkB;AAAA,EACtB,MAAM;AAAA,EACN,SAAS,CAAC;AAAA,EACV,QAAQ,MAAO;AAAA,EACf,UAAU;AACZ;AAQA,IAAM,QAAQ,CAAC,kBAAsB;AACnC,MAAI,WAAW,OAAO,OAAO,mBAAM,MAAM,eAAe,GAAG,aAAa;AAGxE,MAAI,SAAS,SAAS;AACpB,kCAA8B,aAAa;AAC3C,WAAO;AAAA,EACT;AAEA,mBAAiB,aAAa;AAE9B,aAAW,OAAO,OAAO,UAAU,UAAU,SAAS,OAAO,SAAS,YAAY,CAAC;AACnF,aAAW,OAAO;AAAA,IAChB;AAAA,IACA,YAAY,SAAS,OAAO,SAAS,QAAQ,SAAS,cAAc,SAAS,cAAc;AAAA,EAC7F;AACA,aAAW,OAAO,OAAO,UAAU,gBAAgB,SAAS,OAAO,SAAS,YAAY,CAAC;AAEzF,SAAO;AACT;AAYA,IAAM,cAAc,CAAC,OAA0B,QAAgB,cAAsB,mBAA4B;AAC/G,MAAI,MAAM,IAAI,OAAO,MAAM;AAC3B,MAAI,IAAI,KAAK,MAAM,GAAG;AACpB,QAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,YAAM,YAAY,MAAM;AAAA,QAAI,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aAAuB;AACrC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,SAAS,SAAS,UAAU,mBAAmB;AACrD,eAAW,MAAM,QAAQ;AACzB,QAAI,CAAC,SAAS,MAAM;AAClB,aAAO,QAAQ,EACZ,KAAK,CAAC,aAAqB;AAC1B,YAAI,SAAS,UAAU;AACrB,mBAAS,SAAS,MAAM,QAAQ;AAAA,QAClC;AACA,gBAAQ,QAAQ;AAAA,MAClB,CAAC,EACA,MAAM,CAAC,QAAe;AACrB,YAAI,SAAS,UAAU;AACrB,mBAAS,SAAS,GAAG;AAAA,QACvB;AACA,eAAO,GAAG;AAAA,MACZ,CAAC;AAAA,IACL,OAAO;AACL,YAAM,WAAW,OAAO,QAAQ;AAChC,UAAI,SAAS,UAAU;AACrB,iBAAS,SAAS,MAAM,QAAQ;AAAA,MAClC;AACA,cAAQ,QAAQ;AAAA,IAClB;AAAA,EACF,CAAC;AACH;AAKA,IAAO,cAAQ;","names":["globby","path","import_utils","fs","mkdirp","import_utils"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,201 @@
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
+ */
201
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/setup.ts","../src/compress.ts","../src/compressInMemory.ts","../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport path from 'path';\nimport globby from 'globby';\nimport { utils } from '@node-minify/utils';\n\n/**\n * Default settings.\n */\nconst defaultSettings = {\n sync: false,\n options: {},\n buffer: 1000 * 1024,\n callback: false\n};\n\n/**\n * Run setup.\n *\n * @param {Object} inputSettings\n * @return {Object}\n */\nconst setup = (inputSettings: {}) => {\n let settings = Object.assign(utils.clone(defaultSettings), inputSettings);\n\n // In memory\n if (settings.content) {\n checkMandatoriesMemoryContent(inputSettings);\n return settings;\n }\n\n checkMandatories(inputSettings);\n\n settings = Object.assign(settings, wildcards(settings.input, settings.publicFolder));\n settings = Object.assign(\n settings,\n checkOutput(settings.input, settings.output, settings.publicFolder, settings.replaceInPlace)\n );\n settings = Object.assign(settings, setPublicFolder(settings.input, settings.publicFolder));\n\n return settings;\n};\n\n/**\n * Check the output path, searching for $1\n * if exist, returns the path remplacing $1 by file name\n *\n * @param {String|Array} input - Path file\n * @param {String} output - Path to the output file\n * @param {String} publicFolder - Path to the public folder\n * @param {Boolean} replaceInPlace - True to replace file in same folder\n * @return {Object}\n */\nconst checkOutput = (input: string | string[], output: string, publicFolder: string, replaceInPlace: boolean) => {\n let reg = new RegExp('\\\\$1');\n if (reg.test(output)) {\n if (Array.isArray(input)) {\n const outputMin = input.map(file =>\n utils.setFileNameMin(file, output, replaceInPlace ? null : publicFolder, replaceInPlace)\n );\n return { output: outputMin };\n } else {\n return { output: utils.setFileNameMin(input, output, replaceInPlace ? null : publicFolder, replaceInPlace) };\n }\n }\n};\n\n/**\n * Handle wildcards in a path, get the real path of each files.\n *\n * @param {String|Array} input - Path with wildcards\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst wildcards = (input: string | string[], publicFolder: string) => {\n // If it's a string\n if (!Array.isArray(input)) {\n return wildcardsString(input, publicFolder);\n }\n\n return wildcardsArray(input, publicFolder);\n};\n\n/**\n * Handle wildcards in a path (string only), get the real path of each files.\n *\n * @param {String} input - Path with wildcards\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst wildcardsString = (input: string, publicFolder: string) => {\n const output: { input?: string[] } = {};\n\n if (input.indexOf('*') > -1) {\n output.input = getFilesFromWildcards(input, publicFolder);\n }\n\n return output;\n};\n\n/**\n * Handle wildcards in a path (array only), get the real path of each files.\n *\n * @param {Array} input - Path with wildcards\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst wildcardsArray = (input: string[], publicFolder: string) => {\n let output: { input?: string[] } = {};\n let isWildcardsPresent = false;\n\n output.input = input;\n\n // Transform all wildcards to path file\n const inputWithPublicFolder = input.map(item => {\n if (item.indexOf('*') > -1) {\n isWildcardsPresent = true;\n }\n return (publicFolder || '') + item;\n });\n\n if (isWildcardsPresent) {\n output.input = globby.sync(inputWithPublicFolder);\n }\n\n // Remove all wildcards from array\n for (let i = 0; i < output.input.length; i++) {\n if (output.input[i].indexOf('*') > -1) {\n output.input.splice(i, 1);\n\n i--;\n }\n }\n\n return output;\n};\n\n/**\n * Get the real path of each files.\n *\n * @param {String} input - Path with wildcards\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst getFilesFromWildcards = (input: string, publicFolder: string) => {\n let output: string[] = [];\n\n if (input.indexOf('*') > -1) {\n output = globby.sync((publicFolder || '') + input);\n }\n\n return output;\n};\n\n/**\n * Prepend the public folder to each file.\n *\n * @param {String|Array} input - Path to file(s)\n * @param {String} publicFolder - Path to the public folder\n * @return {Object}\n */\nconst setPublicFolder = (input: string | string[], publicFolder: string) => {\n let output: { input?: string | string[] } = {};\n\n if (typeof publicFolder !== 'string') {\n return output;\n }\n\n publicFolder = path.normalize(publicFolder);\n\n if (Array.isArray(input)) {\n output.input = input.map(item => {\n // Check if publicFolder is already in path\n if (path.normalize(item).indexOf(publicFolder) > -1) {\n return item;\n }\n return path.normalize(publicFolder + item);\n });\n return output;\n }\n\n input = path.normalize(input);\n\n // Check if publicFolder is already in path\n if (input.indexOf(publicFolder) > -1) {\n output.input = input;\n return output;\n }\n\n output.input = path.normalize(publicFolder + input);\n\n return output;\n};\n\n/**\n * Check if some settings are here.\n *\n * @param {Object} settings\n */\nconst checkMandatories = (settings: {}) => {\n ['compressor', 'input', 'output'].forEach(item => mandatory(item, settings));\n};\n\n/**\n * Check if some settings are here for memory content.\n *\n * @param {Object} settings\n */\nconst checkMandatoriesMemoryContent = (settings: {}) => {\n ['compressor', 'content'].forEach(item => mandatory(item, settings));\n};\n\n/**\n * Check if the setting exist.\n *\n * @param {String} setting\n * @param {Object} settings\n */\ninterface Dictionary<T> {\n [Key: string]: T;\n}\nconst mandatory = (setting: string, settings: Dictionary<string>) => {\n if (!settings[setting]) {\n throw new Error(setting + ' is mandatory.');\n }\n};\n\n/**\n * Expose `setup()`.\n */\nexport { setup };\n","/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport fs from 'fs';\nimport mkdirp from 'mkdirp';\nimport { utils } from '@node-minify/utils';\nimport { Settings } from '@node-minify/types';\n\n/**\n * Run compressor.\n *\n * @param {Object} settings\n */\nconst compress = (settings: Settings) => {\n if (typeof settings.compressor !== 'function') {\n throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);\n }\n\n createDirectory(settings.output);\n\n if (Array.isArray(settings.output)) {\n return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);\n } else {\n return utils.compressSingleFile(settings);\n }\n};\n\n/**\n * Compress an array of files in sync.\n *\n * @param {Object} settings\n */\nconst compressArrayOfFilesSync = (settings: Settings) => {\n return (\n Array.isArray(settings.input) &&\n settings.input.forEach((input, index) => {\n const content = utils.getContentFromFiles(input);\n return utils.runSync({ settings, content, index });\n })\n );\n};\n\n/**\n * Compress an array of files in async.\n *\n * @param {Object} settings\n */\nconst compressArrayOfFilesAsync = (settings: Settings) => {\n let sequence = Promise.resolve();\n Array.isArray(settings.input) &&\n settings.input.forEach((input, index) => {\n const content = utils.getContentFromFiles(input);\n sequence = sequence.then(() => utils.runAsync({ settings, content, index }));\n });\n return sequence;\n};\n\n/**\n * Create folder of the target file.\n *\n * @param {String} file - Full path of the file\n */\nconst createDirectory = (file: string) => {\n if (Array.isArray(file)) {\n file = file[0];\n }\n const dir = file && file.substr(0, file.lastIndexOf('/'));\n if (!dir) {\n return;\n }\n if (!fs.statSync(dir).isDirectory()) {\n mkdirp.sync(dir);\n }\n};\n\n/**\n * Expose `compress()`.\n */\nexport { compress };\n","/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport { utils } from '@node-minify/utils';\nimport { Settings } from '@node-minify/types';\n\n/**\n * Run compressor.\n *\n * @param {Object} settings\n */\nconst compressInMemory = (settings: Settings) => {\n if (typeof settings.compressor !== 'function') {\n throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);\n }\n\n return utils.compressSingleFile(settings);\n};\n\n/**\n * Expose `compress()`.\n */\nexport { compressInMemory };\n","/*!\n * node-minify\n * Copyright(c) 2011-2022 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport { setup } from './setup';\nimport { compress } from './compress';\nimport { compressInMemory } from './compressInMemory';\nimport { Settings } from '@node-minify/types';\n\n// export interface Settings {\n// compressorLabel: string | Function;\n// compressor?: string | Function;\n// sync?: boolean;\n// callback?: Function;\n// content?: string;\n// input: string | string[];\n// output: string;\n// options?: string;\n// }\n\n/**\n * Run node-minify.\n *\n * @param {Object} settings - Settings from user input\n */\nconst minify = (settings: Settings) => {\n return new Promise((resolve, reject) => {\n const method = settings.content ? compressInMemory : compress;\n settings = setup(settings);\n if (!settings.sync) {\n method(settings)\n .then((minified: string) => {\n if (settings.callback) {\n settings.callback(null, minified);\n }\n resolve(minified);\n })\n .catch((err: Error) => {\n if (settings.callback) {\n settings.callback(err);\n }\n reject(err);\n });\n } else {\n const minified = method(settings);\n if (settings.callback) {\n settings.callback(null, minified);\n }\n resolve(minified);\n }\n });\n};\n\n/**\n * Expose `minify()`.\n */\nexport default minify;\n"],"mappings":";AASA,OAAO,UAAU;AACjB,OAAO,YAAY;AACnB,SAAS,aAAa;AAKtB,IAAM,kBAAkB;AAAA,EACtB,MAAM;AAAA,EACN,SAAS,CAAC;AAAA,EACV,QAAQ,MAAO;AAAA,EACf,UAAU;AACZ;AAQA,IAAM,QAAQ,CAAC,kBAAsB;AACnC,MAAI,WAAW,OAAO,OAAO,MAAM,MAAM,eAAe,GAAG,aAAa;AAGxE,MAAI,SAAS,SAAS;AACpB,kCAA8B,aAAa;AAC3C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GAAG;AACvB,WAAO,KAAK;AAAA,EACd;AACA,QAAM,MAAM,QAAQ,KAAK,OAAO,GAAG,KAAK,YAAY,GAAG,CAAC;AACxD,MAAI,CAAC,KAAK;AACR;AAAA,EACF;AACA,MAAI,CAAC,GAAG,SAAS,GAAG,EAAE,YAAY,GAAG;AACnC,WAAO,KAAK,GAAG;AAAA,EACjB;AACF;;;ACtEA,SAAS,SAAAC,cAAa;AAQtB,IAAM,mBAAmB,CAAC,aAAuB;AAC/C,MAAI,OAAO,SAAS,eAAe,YAAY;AAC7C,UAAM,IAAI,MAAM,6EAA6E;AAAA,EAC/F;AAEA,SAAOA,OAAM,mBAAmB,QAAQ;AAC1C;;;ACOA,IAAM,SAAS,CAAC,aAAuB;AACrC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,SAAS,SAAS,UAAU,mBAAmB;AACrD,eAAW,MAAM,QAAQ;AACzB,QAAI,CAAC,SAAS,MAAM;AAClB,aAAO,QAAQ,EACZ,KAAK,CAAC,aAAqB;AAC1B,YAAI,SAAS,UAAU;AACrB,mBAAS,SAAS,MAAM,QAAQ;AAAA,QAClC;AACA,gBAAQ,QAAQ;AAAA,MAClB,CAAC,EACA,MAAM,CAAC,QAAe;AACrB,YAAI,SAAS,UAAU;AACrB,mBAAS,SAAS,GAAG;AAAA,QACvB;AACA,eAAO,GAAG;AAAA,MACZ,CAAC;AAAA,IACL,OAAO;AACL,YAAM,WAAW,OAAO,QAAQ;AAChC,UAAI,SAAS,UAAU;AACrB,iBAAS,SAAS,MAAM,QAAQ;AAAA,MAClC;AACA,cAAQ,QAAQ;AAAA,IAClB;AAAA,EACF,CAAC;AACH;AAKA,IAAO,cAAQ;","names":["utils","utils"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-minify/core",
3
- "version": "7.1.0",
3
+ "version": "8.0.1-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": "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
- "lib"
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 --sourcemap",
44
+ "prepublishOnly": "npm run build"
45
+ },
34
46
  "dependencies": {
35
- "@node-minify/utils": "^7.1.0",
36
- "globby": "11.0.4",
47
+ "@node-minify/utils": "8.0.1-beta.0",
48
+ "globby": "11.1.0",
37
49
  "mkdirp": "1.0.4"
38
50
  },
39
- "gitHead": "94cef2d5d653c3bddc3e603b4e25c135b0b6f4b3"
51
+ "devDependencies": {
52
+ "@node-minify/types": "8.0.1-beta.0",
53
+ "@types/mkdirp": "^1.0.2"
54
+ },
55
+ "gitHead": "35a8a41ec86a11cbec05d8a7db9113475ece2cef"
40
56
  }
package/lib/compress.js DELETED
@@ -1,93 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.compress = void 0;
7
- var _fs = _interopRequireDefault(require("fs"));
8
- var _mkdirp = _interopRequireDefault(require("mkdirp"));
9
- var _utils = require("@node-minify/utils");
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
- /*!
12
- * node-minify
13
- * Copyright(c) 2011-2022 Rodolphe Stoclin
14
- * MIT Licensed
15
- */
16
-
17
- /**
18
- * Module dependencies.
19
- */
20
-
21
- /**
22
- * Run compressor.
23
- *
24
- * @param {Object} settings
25
- */
26
- const compress = settings => {
27
- if (typeof settings.compressor !== 'function') {
28
- throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
29
- }
30
- createDirectory(settings.output);
31
- if (Array.isArray(settings.output)) {
32
- return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
33
- } else {
34
- return _utils.utils.compressSingleFile(settings);
35
- }
36
- };
37
-
38
- /**
39
- * Compress an array of files in sync.
40
- *
41
- * @param {Object} settings
42
- */
43
- exports.compress = compress;
44
- const compressArrayOfFilesSync = settings => {
45
- return settings.input.forEach((input, index) => {
46
- const content = _utils.utils.getContentFromFiles(input);
47
- return _utils.utils.runSync({
48
- settings,
49
- content,
50
- index
51
- });
52
- });
53
- };
54
-
55
- /**
56
- * Compress an array of files in async.
57
- *
58
- * @param {Object} settings
59
- */
60
- const compressArrayOfFilesAsync = settings => {
61
- let sequence = Promise.resolve();
62
- settings.input.forEach((input, index) => {
63
- const content = _utils.utils.getContentFromFiles(input);
64
- sequence = sequence.then(() => _utils.utils.runAsync({
65
- settings,
66
- content,
67
- index
68
- }));
69
- });
70
- return sequence;
71
- };
72
-
73
- /**
74
- * Create folder of the target file.
75
- *
76
- * @param {String} file - Full path of the file
77
- */
78
- const createDirectory = file => {
79
- if (Array.isArray(file)) {
80
- file = file[0];
81
- }
82
- const dir = file && file.substr(0, file.lastIndexOf('/'));
83
- if (!dir) {
84
- return;
85
- }
86
- if (!_fs.default.statSync(dir).isDirectory()) {
87
- _mkdirp.default.sync(dir);
88
- }
89
- };
90
-
91
- /**
92
- * Expose `compress()`.
93
- */
@@ -1,33 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.compressInMemory = void 0;
7
- var _utils = require("@node-minify/utils");
8
- /*!
9
- * node-minify
10
- * Copyright(c) 2011-2022 Rodolphe Stoclin
11
- * MIT Licensed
12
- */
13
-
14
- /**
15
- * Module dependencies.
16
- */
17
-
18
- /**
19
- * Run compressor.
20
- *
21
- * @param {Object} settings
22
- */
23
- const compressInMemory = settings => {
24
- if (typeof settings.compressor !== 'function') {
25
- throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
26
- }
27
- return _utils.utils.compressSingleFile(settings);
28
- };
29
-
30
- /**
31
- * Expose `compress()`.
32
- */
33
- exports.compressInMemory = compressInMemory;
package/lib/core.js DELETED
@@ -1,50 +0,0 @@
1
- "use strict";
2
-
3
- var _setup = require("./setup");
4
- var _compress = require("./compress");
5
- var _compressInMemory = require("./compressInMemory");
6
- /*!
7
- * node-minify
8
- * Copyright(c) 2011-2022 Rodolphe Stoclin
9
- * MIT Licensed
10
- */
11
-
12
- /**
13
- * Module dependencies.
14
- */
15
-
16
- /**
17
- * Run node-minify.
18
- *
19
- * @param {Object} settings - Settings from user input
20
- */
21
- const minify = settings => {
22
- return new Promise((resolve, reject) => {
23
- const method = settings.content ? _compressInMemory.compressInMemory : _compress.compress;
24
- settings = (0, _setup.setup)(settings);
25
- if (!settings.sync) {
26
- method(settings).then(minified => {
27
- if (settings.callback) {
28
- settings.callback(null, minified);
29
- }
30
- resolve(minified);
31
- }).catch(err => {
32
- if (settings.callback) {
33
- settings.callback(err);
34
- }
35
- reject(err);
36
- });
37
- } else {
38
- const minified = method(settings);
39
- if (settings.callback) {
40
- settings.callback(null, minified);
41
- }
42
- resolve(minified);
43
- }
44
- });
45
- };
46
-
47
- /**
48
- * Expose `minify()`.
49
- */
50
- module.exports = minify;
package/lib/setup.js DELETED
@@ -1,223 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.setup = void 0;
7
- var _path = _interopRequireDefault(require("path"));
8
- var _globby = _interopRequireDefault(require("globby"));
9
- var _utils = require("@node-minify/utils");
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
- /*!
12
- * node-minify
13
- * Copyright(c) 2011-2022 Rodolphe Stoclin
14
- * MIT Licensed
15
- */
16
-
17
- /**
18
- * Module dependencies.
19
- */
20
-
21
- /**
22
- * Default settings.
23
- */
24
- const defaultSettings = {
25
- sync: false,
26
- options: {},
27
- buffer: 1000 * 1024,
28
- callback: false
29
- };
30
-
31
- /**
32
- * Run setup.
33
- *
34
- * @param {Object} inputSettings
35
- * @return {Object}
36
- */
37
- const setup = inputSettings => {
38
- let settings = Object.assign(_utils.utils.clone(defaultSettings), inputSettings);
39
-
40
- // In memory
41
- if (settings.content) {
42
- checkMandatoriesMemoryContent(inputSettings);
43
- return settings;
44
- }
45
- checkMandatories(inputSettings);
46
- settings = Object.assign(settings, wildcards(settings.input, settings.publicFolder));
47
- settings = Object.assign(settings, checkOutput(settings.input, settings.output, settings.publicFolder, settings.replaceInPlace));
48
- settings = Object.assign(settings, setPublicFolder(settings.input, settings.publicFolder));
49
- return settings;
50
- };
51
-
52
- /**
53
- * Check the output path, searching for $1
54
- * if exist, returns the path remplacing $1 by file name
55
- *
56
- * @param {String|Array} input - Path file
57
- * @param {String} output - Path to the output file
58
- * @param {String} publicFolder - Path to the public folder
59
- * @param {Boolean} replaceInPlace - True to replace file in same folder
60
- * @return {Object}
61
- */
62
- exports.setup = setup;
63
- const checkOutput = (input, output, publicFolder, replaceInPlace) => {
64
- let reg = new RegExp('\\$1');
65
- if (reg.test(output)) {
66
- if (Array.isArray(input)) {
67
- const outputMin = input.map(file => _utils.utils.setFileNameMin(file, output, replaceInPlace ? null : publicFolder, replaceInPlace));
68
- return {
69
- output: outputMin
70
- };
71
- } else {
72
- return {
73
- output: _utils.utils.setFileNameMin(input, output, replaceInPlace ? null : publicFolder, replaceInPlace)
74
- };
75
- }
76
- }
77
- };
78
-
79
- /**
80
- * Handle wildcards in a path, get the real path of each files.
81
- *
82
- * @param {String|Array} input - Path with wildcards
83
- * @param {String} publicFolder - Path to the public folder
84
- * @return {Object}
85
- */
86
- const wildcards = (input, publicFolder) => {
87
- // If it's a string
88
- if (!Array.isArray(input)) {
89
- return wildcardsString(input, publicFolder);
90
- }
91
- return wildcardsArray(input, publicFolder);
92
- };
93
-
94
- /**
95
- * Handle wildcards in a path (string only), get the real path of each files.
96
- *
97
- * @param {String} input - Path with wildcards
98
- * @param {String} publicFolder - Path to the public folder
99
- * @return {Object}
100
- */
101
- const wildcardsString = (input, publicFolder) => {
102
- const output = {};
103
- if (input.indexOf('*') > -1) {
104
- output.input = getFilesFromWildcards(input, publicFolder);
105
- }
106
- return output;
107
- };
108
-
109
- /**
110
- * Handle wildcards in a path (array only), get the real path of each files.
111
- *
112
- * @param {Array} input - Path with wildcards
113
- * @param {String} publicFolder - Path to the public folder
114
- * @return {Object}
115
- */
116
- const wildcardsArray = (input, publicFolder) => {
117
- let output = {};
118
- let isWildcardsPresent = false;
119
- output.input = input;
120
-
121
- // Transform all wildcards to path file
122
- const inputWithPublicFolder = input.map(item => {
123
- if (item.indexOf('*') > -1) {
124
- isWildcardsPresent = true;
125
- }
126
- return (publicFolder || '') + item;
127
- });
128
- if (isWildcardsPresent) {
129
- output.input = _globby.default.sync(inputWithPublicFolder);
130
- }
131
-
132
- // Remove all wildcards from array
133
- for (let i = 0; i < output.input.length; i++) {
134
- if (output.input[i].indexOf('*') > -1) {
135
- output.input.splice(i, 1);
136
- i--;
137
- }
138
- }
139
- return output;
140
- };
141
-
142
- /**
143
- * Get the real path of each files.
144
- *
145
- * @param {String} input - Path with wildcards
146
- * @param {String} publicFolder - Path to the public folder
147
- * @return {Object}
148
- */
149
- const getFilesFromWildcards = (input, publicFolder) => {
150
- let output = [];
151
- if (input.indexOf('*') > -1) {
152
- output = _globby.default.sync((publicFolder || '') + input);
153
- }
154
- return output;
155
- };
156
-
157
- /**
158
- * Prepend the public folder to each file.
159
- *
160
- * @param {String|Array} input - Path to file(s)
161
- * @param {String} publicFolder - Path to the public folder
162
- * @return {Object}
163
- */
164
- const setPublicFolder = (input, publicFolder) => {
165
- let output = {};
166
- if (typeof publicFolder !== 'string') {
167
- return output;
168
- }
169
- publicFolder = _path.default.normalize(publicFolder);
170
- if (Array.isArray(input)) {
171
- output.input = input.map(item => {
172
- // Check if publicFolder is already in path
173
- if (_path.default.normalize(item).indexOf(publicFolder) > -1) {
174
- return item;
175
- }
176
- return _path.default.normalize(publicFolder + item);
177
- });
178
- return output;
179
- }
180
- input = _path.default.normalize(input);
181
-
182
- // Check if publicFolder is already in path
183
- if (input.indexOf(publicFolder) > -1) {
184
- output.input = input;
185
- return output;
186
- }
187
- output.input = _path.default.normalize(publicFolder + input);
188
- return output;
189
- };
190
-
191
- /**
192
- * Check if some settings are here.
193
- *
194
- * @param {Object} settings
195
- */
196
- const checkMandatories = settings => {
197
- ['compressor', 'input', 'output'].forEach(item => mandatory(item, settings));
198
- };
199
-
200
- /**
201
- * Check if some settings are here for memory content.
202
- *
203
- * @param {Object} settings
204
- */
205
- const checkMandatoriesMemoryContent = settings => {
206
- ['compressor', 'content'].forEach(item => mandatory(item, settings));
207
- };
208
-
209
- /**
210
- * Check if the setting exist.
211
- *
212
- * @param {String} setting
213
- * @param {Object} settings
214
- */
215
- const mandatory = (setting, settings) => {
216
- if (!settings[setting]) {
217
- throw new Error(setting + ' is mandatory.');
218
- }
219
- };
220
-
221
- /**
222
- * Expose `setup()`.
223
- */