Skip to content

Commit 45a9848

Browse files
authored
Update docs & install for referencing images in JS (#158)
* asset path should use / so it works on more than just the root routes * update the image loading instructions so that files are marked and skipped by sprockets
1 parent 0ac7d62 commit 45a9848

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ If you [import CSS](https://esbuild.github.io/content-types/#css-from-js) in you
5050
Suppose you have an image `app/javascript/images/example.png` that you need to reference in frontend code built with esbuild.
5151

5252
1. Create the image at `app/javascript/images/example.png`.
53-
1. In `package.json`, under `"scripts"` and `"build"`, add the option `--loader:.png=file` to the esbuild script, which instructs esbuild to copy png files to the build directory.
54-
1. When esbuild runs, it will copy the png file to something like `app/assets/builds/example-5SRKKTLZ.png`.
53+
1. In `package.json`, under `"scripts"` and `"build"`, add the additional arguments:
54+
* `--loader:.png=file` This instructs esbuild to copy png files to the build directory.
55+
* `--asset-names=[name]-[hash].digested` This tells esbuild to append `.digested` to the file name so that sprockets or propshaft will not append an additional digest hash to the file.
56+
1. When esbuild runs, it will copy the png file to something like `app/assets/builds/example-5SRKKTLZ.digested.png`.
5557
1. In frontend code, the image is available for import by its original name: `import Example from "../images/example.png"`.
5658
1. The image itself can now be referenced by its imported name, e.g. in React, `<img src={Example} />`.
57-
1. The path of the image resolves to `/assets/example-5SRKKTLZ.png`, which is served by the asset pipeline.
59+
1. The path of the image resolves to `/assets/example-5SRKKTLZ.digested.png`, which is served by the asset pipeline.
5860

5961
## License
6062

lib/install/esbuild/install.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
run "yarn add esbuild"
33

44
say "Add build script"
5-
build_script = "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=assets"
5+
build_script = "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=/assets"
66

77
case `npx -v`.to_f
88
when 7.1...8.0

0 commit comments

Comments
 (0)