WillyC WillyC - 1 year ago 239
TypeScript Question

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

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

it. Even Meteor itself, I have to do
import {Meteor} from 'meteor/meteor';
. For other similar Atmosphere packages, such as
I have to also write a line like
import {Counts} from 'meteor/tmeasday:publish-counts';
. ...furthermore, even if I wanted to import
I can see no import name that will work without errors. They are listed the same way in the
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
, 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.


Answer Source

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('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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download