tts-voice-creator-clone-and.../scripts/minify.mjs

43 lines
2.0 KiB
JavaScript

// Build an opt-in production bundle WITHOUT changing runtime semantics.
//
// The feature modules share a single global scope and are executed in a fixed order
// (see loader.js). We therefore CONCATENATE them in that exact order into one file and
// minify whitespace/syntax only — identifiers are kept, because inline HTML handlers
// (onclick="navTo(...)") and cross-file references rely on global names.
//
// Run: npm run minify → static/dist/main.min.js
// Enable at runtime with window.APP_USE_BUNDLE=true (or ?bundle=1). Default stays
// per-file so editing static/js/* is live without a rebuild.
import { transform } from 'esbuild';
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
const jsDir = join(root, 'static', 'js');
const outDir = join(root, 'static', 'dist');
mkdirSync(outDir, { recursive: true });
// Bundle only the foundation. Section-specific modules are loaded on demand by
// loader.js; keeping the same manifest as the runtime prevents drift.
const MANIFEST = JSON.parse(readFileSync(join(root, 'static', 'module-manifest.json'), 'utf8'));
const MODULES = MANIFEST.core;
if (!Array.isArray(MODULES) || !MODULES.length) throw new Error('module manifest has no core modules');
const MAIN = MODULES.map(n => join(jsDir, n + '.js'));
const source = MAIN.map(f => `\n/* ==== ${f.split('/').pop()} ==== */\n` + readFileSync(f, 'utf8')).join('\n');
const { code } = await transform(source, {
loader: 'js',
minify: false,
minifyWhitespace: true,
minifySyntax: true,
minifyIdentifiers: false, // keep global names referenced by HTML & other files
legalComments: 'none',
target: ['es2019', 'safari14'],
});
const out = join(outDir, 'main.min.js');
writeFileSync(out, code);
console.log(`Bundled ${MAIN.length} modules → static/dist/main.min.js (${(code.length / 1024).toFixed(0)} KB)`);