##// END OF EJS Templates
separating configuration logic from Container
separating configuration logic from Container

File last commit:

r42:3a5e68edd843 di-typescript
r43:477f9b6ba67f di-typescript
Show More
RequireJsResolver.ts
100 lines | 2.9 KiB | video/mp2t | TypeScriptLexer
/ src / ts / di / RequireJsResolver.ts
cin
ported IoC container to typescript...
r34 import { ModuleResolverBase } from "./ModuleResolverBase";
import { Uuid } from "../Uuid";
import { argumentNotEmptyString } from "../safe";
import { TraceSource } from "../log/TraceSource";
cin
Container.configure sync/async tests
r42 type RequireFn = (modules: string[], cb?: (...args: any[]) => any) => void;
cin
ported IoC container to typescript...
r34
cin
Container.configure sync/async tests
r42 declare const require: RequireFn;
declare function define(name: string, modules: string[], cb?: (...args: any[]) => any, eb?: (e) => any): void;
declare function define(modules: string[], cb?: (...args: any[]) => any, eb?: (e) => any): void;
cin
ported IoC container to typescript...
r34
cin
working on IoC container
r38 interface RequireJsResolverParams {
contextRequire: (modules: string[], cb?: (...args: any[]) => any) => void;
cin
ported IoC container to typescript...
r34
cin
working on IoC container
r38 base: string;
cin
ported IoC container to typescript...
r34 }
cin
Container.configure sync/async tests
r42 const trace = TraceSource.get("@implab/core/di/RequireJsResolver");
cin
ported IoC container to typescript...
r34
export class RequireJsResolver extends ModuleResolverBase {
cin
working on IoC container
r38 _contextRequire = require;
cin
ported IoC container to typescript...
r34
cin
working on IoC container
r38 _base: string;
cin
ported IoC container to typescript...
r34
cin
Container.configure sync/async tests
r42 constructor(opts?: RequireJsResolverParams) {
cin
ported IoC container to typescript...
r34 super();
if (opts) {
if (opts.contextRequire)
this._contextRequire = opts.contextRequire;
if (opts.base) {
cin
working on IoC container
r38 if (opts.base.indexOf("./") === 0)
cin
ported IoC container to typescript...
r34 throw new Error(`A module id should be an absolute: '${opts.base}'`);
this._base = opts.base;
}
}
}
async createResolver(moduleName: string): Promise<ModuleResolverBase> {
argumentNotEmptyString(moduleName, "moduleName");
cin
Container.configure sync/async tests
r42 trace.log("createResolver({0})", moduleName);
cin
working on IoC container
r38 const parts = moduleName.split("/");
if (parts[0] === ".") {
cin
ported IoC container to typescript...
r34 if (this._base)
parts[0] = this._base;
else
throw new Error(`Can't resolve a relative module '${moduleName}'`);
}
cin
working on IoC container
r38 if (parts.length > 1)
parts.splice(-1, 1, Uuid());
cin
ported IoC container to typescript...
r34 else
parts.push(Uuid());
cin
working on IoC container
r38 const shim = parts.join("/");
cin
ported IoC container to typescript...
r34
cin
Container.configure sync/async tests
r42 trace.debug(`define shim ${shim}`);
cin
ported IoC container to typescript...
r34
cin
Container.configure sync/async tests
r42 try {
const contextRequire = await new Promise<RequireFn>(
(resolve, reject) => {
try {
define(shim, ["require"], r => {
trace.debug("shim resolved");
resolve(r);
}, reject);
require([shim]);
} catch (e) {
reject(e);
}
}
);
trace.debug("creating new moduleResolver");
return new RequireJsResolver({
base: parts.slice(0, -1).join("/"),
contextRequire
});
} catch (e) {
trace.error(e);
throw e;
}
cin
ported IoC container to typescript...
r34 }
cin
working on IoC container
r38 async loadModule(moduleName: string): Promise<object> {
cin
Container.configure sync/async tests
r42 trace.log(`loadModule(${moduleName})`);
cin
working on IoC container
r38 return new Promise<object>(
resolve => this._contextRequire.call(null, [moduleName], resolve)
cin
ported IoC container to typescript...
r34 );
}
cin
working on IoC container
r38 }