caiocaio

caiocaio

Sass without Webpack

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?

Marked As Solved

peerreynders

peerreynders

Glad I could help you to stop wasting your time!

Also Liked

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.

kokolegorille

kokolegorille

Hello and welcome to the forum…

It’s mostly due to the ever moving js world. There is a post by @peerreynders here that explains how to procede with babel config. Don’t miss the ncu -u part, which is really cool tip to update your npm dependencies.

Don’t forget to add node-sass and sass-loader.

A good start would be to show part of your package.json and webpack.config.js.

These are rules I am using for sass, fonts, images… (in webpack.config.js)

      // Load stylesheets
      {
        test: /\.(css|scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'sass-loader',
        ]
      },
      // Load images
      {
        test: /\.(png|svg|jpe?g|gif)(\?.*$|$)/,
        loader: 'url-loader?limit=10000',
      },
      // Load fonts
      {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?(\?.*$|$)/,
        use: 'url-loader?&limit=10000&name=/fonts/[name].[ext]',
      },
      {
        test: /\.(eot|ttf|otf)?(\?.*$|$)/,
        loader: 'file-loader?&limit=10000&name=/fonts/[name].[ext]',
      },
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.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement