requirejs / r.js

Runs RequireJS in Node and Rhino, and used to run the RequireJS optimizer
Other
2.57k stars 672 forks source link

Issue with r.js optimization of multiple modules. #554

Open Devrao opened 10 years ago

Devrao commented 10 years ago

I have created a single page application having multiple modules in it using requirejs and backbonejs combination.

I have used r.js to optimize js files. My build.js for optimization looks like

({
    appDir: './',
    baseUrl: './js',
    dir: './min',
    mainConfigFile: './js/web/main.js',
    modules: [
            { 
                name: "web/main",
                include: ['jQuery','underscore','handlebar','backbone','webRoot/router','webViews/main','datepicker'],
            },
            { 
                name: "views/web/signUp",
                exclude: ['web/main']
            }
    ],
    fileExclusionRegExp: /^(r|build)\.js$/,
    optimizeCss: 'standard',
    skipDirOptimize :true,
    optimize:'uglify',
    paths: {
        /*
        paths
        */  
    },
      shim: {
        /*
        shim configuration
        */
    }
})

I am optimizing the code into two modules

  1. main.js
  2. signUp.js

But my code fails at the following point.

require(["views/web/signUp"], function (SignUpView) {
   var signUpView = new SignUpView();
    signUpView.render();
});

signUp.js get loaded into my DOM, but I get SignUpView as undefined. Where as it works in non-optimized version very well.

I have done some workaround for now I have modified my code as follow and it worked for me.

require(["views/web/signUp"], function () {
    require(["views/web/signUp"], function (SignUpView) {
        var signUpView = new SignUpView();
        signUpView.render();
    });
});

Please suggest the changes that I need to do.

Thanks, Devrao

ooxi commented 10 years ago

Please replace views/web/signUp.js with a dummy function and post the unoptimized version (optimize: 'none') of your r.js output.

P.S.: please use code markup using js or at least, it's really hard to read your issue

Devrao commented 10 years ago

Thanks ooxi. The code I posted is unpotimized only.

Added code markup, hope you can understand issue better now.

ooxi commented 10 years ago

@Devrao I suppose the code you posted is from your main.js. Could you also post signUp.js? Maybe also the r.js console log.