vite-fastify-boilerplate
vite-fastify-boilerplate copied to clipboard
How does it work with @fastify/autoload ?
Very cool boilerplate, but I got lost when trying to add @fastify/autoload...
It works as usual, like described here https://github.com/fastify/fastify-autoload
You should use ESM syntax:
// src/app.ts
import autoLoad from '@fastify/autoload'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'
export default async (opts?: FastifyServerOptions) => {
const fastify = Fastify(opts)
fastify.register(autoLoad, {
dir: join(__dirname, 'plugins')
})
...
just found that the @fastify/autoload can not utilize the vite-plugin-node, it's reported here https://github.com/fastify/fastify-autoload/issues/230
and here is my test result:
❯ npm run dev
[email protected] dev vite
vite v2.9.9 dev server running at:
Local: http://localhost:3000/ Network: use
--hostto expose
ready in 270ms.
/home/ian/templates/fastify/node_modules/.pnpm/@[email protected]/node_modules/@fastify/autoload/index.js:222
throw new Error(@fastify/autoload cannot import plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.)
^
Error: @fastify/autoload cannot import plugin at '/home/ian/templates/fastify/src/routes/root.ts'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app. at findPlugins (/home/ian/templates/fastify/node_modules/.pnpm/@[email protected]/node_modules/@fastify/autoload/index.js:222:15) at async autoload (/home/ian/templates/fastify/node_modules/.pnpm/@[email protected]/node_modules/@fastify/autoload/index.js:30:22)
Node.js v18.2.0
Thanks for your feedback. I'll take a look...
Also for me, same issue.
Similar issue in vitest that has a solution https://github.com/fastify/fastify-autoload/issues/230#issuecomment-1448972001
I have no idea if there's a similar solution for vite-plugin-node
Thanks for the link. I will take a try...