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

Added Multi extension support. #26

Closed

Conversation

alansouzati
Copy link
Contributor

Hi, as agreed, I'm creating the PR again to add multi extension support!

@alansouzati
Copy link
Contributor Author

This PR is intended to fix #24.

@tkellen
Copy link

tkellen commented Jun 3, 2015

@phated Any idea why the test suite is failing for babel.js?

@alansouzati alansouzati force-pushed the MULTI_EXTENSION_SUPPORT branch from 3380e69 to ed6cabb Compare June 3, 2015 20:52
@alansouzati
Copy link
Contributor Author

It was actually my fault. The supportsExtension check should iterate over exts. If we don't find on the first one, move forward. In the babel case:

1 - .babel.js is not supported by default
2 - Then we move the to next extesion: .js, which is supported
3 - Result? It tries to interpret the file as raw js, when in fact it is babel.


var supportsExtension = Object.keys(require.extensions).indexOf(exts[0]) !== -1;

if (supportsExtension) {
return true;
Copy link

Choose a reason for hiding this comment

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

You can't return here. Some extensions require further setup which is performed later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I get your point. I believe this return was here before. This is basically the same check as used to be in the master branch.

Copy link

Choose a reason for hiding this comment

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

This early return is not in master. See the configuration for babel.js here. The register method is handled by this, which is called here.

This early exit prevents any of that from happening and it also prevents exposing any errors with registering the module loader.

@silkentrance
Copy link
Contributor

Since you are asking for multiple match groups, you are also implying that there is an upper limit to the number of possible extensions any one file can have. I presume that with at most three extensions the system should work fine, e.g.

const EXTRE = /(?:(?:\.[^.]+)?((?:\.[^.]+)?(\.[^.]+)))$/;

yields

> EXTRE.exec('testing.tmp.babel.js');
[ '.tmp.babel.js',
  '.babel.js',
  '.js',
  index: 7,
  input: 'testing.tmp.babel.js' ]

> EXTRE.exec('testing.babel.js')
[ '.babel.js',
  '.js',
  '.js',
  index: 7,
  input: 'testing.babel.js' ]

> EXTRE.exec('testing.js')
[ '.js',
  '.js',
  '.js',
  index: 7,
  input: 'testing.js' ]

@tkellen
Copy link

tkellen commented Jun 5, 2015

Just grab everything from the first dot, split on dot, and do this:

['.one', '.two', '.three'].map(function (item, idx, arr) {
  return arr.slice(idx).join('');
});

...that will give you ['.one.two.three', '.two.three', '.three']

@alansouzati
Copy link
Contributor Author

Yeah, agreed. I believe this is the simplest solution. I will work on that.

On Fri, Jun 5, 2015 at 11:47 AM, Tyler Kellen notifications@github.com
wrote:

Just grab everything from the last dot, split on dot, and do this:

['.one', '.two', '.three'].map(function (item, idx, arr) {
return arr.slice(idx).join('');
});

...that will give you ['.one.two.three', '.two.three', '.three']


Reply to this email directly or view it on GitHub
#26 (comment).

Alan Souza
Software Engineer
Contact: +1 (408) 421 - 6341

@alansouzati alansouzati force-pushed the MULTI_EXTENSION_SUPPORT branch from ed6cabb to 74ff352 Compare June 5, 2015 19:28
return;
}
return extension[1];
return possibleExtensions.map(function (item, idx, arr) {
return arr.slice(idx).join('');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the insight @tkellen!

Copy link
Contributor

Choose a reason for hiding this comment

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

great solution btw!

@tkellen
Copy link

tkellen commented Jun 9, 2015

@alansouzati Is this ready for review?

@alansouzati
Copy link
Contributor Author

Sorry, let me just fix the typo in extension and it should be ready for review.

@phated
Copy link
Member

phated commented Jun 9, 2015

Also, can you add to the test a path that includes a directory with a dot in it?

@alansouzati alansouzati force-pushed the MULTI_EXTENSION_SUPPORT branch from 74ff352 to f5cc3dd Compare June 9, 2015 17:14
@alansouzati
Copy link
Contributor Author

Sure @phated !

if (Object.keys(require.extensions).indexOf(ext) !== -1) {
var exts = extension(filepath);

var supportsExtension = Object.keys(require.extensions).indexOf(exts[0]) !== -1;
Copy link
Member

Choose a reason for hiding this comment

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

i think we need to filter here and then check length, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, if you look at the validation I'm using only the first option exts[0] (e.g .babel.js). If we don't do this check we would treat .babel.js as .js files. You can check that by removing it and running the tests.

Copy link
Member

Choose a reason for hiding this comment

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

but shouldn't we check every extension to see if we support it? e.g. what if a user names something .babel.coffee for some reason

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's a good point, but I'm not sure how to solve this unless we check semantics on the file, right?

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking

var registered = exts.filter(function(ext){ return Object.keys(require.extensions).indexOf(ext) !== -1; });
if(exts.length === registered.length){
  return true
}

@tkellen am I thinking about this correctly?

Copy link
Member

Choose a reason for hiding this comment

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

actually, i don't think require.extensions will ever have something like .babel.js because node's loader doesn't work with that. hmmm, this is a tricky check

Copy link

Choose a reason for hiding this comment

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

In the context of Liftoff, it was wrong of rechoir to throw on an unknown extension. An unknown extension could very well be standard JS (as is the case for @alansouzati). I've fixed this here: 62fb2a6

We still need to use some portion of this PR to support things like tmp.babel.js, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. feel free to pick and choose whatever you need from this code. Then after that we can easily close the PR.

Fixed supportsExtension check.

Fixed extensions for multi period.

Fixing typo on the extension.

Added tests for folders with dots.
@phated
Copy link
Member

phated commented Jul 12, 2015

@alansouzati any chance you can tackle the remaining issue? I'd like to close some of the issues open related to this.

@alansouzati
Copy link
Contributor Author

I'm uncertain how to tackle the remaining issue.. any suggestions?

@phated
Copy link
Member

phated commented Jul 13, 2015

@alansouzati maybe that check can go away and we can check if the module that matches the extension is already in the require.cache - @tkellen thoughts? Someone opened this as a bug on gulp, so I'd like to get it wrapped up

@dignifiedquire
Copy link

@tkellen any chance of fixing this? I'm looking into using this module for karma config loading where I have files like karma.conf.babel.js or karma.conf.coffee or karma.config.js and this is a blocker for me.

dignifiedquire added a commit to karma-runner/karma that referenced this pull request Jun 23, 2016
This is currently blocked by gulpjs/rechoir#26 to work.

When finished this will fix #2180,#1597,#1727
@phated
Copy link
Member

phated commented Jan 2, 2019

Superseded by #37

@phated phated closed this Jan 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants