clientside-require
Advanced tools
Comparing version 3.1.1 to 3.3.1
{ | ||
"name": "clientside-require", | ||
"version": "3.1.1", | ||
"version": "3.3.1", | ||
"description": "require() modules, js, css, html, and json in the browser with as little effort as loading jquery", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
131
src/index.js
@@ -0,1 +1,2 @@ | ||
var clientside_require = { // a singleton object | ||
@@ -189,7 +190,14 @@ modules_root : (typeof window.node_modules_root == "undefined")? location.origin + "/node_modules/" : window.node_modules_root, // define root; default is /node_modules/ | ||
.then((package_json)=>{ | ||
/* | ||
core functionality | ||
*/ | ||
var base_path = this.modules_root + request + "/"; | ||
var main = package_json.main; | ||
var path = base_path + main; // generate path based on the "main" data in the package json | ||
/* | ||
injection require type functionality | ||
*/ | ||
var injection_require_type = // define require mode for module; overwrites user selected and percolated injection_require_type passed as an argument to this function | ||
(typeof package_json.require_mode == "undefined" || package_json.require_mode !== "async")? "sync" : "async"; // either user package.json defines injection_require_type="async", or we assume its "sync"; | ||
var main = package_json.main; | ||
var path = this.modules_root + request + "/" + main; // generate path based on the "main" data in the package json | ||
if(injection_require_type == "sync"){ // extract dependencies from pacakge list and parsed file | ||
@@ -205,3 +213,17 @@ var module_dependencies = (typeof package_json.dependencies == "undefined")? [] : Object.keys(package_json.dependencies); // get modules this module is dependent on | ||
return Promise.all(["js", path, injection_require_type, promise_dependencies]); // promise all data to be generated | ||
/* | ||
default options functions functionality | ||
*/ | ||
var default_options_functions_path = package_json.require_options_functions; | ||
if(typeof default_options_functions_path == "undefined"){ | ||
var promise_default_options_functions = Promise.resolve({}); // empty | ||
} else { | ||
var full_path_to_options_functions = base_path + default_options_functions_path; | ||
var promise_default_options_functions = clientside_require.require(full_path_to_options_functions) // retreive the config file | ||
} | ||
/* | ||
return the data | ||
*/ | ||
return Promise.all(["js", path, injection_require_type, promise_dependencies, promise_default_options_functions]); // promise all data to be generated | ||
}) | ||
@@ -221,3 +243,3 @@ } else if(is_a_path && ["js", "json", "css", "html"].indexOf(extension) > -1){ // if its an acceptable extension and not defining a module | ||
} | ||
var promise_details = Promise.all([extension, path, injection_require_type, promise_dependencies]) | ||
var promise_details = Promise.all([extension, path, injection_require_type, promise_dependencies, {}]) | ||
} | ||
@@ -234,4 +256,4 @@ | ||
return promise_details | ||
.then(([extension, path, injection_require_type, dependencies])=>{ | ||
return {type : extension, path : path, injection_require_type : injection_require_type, dependencies : dependencies} | ||
.then(([extension, path, injection_require_type, dependencies, default_options_functions])=>{ | ||
return {type : extension, path : path, injection_require_type : injection_require_type, dependencies : dependencies, default_options_functions : default_options_functions} | ||
}) | ||
@@ -275,21 +297,71 @@ } | ||
/* | ||
options functionality | ||
package defaults functionality | ||
*/ | ||
options_functionality : { | ||
append_functions_to_promise : function(resolution_promise, options){ // options.functions functionality | ||
if(typeof options != "undefined" && typeof options.functions != "undefined"){ | ||
var blacklist = ["then", "catch", "spread"]; | ||
var function_keys = Object.keys(options.functions); | ||
for(var i = 0; i < function_keys.length; i++){ | ||
var function_key = function_keys[i]; | ||
if(blacklist.indexOf(function_key) > -1) { | ||
console.warn("functions in require(__, {functions : {} }) included a blacklisted function name : `"+key+"`. skipping this function.") | ||
package_defaults_functionality : { | ||
append_functions_to_promise : function(original_promise, promise_default_options_functions){ // options.functions functionality | ||
var promise_options_functions = promise_default_options_functions; | ||
// generate asnyc property promises rather than regular promises. Uses a proxy and a builder to do so. | ||
// https://stackoverflow.com/questions/48812252/how-to-add-properties-to-a-promise-asynchronously | ||
var unknown_properties_deferment_handler = { | ||
return_defined_target_value : function(target, prop){ | ||
var value = target[prop]; | ||
var bound_value = typeof value == 'function' ? value.bind(target) : value; // bind functions to target, as they would expect | ||
return bound_value; // return the requested name or parameters | ||
}, | ||
get: function(target, prop) { | ||
if(prop in target){ | ||
return this.return_defined_target_value(target, prop); // if the requested method or parameter is in the target object, just return it | ||
} else { | ||
var requested_function = options.functions[function_key]; | ||
resolution_promise[function_key] = requested_function; // append the function to the promise | ||
return target.promise_to_attempt_to_get_async_property(prop); | ||
} | ||
} | ||
}; | ||
class AsyncPropertyPromise { | ||
constructor(original_promise, promise_properties) { | ||
this.original_promise = original_promise; | ||
this.promise_properties = promise_properties; | ||
var proxied_self = new Proxy(this, unknown_properties_deferment_handler); | ||
return proxied_self; | ||
} | ||
then(...args) { | ||
return this.original_promise.then(...args); | ||
} | ||
catch(...args){ | ||
return this.original_promise.catch(...args); | ||
} | ||
promise_to_attempt_to_get_async_property(property){ | ||
// 1. return a function - NOTE - this assumes that any property not statically defiend is a function | ||
// 2. make that function resolve with an AsnycPropertyPromise that | ||
// a. returns the value of the property (method) if it exists | ||
// b. throws error if it does not | ||
return function(...args){ // 1 | ||
var raw_response_promise = this.promise_properties // 2 | ||
.then((loaded_properties)=>{ | ||
if(!(property in loaded_properties)){ // 2.a | ||
//console.error("property " + property + " is not defined for a required object"); | ||
throw "property " + property + " is undefined."; // throw error | ||
} | ||
var value = loaded_properties[property]; | ||
var bound_value = value.bind(this); // bind to original_promise | ||
return bound_value(...args); // evaluate and return response while passing orig arguments; see `spread` https://stackoverflow.com/a/31035825/3068233 | ||
}); | ||
var async_proxied_response_promise = this._wrap_a_promise(raw_response_promise); | ||
return async_proxied_response_promise; | ||
} | ||
} | ||
_wrap_a_promise(raw_promise){ | ||
return new this.constructor(raw_promise, this.promise_properties); | ||
} | ||
} | ||
return resolution_promise; | ||
var async_property_promise = new AsyncPropertyPromise(original_promise, promise_options_functions); | ||
return async_property_promise; | ||
}, | ||
}, | ||
/* | ||
options functionality | ||
*/ | ||
options_functionality : { | ||
extract_relative_path_root : function(options){ | ||
@@ -358,4 +430,4 @@ if(typeof options != "undefined" && typeof options.relative_path_root != "undefined"){ // if rel_path is defined, use it; occurs when we are in modules | ||
var promise_resolution = this.options_functionality.append_functions_to_promise(promise_resolution, options); // options.functions functionality | ||
var promise_default_options_functions = cache.default_options_functions; | ||
var promise_resolution = this.package_defaults_functionality.append_functions_to_promise(promise_resolution, promise_default_options_functions); // options.functions functionality | ||
return promise_resolution; | ||
@@ -369,5 +441,4 @@ }, | ||
*/ | ||
_cache : {promise : {}, content : {}}, | ||
promise_to_require : function(module_or_path, options){// load script into iframe to create closed namespace | ||
// TODO : handle require() requests inside of the module with caching included | ||
_cache : {promise : {}, content : {}}, // promise for async, content for sync | ||
promise_to_require : function(module_or_path, options){ | ||
var cache_path = this.options_functionality.generate_cache_path(module_or_path, options); | ||
@@ -381,2 +452,3 @@ if(typeof this._cache.promise[cache_path] == "undefined"){ // if not in cache, build into cache | ||
var promise_path = promise_request_details.then((request)=>{return request.path}); | ||
var promise_default_options_functions = promise_request_details.then((request)=>{return request.default_options_functions}); | ||
var promise_content = promise_request_details | ||
@@ -401,3 +473,3 @@ .then((request)=>{ | ||
}) | ||
this._cache.promise[cache_path] = {promise_content: promise_content, promise_path : promise_path}; // cache the promise (and consequently the result) | ||
this._cache.promise[cache_path] = {promise_content: promise_content, promise_path : promise_path, default_options_functions : promise_default_options_functions}; // cache the promise (and consequently the result) | ||
} | ||
@@ -410,7 +482,6 @@ | ||
}, | ||
synchronous_require : function(module_or_path, options){ // NOTE - synchronous_require is ONLY usable from required scripts and is automatically injected. | ||
synchronous_require : function(request, options){ | ||
// NOTE - synchronous_require is ONLY usable from required scripts and is automatically injected. | ||
// synchronous require expects all dependencies to already be loaded into cache. | ||
// console.log("requesting a synchronous_require: " + request); | ||
// console.log(this._cache.content); | ||
var cache_path = this.options_functionality.generate_cache_path(module_or_path, options); | ||
var cache_path = this.options_functionality.generate_cache_path(request, options); | ||
return this._cache.content[cache_path]; | ||
@@ -417,0 +488,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75055
449