bora89
3/4/2017 - 8:33 AM

Async react with bundle-loader https://github.com/webpack-contrib/bundle-loader/issues/18

/**
 * 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"});