Reputation: 5782
We are using Vite for our frontend (inside SvelteKit) and it works nicely with creating SSR and frontend code.
I especially like the prebundling of 3rd party packages via esbuild.
Can someone tell me if it’s possible to use the Vite bundling pipeline for a backend-only project (nodejs server based on koa)?
Upvotes: 46
Views: 73255
Reputation: 353
import { builtinModules } from 'node:module';
const NODE_BUILT_IN_MODULES = builtinModules.filter(m => !m.startsWith('_'));
NODE_BUILT_IN_MODULES.push(...NODE_BUILT_IN_MODULES.map(m => `node:${m}`))
const noReload = process.env.VITE_NO_RELOAD !== undefined;
{
optimizeDeps: {
exclude: NODE_BUILT_IN_MODULES,
},
build: {
rollupOptions: {
external: NODE_BUILT_IN_MODULES,
},
lib: {
entry: ['src/index.ts'],
formats: ['es'],
fileName: 'mylib'
},
},
}
The above vite.config
fragment seems like the bare minimum for building a basic lib targeting Node using Vite without plugins and custom runtimes. Sharing for folks who prefer fewer third-party dependencies.
Works for me with both es
and cjs
builds. Mileage may vary if you're building something more exotic.
See:
Vite OptimizeDeps
Rollup External
Upvotes: 1
Reputation: 474
You can try vite-plugin-node. This vite plugin supports multiple nodejs frameworks out of the box including koa. I have created a simple express app with it in typescript which worked fine for me. One downside is the plugin is fairly new and still in its early stage, so you may not want to use it in a serious production app.
Upvotes: 29