File tree 2 files changed +27
-5
lines changed
2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -11,11 +11,18 @@ function Loader(options) {
11
11
12
12
Loader . prototype . load = function ( modulePath ) {
13
13
if ( ( this . alwaysImport && ! modulePath . endsWith ( '.json' ) ) || modulePath . endsWith ( '.mjs' ) ) {
14
- // The ES module spec requires import paths to be valid URLs. As of v14,
15
- // Node enforces this on Windows but not on other OSes. On OS X, import
16
- // paths that are URLs must not contain parent directory references.
17
- const url = `file://${ this . resolvePath_ ( modulePath ) } ` ;
18
- return this . import_ ( url )
14
+ let importSpecifier ;
15
+
16
+ if ( modulePath . indexOf ( '/' ) === - 1 ) {
17
+ importSpecifier = modulePath ;
18
+ } else {
19
+ // The ES module spec requires import paths to be valid URLs. As of v14,
20
+ // Node enforces this on Windows but not on other OSes. On OS X, import
21
+ // paths that are URLs must not contain parent directory references.
22
+ importSpecifier = `file://${ this . resolvePath_ ( modulePath ) } ` ;
23
+ }
24
+
25
+ return this . import_ ( importSpecifier )
19
26
. then (
20
27
mod => mod . default ,
21
28
e => {
Original file line number Diff line number Diff line change @@ -39,6 +39,21 @@ describe('loader', function() {
39
39
} ) ;
40
40
} ) ;
41
41
42
+ it ( 'imports non-local modules' , async function ( ) {
43
+ const payload = { default : { } } ;
44
+ const requireShim = jasmine . createSpy ( 'requireShim' ) ;
45
+ const importShim = jasmine . createSpy ( 'importShim' )
46
+ . and . returnValue ( Promise . resolve ( payload ) ) ;
47
+ const loader = new Loader ( { requireShim, importShim} ) ;
48
+ loader . alwaysImport = true ;
49
+
50
+ const result = await loader . load ( 'some-module' ) ;
51
+
52
+ expect ( result ) . toBe ( payload . default ) ;
53
+ expect ( requireShim ) . not . toHaveBeenCalled ( ) ;
54
+ expect ( importShim ) . toHaveBeenCalledWith ( 'some-module' ) ;
55
+ } ) ;
56
+
42
57
it ( 'uses require to load JSON files' , async function ( ) {
43
58
const requireShim = jasmine . createSpy ( 'requireShim' )
44
59
. and . returnValue ( Promise . resolve ( ) ) ;
You can’t perform that action at this time.
0 commit comments