diff --git a/.changeset/synchronous-template-require.md b/.changeset/synchronous-template-require.md new file mode 100644 index 00000000..b050b36f --- /dev/null +++ b/.changeset/synchronous-template-require.md @@ -0,0 +1,6 @@ +--- +"swagger-typescript-api": patch +--- + +Support synchronous require in templates using Node's createRequire. + diff --git a/src/templates-worker.ts b/src/templates-worker.ts index 4359c9f5..2d01c750 100644 --- a/src/templates-worker.ts +++ b/src/templates-worker.ts @@ -1,3 +1,4 @@ +import * as module from "node:module"; import * as path from "node:path"; import * as url from "node:url"; import { consola } from "consola"; @@ -7,6 +8,8 @@ import type { CodeGenProcess } from "./code-gen-process.js"; import type { CodeGenConfig } from "./configuration.js"; import type { FileSystem } from "./util/file-system.js"; +const require = module.createRequire(import.meta.url); + export class TemplatesWorker { config: CodeGenConfig; fileSystem: FileSystem; @@ -74,21 +77,21 @@ export class TemplatesWorker { ); }; - requireFnFromTemplate = async (packageOrPath: string) => { + requireFnFromTemplate = (packageOrPath: string) => { const isPath = packageOrPath.startsWith("./") || packageOrPath.startsWith("../"); if (isPath) { - return await import( + return require( path.resolve( this.config.templatePaths.custom || this.config.templatePaths.original, packageOrPath, - ) + ), ); } - return await import(packageOrPath); + return require(packageOrPath); }; getTemplate = (name: string, fileName: string, path?: string) => {