now resolving paths with npm
This commit is contained in:
20
ts/index.ts
20
ts/index.ts
@ -22,11 +22,15 @@ let smartsystem = new Smartsystem()
|
||||
/**
|
||||
* defines a LazyModule
|
||||
*/
|
||||
|
||||
export type TLoader = 'npm' | 'systemjs'
|
||||
|
||||
export class LazyModule<T> {
|
||||
name: string
|
||||
nameIsPath: boolean
|
||||
cwd: string
|
||||
whenLoaded: q.Promise<T>
|
||||
loader: TLoader = 'npm'
|
||||
private whenLoadedDeferred: q.Deferred<T>
|
||||
constructor(nameArg: string, cwdArg: string = process.cwd()) {
|
||||
this.name = nameArg
|
||||
@ -34,19 +38,29 @@ export class LazyModule<T> {
|
||||
smartsystem.addLazyModule(this) // add module to smartsystem instance
|
||||
this.nameIsPath = /\.\//.test(this.name) // figure out if name is path
|
||||
if (this.nameIsPath) {
|
||||
this.name = path.join(this.cwd,this.name)
|
||||
this.name = path.join(this.cwd, this.name)
|
||||
}
|
||||
this.whenLoadedDeferred = q.defer<T>()
|
||||
this.whenLoaded = this.whenLoadedDeferred.promise
|
||||
}
|
||||
|
||||
setLoader(loaderArg: TLoader) {
|
||||
this.loader = loaderArg
|
||||
}
|
||||
|
||||
/**
|
||||
* loads the module
|
||||
*/
|
||||
load(): q.Promise<T> {
|
||||
let done = q.defer<T>()
|
||||
let loadedModule: T
|
||||
systemjs.import(this.name).then((m) => {
|
||||
let loadingPath: string
|
||||
if (this.loader === 'npm') {
|
||||
loadingPath = require.resolve(this.name)
|
||||
} else {
|
||||
loadingPath = this.name
|
||||
}
|
||||
systemjs.import(loadingPath).then((m) => {
|
||||
loadedModule = m
|
||||
this.whenLoadedDeferred.resolve(loadedModule)
|
||||
done.resolve(loadedModule)
|
||||
@ -58,6 +72,6 @@ export class LazyModule<T> {
|
||||
* loads additional lazy modules specified as arguments and returns them in the promise for easy use
|
||||
*/
|
||||
loadAlso(...args: LazyModule<any>[]) {
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user