react-imported-component
react-imported-component copied to clipboard
Auto-generate imports list from Babel?
Would it not be possible to automatically update the imported-components.js file that contains all the imports during Babel traversal?
In any case, I had some trouble, it prefixed all node modules with ../. Luckily, since it's just source code I could manually adjust it.
BTW, your project is wonderful 😍 ! It worked as soon as I got the pieces in place. Great approach to the problem IMHO.
Babel has a feature about processing each file in a isolation - there is no good way to process multiple files and create some combined result as a output (or I don't know it)
But I could create babel-plugin or webpack-loader to execute scan when it hit imported-components.js. That would make things easier.
I think the scan as it is now is too primitive; it's finding more or less imports than are being used (since the actual built code can import less, or import in imports from non-src directories), plus, it has to come up with the proper name and that's also not always correct (with the ../ added to my node modules).
When the Babel plugin finds the imports, it finds all of the used ones and only the used ones, and it gets the correct name from the start.
So maybe the babel plugin should use a shared file (or even small sqlite db)? Delete the file at the start of a compile and you won't have old imports.
File path generation should be correct. I just need to add some edge cases for the resolution. (I never tried to import webpack alias or node_modules)
Yes, but even if the path generation is correct, it will still find all import statements under the given directory, even if they're not used.
That should not be a problem - it does not execute them.
Found another issue: commented imports are also added, even if they don't exist
And one more question: why are the imports generated as an object instead of a sorted array? It makes git changes bigger…
No real reasons, and I could also generate a sorter object(no keep the current API)
I think this way would not work well with caching, and next year would be a year of caching code compilation. Let's stick to the current implementation.