Importing objects from main app into a Meteor package?

Hi again,

Does anybody have a good pattern for importing objects from the main App into a Meteor package? Structure of the app is like the following:

app/
  imports/
    ui/
    fileWithVariableToExport.js
  /packages
    /myPackage
      /fileThatWillImportVariable.js

I’ve been using coding copilots, and they’ve recommended trying to attach to Node.js global variable, and to the Meteor object. But I haven’t been able to get either of those approaches to work.

The issue at hand is that the Meteor packages use a second compile chain. I’m trying to brainstorm new approaches. Stuff a Mongo Collection into the Meteor.settings? Use an import path that references the post-compile filesystem structure, rather than the pre-compile file layout?

Any ideas?

UPDATE: I’ve managed to get it working on the client, by attaching to the window object. But I can’t seem to get it working on the server side.

UPDATE: Node.js global object on the server is available in the Meteor.startup() function.

I don’t think you should do it. You should pass the objects/variables to Meteor package instead.

I wouldn’t do that. Packages are supposed to be portable, reusable. How about you make another package that contains everything you need and depend on it in both the main app and another package.
Another way is to use classes and define a class in the package. Import that class in the main app and configure the class with a configuration object.

Example:
In your package server or client or isomorphic:

function EventState () {} // when you initialize the function as a named function, you avail of "this" inside the entire class/function.
const YourConstForThePackage = new EventState()

YourConstForThePackage.Configure = (yourConfigurationWithObjectsAndFunctions) => {
// validate the configuration object (Match, check or a schema provider)
// process anything you need, here.
}

export default YourConstForThePackage

Export the file from the package’s package file for client, server or both.

Import from the package into your app and configure it either before use or in the startup.

Example:

import YourConstForThePackage from 'meteor/your_package'

YourConstForThePackage.Configure({
// pass anything you need.
})

After you passed the configuration, you continue to use the constant imported while it it contains everything you wanted.

1 Like