Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config to generate file extensions for module resolution no… #644

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/openapi-ts-fetch/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './schemas.gen';
export * from './services.gen';
export * from './types.gen';
export * from './schemas.gen.js';
export * from './services.gen.js';
export * from './types.gen.js';
4 changes: 2 additions & 2 deletions examples/openapi-ts-fetch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"module": "NodeNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"moduleResolution": "nodenext",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
Expand Down
4 changes: 2 additions & 2 deletions examples/openapi-ts-fetch/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"module": "NodeNext",
"moduleResolution": "nodenext",
"allowSyntheticDefaultImports": true,
"strict": true
},
Expand Down
20 changes: 16 additions & 4 deletions packages/openapi-ts/src/compiler/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ts from 'typescript';

import { getConfig } from '../utils/config';
import {
addLeadingJSDocComment,
type Comments,
Expand All @@ -12,13 +13,18 @@ import {
* @param module - module to export from.
* @returns ts.ExportDeclaration
*/
export const createExportAllDeclaration = (module: string) =>
ts.factory.createExportDeclaration(
export const createExportAllDeclaration = (module: string) => {
const config = getConfig();
const addFileExtension = config.output.addFileExtension;
return ts.factory.createExportDeclaration(
undefined,
false,
undefined,
ots.string(module),
ots.string(
module.startsWith('./') && addFileExtension ? `${module}.js` : module,
),
);
};

type ImportExportItem = ImportExportItemObject | string;

Expand All @@ -33,6 +39,7 @@ export const createNamedExportDeclarations = (
module: string,
): ts.ExportDeclaration => {
items = Array.isArray(items) ? items : [items];

const exportedTypes = Array.isArray(items) ? items : [items];
const hasNonTypeExport = exportedTypes.some(
(item) => typeof item !== 'object' || !item.asType,
Expand Down Expand Up @@ -108,6 +115,8 @@ export const createNamedImportDeclarations = (
module: string,
): ts.ImportDeclaration => {
const importedTypes = Array.isArray(items) ? items : [items];
const config = getConfig();
const addFileExtension = config?.output.addFileExtension;
const hasNonTypeImport = importedTypes.some(
(item) => typeof item !== 'object' || !item.asType,
);
Expand All @@ -125,7 +134,10 @@ export const createNamedImportDeclarations = (
undefined,
namedBindings,
);
const moduleSpecifier = ots.string(module);

const moduleSpecifier = ots.string(
module.startsWith('./') && addFileExtension ? `${module}.js` : module,
);
const statement = ts.factory.createImportDeclaration(
undefined,
importClause,
Expand Down
5 changes: 5 additions & 0 deletions packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export interface ClientConfig {
* The relative location of the output directory
*/
path: string;

/**
* Add file extension to generated files (e.g. '.js') for moduleResolution compatibility (node16/nodenext)
*/
addFileExtension?: boolean;
};
/**
* Path to custom request file
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/test/e2e/client.axios.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import server from './scripts/server'
describe('client.axios', () => {
beforeAll(async () => {
cleanup('client/axios')
await generateClient('client/axios', 'v3', 'axios', false, 'ApiClient')
compileWithTypescript('client/axios')
await generateClient('client/axios', 'v3', 'axios', false, 'ApiClient', true)
compileWithTypescript('client/axios', {overrides: {compilerOptions: {module: 'esnext', 'moduleResolution': 'nodenext'}}})
await server.start('client/axios')
}, 40000)

Expand Down
10 changes: 6 additions & 4 deletions packages/openapi-ts/test/e2e/scripts/compileWithTypescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'

import { EOL } from 'os'
import {EOL} from 'os'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change made by a formatter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

import {
createCompilerHost,
createProgram,
Expand All @@ -11,7 +11,7 @@ import {
sys
} from 'typescript'

export const compileWithTypescript = (dir: string) => {
export const compileWithTypescript = (dir: string, {overrides}: { overrides?: any } = {}) => {
const cwd = `./test/e2e/generated/${dir}/`
const tsconfig = {
compilerOptions: {
Expand All @@ -28,9 +28,11 @@ export const compileWithTypescript = (dir: string) => {
skipLibCheck: true,
sourceMap: false,
strict: true,
target: 'es2020'
target: 'es2020',
...(overrides?.compilerOptions || {})
},
include: ['**/*.ts']
include: ['**/*.ts'],
...overrides || {},
}

// Compile files to JavaScript (ES6 modules)
Expand Down
10 changes: 7 additions & 3 deletions packages/openapi-ts/test/e2e/scripts/generateClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ export const generateClient = async (
version: string,
client: Config['client'],
useOptions: boolean = false,
name?: string
name?: string,
addFileExtension?: boolean,
) => {
await createClient({
client,
input: `./test/spec/${version}.json`,
name,
output: `./test/e2e/generated/${dir}/`,
output: {
addFileExtension,
path: `./test/e2e/generated/${dir}/`,
},
services: {
asClass: true,
},
useOptions
useOptions,
})
}
Loading