##// END OF EJS Templates
tests
tests

File last commit:

r38:d3813a6cdb36 di-typescript
r41:eae7e609c38a di-typescript
Show More
RequireJsResolver.ts
74 lines | 2.0 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";
declare function require(modules: string[], cb?: (...args: any[]) => any): void;
declare function define(name: string, modules: string[], cb?: (...args: any[]) => any): void;
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 }
TraceSource.get("RequireJsResolver");
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
constructor(opts) {
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
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
working on IoC container
r38 const contextRequire = await new Promise(
resolve => define(shim, ["require"], resolve)
);
cin
ported IoC container to typescript...
r34
return new RequireJsResolver({
cin
working on IoC container
r38 base: parts.slice(0, -1).join("/"),
contextRequire
cin
ported IoC container to typescript...
r34 });
}
cin
working on IoC container
r38 async loadModule(moduleName: string): Promise<object> {
return new Promise<object>(
resolve => this._contextRequire.call(null, [moduleName], resolve)
cin
ported IoC container to typescript...
r34 );
}
cin
working on IoC container
r38 }