dev-web.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Watch-build for client-plugin HMR: runs every dshClient plugin package
  3. * through the tsdown JS API in watch mode. Reload signaling is not this
  4. * script's business — the host webserver stat-polls the bundles it serves and
  5. * broadcasts `rebuilt` frames itself (`dsh web --dev`), so any process that
  6. * rewrites `lib/client.js` files triggers reloads; this script is merely the
  7. * convenient way to keep them all rebuilt on source change.
  8. *
  9. * Usage: `pnpm exec tsx scripts/dev-web.ts [--poll[=ms]]`. Requires the
  10. * packages' node halves built once (`tsc -b tsconfig.build.json`): the lib
  11. * config's entries are tsc output. `--poll` switches the source-file watcher
  12. * to polling (default 500ms): network mounts (weka) deliver no inotify
  13. * events, so native watching sees the initial build only and never a source
  14. * change.
  15. *
  16. * Each package keeps its own tsdown.config.ts untouched: this script layers
  17. * `watch` through API-level inline config (tsdown workspace mode fills inline
  18. * keys under each package's file config, and no package config defines it).
  19. */
  20. import { globSync, readFileSync } from 'node:fs'
  21. import { dirname, join, sep } from 'node:path'
  22. import { fileURLToPath } from 'node:url'
  23. import { build } from 'tsdown'
  24. const repoRoot = fileURLToPath(new URL('..', import.meta.url))
  25. /**
  26. * Discover the watch workspace by declaration: every packages/<group>/<name>
  27. * whose package.json carries `dshClient` with platform "web" is a client
  28. * plugin bundle emitter. Scanned once at startup — a package added while
  29. * watching means restarting this script.
  30. * @returns workspace-relative plugin package directories.
  31. */
  32. function discoverPluginDirs(): string[] {
  33. const dirs: string[] = []
  34. for (const manifestPath of globSync('packages/*/*/package.json', { cwd: repoRoot }).sort()) {
  35. const manifest = JSON.parse(readFileSync(join(repoRoot, manifestPath), 'utf8')) as { dshClient?: { platform?: unknown } }
  36. if (manifest.dshClient?.platform === 'web') dirs.push(dirname(manifestPath).split(sep).join('/'))
  37. }
  38. return dirs
  39. }
  40. const PLUGIN_DIRS = discoverPluginDirs()
  41. if (PLUGIN_DIRS.length === 0) {
  42. console.error('dev-web: no dshClient (platform "web") packages found under packages/')
  43. process.exit(1)
  44. }
  45. const args = process.argv.slice(2)
  46. const pollArg = args.find(a => a === '--poll' || a.startsWith('--poll='))
  47. if (args.some(a => a !== pollArg)) {
  48. console.error('dev-web: usage: tsx scripts/dev-web.ts [--poll[=ms]]')
  49. process.exit(1)
  50. }
  51. const pollInterval = pollArg === undefined ? undefined : Number(pollArg.split('=')[1] ?? '500')
  52. if (pollInterval !== undefined && (!Number.isInteger(pollInterval) || pollInterval <= 0)) {
  53. console.error(`dev-web: invalid --poll interval "${pollArg ?? ''}"`)
  54. process.exit(1)
  55. }
  56. await build({
  57. cwd: repoRoot,
  58. workspace: PLUGIN_DIRS,
  59. watch: true,
  60. // Rolldown watch options ride through inputOptions (tsdown has no watcher
  61. // tuning of its own); polling is opt-in for network mounts without inotify.
  62. ...pollInterval !== undefined
  63. ? { inputOptions: { watch: { watcher: { usePolling: true, pollInterval } } } }
  64. : {},
  65. })
  66. console.log(
  67. `dev-web: watching ${String(PLUGIN_DIRS.length)} dshClient plugin packages`
  68. + `${pollInterval !== undefined ? ` (polling ${String(pollInterval)}ms)` : ''}:\n ${PLUGIN_DIRS.join('\n ')}`,
  69. )