rails / jsbundling-rails

Bundle and transpile JavaScript in Rails with esbuild, rollup.js, or Webpack.
MIT License
831 stars 143 forks source link

Update docs & install for referencing images in JS #158

Closed tgaff closed 1 year ago

tgaff commented 1 year ago

This should resolve #76 at least for the default esbuild case. Sprockets has already added support for ignoring .digested assets.

As a demonstration I have a reproduction case repo here.

Note the above deploys are "free" render.com instances. You may need to wait and retry after the initial request while it spins up.

The diff between the two is merely:

diff --git a/package.json b/package.json
index f740713..7ff9177 100644
--- a/package.json
+++ b/package.json
@@ -7,6 +7,6 @@
     "esbuild": "^0.17.19"
   },
   "scripts": {
-    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=/assets --loader:.png=file"
+    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=/assets --loader:.png=file --asset-names=[name]-[hash].digested"
   }
 }

This is mainly a doc change, walking through making this change to the build script.
However, I think there may be a good argument to be made for just including these in the build script by default. Not every user will need it but it probably wouldn't do any harm. (Not sure if we'd want just png, or if we want to include other formats) Please let me know if you'd like me to prepare that change.

shrmnk commented 1 year ago

Adding --asset-names=[name]-[hash].digested causes the build to fail with error

No matches found: "--asset-names=[name]-[hash].digested"

Full scripts.build in package.json:

"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=/assets --loader:.png=file --asset-names=[name]-[hash].digested",

Versions

--

EDIT: Turns out all I had to do was to wrap the value of --asset-names in quotes:

"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=/assets --loader:.png=file --asset-names=\"[name]-[hash].digested\"",

Sorry for the noise!

tgaff commented 1 year ago

@shrmnk I'm curious why you had to do that. I tried a few different scenarios like spaces and couldn't reproduce it. Is there something different about your platform? (I didn't try Windows). Symbols in file names maybe?

shrmnk commented 1 year ago

Hmm yeah that's possible. I'm on macOS, zsh.

tgaff commented 1 year ago

Same platform and shell as me. 🤷