Files
orca/config/scripts/build-plugin-declarative-runtime.mjs
T

62 lines
1.7 KiB
JavaScript

// Bundle the guest `orca` module (declarative plugin UI) into one dependency-free ES module.
// `--check` fails when the checked-in file is stale instead of rewriting it.
import { readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { build } from 'esbuild'
const ROOT = path.resolve(import.meta.dirname, '..', '..')
const ENTRY = path.join(
ROOT,
'src',
'shared',
'plugins',
'declarative',
'runtime',
'orca-module.ts'
)
export const DECLARATIVE_RUNTIME_OUTPUT = path.join(
ROOT,
'resources',
'plugins',
'declarative',
'orca-runtime.mjs'
)
const BANNER =
'// Generated by config/scripts/build-plugin-declarative-runtime.mjs from\n' +
'// src/shared/plugins/declarative/runtime/. Do not edit.'
export async function bundleDeclarativeRuntime() {
const result = await build({
entryPoints: [ENTRY],
bundle: true,
format: 'esm',
platform: 'browser',
target: 'es2022',
charset: 'utf8',
legalComments: 'none',
banner: { js: BANNER },
write: false,
logLevel: 'silent'
})
return result.outputFiles[0].text
}
async function main() {
const source = await bundleDeclarativeRuntime()
if (process.argv.includes('--check')) {
if (readFileSync(DECLARATIVE_RUNTIME_OUTPUT, 'utf8') !== source) {
console.error(
`${path.relative(ROOT, DECLARATIVE_RUNTIME_OUTPUT)} is stale; run pnpm generate:plugin-declarative-runtime`
)
process.exit(1)
}
return
}
writeFileSync(DECLARATIVE_RUNTIME_OUTPUT, source)
console.log(`wrote ${path.relative(ROOT, DECLARATIVE_RUNTIME_OUTPUT)}`)
}
if (process.argv[1] && path.resolve(process.argv[1]) === import.meta.filename) {
await main()
}