|
| 1 | +const browserify = require("browserify"); |
| 2 | +const exorcist = require("exorcist"); |
| 3 | +const fs = require("node:fs"); |
| 4 | +const path = require("node:path"); |
| 5 | +const { minify } = require("terser"); |
| 6 | + |
| 7 | +const generateIndex = require("./generateIndex.js"); |
| 8 | +const CL = require("./libs/ColorLogger.js"); |
| 9 | + |
| 10 | +const script_name = "JavaLibraryScript"; |
| 11 | + |
| 12 | +const baseDir = path.join(__dirname, ".."); |
| 13 | + |
| 14 | +const distDir = path.join(baseDir, "dist"); |
| 15 | +const entryDir = path.join(baseDir, "src"); |
| 16 | + |
| 17 | +const entry = path.join(entryDir, "main.js"); |
| 18 | +const bundlePath = path.join(distDir, `${script_name}.js`); |
| 19 | +const bundleMapPath = path.join(distDir, `${script_name}.js.map`); |
| 20 | +const minPath = path.join(distDir, `${script_name}.min.js`); |
| 21 | +const minMapPath = path.join(distDir, `${script_name}.min.js.map`); |
| 22 | + |
| 23 | +// 相対座標を取得 |
| 24 | +function getRelativePath(name) { |
| 25 | + return path.relative(baseDir, name); |
| 26 | +} |
| 27 | + |
| 28 | +// distディレクトリのクリーン&作成 |
| 29 | +function prepareDist() { |
| 30 | + if (fs.existsSync(distDir)) { |
| 31 | + fs.rmSync(distDir, { recursive: true, force: true }); |
| 32 | + } |
| 33 | + fs.mkdirSync(distDir, { recursive: true }); |
| 34 | +} |
| 35 | + |
| 36 | +// ファイルサイズを見やすく表示 |
| 37 | +function formatSize(bytes) { |
| 38 | + if (bytes < 1024) return bytes + " B"; |
| 39 | + if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + " KB"; |
| 40 | + return (bytes / (1024 * 1024)).toFixed(2) + " MB"; |
| 41 | +} |
| 42 | + |
| 43 | +// ファイルサイズ取得 |
| 44 | +function showFileSize(filePath) { |
| 45 | + const stat = fs.statSync(filePath); |
| 46 | + console.log(`📦 ${CL.brightWhite(path.basename(filePath))}: ${CL.brightGreen(formatSize(stat.size))}`); |
| 47 | +} |
| 48 | + |
| 49 | +// Browserifyでバンドル |
| 50 | +function bundle() { |
| 51 | + return new Promise((resolve, reject) => { |
| 52 | + const b = browserify(entry, { |
| 53 | + cache: {}, |
| 54 | + packageCache: {}, |
| 55 | + debug: true, // source map生成のため必須 |
| 56 | + }); |
| 57 | + const writeStream = fs.createWriteStream(bundlePath); |
| 58 | + |
| 59 | + b.bundle() |
| 60 | + .pipe(exorcist(bundleMapPath)) // ここでmapを外部化 |
| 61 | + .pipe(writeStream); |
| 62 | + |
| 63 | + writeStream.on("finish", () => { |
| 64 | + const code = fs.readFileSync(bundlePath, "utf-8"); |
| 65 | + resolve(code); |
| 66 | + }); |
| 67 | + writeStream.on("error", reject); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +// Terserでminify(drop_consoleなど最適化オプション付き) |
| 72 | +async function minifyCode(code) { |
| 73 | + const result = await minify(code, { |
| 74 | + compress: { |
| 75 | + drop_console: true, |
| 76 | + passes: 2, |
| 77 | + pure_funcs: ["console.info", "console.debug", "console.warn"], |
| 78 | + }, |
| 79 | + mangle: { |
| 80 | + toplevel: true, |
| 81 | + }, |
| 82 | + format: { |
| 83 | + comments: false, |
| 84 | + }, |
| 85 | + sourceMap: { |
| 86 | + filename: path.basename(minPath), |
| 87 | + url: path.basename(minMapPath), |
| 88 | + }, |
| 89 | + }); |
| 90 | + fs.writeFileSync(minPath, result.code); |
| 91 | + fs.writeFileSync(minMapPath, result.map); |
| 92 | +} |
| 93 | + |
| 94 | +(async () => { |
| 95 | + try { |
| 96 | + // |
| 97 | + console.log(`🔁 ${CL.brightWhite("index.js自動生成開始...")}`); |
| 98 | + generateIndex(entryDir); |
| 99 | + console.log(`🌱 ${CL.brightWhite("自動生成完了")}`); |
| 100 | + // |
| 101 | + console.log(`🗑️ ${CL.brightWhite("distフォルダリセット")}`); |
| 102 | + prepareDist(); |
| 103 | + // |
| 104 | + console.log(`🗂️ ${CL.brightWhite("バンドル中...")}`); |
| 105 | + const code = await bundle(); |
| 106 | + console.log(`✨ ${CL.brightWhite("バンドル完了")}: ${getRelativePath(bundlePath)}`); |
| 107 | + console.log(`🗺️ ${CL.brightWhite("ソースマップ生成")}: ${getRelativePath(bundleMapPath)}`); |
| 108 | + |
| 109 | + console.log(`🔧 ${CL.brightWhite("Minify中...")}`); |
| 110 | + await minifyCode(code); |
| 111 | + console.log(`✅ ${CL.brightWhite("Minify完了:")} ${getRelativePath(minPath)}`); |
| 112 | + console.log(`🗺️ ${CL.brightWhite("ソースマップ生成[min]")}: ${getRelativePath(minMapPath)}`); |
| 113 | + showFileSize(bundlePath); |
| 114 | + showFileSize(minPath); |
| 115 | + } catch (e) { |
| 116 | + console.error("❌ ビルド失敗:", e); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | +})(); |
0 commit comments