Files
dsh-context/scripts/build.mjs
T
bowenliang123 38dbd2ad92 refactor(ui): move the context tab onto the harness's React stack
All view components are now tsx JSX function components instead of
hand-rolled h() factories; the bundle externalizes the framework seed words
(react, @deepseek-ai/dsh-client-ui-primitives) so the loader module table
resolves the single shared framework instance at runtime.

Reuses shared ui-primitives where a clean fit exists (injection and
model-switch event glyphs -> IconPlusOutline16/IconBranchOutline16); the
bespoke data-viz chrome (charts, shared-hover tooltip) intentionally stays
custom but continues to draw the shared --dsw-alias-* tokens.

esbuild uses the classic JSX transform, so the emitted createElement trees
are isomorphic to the previous h() calls and the element-parsing tests, the
chart-render assertions, and the real-React repro all stay green unchanged.
Adds @deepseek-ai/dsh-client-ui-primitives as a type devDep and documents the
framework-reuse findings in docs/redesign.md §8.
2026-08-16 03:51:00 +08:00

93 lines
3.8 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* dsh-context build — one runtime dependency (zod, for the projection's wire
* schema; every `@deepseek-ai/*` contract is type-only and erased here).
*
* Produces the npm-package artifacts under lib/:
* lib/index.js — host half: src/host/ bundled to plain ESM (zod external).
* 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 (zod stays external — the user's harness
// provides the wire schema validator, like every other @deepseek-ai/* runtime
// service; the package ships zod as a regular dependency) ----------------
await build({
entryPoints: [join(ROOT, 'src', 'host', 'index.ts')],
outfile: join(ROOT, 'lib', 'index.js'),
format: 'esm',
platform: 'node',
target: 'es2022',
bundle: true,
external: ['zod'],
sourcemap: false,
})
// ---- client half: bundled TS -> CJS, then wrap in the loader handoff -------
// `react` stays external (the browser module table answers the injected
// require), as do the framework seed words this bundle uses — esbuild turns
// JSX into plain React.createElement calls (classic transform), and the loader
// resolves the shared framework singletons at runtime. 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', '@deepseek-ai/dsh-client-ui-primitives'],
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('sessionProjections')) {
throw new Error('host half must inject the sessionProjections 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/)')