Skip to content

Commit 234c7f4

Browse files
authored
Merge pull request #3518 from hey-api/chore/client-import-path
chore: update client import paths
2 parents f2c428f + 763240d commit 234c7f4

2 files changed

Lines changed: 19 additions & 27 deletions

File tree

packages/openapi-python/src/generate/client.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44

5-
import type { IProject, ProjectRenderMeta } from '@hey-api/codegen-core';
5+
import type { IProject } from '@hey-api/codegen-core';
66
import type { DefinePlugin, OutputHeader } from '@hey-api/shared';
77
import { 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(/from\s+['"]\.\.\/\.\.\/client-core\/bundle\//g, "from '../core/");
126-
// ../../client-core/bundle' (index import)
127-
content = content.replace(/from\s+['"]\.\.\/\.\.\/client-core\/bundle['"]/g, "from '../core'");
122+
// ...client_core.bundle.foo -> ..core.foo
123+
content = content.replace(
124+
/from\s+(\.{3,})\.?client_core\.bundle\./g,
125+
(match, dots) => `from ${dots === '...' ? '..' : dots.slice(0, -1)}core.`,
126+
);
127+
// ...client_core.bundle (index import) -> ..core
128+
content = content.replace(
129+
/from\s+(\.{3,})\.?client_core\.bundle['"]?/g,
130+
(match, dots) => `from ${dots === '...' ? '..' : dots.slice(0, -1)}core`,
131+
);
128132
}
129133

130-
content = content.replace(/from\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(/from\s+(.+?)\s+import/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
*/
155154
export 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
}

packages/openapi-python/src/generate/output.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ export async function generateOutput(context: Context): Promise<void> {
2525
// @ts-expect-error
2626
config._FRAGILE_CLIENT_BUNDLE_RENAMED = generateClientBundle({
2727
header: config.output.header,
28-
meta: {
29-
importFileExtension: config.output.importFileExtension,
30-
},
3128
outputPath,
3229
// @ts-expect-error
3330
plugin: client,

0 commit comments

Comments
 (0)