Lazy imports from packages, HOW?!

TL;DR How can I import only some modules from a Meteor package?


I have a package (let’s call it arggh:schemas) that contains all my document schemas (just a bunch of JS classes).

I have also divided our app into smaller mini apps, one of which only requires three of the ~30 total schemas.

How on earth should I import just those three schemas?

In the package’s package.js file, I declare the mainModule like so:

api.mainModule('index.js', ['client', 'server'], { lazy: true });

and index.js is basically a redirecting module, which re-exports all my schemas/models.

export * from './models/userModel.js';
export * from './models/paymentModel.js';
...etc

…which has the expected result: the entire package is basically gone from the client bundle. So far so good.

Next, on the client, I try to do this…

import { UserModel } from 'meteor/arggh:schemas

…and boom :fire:, all my ~30 or so schemas/models are now included in the bundle. Not what I expected.

What should I try next? Maybe changing the import statement to something like this:

import { UserModel } from 'meteor/arggh:schemas/models/userModel.js';

…and bingo!:pray: Only the UserModel gets bundled for the client. But this is not very convenient, having to remember the file structure everywhere I import those files!

Now, this information is not to be found in the guide or docs, I mostly gathered it from issue comments and the changelog.

So I’m wondering: is this the correct way to import only the files you need from the packages? I thought Meteor would be smart enough to not bundle stuff that is not actually imported, but apparently I was wrong or don’t know how to?

Yeah, Meteor doesn’t support this level of tree-shaking yet, just file level stuff.
I’ve had to do the same thing

Oh ok. I’ve been fiddling too much with Rollup lately, kinda took this for granted, it appears. I really hope Meteor gets tree-shaking…https://github.com/meteor/meteor-feature-requests/issues/206… one day, perhaps…maybe… :crossed_fingers:

From that thread, it looks like some tree-shaking is supported for npm packages?
I wonder how much and if that can be applied to packages?