15
15
* ImportHook,
16
16
* ImportNowHook,
17
17
* RedirectStaticModuleInterface,
18
- * StaticModuleType
18
+ * StaticModuleType,
19
+ VirtualModuleSource
19
20
* } from 'ses'
20
21
* @import {
21
22
* CompartmentDescriptor,
@@ -691,19 +692,13 @@ export function makeImportNowHookMaker(
691
692
692
693
/** @type {ImportNowHook } */
693
694
const importNowHook = moduleSpecifier => {
694
- // many dynamically-required specifiers will be absolute paths owing to use of `require.resolve()` and `path.resolve()`
695
- if ( isAbsolute ( moduleSpecifier ) ) {
696
- const record = findRedirect ( {
697
- compartmentDescriptor,
698
- compartmentDescriptors,
699
- compartments,
700
- absoluteModuleSpecifier : moduleSpecifier ,
701
- } ) ;
702
- if ( record ) {
703
- return record ;
704
- }
705
- // if there is no record found this way, we will search for it instead of considering it to be an exit module
706
- } else if ( moduleSpecifier !== '.' && ! moduleSpecifier . startsWith ( './' ) ) {
695
+ /**
696
+ * Attempt to load the moduleSpecifier via an {@link ExitModuleImportNowHook}, if one exists.
697
+ *
698
+ * If it doesn't exist, then throw an exception.
699
+ * @returns {VirtualModuleSource }
700
+ */
701
+ const tryExitModuleImportNowHook = ( ) => {
707
702
if ( exitModuleImportNowHook ) {
708
703
// This hook is responsible for ensuring that the moduleSpecifier
709
704
// actually refers to an exit module.
@@ -722,14 +717,38 @@ export function makeImportNowHookMaker(
722
717
} ) ;
723
718
return exitRecord ;
724
719
}
720
+ throw Error (
721
+ `Cannot find external module ${ q (
722
+ moduleSpecifier ,
723
+ ) } in package at ${ packageLocation } `,
724
+ ) ;
725
+ } else {
726
+ throw Error (
727
+ `Cannot find external module ${ q (
728
+ moduleSpecifier ,
729
+ ) } from package at ${ packageLocation } ; try providing an importNowHook`,
730
+ ) ;
725
731
}
726
- throw Error (
727
- `Cannot find external module ${ q (
728
- moduleSpecifier ,
729
- ) } in package ${ packageLocation } `,
730
- ) ;
732
+ } ;
733
+
734
+ // many dynamically-required specifiers will be absolute paths owing to use of `require.resolve()` and `path.resolve()`
735
+ if ( isAbsolute ( moduleSpecifier ) ) {
736
+ const record = findRedirect ( {
737
+ compartmentDescriptor,
738
+ compartmentDescriptors,
739
+ compartments,
740
+ absoluteModuleSpecifier : moduleSpecifier ,
741
+ } ) ;
742
+ if ( record ) {
743
+ return record ;
744
+ }
745
+ } else if ( moduleSpecifier !== '.' && ! moduleSpecifier . startsWith ( './' ) ) {
746
+ // could be a builtin, which means we should not bother bouncing on the trampoline to find it.
747
+ return tryExitModuleImportNowHook ( ) ;
731
748
}
732
749
750
+ // we might have an absolute path here, but it might be within the compartment, so
751
+ // we will try to find it.
733
752
const candidates = nominateCandidates ( moduleSpecifier , searchSuffixes ) ;
734
753
735
754
const record = syncTrampoline (
@@ -759,11 +778,8 @@ export function makeImportNowHookMaker(
759
778
return record ;
760
779
}
761
780
762
- throw new Error (
763
- `Could not import module: ${ q (
764
- moduleSpecifier ,
765
- ) } ; try providing an importNowHook`,
766
- ) ;
781
+ // at this point, we haven't found the module by guessing, so we'll try the importer-of-last-resort
782
+ return tryExitModuleImportNowHook ( ) ;
767
783
} ;
768
784
769
785
return importNowHook ;
0 commit comments