All Downloads are FREE. Search and download functionalities are using the official Maven repository.

js.prompto.javascript.JavaScriptModule.js Maven / Gradle / Ivy

var isNodeJs = typeof window === 'undefined' && typeof importScripts === 'undefined';
var mod = isNodeJs ? require("module") : null;
var path = isNodeJs ? require('path') : null;

function JavaScriptModule(ids) {
    this.ids = ids;
}

JavaScriptModule.prototype.toString = function() {
    var res = this.ids.join("/");
    if("js"==this.ids[this.ids.length-1]) {
        res = res.replace("/js", ".js")
    }
    return res;
}

JavaScriptModule.prototype.resolve = function() {
    var o = this.resolve_webpack();
    if(o!=null) {
        return o;
    }
    o = this.resolve_module();
    if(o!=null) {
        return o;
    }
    o = this.resolve_runtime();
    if(o!=null) {
        return o;
    }
    o = this.resolve_path("prompto");
    if(o!=null) {
        return o;
    }
    o = this.resolve_path("main");
    if(o!=null) {
        return o;
    }
    return null;
};


JavaScriptModule.prototype.resolve_webpack = function() {
    try {
        // get a copy of the path identifiers
        var ids = [].concat(this.ids);
        // drop the 'js' extension
        if(ids[ids.length-1]=="js")
            ids.pop();
        // last id is the function, forget it
        ids.pop();
        // eval root module
        var m = eval(ids.shift());
        // drill down
        ids.map(function(id) {
            m = m[id];
        })
        return m || null;
    } catch (e) {
        return null;
    }
};


JavaScriptModule.prototype.resolve_module = function() {
    try {
        var path = this.toString();
        return eval("require('" + path + "')");
    } catch (e) {
        return null;
    }
};

JavaScriptModule.prototype.resolve_runtime = function() {
    try {
        var folder = path.sep + "JavaScript-Core" + path.sep;
        var idx = module.filename.lastIndexOf(folder);
        var rootpath = module.filename.substring(0, idx + 1) + "JavaScript-Runtime" + path.sep + "src" + path.sep + "main" + path.sep;
        // for now let's assume prompto and the required module are at the same level
        var modulepath = rootpath + this.toString();
        return eval("require('" + modulepath + "')");
    } catch (e) {
        // process.stderr.write("Failed requiring " + modulepath + "\n");
        return null;
    }
};

JavaScriptModule.prototype.resolve_path = function(part) {
    try {
        var folder = path.sep + part + path.sep;
        var idx = module.filename.lastIndexOf(folder);
        var rootpath = module.filename.substring(0, idx + 1);
        // for now let's assume prompto and the required module are at the same level
        var modulepath = rootpath + this.toString();
        return eval("require('" + modulepath + "')");
    } catch (e) {
        return null;
    }
};

JavaScriptModule.prototype.toDialect = function(writer) {
    writer.append(" from module: ");
    this.ids.forEach(function(id) {
        if("js"==id) {
            writer.trimLast(1);
            writer.append('.');
        }
        writer.append(id);
        writer.append('/');
    });
    writer.trimLast(1);
}

exports.JavaScriptModule = JavaScriptModule;




© 2015 - 2025 Weber Informatics LLC | Privacy Policy