Why can't we import a whole directory of files?


I’ve posted this earlier, but I think it deserves its own thread.

I think it’s a pity there is no directory import statement, in the official ES2015 spec or in Meteor otherwise. In my current project I have a lot of migrations, each in a separate file of course. An index file in the directory just imports these files: a dummy file with 20+ lines of import statements. That’s a lot of silly overhead for just including a directory of migration files. In my Rails projects, one can easily require a javascript directory in the asset pipeline.

There are NPM packages that fix these things, but these are not compatible with Meteor: “import-dir”, “import-all” and “import-all-dir”. To circumvent this, I created a shell script to index my imports directory. It writes an index file in each, which imports the files and directories under it. This way I can import directories but still customise load order. This is a solution, but I still think it’s still a problem since one has to write a tool to fix it.

What do you think about this problem and how do you cope with it?


What I think, is that you defined the problem yourself crystal clear. Let me quote:

Apparently you still want to be able to customize load order. If you could just import the directory by using something like import * from './myDirectory', then the load order would not be customizeable.

Second; If you succeeded in building the tool you need; than you could translate it to a babel build module or a meteor build package for example. Than you have the solution to your problem; and if you open it up, other people could profit from it as well.


Usually, the load order only matters in a few cases. Something like import * from './dir' would be perfect in all other cases (98%).

My “tool” is just a small script written in Ruby that creates index files for each directory in /imports. Not really useful for dynamic setups or suitable for a meteor package. I guess it’s okay if anyone wants to use it.