Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

export lazy #394

Closed CaptainN closed 8 months ago

CaptainN commented 5 years ago

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')
StorytellerCZ commented 3 years ago

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.

jankapunkt commented 3 years ago

@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()
CaptainN commented 3 years ago

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.

harryadel commented 8 months ago

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