@@ -9,7 +9,6 @@ import mm from 'micromatch';
99import type { Tsconfig } from '../types' ;
1010import { isDirectory } from './assertion' ;
1111import { createDebugger } from './debug' ;
12- import { createLogger , type LogLevel } from './logger' ;
1312import { joinSafe , normalizeSafe } from './path' ;
1413
1514export const getFileInfo = (
@@ -42,44 +41,36 @@ export const getFileInfo = (
4241
4342const debug = createDebugger ( 'orval:file-load' ) ;
4443
45- const cache = new Map < string , { file ?: any ; error ?: any } > ( ) ;
44+ const cache = new Map < string , { file ?: string ; error ?: unknown } > ( ) ;
4645
47- export async function loadFile < File = any > (
46+ export async function loadFile (
4847 filePath ?: string ,
4948 options ?: {
5049 root ?: string ;
5150 defaultFileName ?: string ;
52- logLevel ?: LogLevel ;
53- isDefault ?: boolean ;
5451 alias ?: Record < string , string > ;
5552 tsconfig ?: Tsconfig ;
56- load ?: boolean ;
5753 } ,
5854) : Promise < {
5955 path : string ;
60- file ?: File ;
61- error ?: any ;
56+ file ?: string ;
57+ error ?: unknown ;
6258 cached ?: boolean ;
6359} > {
6460 const {
6561 root = process . cwd ( ) ,
66- isDefault = true ,
6762 defaultFileName,
68- logLevel,
6963 alias,
7064 tsconfig,
71- load = true ,
7265 } = options ?? { } ;
7366 const start = Date . now ( ) ;
7467
7568 let resolvedPath : string | undefined ;
76- let isTS = false ;
7769 let isMjs = false ;
7870
7971 if ( filePath ) {
8072 // explicit path is always resolved from cwd
8173 resolvedPath = path . resolve ( filePath ) ;
82- isTS = filePath . endsWith ( '.ts' ) ;
8374 } else if ( defaultFileName ) {
8475 // implicit file loaded from inline root (if present)
8576 // otherwise from cwd
@@ -107,7 +98,6 @@ export async function loadFile<File = any>(
10798 const tsFile = path . resolve ( root , `${ defaultFileName } .ts` ) ;
10899 if ( fs . existsSync ( tsFile ) ) {
109100 resolvedPath = tsFile ;
110- isTS = true ;
111101 }
112102 }
113103 }
@@ -136,62 +126,23 @@ export async function loadFile<File = any>(
136126 }
137127
138128 try {
139- let file : File | undefined ;
140-
141- if ( ! file && ! isTS && ! isMjs ) {
142- // 1. try to directly require the module (assuming commonjs)
143- try {
144- // clear cache in case of server restart
145- delete require . cache [ require . resolve ( resolvedPath ) ] ;
146-
147- file = require ( resolvedPath ) ;
148-
149- debug ( `cjs loaded in ${ Date . now ( ) - start } ms` ) ;
150- } catch ( error ) {
151- const ignored = new RegExp (
152- [
153- `Cannot use import statement` ,
154- `Must use import to load ES Module` ,
155- // #1635, #2050 some Node 12.x versions don't have esm detection
156- // so it throws normal syntax errors when encountering esm syntax
157- `Unexpected token` ,
158- `Unexpected identifier` ,
159- ] . join ( '|' ) ,
160- ) ;
161- //@ts -ignore
162- if ( ! ignored . test ( error . message ) ) {
163- throw error ;
164- }
165- }
166- }
167-
168- if ( ! file ) {
169- // 2. if we reach here, the file is ts or using es import syntax, or
170- // the user has type: "module" in their package.json (#917)
171- // transpile es import syntax to require syntax using rollup.
172- // lazy require rollup (it's actually in dependencies)
173- const { code } = await bundleFile (
174- resolvedPath ,
175- isMjs ,
176- root || path . dirname ( normalizeResolvedPath ) ,
177- alias ,
178- tsconfig ?. compilerOptions ,
179- ) ;
129+ const { code : file } = await bundleFile (
130+ resolvedPath ,
131+ isMjs ,
132+ root || path . dirname ( normalizeResolvedPath ) ,
133+ alias ,
134+ tsconfig ?. compilerOptions ,
135+ ) ;
180136
181- file = load
182- ? await loadFromBundledFile < File > ( resolvedPath , code , isDefault )
183- : ( code as any ) ;
184-
185- debug ( `bundled file loaded in ${ Date . now ( ) - start } ms` ) ;
186- }
137+ debug ( `bundled file loaded in ${ Date . now ( ) - start } ms` ) ;
187138
188139 cache . set ( resolvedPath , { file } ) ;
189140
190141 return {
191142 path : normalizeResolvedPath ,
192143 file,
193144 } ;
194- } catch ( error : any ) {
145+ } catch ( error ) {
195146 cache . set ( resolvedPath , { error } ) ;
196147
197148 return {
@@ -344,32 +295,6 @@ async function bundleFile(
344295 } ;
345296}
346297
347- interface NodeModuleWithCompile extends NodeModule {
348- _compile ( code : string , filename : string ) : any ;
349- }
350-
351- async function loadFromBundledFile < File = unknown > (
352- fileName : string ,
353- bundledCode : string ,
354- isDefault : boolean ,
355- ) : Promise < File > {
356- const extension = path . extname ( fileName ) ;
357- const defaultLoader = require . extensions [ extension ] ! ;
358- require . extensions [ extension ] = ( module : NodeModule , filename : string ) => {
359- if ( filename === fileName ) {
360- ( module as NodeModuleWithCompile ) . _compile ( bundledCode , filename ) ;
361- } else {
362- defaultLoader ( module , filename ) ;
363- }
364- } ;
365- // clear cache in case of server restart
366- delete require . cache [ require . resolve ( fileName ) ] ;
367- const raw = require ( fileName ) ;
368- const file = isDefault && raw . __esModule ? raw . default : raw ;
369- require . extensions [ extension ] = defaultLoader ;
370- return file ;
371- }
372-
373298export async function removeFilesAndEmptyFolders (
374299 patterns : string [ ] ,
375300 dir : string ,
0 commit comments