capaj / require-globify

transform for browserify, which allows to require files with globbing expressions
MIT License
70 stars 11 forks source link

mode: 'expand' ignores resolve functions #21

Open michael-brade opened 8 years ago

michael-brade commented 8 years ago

I am doing this:

require('./i18n/*.js', {mode: 'expand', resolve: 'strip-ext'});

but this still keeps the .js in the generated require calls. I know that not all resolve functions would make sense with expand, but this one does. I need it because later I just require('./i18n/en'), which cannot find the module.

thanks!

call-a3 commented 8 years ago

As it currently stands resolve isn't used when 'expand' is the selected mode, which makes sense because resolve would only change the names used for matched files, not their paths.

Assuming you have a directory structure of

and example.js contains

require('./i18n/*.js', {mode: 'expand'});

then require-globify will turn this into

require('./i18n/en.js'); require('./i18n/fr.js'); require('./i18n/nl.js'); ...;

Similarly, when you assign the output to a variable, e.g.

var languages = require('./i18n/*.js', {mode: 'expand'});

then require-globify will turn this into

require('./i18n/*.js', {mode: 'expand', resolve: 'strip-ext'});
var languages = [require('./i18n/en.js'),require('./i18n/fr.js'),require('./i18n/nl.js'),...];

If a module cannot be found, that would be because there is something else going on. I'm glad to help with debugging, but then I'd need a bit more info about you setup. Is there a repo/gist I can take a look at?

michael-brade commented 8 years ago

No, no need for debugging, you pretty much hit the nail on the head. It is just the filenames that need changing. What I need is a generated require that looks like this:

require('./i18n/en'); require('./i18n/fr'); require('./i18n/nl'); ...;

because later I dynamically do exactly that, too:

require(i18n_dir + 'en');  // sort of

However, that fails if browserify found the requires with the extensions. Since browserify is statically analyzing the code, you always have to require using the exact same string.

(So right now I "fixed" it by doing require(i18n_dir + 'en' + '.js');)

call-a3 commented 8 years ago

OK, I get what the problem is... I'm not sure how to fix this though... Using the resolve option for the required path seems prone to cause confusion...

michael-brade commented 8 years ago

yeah... well, you could add a mode "expand-strip" or some such as an alternative. It's not perfect either because you could ask: why have strip in mode and in resovle? But I guess something's got to give.