How can I manage Javascript bilbiotecas in my PHP project?

Asked

Viewed 61 times

3

I have a PHP project and use commiserate to manage libraries such as Phpmailer for sending emails.

To be able to use the installed libraries just include the autoload:

require ('vendor/autoload.php');

Recently I also needed to use other libraries, but they are Javascript libraries, and I did the same procedure described above. The «problem» is that I have to include JS files:

<script type="text/javascript" src="vendor/NovaBiblioteca/js/nova.mim.js">

At the end of the second library used it does not seem anything intuitive.

How can I manage Javascript libraries?

  • It looks like a case of npm usage, not Composer.

  • @bfavaretto reading and only see npm connected to Node and Bower connected to JS, CSS and HTML. Can you clarify me?

1 answer

1


Composer is a PHP package manager, you do not can must install Javascript libraries for it, you would have to use another specific package manager for Javascript like NPM, Yarn, Bower or webpack, which in javascript code could use webpack to automatically inject the libraries used in a single file to be included.

See here how to use the webpack https://tableless.com.br/introducao-ao-webpack/

  • But the funny thing is that I can also include Javascript packages for it. They are available when I do composer require PacoteJS.

  • 2

    Yes, these packages can be imported using Composer: https://gist.github.com/odan/b5812c61d494d45becabaaf223875bb3 and https://drupal.stackexchange.com/questions/184666/how-to-add-javascript-css-library-to-Composer-project both CSS and JS but Composer is a PHP package manager, hence its repository does not have all JS libraries like Yarn/Bower and would not be officially supported, leading you to have future problems with some JS libraries

  • 1

    Interesting to know, is that it is not only a matter of simplifying things, can also have influence on the functioning...

  • Yes, the separation of managers facilitates the compatibility of JS packages despite adding a layer of complexity, the use of the webpack I suggested is to be able to make a js dependencies Bundle in a file (Bundle.js) minificado unified to be included in PHP that facilitates the maintenance of PHP code

  • 3

    @Jorgeb. I would try to use only NPM first, without webpack. Maybe with Gulp. The problem with the webpack is that it does a lot of extra things (it allows separating the final code into packages/modules that can be loaded on-demand), and the learning curve is not very user-friendly. I at least suffered a little to learn.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.