requirejs / r.js

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

Loading optimized files with multiversion context #793

Open glenpike opened 9 years ago

glenpike commented 9 years ago

I had a problem with my multiversion context as referred to here https://github.com/jrburke/requirejs/issues/1302 which I managed to resolve by fixing my "main" file (https://github.com/jrburke/requirejs/issues/1302#issuecomment-78234529)

I am now having an issue with the optimization that I believe is due to contexts.

https://github.com/jrburke/r.js/pull/436 and associated require issue https://github.com/jrburke/requirejs/issues/726 are similar to mine.

I tried setting the initial require call in my main-dist file to use the specific context as with the main file:

reqTwo(['require', 'require-config'], function(require) {

    require.config({
        baseUrl: './dist-tmp'
    });
    require(['jquery', 'common'], function($, common) {
        //See comment in main.js
        window.$ = window.jQuery = $;
        require([
            'app'
        ],

        function(App) {
            App.start();
        });
    });
});

r.js fails to optimize that file because reqTwo is not being looked for I guess? (https://groups.google.com/forum/#!topic/requirejs/C-u34eSmUgE)

I also tried wrapping my main-dist file in another file which does use this context, e.g.

//index.html
var reqOne = require.config({
    context: "menu",
    baseUrl: '/static/menu/'
});
reqOne(['require', "/static/menu/base/javascript/sso/menu-sso.js""],
    function(require, menu_sso) {
        console.log('menu sso ');
    }
);

var reqTwo = require.config({
    context: "webapp",
    baseUrl: "./webapp/"
});
reqTwo(['require', 'main-prod'],
    function(require, main) {
        console.log('webapp');
    }
);
//main-prod.js
reqTwo(['require', 'main-dist']);
//main-dist.js
require(['require-config'], function() {

    require.config({
        baseUrl: './dist-tmp'
    });
    require(['common'], function(common) {
        require([
            'app'
        ],

        function(App) {
            App.start();
        });
    });
});

So main-prod doesn't really offer any kind of solution to this problem and I reverted to just require-ing main-dist from the index.html file.

Can anyone shed light on this and if it's possible to have a specific context for my optimized files (even those which nest require calls)?

glenpike commented 9 years ago

After trying to grok a way of adding context variables into r.js' parser, I went for some lower hanging fruit and have a good workaround.

One of our apps is optimised to a single file, which means we can build it with almond.js included and use wrap: true to ensure that it contains it's own require / define variables, then we can use requirejs to load our second app in the normal way.

Not sure if this will help anyone, but that's how I sorted the problem. There will still be an impossible situation if you need multiple contexts for apps that have multiple require'd files (lazy loaders).