1

For use of most frameworks (Zurb Foundation, Bootstrap, etc) or libraries (Like Owl carousel, Slick, etc) within a theme, jQuery has to be added as a library.

Is it enough to add jQuery only once as dependencies in a global library and have it load on every page (imagine that I add this library in Library section of ThemeName.info.yml) Or would I have to add it as dependencies each time I define a new library again?

For example, is the below structure correct:

global:
  css:
      # The path to the css file.
      assets/css/stylesheets-1.css: {}
      assets/css/stylesheets-2.css: {}
  js:
    assets/js/myglobal.js: {}
  dependencies:
    - core/jquery

library-A: # Bootstrap Library.
  css:
      assets/css/stylesheets-3.css: {}
      assets/css/stylesheets-4.css: {}
  js:
    assets/js/myglobal.js: {}
  dependencies:
    - core/jquery

library-B: # Owl carousel Library.
  css:
      assets/css/stylesheets-5.css: {}
      assets/css/stylesheets-6.css {}
  dependencies:
    - core/jquery

Is there any problem If I add jQuery once as library in js: category and again as dependencies: in one library like library-C simultaneously? Something like below? Can it cause the conflict?

library-C:
  css:
      assets/css/stylesheets-c-1.css: {}
      assets/css/stylesheets-c-2.css: {}
  js:
    assets/js/myglobal.js: {}
    assets/js/jQuery.js: {}
  dependencies:
    - core/jquery
4

Every library needs to have all it's dependency listed ! As libraries can be loaded independently.

drupal will make sure to include every file only once.

So this is correct. yes.

  • 1
    Thanks for your help and reply, Is there any problem If I add jQuery once as library and again as dependency in one library like library-A simultaneously? – Mojtaba Reyhani Mar 9 '17 at 17:11
  • the dependency represent an existing library. core/jquery is a library defined in core. so no need to define the jQuery library. you can use a self defined library as dependency in another self defined library. – rémy Mar 9 '17 at 19:57
  • Thank you so much for anything again, but I don't think you understand my mean correctly, Please take a look at the bottom of my question again. – Mojtaba Reyhani Mar 9 '17 at 21:14
  • it makes no sense to add 2 jQeury libraries. if you want to update a library, you need to hook_js_alter - with some logic to do it only when your library is required. if you need an older jQuery version, try stackoverflow.com/questions/21882731/…, for now, and when drupal updates to jQuery 3 use this jQuery migrate plugin. – rémy Mar 10 '17 at 10:27
  • and please stop extending your question. ask a new one ! – rémy Mar 10 '17 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.