0

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?

laukok
  • 47,545
  • 146
  • 388
  • 689

1 Answers1

1

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'
            }
        },
    ]
})
Louis
  • 128,628
  • 25
  • 249
  • 295
  • Thanks so much for answer Louis! :D – laukok Nov 02 '14 at 12:23
  • I still get the same error though... `ERROR: ENOENT, no such file or directory 'c:\wamp\websitename\dist\local\js\jquery.js`. I notice that it happens when `mainConfigFile: '../dev/local/js/main.js'` is moved inside `modules: []`... – laukok Nov 02 '14 at 13:37
  • I'm not sure why you mean by "moved inside `modules: []`. In your question it was *directly* inside `modules`. In my answer it is inside `override`, which makes `mainConfigFile` *indirectly* inside `modules`. Are you talking about a trial you made where you put `mainConfigFile` at the top level? – Louis Nov 02 '14 at 14:01
  • sorry, it was me put it in there first. it works ok now after moving it outside. – laukok Nov 02 '14 at 14:48