Files
dsh-context/scripts/build.mjs
T
bowenliang123 6cb6a96980 refactor: layered module split with first-screen optimizations
- shared/types.ts: single wire contract (type-only) used by both halves
- host/: services (contracts), pricing (token-meter), fold (incremental fold
  + retention), snapshot (build + event turn/step attribution), index (RPC)
- client/: i18n, styles, format, categories, viewkit (shared deps),
  cache (per-session stale-while-revalidate), components/* (one module per
  component), view (composition root), index (plugin entry)
- first-screen: initial render seeds from the session cache, polling pauses
  while the tab is hidden (resume + immediate fetch on visible), and a
  transient fetch error never blanks already-visible data
- build.mjs: bundle:true for both halves (react external in the client
  bundle); the client half is now covered by tsc --noEmit for the first time
2026-08-15 02:19:28 +08:00

86 lines
3.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* dsh-context build — zero runtime dependencies.
*
* Produces the npm-package artifacts under lib/:
* lib/index.js — host half: src/host/ bundled to plain ESM.
* lib/client.js — client half: src/client/ bundled to CJS, then
* wrapped in the web boot handoff
* `window.__ModuleLoader__.load({ id, factory })`, the
* closure-factory format every `dsh.client` package's
* `./client` export must use (React and other platform
* modules arrive through the injected `require`, so
* `react` is marked external and stays a bare require).
*
* Both halves bundle their local modules (shared types are type-only and
* vanish at build time). Type checking is a separate step
* (`npm run typecheck`, tsc --noEmit); this script only transpiles (esbuild)
* and runs smoke checks: both outputs must parse, and the host half must
* import with the expected plugin shape.
*/
import { build } from 'esbuild'
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..')
const pkg = JSON.parse(await readFile(join(ROOT, 'package.json'), 'utf8'))
await mkdir(join(ROOT, 'lib'), { recursive: true })
// ---- host half: bundled TS -> ESM (zero external runtime deps) ----------------
await build({
entryPoints: [join(ROOT, 'src', 'host', 'index.ts')],
outfile: join(ROOT, 'lib', 'index.js'),
format: 'esm',
platform: 'node',
target: 'es2022',
bundle: true,
sourcemap: false,
})
// ---- client half: bundled TS -> CJS, then wrap in the loader handoff -------
// `react` stays external: the browser module table answers the injected
// `require('react')`, everything else is inlined by the bundler.
await build({
entryPoints: [join(ROOT, 'src', 'client', 'index.ts')],
outfile: join(ROOT, 'lib', '_client.js'),
format: 'cjs',
platform: 'browser',
target: 'es2020',
bundle: true,
external: ['react'],
sourcemap: false,
})
const clientSource = await readFile(join(ROOT, 'lib', '_client.js'), 'utf8')
await rm(join(ROOT, 'lib', '_client.js'))
const bundle = [
'/* dsh-context client bundle — generated by scripts/build.mjs from src/client/ */',
'window.__ModuleLoader__.load({',
` id: ${JSON.stringify(pkg.name)},`,
' factory: (require) => {',
' var module = { exports: {} };',
' var exports = module.exports;',
clientSource.replace(/\s+$/, '\n'),
' return module.exports;',
' },',
'});',
'',
].join('\n')
await writeFile(join(ROOT, 'lib', 'client.js'), bundle)
// ---- smoke checks ----
new Function(bundle) // syntax parse only; never executes (window is undefined here)
const host = await import(join(ROOT, 'lib', 'index.js'))
if (host.name !== 'dsh-context' || !Array.isArray(host.inject) || typeof host.apply !== 'function') {
throw new Error('host half does not expose the expected plugin shape (name/inject/apply)')
}
if (!host.inject.includes('connection')) {
throw new Error('host half must inject the connection service')
}
console.log(`built ${pkg.name}:`)
console.log(' lib/index.js (host half, ESM, from src/host/)')
console.log(' lib/client.js (client half, __ModuleLoader__ closure bundle, from src/client/)')