dev-web.spec.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { expect, it } from 'vitest'
  5. import type { TsdownBundle } from 'tsdown'
  6. import { discoverPluginDirs, watchClientPlugins } from './dev-web.ts'
  7. it('discovers dsh.client packages with sibling roles', async () => {
  8. const root = await mkdtemp(join(tmpdir(), 'dsh-dev-web-discovery-'))
  9. try {
  10. const current = join(root, 'packages', 'client', 'current')
  11. await mkdir(current, { recursive: true })
  12. await writeFile(join(current, 'package.json'), JSON.stringify({
  13. dsh: {
  14. bundle: { patch: './cordis.patch.yml' },
  15. client: { platform: 'web' },
  16. profile: { bundles: [] },
  17. },
  18. }))
  19. expect(discoverPluginDirs(root)).toEqual(['packages/client/current'])
  20. } finally {
  21. await rm(root, { recursive: true, force: true })
  22. }
  23. })
  24. it('rebuilds a client-plugin bundle after its source changes', async () => {
  25. const root = await mkdtemp(join(tmpdir(), 'dsh-dev-web-watch-'))
  26. let bundles: TsdownBundle[] = []
  27. try {
  28. await symlink(join(import.meta.dirname, '..', 'node_modules'), join(root, 'node_modules'), 'dir')
  29. await writeFile(join(root, 'package.json'), JSON.stringify({ name: '@dsh-test/dev-web-watch', private: true, type: 'module' }))
  30. await writeFile(join(root, 'tsdown.config.ts'), `
  31. import { defineConfig } from 'tsdown'
  32. export default defineConfig({
  33. entry: { client: 'src.ts' }, outDir: 'lib', format: 'cjs', platform: 'browser', dts: false, clean: false,
  34. outputOptions: { entryFileNames: 'client.js' },
  35. })
  36. `)
  37. const sourcePath = join(root, 'src.ts')
  38. const bundlePath = join(root, 'lib/client.js')
  39. await writeFile(sourcePath, 'export const version = "watch-v1"\n')
  40. bundles = await watchClientPlugins(root, ['.'], 50)
  41. expect(await readFile(bundlePath, 'utf8')).toContain('watch-v1')
  42. await new Promise(resolve => setTimeout(resolve, 1_000))
  43. await writeFile(sourcePath, `export const version = "watch-v2-${'x'.repeat(100)}"\n`)
  44. await expect.poll(async () => (await readFile(bundlePath, 'utf8')).includes('watch-v2-'), {
  45. timeout: 10_000,
  46. }).toBe(true)
  47. } finally {
  48. for (const bundle of bundles) await bundle[Symbol.asyncDispose]()
  49. await rm(root, { recursive: true, force: true })
  50. }
  51. }, 20_000)