alsaraha

alsaraha

Esbuild multiple output files?

I have two separate questions,


1. How to build two files using esbuild?

in the config/config.exs file, here is the default esbuild configuration:

config :esbuild,                                                                                                                                                                            
  version: "0.14.29",                                                                                                                                                                       
  default: [                                                                                                                                                                                
    args:                                                                                                                                                                                   
      ~w(js/app.js --bundle --target=es2017 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*),                                                                       
    cd: Path.expand("../assets", __DIR__),                                                                                                                                                  
    env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}                                                                                                                                  
  ]                                                                                                                                                                                         

it only produces one file which is js/app.js, how can I generate two files using two separate input files?


2. What is the best way to use different js code in different pages?

let’s say that page 1 needs script 1 only

but page 2 needs script 1 AND script 2

is it better to make two separate js files and serve them when needed or just combine every thing in one file and send it to the user (regardless of whether he will visit the pages that need the other script or not)

Marked As Solved

zachallaun

zachallaun

  1. You can add additional files after app.js and they will be generated using the same options. (See esbuild docs for more)
args:
  ~w(js/app.js js/other.js …)
  1. Numerous ways to do this. If the bundles are entirely separate, you could create multiple root layouts. If you always include one and occasionally more, you could make a custom plug and pipeline that you include for routes that need the additional bundle. The plug could set additional assets in the conn assigns that the root layout could loop over.

To answer your question of whether you should bother at all: it really depends on your page speed goals and the size of the bundles. I probably wouldn’t worry about it personally until I was sure that it would be a meaningful improvement (aka I’ve come to the point where I’ve profiled things and I know that JS bundle download/execution is slowing things down).

Last Post!

zachallaun

zachallaun

  1. You can add additional files after app.js and they will be generated using the same options. (See esbuild docs for more)
args:
  ~w(js/app.js js/other.js …)
  1. Numerous ways to do this. If the bundles are entirely separate, you could create multiple root layouts. If you always include one and occasionally more, you could make a custom plug and pipeline that you include for routes that need the additional bundle. The plug could set additional assets in the conn assigns that the root layout could loop over.

To answer your question of whether you should bother at all: it really depends on your page speed goals and the size of the bundles. I probably wouldn’t worry about it personally until I was sure that it would be a meaningful improvement (aka I’ve come to the point where I’ve profiled things and I know that JS bundle download/execution is slowing things down).

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

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
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement