honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
337 stars 113 forks source link

`@hono/zod-openapi` is not compatible with HonoX #416

Closed bruceharrison1984 closed 4 months ago

bruceharrison1984 commented 4 months ago

Attempting to use @hono/zod-openapi with HonoX results in the following error:

image

The following was (attempted) used to get this going:

//server.ts
import { OpenAPIHono } from '@hono/zod-openapi';
import { createApp } from 'honox/server';
import { showRoutes } from 'hono/dev';
import errorHandler from './routes/_error';

const openApi = new OpenAPIHono();

const app = createApp({
  app: openApi,
  init: (app) => {
    app.onError(errorHandler);
  },
});

showRoutes(app);

export default app;

It doesn't seem to matter where in the router tree @hono/zod-openapi is injected, the error persists until it is replaced with normal hono instances.

yusukebe commented 4 months ago

Hi @bruceharrison1984

This is because @asteasolutions/zod-to-openapi used internally by Zod OpenAPI does not support ES Modules. So, the error will not occur if you set vite.config.ts as follows.

export default defineConfig({
  ssr: {
    external: ['@asteasolutions/zod-to-openapi']
  },
  plugins: [honox()]
})
bruceharrison1984 commented 4 months ago

Awesome, I wondered if it was something along those lines. Thanks!

yusukebe commented 4 months ago

Yeah, the matter of CommonJS/ES Modules is annoying.