1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| const fs = require("fs"); const path = require("path"); const { promisify } = require("util");
const readdir = promisify(fs.readdir); const stat = promisify(fs.stat); const readFile = promisify(fs.readFile); const writeFile = promisify(fs.writeFile); const mkdir = promisify(fs.mkdir); const rmdir = promisify(fs.rm);
const { minify: minifyJS } = require("uglify-js"); const CleanCSS = require("clean-css"); const htmlMinifier = require("html-minifier");
const distDir = "dist";
async function checkAndCreateDistDir(dir) { try { const stats = await stat(dir); if (stats.isDirectory()) { await rmdir(dir, { recursive: true }); } } catch (err) { if (err.code !== "ENOENT") { throw err; } }
await mkdir(dir); }
async function copyFiles(dir) { const filesToCopy = [ "assets", "lib", "src", "manifest.json", "popup.html", "web_cmd_x86.o", "web_cmd_arm.o", ];
const copyFile = async (source, target) => { const contents = await readFile(source); await writeFile(target, contents); };
const copyFolder = async (source, target) => { await mkdir(target, { recursive: true }); const files = await readdir(source);
for (const file of files) { const current = path.resolve(source, file); const dest = path.resolve(target, file); const stats = await stat(current);
if (stats.isDirectory()) { await copyFolder(current, dest); } else { await copyFile(current, dest); } } };
for (const file of filesToCopy) { const sourcePath = path.resolve(file); const destPath = path.resolve(dir, file);
const stats = await stat(sourcePath); if (stats.isDirectory()) { await copyFolder(sourcePath, destPath); } else { await copyFile(sourcePath, destPath); } } }
function minifyFiles(dir) { const files = fs.readdirSync(dir);
files.forEach((file) => { const filePath = path.join(dir, file); const stat = fs.statSync(filePath);
if (stat.isDirectory()) { minifyFiles(filePath); } else { const ext = path.extname(file);
if (ext === ".js") { let code = fs.readFileSync(filePath, "utf8"); const { error, code: minifiedCode } = minifyJS(code);
if (!error) { fs.writeFileSync(filePath, minifiedCode); console.log(`Minified ${filePath}`); } else { console.error(`Error minifying ${filePath}: ${error}`); } } else if (ext === ".css") { let code = fs.readFileSync(filePath, "utf8"); const minifiedCode = new CleanCSS().minify(code).styles;
fs.writeFileSync(filePath, minifiedCode); console.log(`Minified ${filePath}`); } else if (ext === ".html") { let code = fs.readFileSync(filePath, "utf8"); const minifiedCode = htmlMinifier.minify(code, { collapseWhitespace: true, removeComments: true, minifyJS: true, minifyCSS: true, });
fs.writeFileSync(filePath, minifiedCode); console.log(`Minified ${filePath}`); } } }); }
async function main() { try { await checkAndCreateDistDir(distDir); await copyFiles(distDir); minifyFiles(path.join(__dirname, "dist")); console.log("Task completed successfully."); } catch (err) { console.error("An error occurred:", err); } }
main();
|