jquerynode.jsrequirejsrjsrequirejs-optimizer

Requirejs Optimizes multiple projects or pages


How can I optimize my multiple requirejs projects? For instance I have this structure below where I have two main.js in different locations/ folders,

build/
   build.js

dev/    
   index.php
    core/
      js/
       main.js
       libs/
          jquery.js


    local/
      js/
        main.js

My build/build.js

({
    // Where my HTML and JS is stored.
    appDir: "../dev/",

    // Top-level directory containing my JS:
    baseUrl: "local/view/javascript/base/",

    // Where to build the optimized project.
    dir: "../dist",

    // Define the modules to compile.
    modules: [
        {
            name: 'main',

            mainConfigFile: '../dev/local/js/main.js'
        },
        {

            name: 'main',

            mainConfigFile: '../dev/core/js/main.js'
        }
    ]
})

It keeps failing compressing or finding the dependencies, such as,

ERROR: ENOENT, no such file or directory 'c:\wamp\websitename\dist\local\js\jquery.js

My query.js is located in core/js/libs/ - whey is it looking for it in local\js?

What have I missed?


Solution

  • main appears twice in your modules setting. So you are telling r.js "please create the module main in ../dist with such and such options and also create module main in ../dist different options". How is r.js supposed to create two different modules that should be the same file in ../dist? It can't.

    You also cannot put mainConfigFile inside of a module configuration just like you do. It is a top level option so if you want to have a per-module setting you need to use override. And setting the mainConfigFile does not tell r.js what module you are in fact trying to optimize.

    Something like this seems to be what is needed on the basis of what you describe in the question:

    ({
        // Where my HTML and JS is stored.
        appDir: "../dev/",
    
        // Top-level directory containing my JS:
        baseUrl: "local/view/javascript/base/",
    
        // Where to build the optimized project.
        dir: "../dist",
    
        // Define the modules to compile.
        modules: [
            {
                // Give a unique name.
                name: 'main_local',
    
                // This module does not exist in the source so tell r.js to 
                // *create* it.
                create: true,
    
                // What we want in it.
                include: ['../dev/local/js/main.js'],
    
                // "Override" the top level mainConfigFile, just for this module.
                override: {
                    mainConfigFile: '../dev/local/js/main.js'
                }
            },
            {
                name: 'main_core',
                create: true,
                include: ['../dev/core/js/main.js'],
    
                override: {
                    mainConfigFile: '../dev/core/js/main.js'
                }
            },
        ]
    })