Skip to content

Commit 339a167

Browse files
authored
Use createRequire for template modules (#1379)
* Use Node module namespace to expose require * Add changeset for synchronous template require
1 parent 912e521 commit 339a167

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
Support synchronous require in templates using Node's createRequire.
6+

src/templates-worker.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as module from "node:module";
12
import * as path from "node:path";
23
import * as url from "node:url";
34
import { consola } from "consola";
@@ -7,6 +8,8 @@ import type { CodeGenProcess } from "./code-gen-process.js";
78
import type { CodeGenConfig } from "./configuration.js";
89
import type { FileSystem } from "./util/file-system.js";
910

11+
const require = module.createRequire(import.meta.url);
12+
1013
export class TemplatesWorker {
1114
config: CodeGenConfig;
1215
fileSystem: FileSystem;
@@ -74,21 +77,21 @@ export class TemplatesWorker {
7477
);
7578
};
7679

77-
requireFnFromTemplate = async (packageOrPath: string) => {
80+
requireFnFromTemplate = (packageOrPath: string) => {
7881
const isPath =
7982
packageOrPath.startsWith("./") || packageOrPath.startsWith("../");
8083

8184
if (isPath) {
82-
return await import(
85+
return require(
8386
path.resolve(
8487
this.config.templatePaths.custom ||
8588
this.config.templatePaths.original,
8689
packageOrPath,
87-
)
90+
),
8891
);
8992
}
9093

91-
return await import(packageOrPath);
94+
return require(packageOrPath);
9295
};
9396

9497
getTemplate = (name: string, fileName: string, path?: string) => {

0 commit comments

Comments
 (0)