Files
archived-orca/mobile/scripts/build-mermaid-page-engine.mjs

59 lines
2.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { writeFile } from 'node:fs/promises'
import path from 'node:path'
import { createRequire } from 'node:module'
import * as esbuild from 'esbuild'
const require = createRequire(import.meta.url)
const mobileRoot = path.resolve(import.meta.dirname, '..')
const outputPath = path.join(
mobileRoot,
'src',
'components',
'mermaid',
'mermaid-page-engine.generated.ts'
)
// Bundle mermaid as one deferred module: internal lazy imports otherwise emit 103 assets against the shell’s 256 cap, despite all bytes already being downloaded.
// Unlike WebView engine’s string artifact, page evaluates this source module typed by mermaid-page-engine.ts.
async function main() {
const { version } = require(require.resolve('mermaid/package.json'))
const { outputFiles } = await esbuild.build({
absWorkingDir: mobileRoot,
stdin: {
contents: "export { default } from 'mermaid'\n",
resolveDir: mobileRoot,
loader: 'ts',
sourcefile: 'mermaid-page-engine-entry.ts'
},
bundle: true,
format: 'esm',
splitting: false,
// Metro transforms this module again; preserve enum assignments for its export analysis.
minifyWhitespace: true,
minifyIdentifiers: true,
minifySyntax: false,
// The floor the shell's WebViews hold, the same pair the terminal engine is built for.
target: ['chrome74', 'safari15'],
write: false,
charset: 'utf8',
legalComments: 'none',
logLevel: 'silent',
nodePaths: [path.join(mobileRoot, 'node_modules')],
// The page imports KaTeX for Markdown math too; left external, both share one katex chunk.
external: ['katex'],
define: { 'process.env.NODE_ENV': '"production"' }
})
const bundle = Buffer.from(outputFiles[0].contents).toString('utf8')
const source = [
'// Generated by scripts/build-mermaid-page-engine.mjs.',
`// Package: mermaid@${version}, bundled as one ESM module for the page.`,
'// Do not edit by hand; regenerate via pnpm postinstall.',
'// @ts-nocheck -- minified vendor output; mermaid-page-engine.ts is where this is typed.',
bundle,
''
].join('\n')
await writeFile(outputPath, source)
}
await main()