Frage

In my require.config I create shorthands for a couple of paths that I use regularly:

require.config({
    paths: {
        text: 'components/requirejs-text/text',
        url: 'config/url',
        List: 'modules/List/main'
        ...

Then on the individual pages (in separate script files), I instantiate a module like this:

require(['List'], function(List){ new List; }); 

My plan was to optimise all files into one file, require that and instantiate a module as in my example, but since the paths of require.config aren't really relevant anymore (Because I now only have main.build.js) how can I instantiate my modules?

UPDATE: Let me rephrase:

I'm trying to instantiate a module outside of the optimised build script, how do I do that?

War es hilfreich?

Lösung

Wherever you want to include the List module just create a new paths configuration which points to the optimized file.

require.config({
    paths: {
        'List': 'js/myApp' 
    }
});
require(['List'], function(List) {
    ...
});

Andere Tipps

Move your config into a separate location (either inline in the head, or in a JS file) that is after the require.js library. Now you can load any modules in the future and they will all be able to read the config regardless of which ones are loaded first.

i think, we have one file called appInit.js, which do instantiate our application, so after that all other modules should instantiate on requirement, and also its not good practice to instantiate modules from every module.js,

consider single instance of Application and all other instance should relate to this application as components, services etc.,

every developer has his own coding style, choose what you are comfortable with..

if you are worrying about paths of modules, after building then don't worry paths are just the name of module, and it should work, but dynamically loaded or created modules won't work any more.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top