148 lines
6.9 KiB
JavaScript
Executable File
148 lines
6.9 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
'use strict'
|
|
// Renderer/dist build step.
|
|
// default → PRODUCTION: copy renderer to dist/ui, obfuscate the
|
|
// RENDERER_JS set (app/drm + pb engine files), minify
|
|
// dist/electron + dist/api.
|
|
// --debug (or env → DEBUG: copy the renderer VERBATIM (readable, real line numbers)
|
|
// CS_DEBUG=1) and drop a marker so the app enables DevTools.
|
|
// --min (or env → DEBUG+MIN: additionally minify the first-party renderer JS
|
|
// CS_DEBUG_MIN=1) for a smaller/faster package (DevTools still on).
|
|
// dist/electron + dist/api are minified in BOTH modes. Console output kept in both.
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
const JavaScriptObfuscator = require('javascript-obfuscator')
|
|
const { minify } = require('terser')
|
|
|
|
const DEBUG = process.argv.includes('--debug') || process.env.CS_DEBUG === '1'
|
|
// MIN: in a debug build, additionally MINIFY (terser, not obfuscate) the
|
|
// first-party renderer JS for a smaller, faster-launching package. DevTools stay
|
|
// on. Enabled by `--min` or CS_DEBUG_MIN=1 — scripts/make-debug.js sets it, so
|
|
// `npm run win:debug` is minified, while `npm run build:debug` stays readable.
|
|
const MIN = DEBUG && (process.argv.includes('--min') || process.env.CS_DEBUG_MIN === '1')
|
|
|
|
const root = path.join(__dirname, '..')
|
|
const srcUi = path.join(root, 'src/ui')
|
|
const distUi = path.join(root, 'dist/ui')
|
|
const debugMarker = path.join(distUi, 'debug-build.flag')
|
|
|
|
// Every asset index.html <script>/<link> loads, plus the JS we (optionally) obfuscate.
|
|
// NOTE: keep this in sync with index.html — a missing file here means a broken feature
|
|
// only in packaged builds (e.g. omitting video*.js silently kills the Video.js engine).
|
|
const STATIC = [
|
|
'index.html',
|
|
'style.css',
|
|
'shaka-player.compiled.js',
|
|
'video.core.min.js', // Video.js core (no bundled VHS)
|
|
'videojs-http-streaming.min.js', // forked VHS with [SC] stall recovery (PlaybackWatcher)
|
|
'videojs-contrib-eme.min.js',
|
|
'visualizer.js', // audio visualizer (no secrets → copied verbatim)
|
|
'equalizer.js', // 10-band equalizer (no secrets → copied verbatim)
|
|
'firebase-app-compat.js', // Firebase SDK (cross-device remote control)
|
|
'firebase-auth-compat.js',
|
|
'firebase-database-compat.js',
|
|
'remote.js', // remote-control client (public Firebase config → copied verbatim)
|
|
]
|
|
// drm.js carries the real Widevine logic; the pb-* files are the playback
|
|
// engine (obfuscated in release, same treatment as the rest of the playback
|
|
// logic — design C §1.2 / addendum ruling 16).
|
|
const RENDERER_JS = ['app.js', 'drm.js', 'pb-engine.js', 'pb-lease.js', 'pb-diag.js']
|
|
const ASSET_DIRS = ['DefaultFont'] // fonts referenced by style.css
|
|
// First-party JS a MIN debug build minifies in-place. Excludes the already-
|
|
// minified vendor bundles (shaka compiled / video.core.min / videojs-eme /
|
|
// firebase-*-compat) — re-minifying those is slow and pointless.
|
|
const DEBUG_MIN_JS = ['app.js', 'drm.js', 'pb-engine.js', 'pb-lease.js', 'pb-diag.js', 'visualizer.js', 'equalizer.js', 'remote.js']
|
|
|
|
// ── 1. Copy static renderer files ────────────────────────────────────────────
|
|
fs.mkdirSync(distUi, { recursive: true })
|
|
for (const f of STATIC) {
|
|
fs.copyFileSync(path.join(srcUi, f), path.join(distUi, f))
|
|
}
|
|
for (const d of ASSET_DIRS) {
|
|
const from = path.join(srcUi, d)
|
|
if (fs.existsSync(from)) fs.cpSync(from, path.join(distUi, d), { recursive: true })
|
|
}
|
|
console.log(`[build-${DEBUG ? 'debug' : 'prod'}] copied src/ui → dist/ui`)
|
|
|
|
// ── 2. Renderer JS: obfuscate (prod) or copy verbatim (debug) ────────────────
|
|
const obfOptions = {
|
|
compact: true,
|
|
stringArray: true,
|
|
stringArrayEncoding: ['base64'],
|
|
stringArrayThreshold: 0.75,
|
|
rotateStringArray: true,
|
|
shuffleStringArray: true,
|
|
splitStrings: true,
|
|
splitStringsChunkLength: 10,
|
|
identifierNamesGenerator: 'hexadecimal',
|
|
selfDefending: false, // must be false in Electron renderer
|
|
disableConsoleOutput: false, // keep console.* working
|
|
deadCodeInjection: false,
|
|
target: 'browser',
|
|
}
|
|
for (const jsFile of RENDERER_JS) {
|
|
const srcPath = path.join(srcUi, jsFile)
|
|
const outPath = path.join(distUi, jsFile)
|
|
if (DEBUG) {
|
|
fs.copyFileSync(srcPath, outPath)
|
|
console.log(`[build-debug] copied dist/ui/${jsFile} (un-obfuscated)`)
|
|
} else {
|
|
const obf = JavaScriptObfuscator.obfuscate(fs.readFileSync(srcPath, 'utf8'), obfOptions)
|
|
fs.writeFileSync(outPath, obf.getObfuscatedCode(), 'utf8')
|
|
console.log(`[build-prod] obfuscated dist/ui/${jsFile}`)
|
|
}
|
|
}
|
|
|
|
// ── 3. Debug marker so the app enables DevTools (read by src/electron/main.ts) ─
|
|
// Written only in debug mode; removed in prod so a release never leaks DevTools.
|
|
if (DEBUG) {
|
|
fs.writeFileSync(debugMarker, '1')
|
|
console.log('[build-debug] wrote dist/ui/debug-build.flag (DevTools enabled)')
|
|
} else if (fs.existsSync(debugMarker)) {
|
|
fs.unlinkSync(debugMarker)
|
|
}
|
|
|
|
// ── 4. Minify compiled main-process + API modules (prod only) ────────────────
|
|
async function minifyDir(dir) {
|
|
if (!fs.existsSync(dir)) return
|
|
for (const file of fs.readdirSync(dir).filter(f => f.endsWith('.js'))) {
|
|
const fp = path.join(dir, file)
|
|
const result = await minify(fs.readFileSync(fp, 'utf8'), {
|
|
compress: { passes: 2 },
|
|
mangle: true,
|
|
})
|
|
if (result.code) {
|
|
fs.writeFileSync(fp, result.code, 'utf8')
|
|
console.log(`[build-${DEBUG ? 'debug' : 'prod'}] minified ${path.relative(root, fp)}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Minify a single already-copied dist/ui JS file in place (used by MIN debug).
|
|
async function minifyUiFile(name) {
|
|
const fp = path.join(distUi, name)
|
|
if (!fs.existsSync(fp)) return
|
|
const result = await minify(fs.readFileSync(fp, 'utf8'), {
|
|
compress: { passes: 2 },
|
|
// toplevel:false (default) keeps global function names, so cross-file
|
|
// window.* calls (app.js ↔ remote.js ↔ visualizer/equalizer) still resolve.
|
|
mangle: true,
|
|
})
|
|
if (result.code) {
|
|
fs.writeFileSync(fp, result.code, 'utf8')
|
|
console.log(`[build-debug] minified dist/ui/${name}`)
|
|
}
|
|
}
|
|
|
|
;(async () => {
|
|
// Minify the compiled main-process + API modules in BOTH modes.
|
|
await minifyDir(path.join(root, 'dist/electron'))
|
|
await minifyDir(path.join(root, 'dist/api'))
|
|
// MIN debug: also minify the first-party renderer JS for a faster-launching
|
|
// package (DevTools still on; readable line numbers traded for speed).
|
|
if (MIN) {
|
|
for (const f of DEBUG_MIN_JS) await minifyUiFile(f)
|
|
}
|
|
console.log(`[build-${DEBUG ? 'debug' : 'prod'}] done${MIN ? ' (renderer minified)' : ''}`)
|
|
})().catch(err => { console.error('[build] error:', err); process.exit(1) })
|