tleunen / babel-plugin-module-resolver

Custom module resolver plugin for Babel
MIT License
3.46k stars 205 forks source link

Importing multiple modules at once #211

Open jarecsni opened 7 years ago

jarecsni commented 7 years ago

Would it be possible to extend this plugin to support something like babel-plugin-import-glob? Unfortunately that plugin doesn't work with React Native.

Importing modules matching a pattern is very powerful tool, it's quite suprising it's this hard to achieve this. (There's a technique that works in Webpack, require.context, but that doesn't work with React Native packager. babel-plugin-import-glob doesn't work either, it's called in the wrong order I think).

Thanks

tleunen commented 6 years ago

We talked a bit about that plugin here #180...

A globing system is interesting. I'd definitely accept a PR to add the feature. It shouldn't be that much complicated... right @fatfisz? It's basically dynamically create root or aliases...?

fatfisz commented 6 years ago

Maybe we could detect if there is a globbing pattern and just apply an alias if it matches or otherwise the first root from the list? I can't think of anything more sensible, since a globbing pattern by its nature can match 0 files in some cases.

tleunen commented 6 years ago

Oh yeah I took a deeper look at the glob plugin. It does more than I thought since it gives you all matches.

I feel like it's outside of the scope of module-resolver. Might be best to make sure it works fine with it instead of duplicating the logic here.

@novemberborn @lukescott - Except from the comment in #180, do you know what would be required from our side to make it work properly? If it's not already the case since our recent update

novemberborn commented 6 years ago

@tleunen there is some multi-pass support in Babel 7 (or 6, even?). That might do the trick, doing one pass with import-glob and then another with module-resolver. I haven't really come across good documentation for it though.

fatfisz commented 6 years ago

The problem is that import-glob might have a path argument that should go through module-resolver, but module-resolver in turn verifies if the files exist (which in case of a glob pattern won't really work) - so it's not a problem of how Babel handles things, but a problem of conflicting goals of the two tools.

novemberborn commented 6 years ago

Oh right I wouldn't expect module-resolver to expand part of the glob pattern, no.