Skip to content

Commit fac4265

Browse files
Merge pull request #3235 from bdezso/esm-compatible-require
feat: make generated require() ESM compatible
2 parents eef8de0 + 43eb567 commit fac4265

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

lib/plugin/utils/plugin-utils.ts

+15
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ export function hasPropertyKey(
137137
.some((item) => item.name.getText() === key);
138138
}
139139

140+
function getOutputExtension(fileName: string): string {
141+
if (fileName.endsWith('.mts')) {
142+
return '.mjs';
143+
} else if (fileName.endsWith('.cts')) {
144+
return '.cjs';
145+
} else{
146+
return ".js";
147+
}
148+
}
149+
140150
export function replaceImportPath(
141151
typeReference: string,
142152
fileName: string,
@@ -190,6 +200,10 @@ export function replaceImportPath(
190200
if (indexPos >= 0) {
191201
relativePath = relativePath.slice(0, indexPos);
192202
}
203+
} else {
204+
// Add appropriate extension for non-node_modules imports
205+
const extension = getOutputExtension(fileName);
206+
relativePath += extension;
193207
}
194208

195209
typeReference = typeReference.replace(importPath, relativePath);
@@ -203,6 +217,7 @@ export function replaceImportPath(
203217
importPath: relativePath
204218
};
205219
}
220+
206221
return {
207222
typeReference: typeReference.replace('import', 'require'),
208223
importPath: relativePath

test/plugin/fixtures/parameter-property.dto.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class ParameterPropertyDto {
2929
this.protectedValue = protectedValue;
3030
}
3131
static _OPENAPI_METADATA_FACTORY() {
32-
return { readonlyValue: { required: false, type: () => String }, privateValue: { required: true, type: () => String, nullable: true }, publicValue: { required: true, type: () => [require("./parameter-property.dto").ItemDto] }, protectedValue: { required: true, type: () => String, default: "1234" } };
32+
return { readonlyValue: { required: false, type: () => String }, privateValue: { required: true, type: () => String, nullable: true }, publicValue: { required: true, type: () => [require("./parameter-property.dto.js").ItemDto] }, protectedValue: { required: true, type: () => String, default: "1234" } };
3333
}
3434
}
3535
export var LettersEnum;
@@ -43,7 +43,7 @@ export class ItemDto {
4343
this.enumValue = enumValue;
4444
}
4545
static _OPENAPI_METADATA_FACTORY() {
46-
return { enumValue: { required: true, enum: require("./parameter-property.dto").LettersEnum } };
46+
return { enumValue: { required: true, enum: require("./parameter-property.dto.js").LettersEnum } };
4747
}
4848
}
4949
`;

test/plugin/fixtures/serialized-meta.fixture.ts

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// @ts-nocheck
22
export default async () => {
33
const t = {
4-
['./cats/dto/pagination-query.dto']: await import(
5-
'./cats/dto/pagination-query.dto'
4+
['./cats/dto/pagination-query.dto.js']: await import(
5+
'./cats/dto/pagination-query.dto.js'
66
),
7-
['./cats/dto/create-cat.dto']: await import('./cats/dto/create-cat.dto'),
8-
['./cats/dto/tag.dto']: await import('./cats/dto/tag.dto'),
9-
['./cats/classes/cat.class']: await import('./cats/classes/cat.class')
7+
['./cats/dto/create-cat.dto.js']: await import(
8+
'./cats/dto/create-cat.dto.js'
9+
),
10+
['./cats/dto/tag.dto.js']: await import('./cats/dto/tag.dto.js'),
11+
['./cats/classes/cat.class.js']: await import('./cats/classes/cat.class.js')
1012
};
1113
return {
1214
'@nestjs/swagger': {
@@ -21,16 +23,16 @@ export default async () => {
2123
constrainedLimit: { required: false, type: () => Number },
2224
enum: {
2325
required: true,
24-
enum: t['./cats/dto/pagination-query.dto'].LettersEnum
26+
enum: t['./cats/dto/pagination-query.dto.js'].LettersEnum
2527
},
2628
enumArr: {
2729
required: true,
28-
enum: t['./cats/dto/pagination-query.dto'].LettersEnum,
30+
enum: t['./cats/dto/pagination-query.dto.js'].LettersEnum,
2931
isArray: true
3032
},
3133
letters: {
3234
required: true,
33-
enum: t['./cats/dto/pagination-query.dto'].LettersEnum,
35+
enum: t['./cats/dto/pagination-query.dto.js'].LettersEnum,
3436
isArray: true
3537
},
3638
beforeDate: { required: true, type: () => Date },
@@ -60,11 +62,11 @@ export default async () => {
6062
options: { required: false, type: () => [Object] },
6163
enum: {
6264
required: true,
63-
enum: t['./cats/dto/pagination-query.dto'].LettersEnum
65+
enum: t['./cats/dto/pagination-query.dto.js'].LettersEnum
6466
},
6567
enumArr: {
6668
required: true,
67-
enum: t['./cats/dto/pagination-query.dto'].LettersEnum
69+
enum: t['./cats/dto/pagination-query.dto.js'].LettersEnum
6870
},
6971
uppercaseString: { required: true, type: () => String },
7072
lowercaseString: { required: true, type: () => String },
@@ -137,30 +139,30 @@ export default async () => {
137139
options: { required: false, type: () => [Object] },
138140
enum: {
139141
required: true,
140-
enum: t['./cats/dto/pagination-query.dto'].LettersEnum
142+
enum: t['./cats/dto/pagination-query.dto.js'].LettersEnum
141143
},
142144
state: {
143145
required: false,
144146
description: 'Available language in the application',
145147
example: 'FR',
146-
enum: t['./cats/dto/create-cat.dto'].CategoryState
148+
enum: t['./cats/dto/create-cat.dto.js'].CategoryState
147149
},
148150
enumArr: {
149151
required: true,
150-
enum: t['./cats/dto/pagination-query.dto'].LettersEnum
152+
enum: t['./cats/dto/pagination-query.dto.js'].LettersEnum
151153
},
152154
enumArr2: {
153155
required: true,
154-
enum: t['./cats/dto/pagination-query.dto'].LettersEnum,
156+
enum: t['./cats/dto/pagination-query.dto.js'].LettersEnum,
155157
isArray: true
156158
},
157159
tag: {
158160
required: true,
159-
type: () => t['./cats/dto/tag.dto'].TagDto
161+
type: () => t['./cats/dto/tag.dto.js'].TagDto
160162
},
161163
multipleTags: {
162164
required: true,
163-
type: () => [t['./cats/dto/tag.dto'].TagDto]
165+
type: () => [t['./cats/dto/tag.dto.js'].TagDto]
164166
},
165167
nested: {
166168
required: true,
@@ -197,11 +199,11 @@ export default async () => {
197199
import('./cats/cats.controller'),
198200
{
199201
CatsController: {
200-
create: { type: t['./cats/classes/cat.class'].Cat },
201-
findOne: { type: t['./cats/classes/cat.class'].Cat },
202+
create: { type: t['./cats/classes/cat.class.js'].Cat },
203+
findOne: { type: t['./cats/classes/cat.class.js'].Cat },
202204
findAll: {},
203-
createBulk: { type: t['./cats/classes/cat.class'].Cat },
204-
createAsFormData: { type: t['./cats/classes/cat.class'].Cat },
205+
createBulk: { type: t['./cats/classes/cat.class.js'].Cat },
206+
createAsFormData: { type: t['./cats/classes/cat.class.js'].Cat },
205207
getWithEnumParam: {},
206208
getWithRandomQuery: {}
207209
}

0 commit comments

Comments
 (0)