如何在 requirejs 中添加具有子依赖项的 AMD/legacy 依赖项(传单)

How to add AMD/legacy dependency (leaflet) with sub dependencies in requirejs

我安装了一个名为 leaflet 的 AMD 模块并成功地将其用作 "L"。 接下来我需要一个名为 leaflet.draw 的插件,但我对依赖项感到困惑。考虑以下代码:

requirejs.config({
    baseUrl: 'bower_components',
    paths: {
        leaflet: 'leaflet/dist/leaflet-src',
        leafletdraw: 'leaflet-draw/dist/leaflet.draw-src'
        ...

requirejs(["leaflet", "leafletdraw"], function(L, leafletdraw) {
    var map = new L.Map('map');
    ...

这给出了一个参考错误:L 未定义在 Leaflet.draw.js:4。所以我猜它需要传单 (L) 作为依赖项,对吗?然后我尝试将它添加到垫片配置中:

shim: {
    leafletdraw: {
        deps: 'leaflet'
    }
}

这导致 "Invalid require call"。所以我的问题是:如何正确地要求具有子依赖性的插件?

解决方案:填充 leaflet 本身,并让它导出 'L'。然后将 deps 放入插件中即可。我的完整配置:

requirejs.config({
    baseUrl: 'bower_components',
    paths: {
        jquery: 'jquery/dist/jquery.min',
        leaflet: 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet',
        'leaflet-draw': 'http://cdn.osmbuildings.org/Leaflet.draw/0.2.0/leaflet.draw',        
        OSMBuildings: ['http://cdn.osmbuildings.org/OSMBuildings-Leaflet']    
    },    
    shim: {
        leaflet: {
            exports: 'L'
        },
        'leaflet-draw': {
            deps: ['leaflet']
        },        
        OSMBuildings: {
            deps: ['leaflet'],
            exports: 'OSMBuildings'
        }
    }
});

requirejs(["jquery", "leaflet", "leaflet-draw", "OSMBuildings"], function($, L, dummy, OSMBuildings) {
    var map = new L.Map('map');