3

I just started using ES6 modules and was wondering if it's bad practice if I do something like this:

pois.js:

import './methods.js';
export default Pois = new Mongo.Collection("pois");

methods.js:

import Pois from './pois.js';

Meteor.methods({
    'pois.insert' (text) {
        Pois.insert({text: text});
    }
})

This seems like a circular reference to me. Is this resolved internally? Can I leave it like that? The reason I have it that way is because methods.js depoends on pois.js and pois.js is later imported elsewhere and I want it to include all further poi related references.

Chris
  • 13,100
  • 23
  • 79
  • 162
  • It might depend on what module loader you're using, but I think circular references might cause you some problems - see http://stackoverflow.com/questions/30378226/circular-imports-with-webpack-returning-empty-object. In my opinion, they're generally something you should try to avoid when possible - even if they don't cause *technical* problems, they make the flow of your code harder to follow. – Joe Clay Apr 06 '16 at 10:29
  • At present, it doesn't look like circular dependencies are allow [by the whatwg-loader spec](https://whatwg.github.io/loader/#resolve-export). Some loader polyfills do allow it though – CodingIntrigue Apr 06 '16 at 10:43
  • @RGraham `method.js` doesn't export anything, so `ResolveExport` won't be called. I see no problem that would prevent the example from working. `method.js` uses `Pois` only at "runtime", not while the module is being loaded. AFAIK the module spec allows for cyclic dependencies. E.g.: _"If a pair consisting of specific Module Record and exportName is reached that is already in resolveSet, an import circularity has been encountered. Before recursively calling ResolveExport, a pair consisting of module and exportName is added to resolveSet"_ – a better oliver Apr 06 '16 at 11:29
  • @zeroflagL Good point about the lack of export in `method.js` - I missed that. Which spec are you quoting there, I can't find that in the linked spec – CodingIntrigue Apr 06 '16 at 12:10
  • 1
    @RGraham Sorry, the [ECMAScript spec](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-resolveexport). Compatible loaders obviously must follow this specification. – a better oliver Apr 06 '16 at 12:14

0 Answers0