|
| 1 | +import type { Plugin } from 'vite' |
| 2 | +import type { OnboardingState, OnboardingStatus } from '../src/types' |
| 3 | +import { spawn } from 'node:child_process' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { defineConfig } from 'vite' |
| 6 | +import { defaultMessages } from '../src/index' |
| 7 | + |
| 8 | +const root = fileURLToPath(new URL('..', import.meta.url)) |
| 9 | +const productName = 'Devframes' |
| 10 | + |
| 11 | +/** |
| 12 | + * A fake of the `__onboard/*` routes `createOnboarding` serves. The client |
| 13 | + * resolves them next to its own module (`/src/client/`). The playground page |
| 14 | + * picks the state through `/__mock/state`; Install runs a timer instead of a |
| 15 | + * package manager and ends in `installed` or `error`. |
| 16 | + */ |
| 17 | +function mockOnboarding(): Plugin { |
| 18 | + let state: OnboardingState = 'idle' |
| 19 | + let outcome: 'installed' | 'error' = 'installed' |
| 20 | + let timer: ReturnType<typeof setTimeout> | undefined |
| 21 | + |
| 22 | + const status = (): OnboardingStatus => ({ |
| 23 | + state, |
| 24 | + command: ['pnpm', 'add', '-D', '@devframes/hub', '@devframes/hub-ui'], |
| 25 | + branding: { productName, primaryColor: '#646cff' }, |
| 26 | + messages: defaultMessages(productName), |
| 27 | + error: state === 'error' ? { code: 'DF9001', message: 'Mock install failed.' } : undefined, |
| 28 | + }) |
| 29 | + |
| 30 | + return { |
| 31 | + name: 'mock-onboarding', |
| 32 | + configureServer(server) { |
| 33 | + server.middlewares.use((req, res, next) => { |
| 34 | + const url = new URL(req.url ?? '/', 'http://localhost') |
| 35 | + const send = (body: unknown) => { |
| 36 | + res.setHeader('Content-Type', 'application/json') |
| 37 | + res.setHeader('Cache-Control', 'no-store') |
| 38 | + res.end(JSON.stringify(body)) |
| 39 | + } |
| 40 | + if (url.pathname === '/__mock/state') { |
| 41 | + clearTimeout(timer) |
| 42 | + state = (url.searchParams.get('state') ?? 'idle') as OnboardingState |
| 43 | + outcome = url.searchParams.get('outcome') === 'error' ? 'error' : 'installed' |
| 44 | + return send({ state, outcome }) |
| 45 | + } |
| 46 | + if (!url.pathname.startsWith('/src/client/__onboard/')) |
| 47 | + return next() |
| 48 | + const route = url.pathname.split('/').pop() |
| 49 | + if (route === 'install') { |
| 50 | + state = 'installing' |
| 51 | + timer = setTimeout(() => (state = outcome), 2500) |
| 52 | + } |
| 53 | + else if (route === 'disable') { |
| 54 | + state = 'disabled' |
| 55 | + } |
| 56 | + send(route === 'status' ? status() : { ok: true }) |
| 57 | + }) |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * The button's styles are compiled ahead of time into `.generated/css.ts`. |
| 64 | + * When a source file, `style.css` or a UnoCSS config changes, run the same |
| 65 | + * script `pnpm build:css` runs, then reload. The client is a self-running |
| 66 | + * module, so HMR would mount a second button: every change is a full reload. |
| 67 | + */ |
| 68 | +function rebuildCss(): Plugin { |
| 69 | + const watched = [ |
| 70 | + fileURLToPath(new URL('../src/client/', import.meta.url)), |
| 71 | + fileURLToPath(new URL('../uno.config.ts', import.meta.url)), |
| 72 | + fileURLToPath(new URL('../../../design/', import.meta.url)), |
| 73 | + ] |
| 74 | + const generated = fileURLToPath(new URL('../src/client/.generated/', import.meta.url)) |
| 75 | + let timer: ReturnType<typeof setTimeout> | undefined |
| 76 | + |
| 77 | + return { |
| 78 | + name: 'rebuild-onboard-css', |
| 79 | + configureServer(server) { |
| 80 | + server.watcher.add(watched) |
| 81 | + server.watcher.on('change', (file) => { |
| 82 | + if (file.startsWith(generated) || !watched.some(path => file.startsWith(path))) |
| 83 | + return |
| 84 | + clearTimeout(timer) |
| 85 | + timer = setTimeout(() => { |
| 86 | + const child = spawn('pnpm', ['run', 'build:css'], { cwd: root, stdio: 'inherit' }) |
| 87 | + child.on('close', (code) => { |
| 88 | + if (code === 0) |
| 89 | + server.ws.send({ type: 'full-reload' }) |
| 90 | + }) |
| 91 | + }, 100) |
| 92 | + }) |
| 93 | + }, |
| 94 | + handleHotUpdate: ({ file }) => watched.some(path => file.startsWith(path)) ? [] : undefined, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export default defineConfig({ |
| 99 | + root, |
| 100 | + server: { open: '/playground/' }, |
| 101 | + plugins: [mockOnboarding(), rebuildCss()], |
| 102 | +}) |
0 commit comments