|
| 1 | +// Copied and modified from node-minify-all-js by Adones Pitogo |
| 2 | +// https://github.com/adonespitogo/node-minify-all-js/blob/master/index.js |
| 3 | + |
| 4 | +// @ts-nocheck |
| 5 | +import fs from "node:fs/promises"; |
| 6 | +import path from "node:path"; |
| 7 | +import promiseSeries from "promise.series"; |
| 8 | +import minify from "@node-minify/core"; |
| 9 | +import terser from "@node-minify/terser"; |
| 10 | + |
| 11 | +var failed_files = []; |
| 12 | +var total_files = 0; |
| 13 | +var options = {}; |
| 14 | + |
| 15 | +const minifyJS = async (file) => { |
| 16 | + total_files++; |
| 17 | + try { |
| 18 | + await minify({ |
| 19 | + compressor: terser, |
| 20 | + input: file, |
| 21 | + output: file, |
| 22 | + options: { |
| 23 | + module: options.module, |
| 24 | + mangle: options.mangle, |
| 25 | + compress: { reduce_vars: false }, |
| 26 | + }, |
| 27 | + }); |
| 28 | + } catch (e) { |
| 29 | + failed_files.push(file); |
| 30 | + } |
| 31 | + //process.stdout.write("."); |
| 32 | +}; |
| 33 | + |
| 34 | +const minifyJSON = async (file) => { |
| 35 | + try { |
| 36 | + if (options.compress_json || options.packagejson) { |
| 37 | + total_files++; |
| 38 | + var is_package_json = file.indexOf("package.json") > -1; |
| 39 | + var data = await fs.readFile(file, "utf8"); |
| 40 | + var json = JSON.parse(data); |
| 41 | + var new_json = {}; |
| 42 | + if (options.packagejson && is_package_json) { |
| 43 | + var { name, version, bin, main, binary, engines } = json; |
| 44 | + new_json = { name, version }; |
| 45 | + if (bin) new_json.bin = bin; |
| 46 | + if (binary) new_json.binary = binary; |
| 47 | + if (main) new_json.main = main; |
| 48 | + if (engines) new_json.engines = engines; |
| 49 | + } else { |
| 50 | + new_json = json; |
| 51 | + } |
| 52 | + await fs.writeFile(file, JSON.stringify(new_json)); |
| 53 | + } |
| 54 | + } catch (e) {} |
| 55 | + //process.stdout.write("."); |
| 56 | +}; |
| 57 | + |
| 58 | +const walk = async (currentDirPath) => { |
| 59 | + var js_files = []; |
| 60 | + var json_files = []; |
| 61 | + var dirs = []; |
| 62 | + var current_dirs = await fs.readdir(currentDirPath); |
| 63 | + for (const name of current_dirs) { |
| 64 | + var filePath = path.join(currentDirPath, name); |
| 65 | + var stat = await fs.stat(filePath); |
| 66 | + var is_bin = /\.bin$/; |
| 67 | + if (stat.isFile()) { |
| 68 | + if (filePath.substr(-5) === ".json") json_files.push(filePath); |
| 69 | + else if (filePath.substr(-3) === ".js" || options.all_js) |
| 70 | + js_files.push(filePath); |
| 71 | + } else if (stat.isDirectory() && !is_bin.test(filePath)) { |
| 72 | + dirs.push(filePath); |
| 73 | + } |
| 74 | + } |
| 75 | + var js_promise = Promise.all(js_files.map((f) => minifyJS(f))); |
| 76 | + var json_promise = Promise.all(json_files.map((f) => minifyJSON(f))); |
| 77 | + await Promise.all([js_promise, json_promise]); |
| 78 | + await promiseSeries(dirs.map((dir) => () => walk(dir))); |
| 79 | +}; |
| 80 | + |
| 81 | +export async function minifyAll(dir, opts) { |
| 82 | + Object.assign(options, opts || {}); |
| 83 | + //console.log("minify-all-js options:\n", JSON.stringify(options, null, 2)); |
| 84 | + await walk(dir); |
| 85 | + //process.stdout.write(".\n"); |
| 86 | + //console.log("Total found files: " + total_files); |
| 87 | + if (failed_files.length) { |
| 88 | + console.log(`\n\nFailed to minify files:`); |
| 89 | + failed_files.forEach((f) => console.log("\t" + f)); |
| 90 | + } |
| 91 | +} |
0 commit comments