Skip to content
This repository was archived by the owner on Oct 24, 2021. It is now read-only.

Commit 8ebf68f

Browse files
coagmanoabernix
authored andcommitted
Add docs for lazy mainModule (#154)
* Add docs for lazy mainModule Added info on the option `{ lazy: true }` for api.mainModule to Module docs. Is there an equivalent option for `api.addFiles` that makes them available for import without eager loading? I'd also like to propose adding the `api.mainModule` syntax to the [`Package.js`](http://docs.meteor.com/api/packagejs.html) docs, since that's where I think most people look for the Package.js api * Update modules.md
1 parent 0631660 commit 8ebf68f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

source/packages/modules.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,30 @@ Note that the `import` is `from 'meteor/my-modular-package'`, not `from 'my-modu
212212

213213
Finally, since this package is using the new `modules` package, and the package `Npm.depends` on the "moment" npm package, modules within the package can `import moment from 'moment'` on both the client and the server. This is great news, because previous versions of Meteor allowed npm imports only on the server, via `Npm.require`.
214214

215+
### Lazy loading modules from a package
216+
217+
Packages can also specify a *lazy* main module:
218+
219+
```js
220+
Package.onUse(function (api) {
221+
api.mainModule("client.js", "client", { lazy: true });
222+
});
223+
```
224+
225+
This means the `client.js` module will not be evaluated during app
226+
startup unless/until another module imports it, and will not even be
227+
included in the client bundle if no importing code is found.
228+
229+
To import a method named `exportedPackageMethod`, simply:
230+
231+
```js
232+
import { exportedPackageMethod } from "meteor/<package name>";
233+
```
234+
235+
> Note: Packages with `lazy` main modules cannot use `api.export` to export global
236+
symbols to other packages/apps. Also, prior to Meteor 1.4.4.2 it is neccessary to explicitly name the file containing the module: `import "meteor/<package name>/client.js"`.
237+
238+
215239
## Local `node_modules`
216240

217241
Before Meteor 1.3, the contents of `node_modules` directories in Meteor application code were completely ignored. When you enable `modules`, those useless `node_modules` directories suddenly become infinitely more useful:

0 commit comments

Comments
 (0)