caiocaio

caiocaio

Hi,

I wanted to get SASS working with Phoenix, but I just wasted my entire morning trying unsuccessfully to resolve npm dependencies (ie webpack 4.4 requires a package that requires webpack 4.36, and every attempt to fix is slow as molasses). Is there any way to use Phoenix without having to spend an inordinate amount of time of time messing around with npm or should I give up now?

Showing Posts 12 to 3

jdumont

jdumont

The issues you’re having aren’t with Elixir, Phoenix.

I personally don’t use Webpack or any other JS-based build tool exactly because of the issues you’ve encountered. I use a mixture of Rust CLI tools and believe it or not, Make. These integrate well with Phoenix as @peerreynders explained to you.

The issues you’ve encountered are with front-end development in general. JS and the build tools that often come with it are a fact of life unless you can forego JS and other static asset compilation entirely.

Coming to a forum, asking a question and having a number of people write out extremely thorough, clear and well researched answers — that are better than the docs in this case! — only to throw it back in their face with an arrogant, knee-jerk of a response is not cool at all.

If your “one-man webdev business” is going to survive, then you’ll need to learn about how to be nice to people, not compile your Sass without Webpack.

dimitarvp

dimitarvp

Meaning? Truthfully, the JS part of the stack is a huge pain no matter the backend framework… I’ve tried web frameworks in 5 languages and none were really easing the troubles of the JS tooling.

peerreynders

peerreynders

Glad I could help you to stop wasting your time!

LostKobrakai

LostKobrakai

I’m not disputing that this is the better case, but I still doubt that insisting on the need on some kind of watcher controlled by phoenix is the only way to go. In the past I’ve had great success with tools like CodeKit, which bundles all the dirty details of compiling source files in an easy to use desktop app. Sure this is not great for workflows involving CI/CD, but such applications can still be viable solutions outside of those constraints. Not everybody needs/uses a project building workflow with all the bells and whistles. When aware of the potential downsides it can be perfectly fine to opt for simpler solutions.

peerreynders

peerreynders

The OP is talking about using Sass.

In that situation the file edited would be an .scss or .sass file. A recurring theme in top 10 web development mistakes lists is not using automatic browser refresh when any of the development assets are being updated - that is what the watchers configuration is for.

A watcher script preparing (and copying) frontend assets is an essential ingredient in a modern web development workflow that aims to automate browser refreshes for development.

If your aim is to trigger an automatic browser refresh (i.e. live reload development) when any .scss or .sass file is modified then you need to configure watchers.

LostKobrakai

LostKobrakai

Not really. I could just as well edit a style.css file manually in priv/static and it would be live reloaded. A watcher is only needed if there’s a compilation step to be done when source files are edited. For anyone working directly with the to-be-deployed assets this can be skipped completely.

peerreynders

peerreynders

It is unrelated to Phoenix.LiveReload - but it it is an integral part to the live reloading development cycle.

Phoenix.LiveReload won’t have anything to do (frontend asset -wise) unless files in those directories are changed - (watching and) updating the static asset files is the responsibility of the script that is started by watchers - so the watchers configuration is an integral part of the live reloading development cycle even if that is part of the Phoenix.Endpoint rather than the Phoenix.LiveReload configuration.

LostKobrakai

LostKobrakai

That’s not completely correct.

config :my_app, MyAppWeb.Endpoint, watchers: […] does only handle that e.g. the webpack watcher is started when phoenix is serving the endpoint. This is completely unrelated to live reloading, which only works based on the wildcards listed in :live_reload.

Any external tool, which can put/update files in priv/static will trigger live reloads.

peerreynders

peerreynders

You can opt out of webpack with --no-webpack.

Opting out of npm is a bit more problematic because you then have to be responsible for including phoenix_html.js and phoenix.js.

Webpack isn’t an integral part of Phoenix but since Phoenix’s initial release it has included a bundler for convenience and to promote the use of modern modular JavaScript. Initially Phoenix included brunch as a low-maintenance option but webpack has become the defacto standard with the React and Vue community, so Phoenix 1.4 switched to webpack due to popular demand. But nobody is forced to use webpack.

And while SASS will probably remain the standard for complex projects there has been a noticable trend towards simply using modern CSS features like CSS custom properties and a handful of (Node.js powered) PostCSS plugins instead of full-blown SASS for more basic needs.

At the most basic level Phoenix will serve static assets out of

my_app/priv/static

What is picked up from that folder is governed by

my_app/lib/my_app_web/endpoint.ex
  plug Plug.Static,
    at: "/",
    from: :my_app,
    gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

This default configuration will only pick up the favicon.ico and robots.txt files and whatever files are in the css, fonts, images, js folders (Plug.Static Options).

Given that knowledge you should be able to take full control over the content that is being served.

The one disadvantage is that you won’t have the convenience of live reloading during development. That is why the frontend development assets are kept under

my_app/assets

The tooling there typically places any “refreshed” static assets under

my_app/assets/static

so that it can be copied over to

my_app/priv/static

when it is ready.

Phoenix starts the watch script to build the asset files with:

my_app/config/dev.exs
config :my_app, MyAppWeb.Endpoint,
  http: [port: 4000],
  debug_errors: true,
  code_reloader: true,
  check_origin: false,
  watchers: [
    node: [
      "node_modules/webpack/bin/webpack.js",
      "--mode",
      "development",
      "--watch-stdin",
      cd: Path.expand("../assets", __DIR__)
    ]
  ]

via that watchers entry (Phoenix.Endpoint Runtime configuration).

Little bit further down in that same dev.exs:

  live_reload: [
    patterns: [
      ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
      ~r"priv/gettext/.*(po)$",
      ~r"lib/my_app_web/{live,views}/.*(ex)$",
      ~r"lib/my_app_web/templates/.*(eex)$"
    ]
  ]

Those patterns determine what files will trigger Phoenix.LiveReloader to force a refresh in your browser.

This information should be enough for you to put together a frontend development environment of your own preference.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews