### Start Fastify server in src/server.ts Source: https://github.com/vanilla-icecream/vite-plugin-fastify/blob/main/README.md Example `src/server.ts` file showing how to import and start the Fastify application, including error handling for production environments. ```ts // src/server.ts import app from './app'; const server = app(); const start = () => { try { await server.listen({ host: '127.0.0.1', port: 3000 }); } catch (err) { server.log.error(err); if (process.env.NODE_ENV === 'production') { process.exit(1); } } }; start(); ``` -------------------------------- ### Install vite-plugin-fastify Source: https://github.com/vanilla-icecream/vite-plugin-fastify/blob/main/README.md Instructions to install the vite-plugin-fastify package using various package managers. ```sh $ npm i vite-plugin-fastify -D # or $ yarn add vite-plugin-fastify -D # or $ pnpm i vite-plugin-fastify -D # or $ bun add vite-plugin-fastify -D ``` -------------------------------- ### Define Fastify application in src/app.ts Source: https://github.com/vanilla-icecream/vite-plugin-fastify/blob/main/README.md Example `src/app.ts` file demonstrating how to create a Fastify instance and define a simple API route (`/api/hello-world`). ```ts // src/app.ts import fastify from 'fastify'; export default () => { const app = fastify(); app.get('/api/hello-world', async (req, reply) => { return reply.send('Hello, World!'); }); return app; }; ``` -------------------------------- ### Configure vite-plugin-fastify in vite.config.ts Source: https://github.com/vanilla-icecream/vite-plugin-fastify/blob/main/README.md Example configuration for `vite.config.ts` to integrate `vite-plugin-fastify`, setting server host/port, plugin options (`appPath`, `serverPath`), and path aliases. ```ts // vite.config.ts import { resolve } from 'path'; import { defineConfig } from 'vite'; import fastify from 'vite-plugin-fastify'; export default defineConfig({ server: { host: '127.0.0.1', port: 3000, }, plugins: [ fastify({ appPath: './src/app.ts', // Default: /src/app.ts serverPath: './src/server.ts', // Default: /src/server.ts }), ], resolve: { alias: { '~': resolve(__dirname, 'src'), }, }, }); ``` -------------------------------- ### Add development scripts to package.json Source: https://github.com/vanilla-icecream/vite-plugin-fastify/blob/main/README.md Configure `package.json` with `dev`, `build`, and `preview` scripts for Vite. ```json { // ... "type": "module", "scripts": { "dev": "vite", "build": "vite build", "preview": "vite preview" } // ... } ``` -------------------------------- ### Use vite-node for WebSocket workaround Source: https://github.com/vanilla-icecream/vite-plugin-fastify/blob/main/README.md Modify the `dev` script to use `vite-node -w src/server.ts` as a workaround for `vite-plugin-fastify`'s lack of WebSocket support. ```diff - "dev": "vite", + "dev": "vite-node -w src/server.ts", ``` -------------------------------- ### Add HMR support to vite-node for server restarts Source: https://github.com/vanilla-icecream/vite-plugin-fastify/blob/main/README.md Integrate `import.meta.hot` listeners into `src/server.ts` to gracefully close the Fastify server before a full HMR reload or dispose event when using `vite-node`. ```diff const start = async () => { try { await server.listen({ host: '127.0.0.1', port: 3000 }); } catch (err) { server.log.error(err); if (process.env.NODE_ENV === 'production') { process.exit(1); } } + if (import.meta.hot) { + import.meta.hot.on('vite:beforeFullReload', async () => { + await server.close(); + }); + import.meta.hot.dispose(async () => { + await server.close(); + }); + } }; ``` -------------------------------- ### Disable HMR in vite-plugin-fastify configuration Source: https://github.com/vanilla-icecream/vite-plugin-fastify/blob/main/README.md Set `devMode` to `false` within the `fastify` plugin options in `vite.config.ts` to disable Hot-Module Replacement during development. ```diff plugins: [ fastify({ + devMode: false, }), ], ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.