From 43408c0b2ad3fe8cb516ac659dcec3874eb2790b Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Thu, 28 Aug 2025 16:01:36 +0900 Subject: [PATCH 1/2] Use Node module namespace to expose require --- src/templates-worker.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/templates-worker.ts b/src/templates-worker.ts index 4359c9f5a..2d01c7509 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) => { From 6954f5974cef9f5db792c95b4a5301d3ab7f4472 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Thu, 28 Aug 2025 16:10:06 +0900 Subject: [PATCH 2/2] Add changeset for synchronous template require --- .changeset/synchronous-template-require.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/synchronous-template-require.md diff --git a/.changeset/synchronous-template-require.md b/.changeset/synchronous-template-require.md new file mode 100644 index 000000000..b050b36fe --- /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. +