1.0.4 • Published 6 years ago
@reinjs/rein-loader v1.0.4
rein-loader
The framework loader of rein.
Install
npm i @reinjs/rein-loader
Usage
We can use addCompiler
to expose compilers.
const Loader = require('@reinjs/rein-loader');
const loader = new Loader();
loader.addCompiler(name, callback);
loader.addProgram(dir);
loader.compile();
addCompiler
it has two args.name
the compiler namecallback
how to build data with args ofdir
addProgram
it has one arg ofdir
which tell compiler how to build under the dir.compile
build the data method.
System Compilers
We can got some compilers easily by system.
Controller Compiler
Arguments:
options
pathname
how to build dir by arg of dir.- ...args
e.g.
loader.addControllerCompiler(Object.assign(
config.controller || {},
{
inject: this.app,
target: this.app.controller = {},
pathname: this.makePathname(scope, 'controller')
}
));
Middleware Compiler
Arguments:
options
pathname
how to build dir by arg of dir.- ...args
e.g.
loader.addMiddlewareCompiler(Object.assign(
config.middleware || {},
{
inject: this.app,
target: this.app.middleware = {},
pathname: this.makePathname(scope, 'middleware')
}
));
Service Compiler
Arguments:
options
pathname
how to build dir by arg of dir.- ...args
e.g.
loader.addServiceCompiler(Object.assign(
config.service || {},
{
inject: this.app,
target: this.app.service = {},
pathname: this.makePathname(scope, 'service')
}
));
Extend Compiler
loader.addExtendCompiler(property, file, options)
Arguments:
property
the name of extendingfilecallback
how to get dir by arg of dir?options
target
which object can been built on?inject
inject objectoverride
can override properties?originalPrototypes
original prototypes from koa or other framework
It also has four custom methods by alias name:
In KOA:
addContextCompiler
extend context.jsaddRequestCompiler
extend request.jsaddResponseCompiler
extend response.jsaddApplicationCompiler
extend application.js
this.loader
.addContextCompiler(this.extendCondition(this.app.context, scope, 'extend/context.js'))
.addRequestCompiler(this.extendCondition(this.app.request, scope, 'extend/request.js'))
.addResponseCompiler(this.extendCondition(this.app.response, scope, 'extend/response.js'))
.addApplicationCompiler(this.extendCondition(this.app, scope, 'extend/application.js'));
Message Compiler
On agent you should add this compiler for ipc sender.
this.loader.addMessageCompiler(inject, fileCallback)
;
Arguments:
inject
inject objectfileCallback
hwo to get dir by arg of dir?
this.loader.addMessageCompiler(this.app, ({ type, pathname }) => {
if (type === 'project') return path.resolve(pathname, `app/${this.app.name}.js`);
return path.resolve(pathname, 'app/message.js');
});
License
It is MIT licensed.