Require.js is hurting my brain. Some fundamental questions about the way it loads scripts/modules

前端 未结 2 1193
梦谈多话
梦谈多话 2021-01-29 20:56

Let\'s assume this is my config.js or main.js:

require.config({
    // paths are analogous to old-school 

        
2条回答
  •  有刺的猬
    2021-01-29 21:49

    1. Paths tell require.js where to look when you require that dependency.

      For example i have things configured like this:

      "paths": { 
          "jquery": "require_jquery"
      },
      "shim": {
          "jquery-cookie"  : ["jquery"],
          "bootstrap-tab"  : ["jquery"],
          "bootstrap-modal": ["jquery"],
          "bootstrap-alert": ["jquery"]
      },
      

      this means that every time in a module I do

      define( ['jquery']
      

      requirejs loads the file require_jquery from the main path instead of trying to load jquery.js. In your case it would load the jQuery source file, which would then be globally available. I personally don't like that approach and for that reason in the require_jquery.js file I do:

      define( ["jquery_1.7.2"], function() {
          // Raw jQuery does not return anything, so return it explicitly here.
          return jQuery.noConflict( true );
      } );
      

      which means that jQuery will be defined only inside my modules. (This is because i write Wordpress plugins and so I can include my own version of jQuery without touching the outside version)

    2. Exports (reading from the docs simply should be the name of the module you are using so that it can be detected if loading went correctly. Here is explained. So if you want to set an export for underscore it should be _

    3. jQuery should be global as I explained, if you simply import it the file is executed and jQuery is global

    EDIT - to answer the comments.

    1. yes i mean that, you must export $ or jQuery for jQuery and _ for backbone. From what i got from the docs this is needed only in some edge cases and would not be necessary for libraries that declare themselves in the global namespace as jQuery.

      I think that requirejs needs them when it has to fallback from loading jQuery from a CDN. i think that requirejs first tries to load jQuery from the CDN, then makes a check to verify that it was loaded correctly by checking that the "exported" variable exists, and if it doesn't it loads it form the local filesystem (if you had configured fallbacks, of course). This is something that it's needed when requirejs can't see a 404 coming back.

    2. jQuery is globally available because it's declared global. If you simply load and execute the jQuery script, you will end up with two globals, $ and jQuery (or you can do as i did and avoid that). Inside the define() function you can alias jQuery to be whatever you want.

      define( [ 'jquery' ], function( jq ) {
          // jq is jquery inside this function. if you declared it 
          // globally it will be also available as $ and jQuery
      } );
      

提交回复
热议问题