Skip to content

vite - Expose ability to add middlewares to Vite dev server (or similar extensibility) #25777

@mattdsteele

Description

@mattdsteele

Command

serve

Description

With the goal to move off the Webpack-based development server, we have a need to port some of the functionality currently supported by the @angular-builders/custom-webpack module.

In particular, we would like the ability to add custom middlewares to the dev server. In our case, this would be used to inject a connect-compatible middleware that acts as a means of providing authenticated access to proxied endpoints, by setting session cookies used for authorization.

Describe the solution you'd like

Vite's dev server currently supports proxy endpoints via an embedded http-proxy module, and provides documentation on customizing with additional middlewares:

In some cases, you might also want to configure the underlying dev server (e.g. to add custom middlewares to the internal connect app). In order to do that, you need to write your own plugin and use configureServer function.

The configureServer method exposes the custom middleware hook:

const myPlugin = () => ({
  name: 'configure-server',
  configureServer(server) {
    server.middlewares.use((req, res, next) => {
      // custom handle request...
    })
  },
})

Describe alternatives you've considered

Exposing direct access to the Vite API may not align with the philosophy of the Angular CLI, but supporting the extensibility via custom builders would make these use cases feasible.

I see similar requests in other issues such as #24163 and just-jeb/angular-builders#1288

On a side note, I'm very excited to see CLI's build process move over to Vite; I've been using it outside Angular, and have given talks about how much I've enjoyed it: https://steele.blue/vite-is-neat/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions