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 2 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 = /^[.]?[^.]+([.].+)$/;
Copy link
Contributor

Choose a reason for hiding this comment

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

/^[.]?[^.]+([.].+)$/.exec('aaa.js.') returns [ 'aaa.js.', '.js.', ...].
And '.js.'.match(EXTRE) returns '.js'.
Is it correct that const LONGEXTRE = /^[.]?[^.]+([.][^.]+)$/;?

Copy link
Member Author

Choose a reason for hiding this comment

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

@sttk yeah, I think your regexp is better than mine. Let me come up with some tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sttk actually, it seems the LONGEXTRE needs to capture from the first . to the end of line, including future .s.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sttk I can see what you are trying to achieve. What about using this RegExp: /^[.]?[^.]+([.].+[^.])$/ (visualized at regexper)

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've made the above changes that I suggested and added a test. Let me know what you think.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sttk your above RegExp is pretty good but it completely rejects ANY path that has double dots. I think that is incorrect.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think some of these should return proper extensions, even though there's weird double-dot syntax inside the basename.

Sure. We should change EXTRE to support double-dot.

Copy link
Contributor

Choose a reason for hiding this comment

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

@phated Which should we adopt for EXTRE?

  1. '.bbb.ccc..js'.match(/\.+[^.]+/g); // => [ '.bbb', '.ccc', '..js' ]
  2. '.bbb.ccc..js'.match(/\.[^.]*/g); // => [ '.bbb', '.ccc', '.', '.js' ]

Copy link
Member Author

Choose a reason for hiding this comment

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

@sttk I think 2 is correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sttk I just pushed changes that use your suggestion (2) and included tests. Let me know if you think that solves the issue.


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('');
});
};
13 changes: 9 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ 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');
});
});

Expand Down