now resolving paths with npm

This commit is contained in:
Philipp Kunz 2016-10-14 03:07:37 +02:00
parent f4b8766dae
commit 87c9556064
5 changed files with 35 additions and 9 deletions

3
dist/index.d.ts vendored
View File

@ -4,13 +4,16 @@ import * as q from 'q';
/** /**
* defines a LazyModule * defines a LazyModule
*/ */
export declare type TLoader = 'npm' | 'systemjs';
export declare class LazyModule<T> { export declare class LazyModule<T> {
name: string; name: string;
nameIsPath: boolean; nameIsPath: boolean;
cwd: string; cwd: string;
whenLoaded: q.Promise<T>; whenLoaded: q.Promise<T>;
loader: TLoader;
private whenLoadedDeferred; private whenLoadedDeferred;
constructor(nameArg: string, cwdArg?: string); constructor(nameArg: string, cwdArg?: string);
setLoader(loaderArg: TLoader): void;
/** /**
* loads the module * loads the module
*/ */

18
dist/index.js vendored
View File

@ -17,11 +17,9 @@ class Smartsystem {
} }
// create the internal smartsystem // create the internal smartsystem
let smartsystem = new Smartsystem(); let smartsystem = new Smartsystem();
/**
* defines a LazyModule
*/
class LazyModule { class LazyModule {
constructor(nameArg, cwdArg = process.cwd()) { constructor(nameArg, cwdArg = process.cwd()) {
this.loader = 'npm';
this.name = nameArg; this.name = nameArg;
this.cwd = cwdArg; this.cwd = cwdArg;
smartsystem.addLazyModule(this); // add module to smartsystem instance smartsystem.addLazyModule(this); // add module to smartsystem instance
@ -32,13 +30,23 @@ class LazyModule {
this.whenLoadedDeferred = q.defer(); this.whenLoadedDeferred = q.defer();
this.whenLoaded = this.whenLoadedDeferred.promise; this.whenLoaded = this.whenLoadedDeferred.promise;
} }
setLoader(loaderArg) {
this.loader = loaderArg;
}
/** /**
* loads the module * loads the module
*/ */
load() { load() {
let done = q.defer(); let done = q.defer();
let loadedModule; let loadedModule;
systemjs.import(this.name).then((m) => { let loadingPath;
if (this.loader === 'npm') {
loadingPath = require.resolve(this.name);
}
else {
loadingPath = this.name;
}
systemjs.import(loadingPath).then((m) => {
loadedModule = m; loadedModule = m;
this.whenLoadedDeferred.resolve(loadedModule); this.whenLoadedDeferred.resolve(loadedModule);
done.resolve(loadedModule); done.resolve(loadedModule);
@ -52,4 +60,4 @@ class LazyModule {
} }
} }
exports.LazyModule = LazyModule; exports.LazyModule = LazyModule;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBRXZCLDZCQUE0QjtBQUM1Qix1QkFBc0I7QUFDdEIsNkJBQStCO0FBQy9CLElBQUksUUFBUSxHQUFHLE9BQU8sQ0FBQyxVQUFVLENBQUMsQ0FBQTtBQUVsQztJQUFBO1FBQ0ksZ0JBQVcsR0FBRyxJQUFJLGVBQVMsRUFBbUIsQ0FBQTtJQVFsRCxDQUFDO0lBTkc7O09BRUc7SUFDSCxhQUFhLENBQUMsYUFBOEI7UUFDeEMsSUFBSSxDQUFDLFdBQVcsQ0FBQyxHQUFHLENBQUMsYUFBYSxDQUFDLENBQUE7SUFDdkMsQ0FBQztDQUNKO0FBRUQsa0NBQWtDO0FBQ2xDLElBQUksV0FBVyxHQUFHLElBQUksV0FBVyxFQUFFLENBQUE7QUFFbkM7O0dBRUc7QUFDSDtJQU1JLFlBQVksT0FBZSxFQUFFLE1BQU0sR0FBVyxPQUFPLENBQUMsR0FBRyxFQUFFO1FBQ3ZELElBQUksQ0FBQyxJQUFJLEdBQUcsT0FBTyxDQUFBO1FBQ25CLElBQUksQ0FBQyxHQUFHLEdBQUcsTUFBTSxDQUFBO1FBQ2pCLFdBQVcsQ0FBQyxhQUFhLENBQUMsSUFBSSxDQUFDLENBQUEsQ0FBQyxxQ0FBcUM7UUFDckUsSUFBSSxDQUFDLFVBQVUsR0FBRyxNQUFNLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQSxDQUFDLDZCQUE2QjtRQUN0RSxFQUF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UFHLENBQUMsQ0FBQSxDQUFDLENBQUMsQ0FBQyxDQUFBO1FBQ3JDLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFBO0lBQ3ZCLENBQUM7SUFFRDs7T0FFRztJQUNILFFBQVEsQ0FBQyxHQUFHLElBQXVCO0lBRW5DLENBQUM7Q0FDSjtBQWpERCxnQ0FpREMifQ==

View File

@ -1,4 +1,4 @@
"use strict"; "use strict";
exports.exportedTestBoolean = true; exports.exportedTestBoolean = true;
console.log('moduleExample loaded successfully'); console.log('moduleExample loaded successfully');
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kdWxlRXhhbXBsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIm1vZHVsZUV4YW1wbGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFXLFFBQUEsbUJBQW1CLEdBQUcsSUFBSSxDQUFBO0FBQ3JDLE9BQU8sQ0FBQyxHQUFHLENBQUMsbUNBQW1DLENBQUMsQ0FBQSJ9 //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kdWxlRXhhbXBsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIm1vZHVsZUV4YW1wbGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFXLFFBQUEsbUJBQW1CLEdBQUcsSUFBSSxDQUFBO0FBRXJDLE9BQU8sQ0FBQyxHQUFHLENBQUMsbUNBQW1DLENBQUMsQ0FBQSJ9

View File

@ -1,2 +1,3 @@
export let exportedTestBoolean = true export let exportedTestBoolean = true
import * as q from 'q'
console.log('moduleExample loaded successfully') console.log('moduleExample loaded successfully')

View File

@ -22,11 +22,15 @@ let smartsystem = new Smartsystem()
/** /**
* defines a LazyModule * defines a LazyModule
*/ */
export type TLoader = 'npm' | 'systemjs'
export class LazyModule<T> { export class LazyModule<T> {
name: string name: string
nameIsPath: boolean nameIsPath: boolean
cwd: string cwd: string
whenLoaded: q.Promise<T> whenLoaded: q.Promise<T>
loader: TLoader = 'npm'
private whenLoadedDeferred: q.Deferred<T> private whenLoadedDeferred: q.Deferred<T>
constructor(nameArg: string, cwdArg: string = process.cwd()) { constructor(nameArg: string, cwdArg: string = process.cwd()) {
this.name = nameArg this.name = nameArg
@ -40,13 +44,23 @@ export class LazyModule<T> {
this.whenLoaded = this.whenLoadedDeferred.promise this.whenLoaded = this.whenLoadedDeferred.promise
} }
setLoader(loaderArg: TLoader) {
this.loader = loaderArg
}
/** /**
* loads the module * loads the module
*/ */
load(): q.Promise<T> { load(): q.Promise<T> {
let done = q.defer<T>() let done = q.defer<T>()
let loadedModule: 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 loadedModule = m
this.whenLoadedDeferred.resolve(loadedModule) this.whenLoadedDeferred.resolve(loadedModule)
done.resolve(loadedModule) done.resolve(loadedModule)