Creating a loading sequence of an application











up vote
1
down vote

favorite
1












I am using Webpack to create a sort of loading sequence of the application. I can simply call prepareInfosite().render() and I don't need to care about the hidden async nature of it.



export function prepareInfosite() {
return setupRenderer((resolve) => {
require.ensure(['./infosite'], (require) => resolve(require('./infosite')), 'infosite');
});
}

export function prepareLoading() {...}
export function prepareGame() {...}

const uiContainerElement = document.createElement('div');
document.body.appendChild(uiContainerElement);

function setupRenderer(resolver) {
let uiModule = null;
let rendered = false;
const renderArgs = [uiContainerElement];

function executeRender() {
if (rendered === true && uiModule !== null) {
uiModule.render(...renderArgs);
}
}

resolver((resolvedUiModule) => {
uiModule = resolvedUiModule;
executeRender();
});

return {
render(...args) {
rendered = true;
renderArgs.push(...args);
executeRender();
},
};
}


Originally, the setupRenderer looked like this, but since I am using Babel, it has polyfilled the Promise and size has grown from like 6kB to 22kB (after uglify2!), which isn't very nice.



function setupRenderer(resolver) {
const promise = new Promise(resolver);
return {
render(...args) {
promise.then((uiModule) => uiModule.render(uiContainerElement, ...args));
},
};
}


Do you think there is a nicer way how to express this without any external dependency?



Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?










share|improve this question
















bumped to the homepage by Community 10 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Just as an FYI, we encountered something similar in work... you can "specify the name of the loaded file dynamically", but all files must be known ahead of time. You can use require.context for this, but you wouldn't be able to load just any file, only files that are available at compile-time.
    – Dan Pantry
    Mar 13 '16 at 11:45










  • Yes it's perfectly fine with me, my set of files is well known, I just would like to extract that require.ensurecall to a function and call that with name of file. Can you please post an example on how that can be done with context? I am quite lost in that thing yet.
    – FredyC
    Mar 13 '16 at 13:29

















up vote
1
down vote

favorite
1












I am using Webpack to create a sort of loading sequence of the application. I can simply call prepareInfosite().render() and I don't need to care about the hidden async nature of it.



export function prepareInfosite() {
return setupRenderer((resolve) => {
require.ensure(['./infosite'], (require) => resolve(require('./infosite')), 'infosite');
});
}

export function prepareLoading() {...}
export function prepareGame() {...}

const uiContainerElement = document.createElement('div');
document.body.appendChild(uiContainerElement);

function setupRenderer(resolver) {
let uiModule = null;
let rendered = false;
const renderArgs = [uiContainerElement];

function executeRender() {
if (rendered === true && uiModule !== null) {
uiModule.render(...renderArgs);
}
}

resolver((resolvedUiModule) => {
uiModule = resolvedUiModule;
executeRender();
});

return {
render(...args) {
rendered = true;
renderArgs.push(...args);
executeRender();
},
};
}


Originally, the setupRenderer looked like this, but since I am using Babel, it has polyfilled the Promise and size has grown from like 6kB to 22kB (after uglify2!), which isn't very nice.



function setupRenderer(resolver) {
const promise = new Promise(resolver);
return {
render(...args) {
promise.then((uiModule) => uiModule.render(uiContainerElement, ...args));
},
};
}


Do you think there is a nicer way how to express this without any external dependency?



Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?










share|improve this question
















bumped to the homepage by Community 10 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Just as an FYI, we encountered something similar in work... you can "specify the name of the loaded file dynamically", but all files must be known ahead of time. You can use require.context for this, but you wouldn't be able to load just any file, only files that are available at compile-time.
    – Dan Pantry
    Mar 13 '16 at 11:45










  • Yes it's perfectly fine with me, my set of files is well known, I just would like to extract that require.ensurecall to a function and call that with name of file. Can you please post an example on how that can be done with context? I am quite lost in that thing yet.
    – FredyC
    Mar 13 '16 at 13:29















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I am using Webpack to create a sort of loading sequence of the application. I can simply call prepareInfosite().render() and I don't need to care about the hidden async nature of it.



export function prepareInfosite() {
return setupRenderer((resolve) => {
require.ensure(['./infosite'], (require) => resolve(require('./infosite')), 'infosite');
});
}

export function prepareLoading() {...}
export function prepareGame() {...}

const uiContainerElement = document.createElement('div');
document.body.appendChild(uiContainerElement);

function setupRenderer(resolver) {
let uiModule = null;
let rendered = false;
const renderArgs = [uiContainerElement];

function executeRender() {
if (rendered === true && uiModule !== null) {
uiModule.render(...renderArgs);
}
}

resolver((resolvedUiModule) => {
uiModule = resolvedUiModule;
executeRender();
});

return {
render(...args) {
rendered = true;
renderArgs.push(...args);
executeRender();
},
};
}


Originally, the setupRenderer looked like this, but since I am using Babel, it has polyfilled the Promise and size has grown from like 6kB to 22kB (after uglify2!), which isn't very nice.



function setupRenderer(resolver) {
const promise = new Promise(resolver);
return {
render(...args) {
promise.then((uiModule) => uiModule.render(uiContainerElement, ...args));
},
};
}


Do you think there is a nicer way how to express this without any external dependency?



Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?










share|improve this question















I am using Webpack to create a sort of loading sequence of the application. I can simply call prepareInfosite().render() and I don't need to care about the hidden async nature of it.



export function prepareInfosite() {
return setupRenderer((resolve) => {
require.ensure(['./infosite'], (require) => resolve(require('./infosite')), 'infosite');
});
}

export function prepareLoading() {...}
export function prepareGame() {...}

const uiContainerElement = document.createElement('div');
document.body.appendChild(uiContainerElement);

function setupRenderer(resolver) {
let uiModule = null;
let rendered = false;
const renderArgs = [uiContainerElement];

function executeRender() {
if (rendered === true && uiModule !== null) {
uiModule.render(...renderArgs);
}
}

resolver((resolvedUiModule) => {
uiModule = resolvedUiModule;
executeRender();
});

return {
render(...args) {
rendered = true;
renderArgs.push(...args);
executeRender();
},
};
}


Originally, the setupRenderer looked like this, but since I am using Babel, it has polyfilled the Promise and size has grown from like 6kB to 22kB (after uglify2!), which isn't very nice.



function setupRenderer(resolver) {
const promise = new Promise(resolver);
return {
render(...args) {
promise.then((uiModule) => uiModule.render(uiContainerElement, ...args));
},
};
}


Do you think there is a nicer way how to express this without any external dependency?



Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?







javascript ecmascript-6 babel.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 4 '16 at 4:58









Jamal

30.2k11115226




30.2k11115226










asked Mar 13 '16 at 8:21









FredyC

1624




1624





bumped to the homepage by Community 10 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 10 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • Just as an FYI, we encountered something similar in work... you can "specify the name of the loaded file dynamically", but all files must be known ahead of time. You can use require.context for this, but you wouldn't be able to load just any file, only files that are available at compile-time.
    – Dan Pantry
    Mar 13 '16 at 11:45










  • Yes it's perfectly fine with me, my set of files is well known, I just would like to extract that require.ensurecall to a function and call that with name of file. Can you please post an example on how that can be done with context? I am quite lost in that thing yet.
    – FredyC
    Mar 13 '16 at 13:29




















  • Just as an FYI, we encountered something similar in work... you can "specify the name of the loaded file dynamically", but all files must be known ahead of time. You can use require.context for this, but you wouldn't be able to load just any file, only files that are available at compile-time.
    – Dan Pantry
    Mar 13 '16 at 11:45










  • Yes it's perfectly fine with me, my set of files is well known, I just would like to extract that require.ensurecall to a function and call that with name of file. Can you please post an example on how that can be done with context? I am quite lost in that thing yet.
    – FredyC
    Mar 13 '16 at 13:29


















Just as an FYI, we encountered something similar in work... you can "specify the name of the loaded file dynamically", but all files must be known ahead of time. You can use require.context for this, but you wouldn't be able to load just any file, only files that are available at compile-time.
– Dan Pantry
Mar 13 '16 at 11:45




Just as an FYI, we encountered something similar in work... you can "specify the name of the loaded file dynamically", but all files must be known ahead of time. You can use require.context for this, but you wouldn't be able to load just any file, only files that are available at compile-time.
– Dan Pantry
Mar 13 '16 at 11:45












Yes it's perfectly fine with me, my set of files is well known, I just would like to extract that require.ensurecall to a function and call that with name of file. Can you please post an example on how that can be done with context? I am quite lost in that thing yet.
– FredyC
Mar 13 '16 at 13:29






Yes it's perfectly fine with me, my set of files is well known, I just would like to extract that require.ensurecall to a function and call that with name of file. Can you please post an example on how that can be done with context? I am quite lost in that thing yet.
– FredyC
Mar 13 '16 at 13:29












1 Answer
1






active

oldest

votes

















up vote
0
down vote














Do you think there is a nicer way how to express this without any external dependency?




Use Babel preset to only include browsers from the last two years. In that case, the promise polyfill is not needed. For example



{
"presets": [
["env", {
"targets": {
"browsers": ["since 2016"]
}
}]
]
}



Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?




Use a wrapper function to pass the strings as arguments:



return setupRenderer((resolve) => { ensurance('./infosite', 'infosite')});


And define the function before the call:



function ensurance(path, name){
require.ensure([path], (require) => resolve(require(path)), name);
}


References




  • Babel: env preset


  • caniuse: promises







share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f122713%2fcreating-a-loading-sequence-of-an-application%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote














    Do you think there is a nicer way how to express this without any external dependency?




    Use Babel preset to only include browsers from the last two years. In that case, the promise polyfill is not needed. For example



    {
    "presets": [
    ["env", {
    "targets": {
    "browsers": ["since 2016"]
    }
    }]
    ]
    }



    Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?




    Use a wrapper function to pass the strings as arguments:



    return setupRenderer((resolve) => { ensurance('./infosite', 'infosite')});


    And define the function before the call:



    function ensurance(path, name){
    require.ensure([path], (require) => resolve(require(path)), name);
    }


    References




    • Babel: env preset


    • caniuse: promises







    share|improve this answer



























      up vote
      0
      down vote














      Do you think there is a nicer way how to express this without any external dependency?




      Use Babel preset to only include browsers from the last two years. In that case, the promise polyfill is not needed. For example



      {
      "presets": [
      ["env", {
      "targets": {
      "browsers": ["since 2016"]
      }
      }]
      ]
      }



      Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?




      Use a wrapper function to pass the strings as arguments:



      return setupRenderer((resolve) => { ensurance('./infosite', 'infosite')});


      And define the function before the call:



      function ensurance(path, name){
      require.ensure([path], (require) => resolve(require(path)), name);
      }


      References




      • Babel: env preset


      • caniuse: promises







      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote










        Do you think there is a nicer way how to express this without any external dependency?




        Use Babel preset to only include browsers from the last two years. In that case, the promise polyfill is not needed. For example



        {
        "presets": [
        ["env", {
        "targets": {
        "browsers": ["since 2016"]
        }
        }]
        ]
        }



        Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?




        Use a wrapper function to pass the strings as arguments:



        return setupRenderer((resolve) => { ensurance('./infosite', 'infosite')});


        And define the function before the call:



        function ensurance(path, name){
        require.ensure([path], (require) => resolve(require(path)), name);
        }


        References




        • Babel: env preset


        • caniuse: promises







        share|improve this answer















        Do you think there is a nicer way how to express this without any external dependency?




        Use Babel preset to only include browsers from the last two years. In that case, the promise polyfill is not needed. For example



        {
        "presets": [
        ["env", {
        "targets": {
        "browsers": ["since 2016"]
        }
        }]
        ]
        }



        Additionally, I would like to DRY up the lines with require.ensure. I am not entirely sure how this works, but is there some way how to specify name of loaded file dynamically, perhaps using context?




        Use a wrapper function to pass the strings as arguments:



        return setupRenderer((resolve) => { ensurance('./infosite', 'infosite')});


        And define the function before the call:



        function ensurance(path, name){
        require.ensure([path], (require) => resolve(require(path)), name);
        }


        References




        • Babel: env preset


        • caniuse: promises








        share|improve this answer














        share|improve this answer



        share|improve this answer








        answered May 18 at 14:59


























        community wiki





        Paul Sweatte































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f122713%2fcreating-a-loading-sequence-of-an-application%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ellipse (mathématiques)

            Quarter-circle Tiles

            Mont Emei