Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding --public-path option to the esbuild script #58

Merged
merged 4 commits into from Mar 30, 2022

Conversation

richardkmiller
Copy link
Contributor

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.

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.
@dhh
Copy link
Member

dhh commented Jan 13, 2022

Like this. Could you add a section to the README explaining how to configure these additional steps too?

@richardkmiller
Copy link
Contributor Author

@dhh Sorry for the delay. This should be good to go now.

@dhh dhh merged commit 5e6758f into rails:main Mar 30, 2022
@agrobbin
Copy link

@richardkmiller after making this change, I've noticed it causes source maps to no longer function, and I think it's due to Propshaft double-prefixing here.

When the --public-path option is added, the sourceMappingURL includes the prefix (i.e. /assets/application-[fingerprint].js.map) which is then prefixed again with /assets when Propshaft does its work.

@silva96
Copy link

silva96 commented Jan 13, 2023

@richardkmiller after making this change, I've noticed it causes source maps to no longer function, and I think it's due to Propshaft double-prefixing here.

When the --public-path option is added, the sourceMappingURL includes the prefix (i.e. /assets/application-[fingerprint].js.map) which is then prefixed again with /assets when Propshaft does its work.

@agrobbin did you solve this double prefixing? I removed the --public-path option for now ...

@richardkmiller
Copy link
Contributor Author

@agrobbin FYI, I submitted rails/sprockets-rails#515 to fix this issue in Sprockets. It may be a similar fix in Propshaft, specifically to the SOURCE_MAPPING_PATTERN regex shown in the file you referenced above.

@richardkmiller richardkmiller deleted the add-esbuild-public-path branch January 18, 2023 00:24
MartinSugasti added a commit to MartinSugasti/simplest-resumes that referenced this pull request Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants