vite.config.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { fileURLToPath } from 'node:url'
  2. import { defineConfig } from 'vite'
  3. import type { Plugin } from 'vite'
  4. import react from '@vitejs/plugin-react'
  5. const src = (rel: string): string => fileURLToPath(new URL(rel, import.meta.url))
  6. const STANDALONE_ERROR = 'apps/web is not a standalone application: bare Vite cannot inject window.__DSH_BOOT__. '
  7. + 'From a repository checkout, run `pnpm dsh web`; an installed package uses `dsh web`. '
  8. + 'For client-plugin HMR, run `pnpm dsh web` together with `pnpm run dev:web`.'
  9. /** Fail before a Vite dev or preview server can expose the boot-manifest-free shell. */
  10. function rejectStandaloneServe(): Plugin {
  11. return {
  12. name: 'dsh-reject-standalone-web-serve',
  13. config(_config, env) {
  14. if (env.command === 'serve') throw new Error(STANDALONE_ERROR)
  15. },
  16. }
  17. }
  18. /**
  19. * Vendor-chunk membership, by exact npm package name — the heavy render
  20. * families (math, highlight, markdown) that change only on dependency bumps.
  21. * Only packages workspace code imports DIRECTLY need listing: their private
  22. * transitive dependencies (oniguruma machinery, character tables, …) are
  23. * imported solely by these and rollup's chunk coloring pulls them into
  24. * vendor automatically. A dependency shared with index-side code falls back
  25. * to index — a few kB of dilution, never a correctness problem. Anything not
  26. * listed (react family, the vendored cordis workspace, tiny helpers like
  27. * anser/clsx, all workspace code) stays in the default `index` chunk, so
  28. * editing shell code re-hashes only index and returning clients keep the
  29. * cached vendor chunk.
  30. *
  31. * Every member must be React-free. A package that
  32. * imports react/jsx-runtime must never be listed — rollup folds a module
  33. * shared between the entry and a manual chunk into the manual chunk, so one
  34. * react-importing member would drag the single shared react copy into
  35. * vendor. The React side of markdown/math rendering is workspace code and
  36. * rides index.
  37. */
  38. const VENDOR_PACKAGES: ReadonlySet<string> = new Set([
  39. // math
  40. 'katex',
  41. // syntax highlight (@shikijs/langs is handled separately below —
  42. // lazy grammars must not land here)
  43. 'shiki',
  44. // markdown parse pipeline (micromark/mdast; the incremental React renderer
  45. // over it is workspace code)
  46. 'mdast-util-from-markdown',
  47. 'mdast-util-gfm',
  48. 'mdast-util-math',
  49. 'micromark-core-commonmark',
  50. 'micromark-extension-gfm',
  51. 'micromark-extension-math',
  52. 'micromark-factory-space',
  53. 'micromark-util-character',
  54. 'micromark-util-classify-character',
  55. 'micromark-util-sanitize-uri',
  56. 'micromark-util-symbol',
  57. 'micromark-util-types',
  58. ])
  59. /**
  60. * Boot grammars statically imported by ui-primitives' highlight.ts
  61. * (`@shikijs/langs/typescript` → `dist/typescript.mjs`, etc.). They live in
  62. * the same package as the lazy read-card grammars, but unlike those they are
  63. * part of the initial load and belong in the vendor chunk; the lazy ones must
  64. * stay unassigned so each keeps its own on-demand chunk.
  65. */
  66. const BOOT_GRAMMAR_FILES: readonly string[] = [
  67. 'dist/typescript.mjs',
  68. 'dist/shellscript.mjs',
  69. 'dist/json.mjs',
  70. ]
  71. /** Font asset extensions routed to assets/fonts/ (KaTeX's woff2/woff/ttf faces). */
  72. const FONT_EXTENSIONS: readonly string[] = ['.woff2', '.woff', '.ttf']
  73. /**
  74. * npm package name of a resolved module id: the segment after the last
  75. * `node_modules/`. pnpm nests the real package under an inner node_modules.
  76. */
  77. function npmPackageOf(id: string): string | undefined {
  78. const parts = id.split('/node_modules/')
  79. if (parts.length === 1) return undefined
  80. const [first, second] = parts[parts.length - 1].split('/')
  81. if (first.startsWith('.')) return undefined // .pnpm store segment, not a package
  82. if (first.startsWith('@')) return second === undefined ? undefined : `${first}/${second}`
  83. return first
  84. }
  85. export default defineConfig({
  86. plugins: [rejectStandaloneServe(), react()],
  87. build: {
  88. sourcemap: true,
  89. rollupOptions: {
  90. output: {
  91. // Output layout: the two main chunks stay at assets/ root; lazy
  92. // @shikijs/langs grammar chunks group under assets/langs/; fonts
  93. // (all KaTeX faces referenced by vendor.css) group under
  94. // assets/fonts/. Sourcemaps need no arrangement: rollup writes each
  95. // .map next to its js and references it by bare relative filename.
  96. chunkFileNames(chunk): string {
  97. // Grammar chunks are recognized by their member modules, not the
  98. // facade: shared embedded-grammar chunks (e.g. html+javascript,
  99. // split out because php/ruby/mdx embed them) have no facade at all.
  100. // index and vendor are excluded by name — vendor legitimately
  101. // carries the three boot grammars.
  102. if (chunk.name === 'index' || chunk.name === 'vendor') return 'assets/[name]-[hash].js'
  103. const isLangChunk = chunk.moduleIds.some(id => id.includes('/node_modules/@shikijs/langs/'))
  104. return isLangChunk ? 'assets/langs/[name]-[hash].js' : 'assets/[name]-[hash].js'
  105. },
  106. assetFileNames(asset): string {
  107. const fileName = asset.names[0] ?? ''
  108. const isFont = FONT_EXTENSIONS.some(ext => fileName.endsWith(ext))
  109. return isFont ? 'assets/fonts/[name]-[hash][extname]' : 'assets/[name]-[hash][extname]'
  110. },
  111. manualChunks(id: string): string | undefined {
  112. const pkg = npmPackageOf(id)
  113. if (pkg === undefined) return undefined // workspace + vendored cordis: index
  114. if (pkg === '@shikijs/langs') {
  115. return BOOT_GRAMMAR_FILES.some(file => id.endsWith(`/${file}`)) ? 'vendor' : undefined
  116. }
  117. return VENDOR_PACKAGES.has(pkg) ? 'vendor' : undefined
  118. },
  119. },
  120. },
  121. },
  122. resolve: {
  123. // Workspace packages resolve to SOURCE: package.json exports point at lib
  124. // for Node/type consumers, but the browser bundle must compile src directly
  125. // so CSS rides vite's pipeline instead of the CSS-externalized lib bundle.
  126. // Only the shell's normal package entry is aliased — plugin packages are
  127. // NEVER bundled here (shell self-sufficiency — see
  128. // packages/client/web/README.md); they arrive as runtime
  129. // bundles through the client module system. Order matters — subpath
  130. // aliases must win over bare-name prefixes.
  131. alias: [
  132. // Browserization of the vendored cordis Loader: its only node-only
  133. // import; the two process probes are mapped by `define` below.
  134. { find: /^node:module$/, replacement: src('./src/node-module-stub.ts') },
  135. { find: /^@deepseek-ai\/dsh-client-web$/, replacement: src('../../packages/client/web/src/boot.tsx') },
  136. { find: /^@deepseek-ai\/dsh-client-web-react$/, replacement: src('../../packages/client/web-react/src/index.ts') },
  137. { find: /^@deepseek-ai\/dsh-client-ui-slots$/, replacement: src('../../packages/client/ui-slots/src/index.ts') },
  138. { find: /^@deepseek-ai\/dsh-client-ui-primitives$/, replacement: src('../../packages/client/ui-primitives/src/index.ts') },
  139. { find: /^@deepseek-ai\/dsh-client-ui-attachment$/, replacement: src('../../packages/client/ui-attachment/src/index.ts') },
  140. { find: /^@deepseek-ai\/dsh-client-schema-form$/, replacement: src('../../packages/client/schema-form/src/index.ts') },
  141. { find: /^@deepseek-ai\/dsh-client-modules\/client$/, replacement: src('../../packages/client/modules/src/client/index.ts') },
  142. ],
  143. },
  144. define: {
  145. // vendored loader internal.ts: fromInternal() probes the Node major —
  146. // "0.0.0" takes neither branch, returning undefined (exactly the empty
  147. // internal slot the shell boot fills with the client module loader).
  148. 'process.versions.node': '"0.0.0"',
  149. 'process.execArgv': '[]',
  150. // vendored loader index.ts: envData falls to its default branch.
  151. 'process.env.CORDIS_SHARED': 'undefined',
  152. },
  153. })