Meteor Wonky Magic imports

2019-08-21 02:13发布

问题:

I've been using meteor for a little while now, and am running into an unexpected behavior I've got my file system set up like:

-workspace
    -client
    -server
    -shared
        -shared.js

Inside of shared.js I create my mongo collections:

Collection= new Mongo.Collection('collection');

I can then reference them as global variable from anywhere in my app. The interesting part is: I never once use the word 'shared' in my project, meaning, I never imported this file, I never required this file, it's loading by itself, and working without ever being told to.

I'm currently trying to replicate this in another project, to no avail. Could anyone explain why it's loading in one project, and not another? And how I might get this behavior in my second project?

After looking into it, it appears that in my second project, shared.js is never loaded at all.

回答1:

I'm guessing that your two projects were not created with the same Meteor version.

Since Meteor 1.7, new projects have lazy loading enabled by default even outside the imports/ folder.

This is made by the property mainModule inside the package.json file :

"mainModule": {
  "client": "client/main.js",
  "server": "server/main.js"
},

If you want to use the eager loading (disable the lazy loading) you have to remove the mainModule property from your package.json.


More resources here:

  • Meteor Guide to use the ES Modules: https://guide.meteor.com/structure.html#es2015-modules

  • Meteor Blog Post about 1.7: https://blog.meteor.com/meteor-1-7-and-the-evergreen-dream-a8c1270b0901