Getting dependencies to load correctly in requirej

2019-06-06 10:46发布

问题:

I have been stuck on this problem for the past few hours. I'm trying to get autobahnjs and whenjs to be loaded correctly by requirejs.

require.config({
    paths: {
        angular:            '../bower_components/angular/angular',
        angularBootstrap:   '../bower_components/angular-bootstrap/ui-bootstrap',
        bootstrap:          '../bower_components/bootstrap/dist/js/bootstrap',
        jquery:             '../bower_components/jquery/jquery',
        chosen:             '../bower_components/chosen/chosen.jquery.min',
        text:               '../bower_components/requirejs-text/text',
        autobahn:           '../bower_components/autobahnjs/autobahn/autobahn'

    },
    packages: [
       { name: 'when', location: '../bower_components/when/', main: 'when' }
    ],
    baseUrl: '/bundles/example/app/scripts/',
    shim: {
        angular : { 
            exports : 'angular'
        },
        angularBootstrap: {
            deps: ['angular']
        },
        autobahn: {
            deps: ['when']
        }
    },
    priority: [
        'angular'
    ]
});

require
( [
    'angular',
    'app',
    'autobahn',
    'angularBootstrap',
    'jquery',
    'bootstrap',
    'chosen',
    'controllers/event',
    'services/notify'
], function(angular, app) {
    // more code here
});

Autobahnjs has a dependency on whenjs. All the files are loaded (and in the correct order). but when is always undefined. I have absolutely no idea what I'm doing wrong. I've tried all sorts of ways to solve it. I also have a bower.json file if this helps anyone replicate the problem. Thanks in advance.

EDIT: Autobahnjs does not currently support AMD. Whenjs, however, does support it.

回答1:

As you have noticed already, there is an issue for adding requirejs support to AutobahnJS. There is also more embedded stuff inside AutobahnJS bundled for "convenience", mainly parts from cryptojs.

The challenge simply is: how to best serve all users, not matter if and what module loader they use, and if they want convenience (bundled stuff) or prefer to have stuff separate (and manage/load that themselves).

I can't promise, but I try to address it with priority. However, for further discussion, I think the best place would be the GitHub issue.



回答2:

This has now been implemented in v0.8.0