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

Fix image loading and add example image to prove it #324

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion variants/frontend-base/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,33 @@
EO_CONTENT
end

images_disabled_chunk = <<~EO_IMAGES_DISABLED
// const images = require.context('./images', true)
// const imagePath = (name) => images(name, true)
EO_IMAGES_DISABLED

images_enabled_chunk = <<~EO_ENABLE_IMAGES
const images = require.context('../images', true);
// eslint-disable-next-line no-unused-vars
const imagePath = name => images(name, true);
EO_ENABLE_IMAGES

gsub_file "app/frontend/packs/application.js", images_disabled_chunk, images_enabled_chunk, force: true

# Configure app/views
gsub_file "app/views/layouts/application.html.erb",
"<%= stylesheet_link_tag(",
"<%= stylesheet_pack_tag(",
force: true

copy_file "app/frontend/images/example.png"
body_open_tag_with_img_example = <<~EO_IMG_EXAMPLE
<body>
<%
# An example of how to load an image via Webpacker. This image is in
# app/frontend/images/example.png
%>
<%# image_pack_tag "example.png", alt: "Example Image" %>
<%= image_pack_tag "example.png", alt: "Example Image" %>
EO_IMG_EXAMPLE
gsub_file "app/views/layouts/application.html.erb", "<body>", body_open_tag_with_img_example, force: true

Expand Down
14 changes: 14 additions & 0 deletions variants/frontend-typescript/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@
"process.env.SENTRY_ENV ?? process.env.RAILS_ENV"
)

js_load_images_chunk = <<~EO_JS_ENABLE_IMAGES
const images = require.context('../images', true);
// eslint-disable-next-line no-unused-vars
const imagePath = name => images(name, true);
EO_JS_ENABLE_IMAGES
ts_load_images_chunk = <<~EO_TS_ENABLE_IMAGES
const images = require.context('../images', true);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const imagePath = (name: string): string => {
return String(images(name));
};
EO_TS_ENABLE_IMAGES
gsub_file("app/frontend/packs/application.ts", js_load_images_chunk, ts_load_images_chunk, force: true)

package_json = JSON.parse(File.read("./package.json"))
package_json["scripts"]["typecheck"] = "tsc -p . --noEmit"
File.write("./package.json", JSON.generate(package_json))
Expand Down