Skip to content

Commit 7baea4c

Browse files
authored
Support ES Module import in config file (#2510)
* feat: load config with jiti * fix: _faker as faker * test: improve test of module import in config file
1 parent 081cfd7 commit 7baea4c

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

packages/mock/src/msw/mocks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const getMockPropertiesWithoutFunc = (properties: any, spec: OpenAPIObject) =>
2323
: stringify(value as string)!;
2424

2525
acc[key] = implementation?.replaceAll(
26-
/import_faker.defaults|import_faker.faker/g,
26+
/import_faker\.defaults|import_faker\.faker|_faker\.faker/g,
2727
'faker',
2828
);
2929
return acc;
@@ -284,7 +284,7 @@ export const getMockOptionsDataOverride = (
284284
: stringify(responseOverride);
285285

286286
return implementation?.replaceAll(
287-
/import_faker.defaults|import_faker.faker/g,
287+
/import_faker\.defaults|import_faker\.faker|_faker\.faker/g,
288288
'faker',
289289
);
290290
};

packages/orval/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"execa": "^9.6.0",
9090
"find-up": "8.0.0",
9191
"fs-extra": "^11.3.2",
92+
"jiti": "^2.6.1",
9293
"js-yaml": "4.1.0",
9394
"openapi3-ts": "4.5.0",
9495
"remeda": "^2.32.0",

packages/orval/src/generate.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import process from 'node:process';
4-
import url from 'node:url';
54

65
import {
76
asyncReduce,
@@ -17,6 +16,7 @@ import {
1716
type NormalizedOptions,
1817
removeFilesAndEmptyFolders,
1918
} from '@orval/core';
19+
import { createJiti } from 'jiti';
2020

2121
import { importSpecs } from './import-specs';
2222
import { normalizeOptions } from './utils/options';
@@ -95,10 +95,14 @@ export const generateSpecs = async (
9595

9696
function findConfigFile(configFilePath?: string) {
9797
if (configFilePath) {
98-
if (!fs.existsSync(configFilePath))
98+
const absolutePath = path.isAbsolute(configFilePath)
99+
? configFilePath
100+
: path.resolve(process.cwd(), configFilePath);
101+
102+
if (!fs.existsSync(absolutePath))
99103
throw new Error(`Config file ${configFilePath} does not exist`);
100104

101-
return configFilePath;
105+
return absolutePath;
102106
}
103107

104108
const root = process.cwd();
@@ -113,21 +117,28 @@ function findConfigFile(configFilePath?: string) {
113117
throw new Error(`No config file found in ${root}`);
114118
}
115119

120+
async function loadConfigFile(configFilePath: string): Promise<ConfigExternal> {
121+
const jiti = createJiti(import.meta.url, {
122+
interopDefault: true,
123+
});
124+
125+
const module = await jiti.import(configFilePath, { default: true });
126+
127+
if (module === undefined) {
128+
throw new Error(`${configFilePath} doesn't have a default export`);
129+
}
130+
131+
return await Promise.resolve(module as ConfigExternal);
132+
}
133+
116134
export const generateConfig = async (
117135
configFile?: string,
118136
options?: GlobalOptions,
119137
) => {
120138
const configFilePath = findConfigFile(configFile);
121139
let configExternal: ConfigExternal;
122140
try {
123-
const importPath = url.pathToFileURL(configFilePath).href;
124-
const importedModule = (await import(importPath)) as {
125-
default?: ConfigExternal;
126-
};
127-
if (importedModule.default === undefined) {
128-
throw new Error(`${configFilePath} doesn't have a default export`);
129-
}
130-
configExternal = importedModule.default;
141+
configExternal = await loadConfigFile(configFilePath);
131142
} catch (error) {
132143
const errorMsg = error instanceof Error ? error.message : 'unknown error';
133144
throw new Error(`failed to load from ${configFilePath} => ${errorMsg}`);

tests/configs/multi-file.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
/* eslint-disable simple-import-sort/exports */
2+
/* eslint-disable simple-import-sort/imports */
3+
/* eslint-disable unicorn/prefer-export-from */
14
import { defineConfig } from 'orval';
25

6+
// test esm module import/export
7+
import { MY_CONST, ANOTHER_CONST } from './my-module';
8+
console.log('TESTING ES MODULE IMPORT/EXPORT', MY_CONST, ANOTHER_CONST);
9+
export { MY_CONST, ANOTHER_CONST };
10+
311
export default defineConfig({
412
api: {
513
input: '../specifications/multi-files/api.yaml',

tests/configs/my-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const MY_CONST = 42;
2+
export const ANOTHER_CONST = 'Hello, World!';

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15989,6 +15989,15 @@ __metadata:
1598915989
languageName: node
1599015990
linkType: hard
1599115991

15992+
"jiti@npm:^2.6.1":
15993+
version: 2.6.1
15994+
resolution: "jiti@npm:2.6.1"
15995+
bin:
15996+
jiti: lib/jiti-cli.mjs
15997+
checksum: 10c0/79b2e96a8e623f66c1b703b98ec1b8be4500e1d217e09b09e343471bbb9c105381b83edbb979d01cef18318cc45ce6e153571b6c83122170eefa531c64b6789b
15998+
languageName: node
15999+
linkType: hard
16000+
1599216001
"js-beautify@npm:^1.14.9":
1599316002
version: 1.15.4
1599416003
resolution: "js-beautify@npm:1.15.4"
@@ -18551,6 +18560,7 @@ __metadata:
1855118560
execa: "npm:^9.6.0"
1855218561
find-up: "npm:8.0.0"
1855318562
fs-extra: "npm:^11.3.2"
18563+
jiti: "npm:^2.6.1"
1855418564
js-yaml: "npm:4.1.0"
1855518565
openapi-types: "npm:^12.1.3"
1855618566
openapi3-ts: "npm:4.5.0"

0 commit comments

Comments
 (0)