Anatomy

The structure of a Lore application

/initializers

Initializers provide a means of injecting custom functionality into Lore without going through the full effort of creating and registering a hook. Think of them as functions that get invoked during the build and mounting process of your application, and what happens within that function is entirely up to you.

All files within this directory are assumed to be initializers (functions) and will be invoked during the startup process of your application.

What does an initializer look like?

The signature for an initializer looks like this:

export default function(lore) {
  // do something
};

When are initializers useful?

In practice, initializers have limited value, but one place they can be helpful is when needing to configure and initialize 3rd party libraries used for metrics or error reporting.

When are initializers invoked?

Initializers are invoked between the build and mounting steps of Lore's lore.summon(...) call in index.js. This means that initializers get invoked after the application is built (ensuring the config object reflects the correct environment and all hooks have been executed) but before the application is mounted to the DOM.

Example

The example below illustrates how to create an initializer that can be used to configure the Raven.js client used by Sentry, a web service that provides error reporting.

First, create a new config file called config/raven.js that will be used to hold the settings for the Raven client. We're putting these settings in a config file so we can modify them on a per-environment basis (different settings for development, production, etc).

config/raven.js

export default {

  /**
   * Sentry DSN
   */

  sentryDSN: 'https://******@sentry.io/<project>',

  /**
   * Sentry environment to report as
   */

  environment: 'development',

  /**
   * Enable or disable reporting
   */

  enabled: true

};

Next, create a new initializer called initializers/raven.js that looks like this:

initializers/raven.js

import Raven from 'raven-js';

export default function(lore) {
  const { raven } = lore.config;

  if (raven.enabled) {
    Raven.config(raven.sentryDSN, {
      environment: raven.environment
    }).install();
  }
};

If Raven is enabled for that environment, then the client will be configured for use.