Gathering detailed insights and metrics for vite-plugin-node
Gathering detailed insights and metrics for vite-plugin-node
Gathering detailed insights and metrics for vite-plugin-node
Gathering detailed insights and metrics for vite-plugin-node
vite-plugin-node-polyfills
A Vite plugin to polyfill Node's Core Modules for browser environments.
vite-plugin-node-stdlib-browser
Configure `node-stdlib-browser` for Vite projects
rollup-plugin-node-externals
Automatically declare NodeJS built-in modules and npm dependencies as 'external' in Rollup config
vite-plugin-files-copy
vite plugin copy
npm install vite-plugin-node
Typescript
Module System
Node Version
NPM Version
53.2
Supply Chain
89.6
Quality
76.9
Maintenance
100
Vulnerability
95.8
License
TypeScript (100%)
Total Downloads
1,938,093
Last Day
1,399
Last Week
19,809
Last Month
134,092
Last Year
1,154,566
1,017 Stars
72 Commits
48 Forks
12 Watching
1 Branches
8 Contributors
Latest Version
4.0.0
Package Id
vite-plugin-node@4.0.0
Unpacked Size
33.17 kB
Size
10.98 kB
File Count
33
NPM Version
8.19.3
Node Version
18.13.0
Publised On
09 Oct 2024
Cumulative downloads
Total Downloads
Last day
-76.5%
1,399
Compared to previous day
Last week
-42.3%
19,809
Compared to previous week
Last month
-9.7%
134,092
Compared to previous month
Last year
86.3%
1,154,566
Compared to previous year
A vite plugin to allow you to use vite as node dev server.
esbuild
or swc
to compile your typescript filesInstall vite and this plugin with your favorite package manager, here use npm as example:
1npm install vite vite-plugin-node -D
Create a vite.config.ts
file in your project root to config vite to actually use this plugin:
1import { defineConfig } from 'vite'; 2import { VitePluginNode } from 'vite-plugin-node'; 3 4export default defineConfig({ 5 // ...vite configures 6 server: { 7 // vite server configs, for details see [vite doc](https://vitejs.dev/config/#server-host) 8 port: 3000 9 }, 10 plugins: [ 11 ...VitePluginNode({ 12 // Nodejs native Request adapter 13 // currently this plugin support 'express', 'nest', 'koa' and 'fastify' out of box, 14 // you can also pass a function if you are using other frameworks, see Custom Adapter section 15 adapter: 'express', 16 17 // tell the plugin where is your project entry 18 appPath: './app.ts', 19 20 // Optional, default: 'viteNodeApp' 21 // the name of named export of you app from the appPath file 22 exportName: 'viteNodeApp', 23 24 // Optional, default: false 25 // if you want to init your app on boot, set this to true 26 initAppOnBoot: false, 27 28 // Optional, default: 'esbuild' 29 // The TypeScript compiler you want to use 30 // by default this plugin is using vite default ts compiler which is esbuild 31 // 'swc' compiler is supported to use as well for frameworks 32 // like Nestjs (esbuild dont support 'emitDecoratorMetadata' yet) 33 // you need to INSTALL `@swc/core` as dev dependency if you want to use swc 34 tsCompiler: 'esbuild', 35 36 // Optional, default: { 37 // jsc: { 38 // target: 'es2019', 39 // parser: { 40 // syntax: 'typescript', 41 // decorators: true 42 // }, 43 // transform: { 44 // legacyDecorator: true, 45 // decoratorMetadata: true 46 // } 47 // } 48 // } 49 // swc configs, see [swc doc](https://swc.rs/docs/configuration/swcrc) 50 swcOptions: {} 51 }) 52 ], 53});
Update your server entry to export your app named viteNodeApp
or the name you configured.
1const app = express(); 2 3// your beautiful code... 4 5if (import.meta.env.PROD) 6 app.listen(3000); 7 8export const viteNodeApp = app;
Add a npm script to run the dev server:
1"scripts": { 2 "dev": "vite" 3},
Run the script! npm run dev
If your favorite framework not supported yet, you can either create an issue to request it or use the adapter
option to tell the plugin how to pass down the request to your app. You can take a look how the supported frameworks implementations from the ./src/server
folder.
Example:
1import { defineConfig } from 'vite'; 2import { VitePluginNode } from 'vite-plugin-node'; 3 4export default defineConfig({ 5 plugins: [ 6 ...VitePluginNode({ 7 adapter({ app, server, req, res, next }) { 8 app(res, res); 9 }, 10 appPath: './app.ts' 11 }) 12 ] 13});
This plugin leverages Vite SSR mode to build your app. All you need to do is add a build script to your package.json:
1"scripts": { 2 "build": "vite build" 3},
For more build config please check vite doc
Note: By default, starting from vite v3, the ssr buildle will be in esm format. if you want to build cjs, add
ssr: { format: 'cjs' }
to your vite config.
See the examples folder.
While frontend development tooling is evolving rapidly in recent years, backend DX is still like in stone age. No hot module replacement; Typescript recompiling slow as funk; Lack of plugin system etc. Thanks to Vite.js created by Evan You (A.K.A creator of vue.js; my biggest idol developer), makes all those dreams for backend development come true!
Vite by design has a middleware mode, which allows us to use it programmatically inside other modules. It's originally made for SSR, so that for each request, vite can load the renderer and render the latest changes you made to your app (https://vitejs.dev/guide/ssr.html). This plugin leverages this feature to load and execute your server app entry.
You may ask, isn't super slow, since it re-compiles/reloads the entire app? The answer is NO, because vite is smart. It has a builtin module graph as a cache layer, the graph is built up the first time your app loads. After that, when you update a file, vite will only invalidate that one and its parent modules, so that for next request, only those invalidated modules need to be re-compiled which is super fast thanks to esbuild or swc.
As this plugin just fresh developed, there are still lots ideas need to be implemented, including:
Please create an issue if you found any bugs, to help me improve this project!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 7/22 approved changesets -- score normalized to 3
Reason
1 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
24 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More