Created
March 4, 2017 08:33
-
-
Save bora89/3c8ce59d4930f57d8377a3b0a2da7940 to your computer and use it in GitHub Desktop.
Async react with bundle-loader
https://github.com/webpack-contrib/bundle-loader/issues/18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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