Skip to content

Instantly share code, notes, and snippets.

@bora89
Created March 4, 2017 08:33
Show Gist options
  • Save bora89/3c8ce59d4930f57d8377a3b0a2da7940 to your computer and use it in GitHub Desktop.
Save bora89/3c8ce59d4930f57d8377a3b0a2da7940 to your computer and use it in GitHub Desktop.
/**
* Loads an app-script asynchronously.
*
* @param {String} filename Asset to load
* @see https://github.com/webpack/webpack/issues/118#issuecomment-28559053
* @see https://github.com/webpack/bundle-loader
*/
window.requireAsync = function requireAsync(filename) {
return new Promise((resolve,reject) => {
try {
require('bundle!../app/' + filename)(resolve);
} catch(err) {
reject(err);
}
});
};
const _ = require('lodash');
window.runScript = function(filename, ...args) {
return requireAsync(filename).then(module => {
if(module) {
if(_.isFunction(module)) {
return module(...args);
}
if(_.isFunction(module.default)) {
return module.default(...args);
}
}
});
};
// usage
runScript("app_menu.js",{"app_id":"io","system_type":"ecr"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment