npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-node

v4.0.0

Published

Vite plugin to enable your node server HMR

Downloads

146,865

Readme

Vite Plugin Node

A vite plugin to allow you to use vite as node dev server.

Features

  • All the perks from Vite plus:
  • Node server HMR! (hot module replacement)
  • Support Express, Fastify, Koa and Nest out of the box
  • Support Custom Request Adapter
  • You can choose to use esbuild or swc to compile your typescript files

Get started


  1. Install vite and this plugin with your favorite package manager, here use npm as example:

    npm install vite vite-plugin-node -D
  2. Create a vite.config.ts file in your project root to config vite to actually use this plugin:

    import { defineConfig } from 'vite';
    import { VitePluginNode } from 'vite-plugin-node';
    
    export default defineConfig({
      // ...vite configures
      server: {
        // vite server configs, for details see [vite doc](https://vitejs.dev/config/#server-host)
        port: 3000
      },
      plugins: [
        ...VitePluginNode({
          // Nodejs native Request adapter
          // currently this plugin support 'express', 'nest', 'koa' and 'fastify' out of box,
          // you can also pass a function if you are using other frameworks, see Custom Adapter section
          adapter: 'express',
    
          // tell the plugin where is your project entry
          appPath: './app.ts',
    
          // Optional, default: 'viteNodeApp'
          // the name of named export of you app from the appPath file
          exportName: 'viteNodeApp',
    
          // Optional, default: false
          // if you want to init your app on boot, set this to true
          initAppOnBoot: false,
    
          // Optional, default: 'esbuild'
          // The TypeScript compiler you want to use
          // by default this plugin is using vite default ts compiler which is esbuild
          // 'swc' compiler is supported to use as well for frameworks
          // like Nestjs (esbuild dont support 'emitDecoratorMetadata' yet)
          // you need to INSTALL `@swc/core` as dev dependency if you want to use swc
          tsCompiler: 'esbuild',
    
          // Optional, default: {
          // jsc: {
          //   target: 'es2019',
          //   parser: {
          //     syntax: 'typescript',
          //     decorators: true
          //   },
          //  transform: {
          //     legacyDecorator: true,
          //     decoratorMetadata: true
          //   }
          // }
          // }
          // swc configs, see [swc doc](https://swc.rs/docs/configuration/swcrc)
          swcOptions: {}
        })
      ],
    });
  3. Update your server entry to export your app named viteNodeApp or the name you configured.

ExpressJs

const app = express();

// your beautiful code...

if (import.meta.env.PROD)
  app.listen(3000);

export const viteNodeApp = app;

More Examples:

  1. Add a npm script to run the dev server:

    "scripts": {
      "dev": "vite"
    },
  2. Run the script! npm run dev

Custom Adapter

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:

import { defineConfig } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';

export default defineConfig({
  plugins: [
    ...VitePluginNode({
      adapter({ app, server, req, res, next }) {
        app(res, res);
      },
      appPath: './app.ts'
    })
  ]
});

Build

This plugin leverages Vite SSR mode to build your app. All you need to do is add a build script to your package.json:

"scripts": {
  "build": "vite build"
},

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.

Examples

See the examples folder.

Why?


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!

How?


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.

To-Do

As this plugin just fresh developed, there are still lots ideas need to be implemented, including:

  • [ ] Test with a large node project, I need y'all helps on this!
  • [ ] Unit tests

Bugs

Please create an issue if you found any bugs, to help me improve this project!