* Resolve the given import or export source. If the source is a filepath, it * will attempt to resolve the module relative to the given filepath.
( source: string, filepath: string, )
| 521 | * will attempt to resolve the module relative to the given filepath. |
| 522 | */ |
| 523 | function resolve( |
| 524 | source: string, |
| 525 | filepath: string, |
| 526 | ): ResolveBareModule | ResolveRelativeModule | ResolveAbsoluteModule | ResolveError { |
| 527 | if (source.startsWith('.')) { |
| 528 | const directory = path.dirname(filepath) |
| 529 | const extension = path.extname(source) |
| 530 | if (extension === '' || !extensions.includes(extension)) { |
| 531 | const candidate = extensions |
| 532 | .flatMap(extension => { |
| 533 | return [ |
| 534 | path.resolve( |
| 535 | directory, |
| 536 | path.format({ |
| 537 | name: source, |
| 538 | ext: extension, |
| 539 | }), |
| 540 | ), |
| 541 | path.resolve( |
| 542 | directory, |
| 543 | path.format({ |
| 544 | dir: source, |
| 545 | name: 'index', |
| 546 | ext: extension, |
| 547 | }), |
| 548 | ), |
| 549 | ] |
| 550 | }) |
| 551 | .find(candidate => { |
| 552 | return existsSync(candidate) |
| 553 | }) |
| 554 | if (candidate) { |
| 555 | return { |
| 556 | type: 'relative', |
| 557 | value: candidate, |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | return { |
| 562 | type: 'error', |
| 563 | error: new Error(`Unable to find file: ${source} from: ${filepath}`), |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | const resolvedPath = path.resolve(directory, source) |
| 568 | if (!existsSync(resolvedPath)) { |
| 569 | return { |
| 570 | type: 'error', |
| 571 | error: new Error(`Unable to find file: ${source} from: ${filepath}`), |
| 572 | } |
| 573 | } |
| 574 | return { |
| 575 | type: 'relative', |
| 576 | value: resolvedPath, |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (source.startsWith('/')) { |
no outgoing calls
no test coverage detected