2

in the {theme}.libraries.yml why we need to declare the jquery js or any other library as a depedency :

  js: 
    assets/lib/bootstrap-4.0.0-dist/js/bootstrap.js: {}
    assets/script.js: {}

 dependencies: 
     - core/jquery

an not just include it for example as a simple js, before all other js that depends on it :

  js:
    assets/jquery.js: {} # load dependency library before the dependent libraries.
    assets/lib/bootstrap-4.0.0-dist/js/bootstrap.js: {}
    assets/script.js: {}

in the 2nd scenario the js file ex: jquery will be loaded before the other js files.

so what is the difference?

4
  • 4
    If every module adds his own jQuery file, instead of the jQuery core dependency, you would end up with a LOT of unnecessary duplicated JS libs massivly bloating your website. – Hudri Dec 17 '20 at 18:48
  • 5
    because that pulls in that library, not just the jquery file, where it (a library) may have a few files it needs to function right. – Kevin Dec 17 '20 at 19:16
  • 1
    It also ensures if the file path changes for any reason, it doesn't break your library. – Kevin Dec 17 '20 at 19:26
  • @Kevin thank u I get the point we depend on a library that can consist of a set of files, and not just one file – Marwen Amri Dec 17 '20 at 20:53
2

A library is a set of files (CSS or JavaScript) which could be local or loaded from another site. Declaring a dependency from another library has the vantage of not requiring to know:

  • Which files need to be loaded
  • Where those files are located
  • Which dependencies that library has

Using a library and declaring its dependencies also means that:

  • A file is loaded only once
  • There is only a copy of a file for all the modules
  • A module that depends from a library doesn't need to update the list of files (which are part of that library) when the library changes its files

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.