@@ -9,6 +9,7 @@ import { getClientPlugin } from '../plugins/@hey-api/client-core/utils';
9
9
import type { DefinePlugin } from '../plugins/types' ;
10
10
import type { ImportExportItemObject } from '../tsc/utils' ;
11
11
import type { Config } from '../types/config' ;
12
+ import { _test } from './file' ;
12
13
import { ensureDirSync , relativeModulePath } from './utils' ;
13
14
14
15
// Use require.resolve to find the package root, then construct the path
@@ -89,7 +90,7 @@ const copyRecursivePnP = (src: string, dest: string) => {
89
90
}
90
91
} ;
91
92
92
- const replaceRelativeImports = ( filePath : string ) => {
93
+ const appendRelativeImportsSuffix = ( filePath : string , suffix = '.js' ) => {
93
94
let content = fs . readFileSync ( filePath , 'utf8' ) ;
94
95
95
96
// Replace relative imports to append .js extension for ESM compatibility
@@ -105,13 +106,38 @@ const replaceRelativeImports = (filePath: string) => {
105
106
if ( fileName . includes ( '.' ) ) {
106
107
return match ;
107
108
}
108
- return `from '${ importPath } .js '` ;
109
+ return `from '${ importPath } ${ suffix } '` ;
109
110
} ,
110
111
) ;
111
112
112
113
fs . writeFileSync ( filePath , content , 'utf8' ) ;
113
114
} ;
114
115
116
+ const replaceRelativeImports = ( filePath : string ) =>
117
+ appendRelativeImportsSuffix ( filePath ) ;
118
+
119
+ const infixDotGenToFiles = ( outputPath : string ) => {
120
+ const coreFiles = fs . readdirSync ( outputPath ) ;
121
+ for ( const file of coreFiles ) {
122
+ const filePath = path . resolve ( outputPath , file ) ;
123
+ if ( file !== 'index.ts' ) {
124
+ const { extension, name } = _test . splitNameAndExtension ( filePath ) ;
125
+ const newFilePath = path . resolve (
126
+ outputPath ,
127
+ [ name , 'gen' , extension ] . filter ( Boolean ) . join ( '.' ) ,
128
+ ) ;
129
+ fs . renameSync ( filePath , newFilePath ) ;
130
+
131
+ appendRelativeImportsSuffix (
132
+ path . resolve ( outputPath , newFilePath ) ,
133
+ '.gen' ,
134
+ ) ;
135
+ } else {
136
+ appendRelativeImportsSuffix ( path . resolve ( outputPath , filePath ) , '.gen' ) ;
137
+ }
138
+ }
139
+ } ;
140
+
115
141
/**
116
142
* Creates a `client` folder containing the same modules as the client package.
117
143
*/
@@ -135,6 +161,9 @@ export const generateClientBundle = ({
135
161
ensureDirSync ( coreOutputPath ) ;
136
162
const coreDistPath = path . resolve ( packageRoot , 'dist' , 'clients' , 'core' ) ;
137
163
copyRecursivePnP ( coreDistPath , coreOutputPath ) ;
164
+
165
+ infixDotGenToFiles ( coreOutputPath ) ;
166
+
138
167
if ( shouldAppendJs ) {
139
168
const coreFiles = fs . readdirSync ( coreOutputPath ) ;
140
169
for ( const file of coreFiles ) {
@@ -152,6 +181,9 @@ export const generateClientBundle = ({
152
181
clientDistFolderName ,
153
182
) ;
154
183
copyRecursivePnP ( clientDistPath , clientOutputPath ) ;
184
+
185
+ infixDotGenToFiles ( clientOutputPath ) ;
186
+
155
187
if ( shouldAppendJs ) {
156
188
const clientFiles = fs . readdirSync ( clientOutputPath ) ;
157
189
for ( const file of clientFiles ) {
0 commit comments