devongovett / babel-plugin-transform-glob-import

Import multiple files at once with globs
5 stars 4 forks source link

Support globs inside a node module folder #1

Open markhicken opened 5 years ago

markhicken commented 5 years ago

We have a need to import all files in a folder that exist in a child installed node module.

We expect this...

import intlMessages from '@scopeName/packageName/intl/*.json';

...to resolve to here...

currentWorkingDirectory/node_modules/@scopeName/packageName/intl/*.json

...but instead, it tries to resolve to here...

currentWorkingDirectory/sourceFilePath/@scopeName/packageName/intl/*.json

...which of course breaks because the path doesn't exist.

One simple fix might be to do the following in index.js...

let files = glob.sync(pattern, {strict: true, nodir: true});
if (files.length === 0) {
  pattern = 'node_modules/' + path.node.source.value;
  files = glob.sync(pattern, {strict: true, nodir: true});
}

I've tested this locally and it appears to work against relative paths, regular node module paths, and scoped npm package paths.


  1. I'm happy to submit a PR if you don't have time to make this change. Would you consider a PR if I submit one?
  2. Is there anything else we'd need to test?

Thanks!

@jhicken Thanks for the help digging into this issue!

pineaulo commented 4 years ago

Since the developer do not answer, how can we use the fixed version of the module with npm (or yarn) ?

markhicken commented 4 years ago

We just forked it and referenced my fork directly in our package.json... https://github.com/markhicken/babel-plugin-transform-glob-import

"babel-plugin-transform-glob-import": "git+https://github.com/markhicken/babel-plugin-transform-glob-import.git#glob-inside-node-module",

I just made a PR so you can see the code diff though.

pineaulo commented 4 years ago

Thank you !