Why don't I have to `import` `anti:fake` into my Meteor source files?

左心房为你撑大大i 提交于 2019-12-12 23:08:47

问题


I have seen a few similar questions to this but none seem to apply to my case.

I'm creating a Meteor project and using the Atmosphere package 'anti:fake' to generate test data.

For any other package, I have to import it. Even Meteor itself, I have to do import {Meteor} from 'meteor/meteor';. For other similar Atmosphere packages, such as publish-counts I have to also write a line like import {Counts} from 'meteor/tmeasday:publish-counts';. ...furthermore, even if I wanted to import anti:fake I can see no import name that will work without errors. They are listed the same way in the packages file and were added to my project the same way so, on the surface, they all look the same.

None of this really matters as I can just magically use an object called Fake, but I don't really understand why that is the case and how I'd know in the future what I need to import explicitly and what is just implicitly imported.

Thanks!


回答1:


Meteor 1.3 introduced ES2015 Modules, however to maintain backwards compatibility Meteor still retains the "eager loading" behavior that is importing the anti:fake package for you, binding it to the global variable Fake.

import {Meteor} from 'meteor/meteor' is not actually required for the same reason, but highly recommended as best practice (and potentially could become necessary if a future release dropped this backwards compatibility by removing the eager loading).

If you look for newer tutorials that target Meteor 1.3 or newer, you will see that most code is placed in the /imports directory, as this is handled differently by the Meteor build tool, and all code in it is not 'eagerly loaded'. All code in this directory must be imported explicitly, which along with the module namespacing and avoiding the old file load order issues, give you much greater clarity about what code is running in your application (less magic!).

Also included in Meteor 1.3 was support for NPM packages, and they give you an alternative to using atmosphere packages, and packages included this way will also need to be explicitly imported into your application.

You could consider using https://www.npmjs.com/package/faker.

However don't feel like you must change if something is working for you, but hopefully this helps you understand why it is working!

Are you sure publish-counts requires the import statement? Looking at the package.js for this package shows these two lines

  api.export('Counts');
  api.export('publishCount', 'server');

This makes the publishCount object available on the server in an app (or package) that imports this package, and the Counts object available on both client and server. No import statement needed (as they are placed in the global namespace)

For more info, see the Migrating to Meteor 1.3 doc



来源:https://stackoverflow.com/questions/39604047/why-dont-i-have-to-import-antifake-into-my-meteor-source-files

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