meteor-collection2 icon indicating copy to clipboard operation
meteor-collection2 copied to clipboard

export lazy

Open CaptainN opened this issue 4 years ago • 3 comments

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

CaptainN avatar Sep 02 '19 16:09 CaptainN

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 avatar Sep 25 '20 14:09 StorytellerCZ

@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()

jankapunkt avatar Jun 02 '21 08:06 jankapunkt

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.

CaptainN avatar Jun 02 '21 19:06 CaptainN

Done in https://github.com/Meteor-Community-Packages/meteor-collection2/pull/444

harryadel avatar Jan 18 '24 12:01 harryadel