diff --git a/package-lock.json b/package-lock.json --- a/package-lock.json +++ b/package-lock.json @@ -90,7 +90,7 @@ }, "duplexer": { "version": "0.1.1", - "resolved": "http://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", + "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=", "dev": true }, @@ -135,7 +135,7 @@ "dependencies": { "tape": { "version": "2.3.3", - "resolved": "http://registry.npmjs.org/tape/-/tape-2.3.3.tgz", + "resolved": "https://registry.npmjs.org/tape/-/tape-2.3.3.tgz", "integrity": "sha1-Lnzgox3wn41oUWZKcYQuDKUFevc=", "dev": true, "requires": { @@ -295,7 +295,7 @@ }, "path-is-absolute": { "version": "1.0.1", - "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "dev": true }, @@ -307,7 +307,7 @@ }, "readable-stream": { "version": "1.1.14", - "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", "dev": true, "requires": { @@ -360,7 +360,7 @@ }, "string_decoder": { "version": "0.10.31", - "resolved": "http://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", "dev": true }, @@ -417,13 +417,13 @@ }, "through": { "version": "2.3.8", - "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", "dev": true }, "through2": { "version": "0.2.3", - "resolved": "http://registry.npmjs.org/through2/-/through2-0.2.3.tgz", + "resolved": "https://registry.npmjs.org/through2/-/through2-0.2.3.tgz", "integrity": "sha1-6zKE2k6jEbbMis42U3SKUqvyWj8=", "dev": true, "requires": { diff --git a/src/amd/ts/di/RequireJsHelper.ts b/src/amd/ts/di/RequireJsHelper.ts --- a/src/amd/ts/di/RequireJsHelper.ts +++ b/src/amd/ts/di/RequireJsHelper.ts @@ -1,12 +1,7 @@ import { Uuid } from "../Uuid"; -import { argumentNotEmptyString } from "../safe"; +import { argumentNotEmptyString, argumentNotNull } from "../safe"; import { TraceSource } from "../log/TraceSource"; -export const rjs = require; - -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; - const trace = TraceSource.get("@implab/core/di/RequireJsHelper"); export async function createContextRequire(moduleName: string): Promise { @@ -25,11 +20,21 @@ export async function createContextRequi trace.debug(`define shim ${shim}`); - return new Promise(fulfill => { + return new Promise(cb => { define(shim, ["require"], r => { trace.debug("shim resolved"); return r; }); - require([shim], fulfill); + require([shim], cb); }); } + +export function makeResolver(req: Require) { + argumentNotNull(req, "req"); + + return (name: string) => { + return new Promise((cb, eb) => { + req([name], cb, eb); + }); + }; +} diff --git a/src/cjs/ts/di/CjsConfiguration.ts b/src/cjs/ts/di/CjsConfiguration.ts new file mode 100644 diff --git a/src/cjs/tsconfig.json b/src/cjs/tsconfig.json new file mode 100644 --- /dev/null +++ b/src/cjs/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "types": [ + "@types/node" + ] + }, + "include": [ + "ts/**/*.ts" + ] +} \ No newline at end of file diff --git a/src/main/ts/di/Configuration.ts b/src/main/ts/di/Configuration.ts --- a/src/main/ts/di/Configuration.ts +++ b/src/main/ts/di/Configuration.ts @@ -20,7 +20,6 @@ import { Container } from "./Container"; import { ReferenceDescriptor } from "./ReferenceDescriptor"; import { TypeServiceDescriptor } from "./TypeServiceDescriptor"; import { FactoryServiceDescriptor } from "./FactoryServiceDescriptor"; -import { rjs, createContextRequire } from "./RequireJsHelper"; import { TraceSource } from "../log/TraceSource"; import { ConfigError } from "./ConfigError"; import { Cancellation } from "../Cancellation"; @@ -44,9 +43,7 @@ async function mapAll(data: object | any } } -interface MapOf { - [key: string]: T; -} +type Resolver = (qname: string) => any; type _key = string | number; @@ -60,7 +57,7 @@ export class Configuration { _configName: string; - _require = rjs; + _require: Resolver; constructor(container: Container) { argumentNotNull(container, container); @@ -68,37 +65,15 @@ export class Configuration { this._path = []; } - async loadConfiguration(moduleName: string, ct = Cancellation.none) { - argumentNotEmptyString(moduleName, "moduleName"); - - trace.log("loadConfiguration {0}", moduleName); - - this._configName = moduleName; - - const config = await this._loadModule(moduleName); - - this._require = await this._createContextRequire(moduleName); - - let services: ServiceMap; - - try { - services = await this._visitRegistrations(config, moduleName); - } catch (e) { - throw this._makeError(e); - } - - this._container.register(services); - } - - async applyConfiguration(data: object, contextRequire?: Require, ct = Cancellation.none) { + async applyConfiguration(data: object, resolver?: Resolver, ct = Cancellation.none) { argumentNotNull(data, "data"); trace.log("applyConfiguration"); this._configName = "$"; - if (contextRequire) - this._require = contextRequire; + if (resolver) + this._require = resolver; let services: ServiceMap; @@ -142,17 +117,11 @@ export class Configuration { async _loadModule(moduleName: string) { trace.debug("loadModule {0}", moduleName); - const m = await new Promise(fulfill => { - this._require([moduleName], fulfill); - }); + const m = await this._require(moduleName); return m; } - _createContextRequire(moduleName: string) { - return createContextRequire(moduleName); - } - async _visitRegistrations(data, name: _key) { this._enter(name); diff --git a/src/main/ts/di/Container.ts b/src/main/ts/di/Container.ts --- a/src/main/ts/di/Container.ts +++ b/src/main/ts/di/Container.ts @@ -99,7 +99,7 @@ export class Container { * The function which will be used to load a configuration or types for services. * */ - async configure(config: string | object, opts?: any, ct = Cancellation.none) { + async configure(config: string | object, opts?, ct = Cancellation.none) { const c = new Configuration(this); if (typeof (config) === "string") { diff --git a/test/js/example.js b/src/test/js/example.js rename from test/js/example.js rename to src/test/js/example.js diff --git a/test/js/mock/config1.js b/src/test/js/mock/config1.js rename from test/js/mock/config1.js rename to src/test/js/mock/config1.js diff --git a/test/js/plan.js b/src/test/js/plan.js rename from test/js/plan.js rename to src/test/js/plan.js diff --git a/test/js/trace-test.js b/src/test/js/trace-test.js rename from test/js/trace-test.js rename to src/test/js/trace-test.js diff --git a/test/ts/ActivatableTests.ts b/src/test/ts/ActivatableTests.ts rename from test/ts/ActivatableTests.ts rename to src/test/ts/ActivatableTests.ts diff --git a/test/ts/CancellationTests.ts b/src/test/ts/CancellationTests.ts rename from test/ts/CancellationTests.ts rename to src/test/ts/CancellationTests.ts diff --git a/test/ts/ContainerTests.ts b/src/test/ts/ContainerTests.ts rename from test/ts/ContainerTests.ts rename to src/test/ts/ContainerTests.ts diff --git a/test/ts/ObservableTests.ts b/src/test/ts/ObservableTests.ts rename from test/ts/ObservableTests.ts rename to src/test/ts/ObservableTests.ts diff --git a/test/ts/TestTraits.ts b/src/test/ts/TestTraits.ts rename from test/ts/TestTraits.ts rename to src/test/ts/TestTraits.ts diff --git a/test/ts/TraceSourceTests.ts b/src/test/ts/TraceSourceTests.ts rename from test/ts/TraceSourceTests.ts rename to src/test/ts/TraceSourceTests.ts diff --git a/test/ts/dummy.ts b/src/test/ts/dummy.ts rename from test/ts/dummy.ts rename to src/test/ts/dummy.ts diff --git a/test/ts/mock/Bar.ts b/src/test/ts/mock/Bar.ts rename from test/ts/mock/Bar.ts rename to src/test/ts/mock/Bar.ts diff --git a/test/ts/mock/Foo.ts b/src/test/ts/mock/Foo.ts rename from test/ts/mock/Foo.ts rename to src/test/ts/mock/Foo.ts diff --git a/test/ts/mock/MockActivationController.ts b/src/test/ts/mock/MockActivationController.ts rename from test/ts/mock/MockActivationController.ts rename to src/test/ts/mock/MockActivationController.ts diff --git a/test/ts/mock/SimpleActivatable.ts b/src/test/ts/mock/SimpleActivatable.ts rename from test/ts/mock/SimpleActivatable.ts rename to src/test/ts/mock/SimpleActivatable.ts diff --git a/test/tsconfig.json b/src/test/tsconfig.json rename from test/tsconfig.json rename to src/test/tsconfig.json diff --git a/src/tsconfig.json b/src/tsconfig.json --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -14,7 +14,8 @@ ], "rootDirs": [ "main/ts", - "amd/ts" + "amd/ts", + "cjs/ts" ], "types": [] }