Description
bagel-module-loader
contains all module loading logic for bagel.
Installation
npm install --save bagel-module-loader
Usage
import createLoader from 'bagel-module-loader';
const load = createLoader({resolvers: [testResolver]});
const context = {foo: 'foo'};
const loaded = load('foo', __dirname, {});
Configuration Options
{
resolvers?: Array<Resolver>,
interceptors?: Array<Interceptor>,
sourceCodeTransformers?: Array<Transformer>,
wrapModule?: source: string => result: string,
generateModuleCacheKey?: GenerateModuleCacheKey,
}
Request Context
A number of the hooks in the module loader provide access to a context object which can be passed in with each call to the modoule loader. The core bagel module supplies a JobHandlerRequest as context.
({
jobRequest,
parentBatchRequest,
jobResponseMetadata,
batchResponseMetadata
}: JobHandlerRequest)
Module Resolvers
type Resolver = (
dependencyID: string,
from: string,
requestContext: {}
) => string | null;
Bagel's resolve method loops through the resolvers provided, falling back on default node resolution if none
of the provided resolvers are able to resolve the module.
Module Interceptors
type Interceptor = ({
moduleID: string,
requestContext: {[string]: any},
next: string => any
}) => any;
Interceptors allow developers to tap into the module loading process. In the process of loading a module, bagel will sequentially step through the interceptors supplied before the actual 'require' function is invoked.
These methods have access to a specific job's moduleID, requestContext
and next
. Interceptors can modify the requestContext supplied and can invoke next()
to delegate to the next interceptor in the chain. Interceptors can also short-circuit the module loading process by returning early. These two flows are illustrated in the diagram above.
Source Code Transformers
type Transformer = ({path: string, source: string}) => {
errors: Array<string>,
transformedSource: string
};
Provide a source code transformer if you would like to transform the initial module source code.
Wrap Module
Pass in your own wrapper function if you would like to customize how a module is wrapped before it is loaded.
Generate Module Cache Key
type GenerateModuleCacheKey = ({
moduleID: string,
requestContext: Object,
pathToSourceFile: string
}) => string | null;
({moduleID}) => `${moduleID}_${pretendCacheBuster}`;
bagel-module-loader
caches compiled source code. If a cache key isn't found, the source code will be loaded from disk in the course of loading the module. generateModuleCacheKey
is a method to generate these cache keys.