First, do you really need an umbrella app ?
I donβt know. Iβm following the directions in βPhoenix in Actionβ, Section 5.1.2.
Now, Iβve done this:
~/phoenix_apps/a_umbrella/apps% mix phx.new.web my_web --no-ecto
giving me this directory structure:
~/phoenix_apps/a_umbrella/apps/my_web% tree -I "outline|solid"
.
βββ README.md
βββ assets
β βββ css
β β βββ app.css
β βββ js
β β βββ app.js
β β βββ my_setup.js
β βββ tailwind.config.js
β βββ vendor
β βββ heroicons
β β βββ LICENSE.md
β β βββ UPGRADE.md
β β βββ optimized
β β βββ 20
β β βββ 24
β βββ jquery.js
β βββ topbar.js
βββ lib
β βββ my_web
β β βββ application.ex
β β βββ components
β β β βββ core_components.ex
β β β βββ layouts
β β β β βββ app.html.heex
β β β β βββ root.html.heex
β β β βββ layouts.ex
β β βββ controllers
β β β βββ error_html.ex
β β β βββ error_json.ex
β β β βββ page_controller.ex
β β β βββ page_html
β β β β βββ home.html.heex
β β β βββ page_html.ex
β β βββ endpoint.ex
β β βββ gettext.ex
β β βββ router.ex
β β βββ telemetry.ex
β βββ my_web.ex
βββ mix.exs
βββ priv
β βββ gettext
β β βββ en
β β β βββ LC_MESSAGES
β β β βββ errors.po
β β βββ errors.pot
β βββ static
β βββ favicon.ico
β βββ robots.txt
βββ test
βββ my_web
β βββ channels
β βββ controllers
β βββ error_html_test.exs
β βββ error_json_test.exs
β βββ page_controller_test.exs
βββ support
β βββ conn_case.ex
βββ test_helper.exs
24 directories, 34 files
I tried adding the file:
// ...assets/js/jquery_setup.js
import jQuery from "../vendor/jquery"
window.jQuery = jQuery
window.$ = jQuery
I downloaded jQuery, and I put it here:
//β¦assets/vendor/jquery.js
My ../assets/js/app.js
file looks like this:
// If you want to use Phoenix channels, run `mix help phx.gen.channel`
// to get started and then uncomment the line below.
// import "./user_socket.js"
// You can include dependencies in two ways.
//
// The simplest option is to put them in assets/vendor and
// import them using relative paths:
//
// import "../vendor/some-package.js"
//
// Alternatively, you can `npm install some-package --prefix assets` and import
// them using a path starting with the package name:
//
// import "some-package"
//
//Setup jQuery:
import "./jquery_setup.js"
// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
import "phoenix_html"
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})
// Show progress bar on live navigation and form submits
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
window.addEventListener("phx:page-loading-stop", _info => topbar.hide())
// connect if there are any LiveViews on the page
liveSocket.connect()
// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket
Second, you just generate a template for an umbrella app, please follow the steps in Overview β Phoenix v1.7.2 1, how to create a phoenix app and how to integrate third party JS libs
I read that before I posted, and I tried this:
Asset Management
Beside producing HTML, most web applications have various assets (JavaScript, CSS, images, fonts and so on).
From Phoenix v1.7, new applications use esbuild to prepare assets via the Elixir esbuild wrapper, and tailwindcss via the Elixir tailwindcss wrapper for CSS. The direct integration with esbuild and tailwind means that newly generated applications do not have dependencies on Node.js or an external build system (e.g. Webpack).
Your JavaScript is typically placed at βassets/js/app.jsβ and esbuild will extract it to βpriv/static/assets/app.jsβ. In development, this is done automatically via the esbuild watcher. In production, this is done by running mix assets.deploy.
esbuild can also handle your CSS files, but by default tailwind handles all CSS building.
Finally, all other assets, that usually donβt have to be preprocessed, go directly to βpriv/staticβ.
third-party-js-packages Third-party JS packages
If you want to import JavaScript dependencies, you have two options to add them to your application:
Vendor those dependencies inside your project and import them in your βassets/js/app.jsβ using a relative path:
import topbar from ββ¦/vendor/topbarβ
In my browserβs javascript console, I keep getting the error:
ReferenceError: Canβt find variable: $