Redundant dependencies with RequireJS

前端 未结 2 1643
时光说笑
时光说笑 2021-02-10 13:56

My problem is with having redundant RequireJS dependencies that point to the same JS library.

The referenced library is jQuery UI, it\'s referenced both

2条回答
  •  忘了有多久
    2021-02-10 14:40

    Looking at the jquery-ui.js file you are trying to load, I see that it is a combination of a multiple functions that could be in separate files and that it calls define only once. So it registers as a single module. This is important to know because if it had called define for each of jquery-ui/mouse, jquery-ui/draggable, etc. then the solution would be different because then we'd be talking about a single jQuery UI file that contains multiple modules.

    Ok, so this being established, what you can do is use a map in your RequireJS configuration. I'm assuming you already have a proper paths configuration that allows loading the jquery-ui.js file as the jquery.ui module. Add this map:

    map: {
        '*': {
            'jquery-ui/mouse': 'jquery.ui',
            'jquery-ui/draggable': 'jquery.ui',
            // And so on for all different cases...
        }
    }
    

    This says in all modules (*) whenever jquery-ui/mouse is requested, then load jquery.ui instead, and so on for all the other modules that will be listed under *.

提交回复
热议问题