Skip to content

Commit 5e6758f

Browse files
Adding --public-path option to the esbuild script (#58)
* Adding --public-path option to the esbuild script Setting `esbuild ... --public-path=assets` allows client-side code to reference images from the asset pipeline. Example: 1. Create image at `app/javascript/images/example.png`. 2. Add esbuild option `--loader:.png=file` (not included in this PR). 3. When esbuild runs, it will output e.g. `app/assets/builds/example-5SRKKTLZ.png`. 4. In frontend code, e.g. a React component, add `import Example from "../images/example.png"` . 5. Now the image can be referenced with `<img src={Example} />`. 6. The path resolves to /assets/example-5SRKKTLZ.png. Without this PR, the path resolves to /example-5SRKKTLZ.png. * Instructions for referencing static assets in frontend code built with esbuild * Looks like this question should be beneath the FAQ header * typo
1 parent fc66837 commit 5e6758f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,20 @@ Or, in Rails 7+, you can preconfigure your new application to use a specific bun
3636

3737
The default build script for esbuild relies on the `app/javascript/*.*` glob pattern to compile multiple entrypoints automatically. This glob pattern is not available by default on Windows, so you need to change the build script in `package.json` to manually list the entrypoints you wish to compile.
3838

39-
## Why does esbuild overwrite my application.css?
39+
### Why does esbuild overwrite my application.css?
4040

4141
If you [import CSS](https://esbuild.github.io/content-types/#css-from-js) in your application.js while using esbuild, you'll be creating both an `app/assets/builds/application.js` _and_ `app/assets/builds/application.css` file when bundling. The latter can conflict with the `app/assets/builds/application.css` produced by [cssbundling-rails](https://github.com/rails/cssbundling-rails). The solution is to either change the output file for esbuild (and the references for that) or for cssbundling. Both are specified in `package.json`.
4242

43+
### How can I reference static assets in JavaScript code?
44+
45+
Suppose you have an image `app/javascript/images/example.png` that you need to reference in frontend code built with esbuild.
46+
47+
1. Create the image at `app/javascript/images/example.png`.
48+
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.
49+
1. When esbuild runs, it will copy the png file to something like `app/assets/builds/example-5SRKKTLZ.png`.
50+
1. In frontend code, the image is available for import by its original name: `import Example from "../images/example.png"`.
51+
1. The image itself can now be referenced by its imported name, e.g. in React, `<img src={Example} />`.
52+
1. The path of the image resolves to `/assets/example-5SRKKTLZ.png`, which is served by the asset pipeline.
4353

4454
## License
4555

lib/install/esbuild/install.rb

+1-1
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"
5+
build_script = "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=assets"
66

77
if (`npx -v`.to_f < 7.1 rescue "Missing")
88
say %(Add "scripts": { "build": "#{build_script}" } to your package.json), :green

0 commit comments

Comments
 (0)