RequireJS how specify shim dependency from jam file package section

我是研究僧i 提交于 2019-12-08 13:26:42

问题


I'm working in a project with backbone-boilerplate which uses RequireJS to load modules and JamJS to manage them. My require config.js file is as follows:

require.config({
  deps: ["../vendor/jam/require.config", "main"],
  paths: {
    "backbone.localStorage": "../vendor/backbone.localStorage-1.0/backbone.localStorage"
  },
  shim: {
    "backbone.localStorage": {
        deps: ['backbone']
    }
  }
});

As you can see the RequireJS load config from jam config file jam/require.config.js which specifies backbone, jquery and underscore. Next is a piece of the file:

    "packages": [
        {
            "name": "backbone.layoutmanager",
            "location": "../vendor/jam/backbone.layoutmanager",
            "main": "backbone.layoutmanager.js"
        }
        {
            "name": "backbone",
            "location": "../vendor/jam/backbone",
            "main": "backbone.js"
        }
    ],
    "version": "0.2.11",
    "shim": {
        "backbone.layoutmanager": {
            "deps": [
                "jquery",
                "backbone",
                "lodash"
            ],
            "exports": "Backbone.LayoutManager"
        }
        "backbone": {
            "deps": [
                "jquery",
                "lodash"
            ],
            "exports": "Backbone"
        }
    }
};

What I want is to specify in my config.js the backbone.localStorare library depends on backbone defined in the package section. Also if I specify the backbone path as follows, then it found backbone library but an error message is found saying underscore is not loaded:

require.config({
  deps: ["../vendor/jam/require.config", "main"],
  paths: {
    "backbone": "../vendor/jam/backbone/backbone",
    "backbone.localStorage": "../vendor/backbone.localStorage-1.0/backbone.localStorage"
  },
  shim: {
    "backbone.localStorage": {
        deps: ['backbone']
    }
  }
});

I could add backbone.localStorage library using jamjs but the version of the available package is old so I prefer to download the last one manually and include by hand.

Any ideas or help will be appreciated.


回答1:


Backbone needs Underscore & jQuery:

shim: {
    "backbone": {
       "deps": [ "underscore", "jquery" ],
       "exports": "Backbone"  //attaches "Backbone" to the window object
    },
    "underscore": {
       "exports": "_"
    }
}

So i think it is best that you adapt your jam/require.config.js.

Edit:

Next, the packages section in your question, that's a part of jam/require.config.js? Is location relative to the location of jam/require.config.js?

Have you tried to add a packages section in your own config.js:

"packages": ["backbone", "backbone.layoutmanager"]



回答2:


Thanks to asgoth I can solve the problem. I put the files below.

With this configuration I can place shim libraries in my config.js file which depends on packages manages by jam at the require.config.js file.

the config.js

// Set the require.js configuration for your application.
require.config({

  // Initialize the application with the main application file and the JamJS
  // generated configuration file.
  deps: ["../vendor/jam/require.config", "main"],

  // Packeges defined at jam/require.config.js required by shim libraries
  packages: [
    {"name" : "backbone"}, 
    {"name" : "jquery"}
  ],

  paths: {
    // Put paths here.
    "codemirror" : "../vendor/codemirror-3.0/lib/codemirror",
    "codemirror-markdown": "../vendor/codemirror-3.0/mode/markdown/markdown",
    "backbone.localStorage": "../vendor/backbone.localStorage-1.0/backbone.localStorage",
    "bootstrap" : "../vendor/bootstrap-2.2.2/js/bootstrap"
  },

  shim: {
    // Put shims here.
    "codemirror-markdown": {
      deps: ["codemirror"]
    },
    "backbone.localStorage": {
      deps: ['backbone']
    },
    "bootstrap": {
      deps: ['jquery']
    }
  }

});

the require.config.js

var jam = {
    "packages": [
        {
            "name": "jquery",
            "location": "../vendor/jam/jquery",
            "main": "jquery.js"
        },
        {
            "name": "backbone.layoutmanager",
            "location": "../vendor/jam/backbone.layoutmanager",
            "main": "backbone.layoutmanager.js"
        },
        {
            "name": "underscore",
            "location": "../vendor/jam/underscore",
            "main": "underscore.js"
        },
        {
            "name": "backbone",
            "location": "../vendor/jam/backbone",
            "main": "backbone.js"
        },
        {
            "name": "lodash",
            "location": "../vendor/jam/lodash",
            "main": "./lodash.js"
        }
    ],
    "version": "0.2.11",
    "shim": {
        "backbone.layoutmanager": {
            "deps": [
                "jquery",
                "backbone",
                "lodash"
            ],
            "exports": "Backbone.LayoutManager"
        },
        "underscore": {
            "exports": "_"
        },
        "backbone": {
            "deps": [
                "jquery",
                "lodash"
            ],
            "exports": "Backbone"
        }
    }
};

if (typeof require !== "undefined" && require.config) {
    require.config({packages: jam.packages, shim: jam.shim});
}
else {
    var require = {packages: jam.packages, shim: jam.shim};
}

if (typeof exports !== "undefined" && typeof module !== "undefined") {
    module.exports = jam;
}


来源:https://stackoverflow.com/questions/14104835/requirejs-how-specify-shim-dependency-from-jam-file-package-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!