0.0.8 • Published 2 months ago

@macropygia/vite-plugin-connect-middleware v0.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@macropygia/vite-plugin-connect-middleware

npm version MIT TypeScript Vite

English | 日本語

Vite plugin to configure middleware for dev and preview servers.

  • This package is currently unstable.
    • Breaking changes may occur without any notice, even if in patch releases.
    • See CHANGELOG for changes.
  • This package only works as ESM.
    • "type": "module" is required.

Usage

Ref. Use middleware - Connect

Both servers use the same middleware

// vite.config.js
import { defineConfig } from 'vite'
import vitePluginConnectMiddleware from '@macropygia/vite-plugin-connect-middleware'

const reDownloads = /^\/downloads\/(?<dir>sound|movie)\/(?<path>.*)$/

export default defineConfig({
  plugins: [
    vitePluginConnectMiddleware(
      (req, res, next) => {
        if (req.url) {
          if (req.url.startsWith('/app/')) {
            req.url = '/app/'
          } else if (reDownloads.test(req.url)) {
            const reResult = req.url.match(reDownloads)
            res.writeHead(301, {
              Location: `https://example.com/downloads/${
                reResult.groups['dir']
              }/${reResult.groups['path']}`,
            })
          }
        }
        return next()
      }
    ),
  ],
})

Servers use different middleware

vitePluginConnectMiddleware({
  // for dev server
  dev: (req, res, next) => {
    // ...
  },
  // for preview server
  preview: (req, res, next) => {
    // ...
  },
}),
  • dev and preview are optional
0.0.8

2 months ago

0.0.7

1 year ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago