How to import by dynamic path with Meteor?


#1

This is how I import all collection declarations with methods, fixtures and publications now:

import './news/collection.js';
import './news/methods.js';

if (Meteor.isServer) {
    import './news/server/fixtures.js';
    import './news/server/publications.js';
}

If you add some new collection, you have to write it again:

import './comments/collection.js';
import './comments/methods.js';

if (Meteor.isServer) {
    import './comments/server/fixtures.js';
    import './comments/server/publications.js';
}

When you have tons of collections you have to write it again and again. Eventually for the sake of DRY you would like to write something like this:

let collections = ['news', 'comments', ... 'everything'];

for (let collection of collections) {
  import `./${collection}/collection.js`;
  import `./${collection}/methods.js`;
  if (Meteor.isServer) {
    import `./${collection}/server/fixtures.js`;
    import `./${collection}/server/publications.js`;
  }
}

Now The Unexpected token, expected { error throws.

I searched Meteor documentation and can’t belive it: is it really no way to import something by dynamic path with Meteor?