gulpjs / rechoir

Prepare a node environment to require files with different extensions.
MIT License
48 stars 12 forks source link

Added Multi extension support. #26

Closed alansouzati closed 5 years ago

alansouzati commented 9 years ago

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

alansouzati commented 9 years ago

This PR is intended to fix #24.

tkellen commented 9 years ago

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

alansouzati commented 9 years ago

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.

silkentrance commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 https://github.com/tkellen/js-rechoir/pull/26#issuecomment-109395384.

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

tkellen commented 9 years ago

@alansouzati Is this ready for review?

alansouzati commented 9 years ago

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

phated commented 9 years ago

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

alansouzati commented 9 years ago

Sure @phated !

phated commented 9 years ago

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

alansouzati commented 9 years ago

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

phated commented 9 years ago

@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 commented 8 years ago

@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.

phated commented 5 years ago

Superseded by #37