hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.36k stars 105 forks source link

Dynamic require of "child_process" is not supported #1079

Closed anchan828 closed 1 month ago

anchan828 commented 1 month ago

Description

An error occurs when the createClient API of ESM is called.

This change will probably solve the problem, but I'm not sure if it's a good solution.

packages/openapi-ts/tsup.config.ts

export default defineConfig((options) => ({
+  banner(ctx) {
+    if(ctx.format === 'esm') {
+      return {
+        js: "import { createRequire } from 'module'; const require = createRequire(import.meta.url);"
+      }
+    }
+  },
  clean: true,
  dts: true,
  entry: ['src/index.ts'],
  format: ['cjs', 'esm'],
  minify: !options.watch,
  shims: false,
  sourcemap: true,
  treeshake: true,
}));

https://github.com/evanw/esbuild/issues/1921#issuecomment-2302290651

Reproducible example or configuration

An error occurs when tsx build.ts is called. Since the command is set in startCommand, an error should be output as soon as the browser is opened. https://stackblitz.com/edit/hey-api-example-dbtez4?file=build.ts

OpenAPI specification (optional)

No response

System information (optional)

{
  "@hey-api/client-fetch": "0.3.2",
  "@hey-api/openapi-ts": "0.53.3"
}
stackblitz[bot] commented 1 month ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

mrlubos commented 1 month ago

@anchan828 Huh, that's exactly what this was supposed to be fixing. Thanks for reporting, I'll have a look

mrlubos commented 1 month ago

I like this fix a lot, simplifies the messy code a great deal, thanks!