EggLoader

EggLoader

new EggLoader(options)

Parameters:
Name Type Description
options Object

options

Properties
Name Type Description
baseDir String

the directory of application

app EggCore

Application instance

logger Logger

logger

[ plugins ] Object

custom plugins

Since:
  • 1.0.0
Source:

Members

appInfo :AppInfo

Since:
  • 1.0.0
Source:

ContextLoader :ContextLoader

Since:
  • 1.0.0
Source:

eggPaths :Array

All framework directories.

You can extend Application of egg, the entry point is options.app,

loader will find all directories from the prototype of Application, you should define Symbol.for('egg#eggPath') property.

// lib/example.js
const egg = require('egg');
class ExampleApplication extends egg.Application {
  constructor(options) {
    super(options);
  }

  get [Symbol.for('egg#eggPath')]() {
    return path.join(__dirname, '..');
  }
}
Since:
  • 1.0.0
Source:
See:
  • EggLoader#getEggPaths

FileLoader :FileLoader

Since:
  • 1.0.0
Source:

pkg :Object

Since:
  • 1.0.0
Source:
See:

plugins :Object

Retrieve enabled plugins

Since:
  • 1.0.0
Source:

serverEnv :String

Since:
  • 1.0.0
Source:
See:

serverScope :String

Source:
See:
  • AppInfo#serverScope

Methods

getAppInfo() → {AppInfo}

Get app info

Since:
  • 1.0.0
Source:

getHomedir() → {String}

Get home directory

Since:
  • 3.4.0
Source:

getLoadUnits() → {Array}

Get all loadUnit

loadUnit is a directory that can be loaded by EggLoader, it has the same structure. loadUnit has a path and a type(app, framework, plugin).

The order of the loadUnits:

  1. plugin
  2. framework
  3. app
Since:
  • 1.0.0
Source:

loadAgentExtend()

mixin Agent.prototype

Since:
  • 1.0.0
Source:

loadApplicationExtend()

mixin Application.prototype

Since:
  • 1.0.0
Source:

loadConfig()

Load config/config.js

Will merge config.default.js 和 config.${env}.js

Since:
  • 1.0.0
Source:

loadContextExtend()

mixin Context.prototype

Since:
  • 1.0.0
Source:

loadFile(filepath, inject) → {Object}

Load single file, will invoke when export is function

Parameters:
Name Type Attributes Description
filepath String

fullpath

inject Array <repeatable>

pass rest arguments into the function when invoke

Since:
  • 1.0.0
Source:
Example
```js
app.loader.loadFile(path.join(app.options.baseDir, 'config/router.js'));
```

loadHelperExtend()

mixin app.Helper.prototype

Since:
  • 1.0.0
Source:

loadMiddleware(opt)

Load app/middleware

app.config.xx is the options of the middleware xx that has same name as config

Parameters:
Name Type Description
opt Object

LoaderOptions

Since:
  • 1.0.0
Source:
Example
```js
// app/middleware/status.js
module.exports = function(options, app) {
  // options == app.config.status
  return function*(next) {
    yield next;
  }
}
```

loadPlugin()

Load config/plugin.js from {EggLoader#loadUnits}

plugin.js is written below

{
  'xxx-client': {
    enable: true,
    package: 'xxx-client',
    dep: [],
    env: [],
  },
  // short hand
  'rds': false,
  'depd': {
    enable: true,
    path: 'path/to/depd'
  }
}

If the plugin has path, Loader will find the module from it.

Otherwise Loader will lookup follow the order by packageName

  1. $APP_BASE/node_modules/${package}
  2. $EGG_BASE/node_modules/${package}

You can call loader.plugins that retrieve enabled plugins.

loader.plugins['xxx-client'] = {
  name: 'xxx-client',                 // the plugin name, it can be used in `dep`
  package: 'xxx-client',              // the package name of plugin
  enable: true,                       // whether enabled
  path: 'path/to/xxx-client',         // the directory of the plugin package
  dep: [],                            // the dependent plugins, you can use the plugin name
  env: [ 'local', 'unittest' ],       // specify the serverEnv that only enable the plugin in it
}

loader.allPlugins can be used when retrieve all plugins.

Since:
  • 1.0.0
Source:

loadRequestExtend()

mixin Request.prototype

Since:
  • 1.0.0
Source:

loadResponseExtend()

mixin Response.prototype

Since:
  • 1.0.0
Source:

loadRouter()

Load app/router.js

Since:
  • 1.0.0
Source:

loadService(opt)

Load app/service

Parameters:
Name Type Description
opt Object

LoaderOptions

Since:
  • 1.0.0
Source:

loadToApp(directory, property, opt)

Load files using FileLoader, inject to Application

Parameters:
Name Type Description
directory String | Array

see FileLoader

property String

see FileLoader

opt Object

see FileLoader

Since:
  • 1.0.0
Source:

loadToContext(directory, property, opt)

Load files using ContextLoader

Parameters:
Name Type Description
directory String | Array

see ContextLoader

property String

see ContextLoader

opt Object

see ContextLoader

Since:
  • 1.0.0
Source: