Question

I have an instance of requirejs that I want to configure in the exact same way that I configured another instance. Is there a way to get the exact config object I used out of require?

Specifically I am dynamically constructing an iframe and writing html to it. I need to inject a version of require into the iframe that is configured identically to the parent document. Is there a good way to do this?

Was it helpful?

Solution

I can think of two ways to do this:

  1. Instead of giving an object literal to require.config, put the configuration you give to RequireJS in a well known location and pass it to the multiple config calls you need to do:

    window.myapp_requirejs_config = { baseUrl: ... };
    
    require.config(window.myapp_requirejs_config);
    

    The require.config call could be repeated in different contexts and use the same object. This won't work if you have RequireJS combine configurations and want to be able to use the configuration that RequireJS uses once they are all combined. However, doing things this way does not run the risk of unexpected breakage because it relies only on documented and public parts of RequireJS.

  2. The configuration you give to RequireJS is located at requirejs.s.contexts.<context name>.config where <context name> is the name of the RequireJS context. The default context is named _ so the configuration for it would be requirejs.s.contexts._.config. However, this is not part of the public API and can change at any time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top