Files
ArtPlayer/scripts/build-site-assets.mjs
T

36 lines
1.9 KiB
JavaScript

import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import { build } from 'esbuild'
import { globSync } from 'glob'
assert(process.argv.slice(2).every(arg => arg === '--check'), 'Use yarn build:site-assets [--check]')
const root = 'packages/artplayer-vitepress'
const englishPaths = globSync('**/*.md', { cwd: `${root}/docs/en`, posix: true }).sort().flatMap(file => file === 'index.md' ? ['', 'index.html'] : [file.replace(/\.md$/, '.html'), file.replace(/\.md$/, '')])
/** @type {Map<string, string>} */
const outputs = new Map()
/** @type {[string, string][]} */
const entries = [['bootstrap.ts', 'docs/assets/js/bootstrap.js'], ['editor.ts', 'docs/assets/js/common.js'], ['editor-loader.ts', 'docs/assets/js/loader.js'], ['mobile.ts', 'docs/assets/js/mobile.js'], ['navigation.ts', `${root}/docs/public/main.js`]]
for (const [entry, output] of entries) {
const result = await build({
...(entry === 'navigation.ts'
? { stdin: { contents: `import { installNavigation } from './navigation.ts'; installNavigation(window, ${JSON.stringify(englishPaths)});`, resolveDir: path.resolve(root, 'browser'), loader: 'ts' } }
: { entryPoints: [path.join(root, 'browser', entry)] }),
bundle: true,
write: false,
format: 'iife',
target: 'es2020',
legalComments: 'none',
logLevel: 'silent',
})
assert(result.outputFiles[0], 'Missing site bundle')
outputs.set(output, `// Generated by yarn build:site-assets. Do not edit.\n${result.outputFiles[0].text}`)
}
for (const [file, content] of outputs) {
if (process.argv.includes('--check'))
assert.equal(fs.readFileSync(file, 'utf8').replaceAll('\r\n', '\n'), content, `Site asset drift: ${file}`)
else fs.writeFileSync(file, content)
}
console.log(`Site browser assets ${process.argv.includes('--check') ? 'checked' : 'generated'}: ${outputs.size}; English routes: ${englishPaths.length}`)