-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cjs): organize resolver logic
- Loading branch information
1 parent
9e647a5
commit bbbf1a7
Showing
7 changed files
with
144 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import path from 'node:path'; | ||
import type { NodeError } from '../../types.js'; | ||
import type { SimpleResolve } from './types.js'; | ||
|
||
export const implicitlyResolvableExtensions = [ | ||
'.ts', | ||
'.tsx', | ||
'.jsx', | ||
] as const; | ||
|
||
const tryExtensions = ( | ||
resolve: SimpleResolve, | ||
request: string, | ||
) => { | ||
for (const extension of implicitlyResolvableExtensions) { | ||
try { | ||
return resolve(request + extension); | ||
} catch {} | ||
} | ||
}; | ||
|
||
export const createImplicitResolver = ( | ||
resolve: SimpleResolve, | ||
): SimpleResolve => (request) => { | ||
try { | ||
return resolve(request); | ||
} catch (_error) { | ||
const nodeError = _error as NodeError; | ||
if ( | ||
nodeError.code === 'MODULE_NOT_FOUND' | ||
) { | ||
const resolved = ( | ||
tryExtensions(resolve, request) | ||
|
||
// Default resolve handles resovling paths relative to the parent | ||
|| tryExtensions(resolve, `${request}${path.sep}index`) | ||
); | ||
if (resolved) { | ||
return resolved; | ||
} | ||
} | ||
|
||
throw nodeError; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type Module from 'module'; | ||
|
||
export type ResolveFilename = typeof Module._resolveFilename; | ||
|
||
export type SimpleResolve = (request: string) => string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.