meteor-collection2
meteor-collection2 copied to clipboard
export lazy
Support the lazy flag, for better code splitting (dynamic-import). This is a breaking change, so it'd require a version bump, but it would also allow us to avoid packing the large dependencies (Mongo/MiniMongo, SimplSchema) in the initial bundle. These changes saved me about 200kb in my initial bundle.
I can make a PR, but there are things to discuss. Doing this would mean the package needs to be imported somewhere before MyCollection.attachSchema
is invoked. I have done it this way:
import 'meteor/aldeed:collection2'
import { Mongo } from 'meteor/mongo'
import { check } from 'meteor/check'
import SimpleSchema from 'simpl-schema'
export const PageSchema = new SimpleSchema({
title: {
type: String,
trim: false
},
slug: {
type: String
},
content: {
type: String,
trim: false
}
}, { check })
export const Pages = new Mongo.Collection('pages')
Pages.attachSchema(PageSchema)
But maybe it would be better if we exported Mongo or even Collection directly from the Collection2 package?
import { Collection } 'meteor/aldeed:collection2'
// ...
const Pages = new Collection('pages')
This is a tough one, even if we disregard backward compatibility. I would be more for the second approach as it at least reduces import statements and makes it clear that the collection has been enhanced.
@StorytellerCZ @CaptainN I have a proposal implemented ~~but I can't open the PR (it always fails from GitHub's side).~~ The branch is lazy-export. The PR is #430
The idea is to once load the module actively before the first attachSchema
is called:
import Collection2 from 'meteor/aldeed:collection2'
Collection2.load()
I believe my solution would have achieved the same thing. For code splitting, you'd need to import that everywhere that might be "split" - so before every collection creation.
Honestly, I'm moving away from Collection2, so I no longer have a strong opinion about this.
Done in https://github.com/Meteor-Community-Packages/meteor-collection2/pull/444