evolvingdev
I’m trying to integrate the javascript from a bootstrap theme into phoenix 1.4 and failing to get the javascript files to work. I’ve placed jquery into assets/vendor, which webpack output is showing as working.
But I’ve found little documentation and confusing posts about how to use assets/js/app.js to import single javascript files from the same folder (assets/js) and how these can be required/used on a per page (template) level and whether this is possible or recommended. Some people reference individual functions and others entire javascript files. I’ve tried importing javascrtip files in assets/js/app.js, but must be doing it wrong as webpack isn’t seeing them, and I’m not sure if I should be using a script tag in the relevant template to call wrapped javascript function/file if/when webpack finds it in assets/js/app.js.
My last question is - with HTTP2, is using assets/js/app.js and packing a single file via webpack the best approach or should I be sending single javascript files down the wire, which appears to be easier anyway as the docs aren’t clear how to import javascript files in assets/js/app.js?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #code-sync
- #exunit
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #blog-post
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Hello, welcome.
Please show some code… especially app.js.
Maybe look at
or (Phoenix 1.3, but it should not be different).
adrianrl
Hi, welcome to the forum. The configuration is very basic and general to give you a starting point, but you may want to tweak it a bit in order to install new dependencies. This should work assuming you have installed the dependencies via NPM or Yarn:
For a more elaborated configuration you should take a look to Webpack’s documentation.
It depends. If you’re using a lot of JavaScript, you may want to split vendor’s dependencies from the bundle of your app. The idea of splitting a bundle is mandatory for SPAs, but for MPAs sometimes is better having a big bundle minified and gzipped (if its size isn’t too big) or using a CDN for the dependencies, as it results in less requests needed from the client.
evolvingdev
Thanks @kokolegorille and @adrianrl, below are my
webpack.config.js,package.json,NPM packagesinstalled globally and locally (to my app’s assets directory), and terminal output from running the app.@adrianrl, you’ll note that in my
webpack.config.js, I don’t have the same entry as you’ve noted, but I haven’t seen it in the docs I’ve followed. How could I edit the current:to be the same as you’ve suggested and do I need to if bootstrap appears to be rendering fine?
The issue I’m having is trying to import assets/js/custom.js into assets/js/app.js - what to write to do that, and whether or not I have to individually reference custom.js in the relevant index.html.eex file or whether I just leave it to
<script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>to import assets/js/custom.js globally - if I could import it.Unless I’ve made an obvious mistake above, how do I import
custom.jsintoapp.jsfrom the same directory,assets/js?Comments in
assets/js/app.jsshow what I’ve tried (and tried single quotes too):———————————————
Showing globally installed NPM packages:
$ npm list -g --depth 0NPM packages installed local to my apps assets directory:
$ npm ls --depth=0———————————————
Here’s my
webpack.config.js———————————————
Here’s my
package.json———————————————
Here’s terminal output:
$ mix phx.server———————————————
I hope that helps!
evolvingdev
Sorry - custom.js isn’t showing about as I renamed it agency.js - and have got webpack finding it.
But my issue now appears to be… how to import/require it in index.html.eex as it’s not working in the render html and javascript?
kokolegorille
If You want to import some js files, You need to export from them.
It would look like
export default …
If it is just some js without export, then You could put this into static, this will be pushed into priv/static, then it would be possible to link directly from html template.
What is inside your agency.js?
Also, webpack default config is simple, but complex config can be REALLY hard.
Concerning your second request, it is possible to optimize the output, for example, splitting app.js and vendor.js, split code per page (if You use a js router), lazy load part of js…
In fact, webpack is very good at that, but You need to read the doc to optimize its output.