Skip to content

Commit 5fd2b91

Browse files
committed
Fixed import of files with nonstandard extensions (e.g. jsx) in default config
* Fixes #188
1 parent 1e133d6 commit 5fd2b91

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

lib/loader.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ Loader.prototype.load = function(modulePath) {
1818
return this.import_(url)
1919
.then(
2020
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+
}
2235
);
2336
} else {
2437
return new Promise(resolve => {

spec/fixtures/import-jsx/jasmine.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"spec_dir": ".",
3+
"spec_files": [
4+
"spec.jsx"
5+
],
6+
"helpers": []
7+
}

spec/fixtures/import-jsx/spec.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
it('is a spec', function() {
2+
});

spec/integration_spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ describe('Integration', function () {
6565
expect(await runJasmine('spec/fixtures/js-loader-default')).toBeSuccess();
6666
});
6767

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+
6873
it('handles load-time exceptions from CommonJS specs properly', async function () {
6974
const {exitCode, output} = await runJasmine('spec/fixtures/cjs-load-exception');
7075
expect(exitCode).toEqual(1);

spec/loader_spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ describe('loader', function() {
1919
esModuleSharedExamples('js', true);
2020
});
2121

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+
2242
it('uses require to load JSON files', async function() {
2343
const requireShim = jasmine.createSpy('requireShim')
2444
.and.returnValue(Promise.resolve());

0 commit comments

Comments
 (0)