Development only statics. Is this the proper way?

Tags: #<Tag:0x00007f1149310608>


I am using a Javascript library named qooxdoo for the frontend. The issue I have with this library is that when it is used like SDK, it makes extra requests for file in folders different than the main folder. This is not an issue when the final app is made in a build version. Anyway here is an example.

The main script is loaded from http://localhost:4000/js/qssite/source/script/qssite.js and it starts requiring supplementary scripts from http://localhost:4000/script/qssite.a1b55c5841e0.js and resources like icons from http://localhost:4000/qooxdoo/framework/source/resource/qx/decoration/Modern/window-captionbar-buttons-combined.png which are saluted with a big red 404.

After some trial and error I came to this solution. In the file endpoint.ex, under the plug Plug.Static ... call I added these lines

  # for debugging the javascript application
  if :dev == Mix.env() do
    plug Plug.Static,
      at: "/",
      from: {:qssite, "priv/static/js/qssite/source/"},
      only_matching: ~w(script)

    plug Plug.Static,
      at: "/qooxdoo/framework/source",
      from: Path.relative_to_cwd("../qooxdoo/framework/source"),
      only: ~w(resource)

And now it works.

Given that this piece of code is needed only while I develop the application and should be excluded in production

  • Is my call to :dev == Mix.env() appropriate or should I check some other condition?
  • Is there any better alternative than adding it to endpoint.ex? I know I could do something with router.ex but it doesn’t seem that clean to me. Can I do it even cleaner than what I already have?


If that is at top module scope (and not inside a function) then it should be fine. :slight_smile:

I’m more curious in how to fix the library though, seems funky…


What do you mean? About the various urls it request? This is just the devel version for easier debugging. The authors probably were using it fully detached from the backend. In production you do a build that combines all javascript in a file and everything extra it may need in a convenient folder and everything is pulled from there. No need for hacks like this.


Should be able to feed it all into your bundler (like brunch that comes with phoenix by default) though?


Maybe. It does have a old python builder. There is a newer javascript builder developed right now. Maybe it could be automated with brunch or whatever but building is not something that needs to be done often. Only when a new class is introduced. Also learning brunch is not high in my list of priorities right now.

Also it does have, like elixir, build and devel environments where code from devel is removed from build. So the devel environment is needed while debugging.