Skip to content

Commit

Permalink
make progress on #42
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed May 20, 2018
1 parent 7454ba1 commit d0d091c
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 109 deletions.
17 changes: 13 additions & 4 deletions dist/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ module.exports = {
- handles scoping and env setup of js files (retreives content based on CommonJS exports)
- supports js, css, html, json, txt, etc
*/
var content = await this.utils.loader_functions[request_details.type](request_details.path);
var content = await this.utils.loader_functions[request_details.type](request_details.path, options);

/*
resolve with content
Expand Down Expand Up @@ -470,6 +470,7 @@ var basic_loaders = {
try {
var data = (JSON.parse(content));
} catch (err){
console.error(err);
throw (err);
}

Expand Down Expand Up @@ -533,7 +534,7 @@ var basic_loaders = require("./basic.js");
*/

module.exports = {
promise_to_retreive_exports : async function(path){
promise_to_retreive_exports : async function(path, options){
//create frame and define environmental variables
var frame = await this.helpers.promise_to_create_frame();

Expand All @@ -546,9 +547,16 @@ module.exports = {
this.provision.browser_variables(frame, path);
this.provision.commonjs_variables(frame, require_function);

// silence console.errors while loading js if log_loading_errors = false;
if(options.log_loading_errors === false) frame.contentWindow.console.error = function(){}; // empty function which does nothing;
console.error('test'); // test to ensure console.error still works

// load the javascript into the environment
await this.helpers.load_module_into_frame(path, frame);

// return console.errors after loading if log_loading_errors = false;
frame.contentWindow.console.error = window.console.error;

// extract the CommonJS-specified exports
var exports = await this.helpers.extract_exports_from_frame(frame);

Expand All @@ -569,7 +577,7 @@ module.exports = {
frame.contentWindow.root_window = window; // pass the root window (browser window) to the module so it can use it if needed
frame.contentWindow.load = load_function; // inject the load function
},
browser_variables : function(frame, path){ // browser environment variables (those not present in iframes)
browser_variables : function(frame, path, log_loading_errors){ // browser environment variables (those not present in iframes)
frame.contentWindow.console = window.console; // pass the console functionality
frame.contentWindow.alert = window.alert; // pass the alert functionality
frame.contentWindow.confirm = window.confirm; // pass the confirm functionality
Expand All @@ -588,6 +596,7 @@ module.exports = {
hostname : anchor.hostname,
port : anchor.port,
pathname : anchor.pathname,
pathdir : anchor.pathname.substring(0, anchor.pathname.lastIndexOf("/")) + "/", // path to this file without the filename
};
},
commonjs_variables : function(frame, require_function){ // CommonJS environment variables
Expand Down Expand Up @@ -657,7 +666,7 @@ var commonjs_loader = require("./commonjs.js");
TODO: find way to preserve scope with css styles
*/
module.exports = {
js : function(path){ return commonjs_loader.promise_to_retreive_exports(path) },
js : function(path, options){ return commonjs_loader.promise_to_retreive_exports(path, options) },
json : function(path){ return basic_loaders.promise_to_retreive_json(path) },
html : function(path){ return basic_loaders.promise_to_get_content_from_file(path) },
css : function(path){ return basic_loaders.promise_to_load_css_into_document(path) },
Expand Down
2 changes: 1 addition & 1 deletion src/retreive.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = {
- handles scoping and env setup of js files (retreives content based on CommonJS exports)
- supports js, css, html, json, txt, etc
*/
var content = await this.utils.loader_functions[request_details.type](request_details.path);
var content = await this.utils.loader_functions[request_details.type](request_details.path, options);

/*
resolve with content
Expand Down
46 changes: 44 additions & 2 deletions src/utilities/content_loading/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var basic_loaders = require("./basic.js");
*/

module.exports = {
promise_to_retreive_exports : async function(path){
promise_to_retreive_exports : async function(path, options){
//create frame and define environmental variables
var frame = await this.helpers.promise_to_create_frame();

Expand All @@ -36,9 +36,16 @@ module.exports = {
this.provision.browser_variables(frame, path);
this.provision.commonjs_variables(frame, require_function);

// silence console.errors while loading js if log_loading_errors = false;
if(options.log_loading_errors === false) frame.contentWindow.console.error = function(){}; // empty function which does nothing;
console.error('test'); // test to ensure console.error still works

// load the javascript into the environment
await this.helpers.load_module_into_frame(path, frame);

// return console.errors after loading if log_loading_errors = false;
frame.contentWindow.console.error = window.console.error;

// extract the CommonJS-specified exports
var exports = await this.helpers.extract_exports_from_frame(frame);

Expand All @@ -59,7 +66,7 @@ module.exports = {
frame.contentWindow.root_window = window; // pass the root window (browser window) to the module so it can use it if needed
frame.contentWindow.load = load_function; // inject the load function
},
browser_variables : function(frame, path){ // browser environment variables (those not present in iframes)
browser_variables : function(frame, path, log_loading_errors){ // browser environment variables (those not present in iframes)
frame.contentWindow.console = window.console; // pass the console functionality
frame.contentWindow.alert = window.alert; // pass the alert functionality
frame.contentWindow.confirm = window.confirm; // pass the confirm functionality
Expand Down Expand Up @@ -136,3 +143,38 @@ module.exports = {
}

}
it('should be possible to not pass loading errors when log_loading_errors = false', async function(){
this.skip(); // due to the testing environment we can not recreate the console.error log in the first place here.
// This test must be evaluated manually.

/*
setup testing environment
*/
var original_console_error_function = window.console.error;
var times_error_reported = 0;
var replacement_console_error_function = function(message){
console.log("here i am!")
times_error_reported += 1;
original_console_error_function(message);
}
window.console.error = replacement_console_error_function; // replace the function

/*
conduct the test
*/
var commonjs_loader = require(process.env.src_root + "/utilities/content_loading/commonjs.js");
try { // this will throw error since the file is not defined
var exports = await commonjs_loader.promise_to_retreive_exports(test_paths.js_commonjs + "/", {log_loading_errors:false});
} catch(error) {
console.log(error);
console.log("^^^^error^^^^")
}
console.log(times_error_reported)

/*
return state to normal
*/
window.console.error = original_console_error_function; // set back to normal
})
})
})
2 changes: 1 addition & 1 deletion src/utilities/content_loading/scoped.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var commonjs_loader = require("./commonjs.js");
TODO: find way to preserve scope with css styles
*/
module.exports = {
js : function(path){ return commonjs_loader.promise_to_retreive_exports(path) },
js : function(path, options){ return commonjs_loader.promise_to_retreive_exports(path, options) },
json : function(path){ return basic_loaders.promise_to_retreive_json(path) },
html : function(path){ return basic_loaders.promise_to_get_content_from_file(path) },
css : function(path){ return basic_loaders.promise_to_load_css_into_document(path) },
Expand Down
Loading

0 comments on commit d0d091c

Please sign in to comment.