Question about app.js - how to use JS libraries in Phoenix?

I am using bunch of javascript library. and in app.js file says,

Brunch automatically concatenates all files in your watched paths. Those paths can be configured atconfig.paths.watched in “brunch-config.js”.

However, those files will only be executed if explicitly imported. The only exception are files in vendor, which are never wrapped in imports and therefore are always executed.

But in my case, it seems not working(for example simple collapse function in bootstrap). So I imported in app.js

import “./bootstrap.min”
then it works…

So Do I have to import all js library into app.js?
If there is any documentation for app.js in phoenix, please let me know
Thanks

You don’t have to, but you would still need to load the library somehow.

Meaning if you don’t want to import them all via app.js, then you’ll need to configure the brunch config to compile the scripts that you want, and load them on the page (e.g. via a script tag on your view/template).

here is a doc on how phoenix handles static assets: https://phoenixframework.org/blog/static-assets

3 Likes