Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi extension support #37

Merged
merged 4 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@ exports.prepare = function (extensions, filepath, cwd, nothrow) {
var attempts = [];
var err;
var onlyErrors = true;
var ext = extension(filepath);
if (Object.keys(require.extensions).indexOf(ext) !== -1) {
var exts = extension(filepath);

var config;
var usedExtension;
if (exts) {
exts.some(function(ext) {
usedExtension = ext;
config = normalize(extensions[ext]);
return !!config;
});
}

if(Object.keys(require.extensions).indexOf(usedExtension) !== -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boolean(require.extensions[ext]) is faster.
Or is it possible that a value of an existing key is falsy?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it is possible that usedExtension is undefined and require.extensions[usedExtension] becomes require.extensions['undefined'].

Cancel the above comment, or move this line after checking config is not nullish if you will modify.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to keep like this.

return true;
}
var config = normalize(extensions[ext]);

if (!config) {
if (nothrow) {
return;
} else {
throw new Error('No module loader found for "'+ext+'".');
throw new Error('No module loader found for "'+usedExtension+'".');
}
}

if (!cwd) {
cwd = path.dirname(path.resolve(filepath));
}
Expand All @@ -45,7 +57,7 @@ exports.prepare = function (extensions, filepath, cwd, nothrow) {
}
}
if (onlyErrors) {
err = new Error('Unable to use specified module loaders for "'+ext+'".');
err = new Error('Unable to use specified module loaders for "'+usedExtension+'".');
err.failures = attempts;
if (nothrow) {
return err;
Expand Down
16 changes: 12 additions & 4 deletions lib/extension.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const path = require('path');

const EXTRE = /^[.]?[^.]+([.].*)$/;
const EXTRE = /\.[^.]*/g;
const LONGEXTRE = /^[.]?[^.]+([.].+[^.])$/;

module.exports = function (input) {
var extension = EXTRE.exec(path.basename(input));
if (!extension) {
var basename = path.basename(input);
var longExtension = LONGEXTRE.exec(basename);
if (!longExtension) {
return;
}
return extension[1];
var possibleExtensions = longExtension[1].match(EXTRE);
if (!possibleExtensions) {
return;
}
return possibleExtensions.map(function (_, idx, exts) {
return exts.slice(idx).join('');
});
};
30 changes: 26 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,32 @@ describe('rechoir', function () {
describe('extension', function () {

it('should extract extension from filename/path from the first dot', function () {
expect(extension('file.js')).to.equal('.js');
expect(extension('file.dot.js')).to.equal('.dot.js');
expect(extension('relative/path/to/file.js')).to.equal('.js');
expect(extension('relative/path/to/file.dot.js')).to.equal('.dot.js');
expect(extension('file.js')[0]).to.equal('.js');
expect(extension('file.tmp.dot.js')[0]).to.equal('.tmp.dot.js');
expect(extension('file.tmp.dot.js')[1]).to.equal('.dot.js');
expect(extension('file.tmp.dot.js')[2]).to.equal('.js');
expect(extension('relative/path/to/file.js')[0]).to.equal('.js');
expect(extension('relative/path/to/file.dot.js')[0]).to.equal('.dot.js');
expect(extension('relative/path/to/file.dot.js')[1]).to.equal('.js');
expect(extension('relative/path.with.dot/to/file.dot.js')[0]).to.equal('.dot.js');
expect(extension('relative/path.with.dot/to/file.dot.js')[1]).to.equal('.js');
});

it('does not match any if the path ends in a dot', function () {
expect(extension('file.js.')).to.equal(undefined);
});

it('treats additional dots as a separate extension', function () {
// Double
expect(extension('file.babel..js')).to.deep.equal(['.babel..js', '..js', '.js']);
expect(extension('file..babel.js')).to.deep.equal(['..babel.js', '.babel.js', '.js']);
// Triple
expect(extension('file.babel...js')).to.deep.equal(['.babel...js', '...js', '..js', '.js']);
expect(extension('file...babel.js')).to.deep.equal(['...babel.js', '..babel.js', '.babel.js', '.js']);
});

it('does not consider a leading dot to be an extension', function () {
expect(extension('.config')).to.equal(undefined);
});
});

Expand Down