In this episode we’ll gonna plug in to the Cogear.JS core to extend its possibilities.

Let’s start from giving a definition to a plugin.

Plugin is a module which can hook into the systems core to change its behaviour.

Node.JS has first-party EventEmitter – a huge API surface with synchronous event emitting, magic error event and flawed memory leak detected. Cogear.JS uses small library called Emittery, which has none of that.

Cogear.JS core is built from modules. Take a look at the main script file in the repository and all modules it has plugged in.

Source of ./lib/cogear.js

module.exports = class Cogear extends Emittery {
  constructor() {
    // Set Cogear.JS as global object
    global.cogear = this;
    // Set package root
    this.baseDir = path.dirname(__dirname);
    // Load plugins

Moreover Cogear.JS main object class is globally available and extends Emittery, so events can be called throughout the entire code.

Cogear.JS as an inheritor of Emittery has two methods – on and emit. With on method you can add callback function for named event. And with emit method you call this event with or without arguments.

Read more at the Emittery docs.

Arguments can be easily fetched from context of globally available cogear instance. Or you can pass them manually.

# without arguments
  // Using context
  if(cogear.mode == 'production'){
    // action
# with arguments
  // action

To look thought all the events use search on GitHub repository. It will show all the event which you can hook into.

So what is a plugin in Cogear.JS environment? It’s a simple object with only one method required – which is called apply.

To learn more watch the video or read the docs.