@@ -2,7 +2,7 @@ import fs from 'node:fs';
22import path from 'node:path' ;
33import { fileURLToPath } from 'node:url' ;
44
5- import type { IProject , ProjectRenderMeta } from '@hey-api/codegen-core' ;
5+ import type { IProject } from '@hey-api/codegen-core' ;
66import type { DefinePlugin , OutputHeader } from '@hey-api/shared' ;
77import { ensureDirSync , outputHeaderToPrefix } from '@hey-api/shared' ;
88
@@ -108,38 +108,37 @@ function replaceImports({
108108 filePath,
109109 header,
110110 isDevMode,
111- meta,
112111 renamed,
113112} : {
114113 filePath : string ;
115114 header ?: string ;
116115 isDevMode ?: boolean ;
117- meta : ProjectRenderMeta ;
118116 renamed : Map < string , string > ;
119117} ) : void {
120118 let content = fs . readFileSync ( filePath , 'utf8' ) ;
121119
122120 // Dev mode: rewrite source bundle imports to match output structure
123121 if ( isDevMode ) {
124- // ../../client-core/bundle/foo -> ../core/foo
125- content = content . replace ( / f r o m \s + [ ' " ] \. \. \/ \. \. \/ c l i e n t - c o r e \/ b u n d l e \/ / g, "from '../core/" ) ;
126- // ../../client-core/bundle' (index import)
127- content = content . replace ( / f r o m \s + [ ' " ] \. \. \/ \. \. \/ c l i e n t - c o r e \/ b u n d l e [ ' " ] / g, "from '../core'" ) ;
122+ // ...client_core.bundle.foo -> ..core.foo
123+ content = content . replace (
124+ / f r o m \s + ( \. { 3 , } ) \. ? c l i e n t _ c o r e \. b u n d l e \. / g,
125+ ( match , dots ) => `from ${ dots === '...' ? '..' : dots . slice ( 0 , - 1 ) } core.` ,
126+ ) ;
127+ // ...client_core.bundle (index import) -> ..core
128+ content = content . replace (
129+ / f r o m \s + ( \. { 3 , } ) \. ? c l i e n t _ c o r e \. b u n d l e [ ' " ] ? / g,
130+ ( match , dots ) => `from ${ dots === '...' ? '..' : dots . slice ( 0 , - 1 ) } core` ,
131+ ) ;
128132 }
129133
130- content = content . replace ( / f r o m \s + [ ' " ] ( \. \. ? \/ [ ^ ' " ] * ?) [ ' " ] / g, ( match , importPath ) => {
131- const importIndex = match . indexOf ( importPath ) ;
132- const extension = path . extname ( importPath ) ;
133- const fileName = path . basename ( importPath , extension ) ;
134- const importDir = path . dirname ( importPath ) ;
135- const replacedName =
136- ( renamed . get ( fileName ) ?? fileName ) +
137- ( meta . importFileExtension ? meta . importFileExtension : extension ) ;
138- const replacedMatch =
139- match . slice ( 0 , importIndex ) +
140- [ importDir , replacedName ] . filter ( Boolean ) . join ( '/' ) +
141- match . slice ( importIndex + importPath . length ) ;
142- return replacedMatch ;
134+ content = content . replace ( / f r o m \s + ( .+ ?) \s + i m p o r t / g, ( match , importPath ) => {
135+ const cleanPath = importPath . replace ( / ^ [ ' " ] | [ ' " ] $ / g, '' ) ;
136+ if ( ! cleanPath . startsWith ( '.' ) ) return match ;
137+ const leadingDots = cleanPath . match ( / ^ \. + / ) ?. [ 0 ] || '' ;
138+ const moduleName = cleanPath . replace ( / ^ \. + / , '' ) ;
139+ if ( ! moduleName ) return match ;
140+ const replacedName = renamed . get ( moduleName ) ?? moduleName ;
141+ return `from ${ leadingDots } ${ replacedName } import` ;
143142 } ) ;
144143
145144 const fileHeader = header ?? '' ;
@@ -154,13 +153,11 @@ function replaceImports({
154153 */
155154export function generateClientBundle ( {
156155 header,
157- meta,
158156 outputPath,
159157 plugin,
160158 project,
161159} : {
162160 header ?: OutputHeader ;
163- meta : ProjectRenderMeta ;
164161 outputPath : string ;
165162 plugin : DefinePlugin < Client . Config & { name : string } > [ 'Config' ] ;
166163 project : IProject ;
@@ -210,7 +207,6 @@ export function generateClientBundle({
210207 // replaceImports({
211208 // filePath: path.resolve(coreOutputPath, file),
212209 // isDevMode: devMode,
213- // meta,
214210 // renamed,
215211 // });
216212 // }
@@ -221,7 +217,6 @@ export function generateClientBundle({
221217 filePath : path . resolve ( clientOutputPath , file ) ,
222218 header : headerPrefix ,
223219 isDevMode : devMode ,
224- meta,
225220 renamed,
226221 } ) ;
227222 }
0 commit comments