babel-plugin-module-resolver icon indicating copy to clipboard operation
babel-plugin-module-resolver copied to clipboard

Importing multiple modules at once

Open jarecsni opened this issue 6 years ago • 6 comments

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

jarecsni avatar Aug 18 '17 11:08 jarecsni

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

tleunen avatar Dec 11 '17 06:12 tleunen

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.

fatfisz avatar Dec 12 '17 22:12 fatfisz

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

tleunen avatar Dec 13 '17 00:12 tleunen

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

novemberborn avatar Dec 13 '17 11:12 novemberborn

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.

fatfisz avatar Dec 13 '17 11:12 fatfisz

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

novemberborn avatar Dec 13 '17 11:12 novemberborn