Skip to content

Commit

Permalink
Fixed import of files with nonstandard extensions (e.g. jsx) in defau…
Browse files Browse the repository at this point in the history
…lt config

* Fixes #188
  • Loading branch information
sgravrock committed Jan 4, 2022
1 parent 1e133d6 commit 5fd2b91
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@ Loader.prototype.load = function(modulePath) {
return this.import_(url)
.then(
mod => mod.default,
e => Promise.reject(fixupImportException(e, modulePath))
e => {
if (e.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
// Extension isn't supported by import, e.g. .jsx. Fall back to
// require(). This could lead to confusing error messages if someone
// tries to use ES module syntax without transpiling in a file with
// an unsupported extension, but it shouldn't break anything and it
// should work well in the normal case where the file is loadable
// as a CommonJS module, either directly or with the help of a
// loader like `@babel/register`.
return this.require_(modulePath);
} else {
return Promise.reject(fixupImportException(e, modulePath));
}
}
);
} else {
return new Promise(resolve => {
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/import-jsx/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"spec_dir": ".",
"spec_files": [
"spec.jsx"
],
"helpers": []
}
2 changes: 2 additions & 0 deletions spec/fixtures/import-jsx/spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
it('is a spec', function() {
});
5 changes: 5 additions & 0 deletions spec/integration_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ describe('Integration', function () {
expect(await runJasmine('spec/fixtures/js-loader-default')).toBeSuccess();
});

it('falls back to require when loading extensions that import does not support', async function() {
expect(await runJasmine('spec/fixtures/import-jsx')).toBeSuccess();
});


it('handles load-time exceptions from CommonJS specs properly', async function () {
const {exitCode, output} = await runJasmine('spec/fixtures/cjs-load-exception');
expect(exitCode).toEqual(1);
Expand Down
20 changes: 20 additions & 0 deletions spec/loader_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ describe('loader', function() {
esModuleSharedExamples('js', true);
});

describe('When the extnesion is not supported by import()', function() {
it('falls back to require()', async function() {
const error = new TypeError();
error.code = 'ERR_UNKNOWN_FILE_EXTENSION';
const payload = {};
const requireShim = jasmine.createSpy('requireShim')
.and.returnValue(Promise.resolve(payload));
const importShim = jasmine.createSpy('importShim')
.and.returnValue(Promise.reject(error));
const loader = new Loader({requireShim, importShim});
loader.alwaysImport = true;

const result = await loader.load('./spec.jsx');

expect(result).toBe(payload);
expect(requireShim).toHaveBeenCalled();
expect(importShim).toHaveBeenCalled();
});
});

it('uses require to load JSON files', async function() {
const requireShim = jasmine.createSpy('requireShim')
.and.returnValue(Promise.resolve());
Expand Down

0 comments on commit 5fd2b91

Please sign in to comment.