Meteor Wonky Magic imports

爷,独闯天下 提交于 2019-12-24 21:17: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



来源:https://stackoverflow.com/questions/51364987/meteor-wonky-magic-imports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!