Skip to content

Commit 12cc1cf

Browse files
committed
feat: make generated require ESM compatible
1 parent 0748518 commit 12cc1cf

File tree

4 files changed

+5298
-9177
lines changed

4 files changed

+5298
-9177
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

0 commit comments

Comments
 (0)