File tree 5 files changed +48
-1
lines changed 5 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,20 @@ Loader.prototype.load = function(modulePath) {
18
18
return this . import_ ( url )
19
19
. then (
20
20
mod => mod . default ,
21
- e => Promise . reject ( fixupImportException ( e , modulePath ) )
21
+ e => {
22
+ if ( e . code === 'ERR_UNKNOWN_FILE_EXTENSION' ) {
23
+ // Extension isn't supported by import, e.g. .jsx. Fall back to
24
+ // require(). This could lead to confusing error messages if someone
25
+ // tries to use ES module syntax without transpiling in a file with
26
+ // an unsupported extension, but it shouldn't break anything and it
27
+ // should work well in the normal case where the file is loadable
28
+ // as a CommonJS module, either directly or with the help of a
29
+ // loader like `@babel/register`.
30
+ return this . require_ ( modulePath ) ;
31
+ } else {
32
+ return Promise . reject ( fixupImportException ( e , modulePath ) ) ;
33
+ }
34
+ }
22
35
) ;
23
36
} else {
24
37
return new Promise ( resolve => {
Original file line number Diff line number Diff line change
1
+ {
2
+ "spec_dir" : " ." ,
3
+ "spec_files" : [
4
+ " spec.jsx"
5
+ ],
6
+ "helpers" : []
7
+ }
Original file line number Diff line number Diff line change
1
+ it ( 'is a spec' , function ( ) {
2
+ } ) ;
Original file line number Diff line number Diff line change @@ -65,6 +65,11 @@ describe('Integration', function () {
65
65
expect ( await runJasmine ( 'spec/fixtures/js-loader-default' ) ) . toBeSuccess ( ) ;
66
66
} ) ;
67
67
68
+ it ( 'falls back to require when loading extensions that import does not support' , async function ( ) {
69
+ expect ( await runJasmine ( 'spec/fixtures/import-jsx' ) ) . toBeSuccess ( ) ;
70
+ } ) ;
71
+
72
+
68
73
it ( 'handles load-time exceptions from CommonJS specs properly' , async function ( ) {
69
74
const { exitCode, output} = await runJasmine ( 'spec/fixtures/cjs-load-exception' ) ;
70
75
expect ( exitCode ) . toEqual ( 1 ) ;
Original file line number Diff line number Diff line change @@ -19,6 +19,26 @@ describe('loader', function() {
19
19
esModuleSharedExamples ( 'js' , true ) ;
20
20
} ) ;
21
21
22
+ describe ( 'When the extnesion is not supported by import()' , function ( ) {
23
+ it ( 'falls back to require()' , async function ( ) {
24
+ const error = new TypeError ( ) ;
25
+ error . code = 'ERR_UNKNOWN_FILE_EXTENSION' ;
26
+ const payload = { } ;
27
+ const requireShim = jasmine . createSpy ( 'requireShim' )
28
+ . and . returnValue ( Promise . resolve ( payload ) ) ;
29
+ const importShim = jasmine . createSpy ( 'importShim' )
30
+ . and . returnValue ( Promise . reject ( error ) ) ;
31
+ const loader = new Loader ( { requireShim, importShim} ) ;
32
+ loader . alwaysImport = true ;
33
+
34
+ const result = await loader . load ( './spec.jsx' ) ;
35
+
36
+ expect ( result ) . toBe ( payload ) ;
37
+ expect ( requireShim ) . toHaveBeenCalled ( ) ;
38
+ expect ( importShim ) . toHaveBeenCalled ( ) ;
39
+ } ) ;
40
+ } ) ;
41
+
22
42
it ( 'uses require to load JSON files' , async function ( ) {
23
43
const requireShim = jasmine . createSpy ( 'requireShim' )
24
44
. and . returnValue ( Promise . resolve ( ) ) ;
You can’t perform that action at this time.
0 commit comments