63 lines
4.1 KiB
JavaScript
63 lines
4.1 KiB
JavaScript
"use strict";
|
|
require("typings-global");
|
|
const path = require("path");
|
|
const q = require("q");
|
|
const lik_1 = require("lik");
|
|
let systemjs = require('systemjs');
|
|
class Smartsystem {
|
|
constructor() {
|
|
this.lazyModules = new lik_1.Objectmap();
|
|
}
|
|
/**
|
|
* add lazyModule to Smartsystem
|
|
*/
|
|
addLazyModule(lazyModuleArg) {
|
|
this.lazyModules.add(lazyModuleArg);
|
|
}
|
|
}
|
|
// create the internal smartsystem
|
|
let smartsystem = new Smartsystem();
|
|
class LazyModule {
|
|
constructor(nameArg, cwdArg = process.cwd()) {
|
|
this.loader = 'npm';
|
|
this.name = nameArg;
|
|
this.cwd = cwdArg;
|
|
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.whenLoadedDeferred = q.defer();
|
|
this.whenLoaded = this.whenLoadedDeferred.promise;
|
|
}
|
|
setLoader(loaderArg) {
|
|
this.loader = loaderArg;
|
|
}
|
|
/**
|
|
* loads the module
|
|
*/
|
|
load() {
|
|
let done = q.defer();
|
|
let loadedModule;
|
|
let loadingPath;
|
|
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);
|
|
}).catch(err => { console.log(err); });
|
|
return done.promise;
|
|
}
|
|
/**
|
|
* loads additional lazy modules specified as arguments and returns them in the promise for easy use
|
|
*/
|
|
loadAlso(...args) {
|
|
}
|
|
}
|
|
exports.LazyModule = LazyModule;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290Ij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