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

Feature Adding AssetPipeline & ImportMaps #1358

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/amber/cli/templates/app/.amber.yml.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ watch:
- ./src/views/**/*.slang
<%- end -%>
- ./src/locales/*.yml
- ./src/javascript/**/*.js
# exclude: # NOTE simplistic implementation: (1) enumerate all includes and excludes; (2) return (includes - excludes)
# - ./src/some_irrelevant_file.cr
spec:
Expand Down
15 changes: 15 additions & 0 deletions src/amber/cli/templates/app/config/initializers/import_map.cr.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "asset_pipeline"

FRONT_LOADER = AssetPipeline::FrontLoader.new(js_source_path: Path["src/javascript"], js_output_path: Path["public"]) do |import_maps|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt of assets/javascript instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kicked that around. If the deployment doesn't require the entire code base be cloned, then it makes total sense.
But, wouldn't most dockerized/containerized deploys mean the entire code base was cloned into the image and then deployed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just copy the assets directory to the docker image instead of src/javascript. I think it will actually make creating the image easier.

import_map = AssetPipeline::ImportMap.new

if Amber.settings.auto_reload
import_map.add_import("client_reload", "/client_reload.js")
end

import_map.add_import("@popperjs/core", "https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js")
import_map.add_import("bootstrap", "https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.esm.min.js")
import_map.add_import("amber", "/amber.js")

import_maps << import_map
drujensen marked this conversation as resolved.
Show resolved Hide resolved
end
drujensen marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/amber/cli/templates/app/config/logger.cr.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ backend.formatter = Log::Formatter.new do |entry, io|
io << " (#{entry.severity})" if entry.severity > Log::Severity::Debug
io << " "
io << entry.message
io << " #{entry.exception}" if entry.exception
end

Log.builder.clear
Expand Down
52 changes: 0 additions & 52 deletions src/amber/cli/templates/app/public/js/client_reload.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/amber/cli/templates/app/shard.yml.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ dependencies:
github: dare892/citrine-i18n
version: ~> 1.0.0

asset_pipeline:
github: amberframework/asset_pipeline
version: ~> 0.34.0

development_dependencies:
ameba:
github: crystal-ameba/ameba
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,28 +231,30 @@ export default {
/**
* Allows delete links to post for security and ease of use similar to Rails jquery_ujs
*/
document.addEventListener("DOMContentLoaded", () => {
let elements = document.querySelectorAll("a[data-method='delete']");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", (e) => {
e.preventDefault();
let message = elements[i].getAttribute("data-confirm") || "Are you sure?";
if (confirm(message)) {
let form = document.createElement("form");
let input = document.createElement("input");
form.setAttribute("action", elements[i].getAttribute("href"));
form.setAttribute("method", "POST");
input.setAttribute("type", "hidden");
input.setAttribute("name", "_method");
input.setAttribute("value", "DELETE");
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
return false;
})
}
});
export function initialize() {
document.addEventListener("DOMContentLoaded", () => {
let elements = document.querySelectorAll("a[data-method='delete']");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", (e) => {
e.preventDefault();
let message = elements[i].getAttribute("data-confirm") || "Are you sure?";
if (confirm(message)) {
let form = document.createElement("form");
let input = document.createElement("input");
form.setAttribute("action", elements[i].getAttribute("href"));
form.setAttribute("method", "POST");
input.setAttribute("type", "hidden");
input.setAttribute("name", "_method");
input.setAttribute("value", "DELETE");
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
return false;
})
}
});
}

if (!Date.prototype.toGranite) {
(function() {
Expand Down
35 changes: 35 additions & 0 deletions src/amber/cli/templates/app/src/javascript/client_reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
let tryReload

export function initializeWebSockets() {
console.log("initializeWebSockets has run...")

tryReload = async function () {
try {
const response = await fetch(window.location.href);
if (!response.ok) {
setTimeout(tryReload, 1000);
} else {
window.location.reload();
}
} catch (error) {
setTimeout(tryReload, 1000);
}
}

if ('WebSocket' in window) {
const protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://'
const address = protocol + window.location.host + '/client-reload'
const socket = new WebSocket(address)

socket.onmessage = function (msg) {
console.log(msg)
if (msg.data == 'reload') {
tryReload()
}
}

socket.onclose = function () {
setTimeout(tryReload, 1000)
}
}
}
14 changes: 10 additions & 4 deletions src/amber/cli/templates/app/src/views/layouts/application.ecr.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>

<script type="module" src="/js/amber.js"></script>
<%="<"%>%- if Amber.settings.auto_reload? -%><script src="/js/client_reload.js"></script><%="<"%>%- end -%>
<%= "<"%>%= FRONT_LOADER.render_import_map_tag %>
<script type="module">
import { initialize } from 'amber'
initialize()

<%= "<"%>%- if Amber.settings.auto_reload? %>
import { initializeWebSockets } from 'client_reload'
initializeWebSockets()
<%= "<"%>% end %>
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ html

.main== content

script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
== FRONT_LOADER.render_import_map_tag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit cosmetic, but maybe a helper function like import_map_tag instead of a constant declared in the initializer could hide a bit of implementation details here.

If the plan is to have import maps as a built in feature, maybe would be worth to deal with it like Amber does with routes and settings, a file config/import_maps.cr with:

Amber::Server.import_maps(source_dir: "src/javascript", output_dir: "public") do |import_maps|
  ...
end

OR, to not fill Amber::Server with frontend stuff:

Amber::ImportMaps.configure(source_dir: "src/javascript", output_dir: "public") do
...


script type="module" src="/js/amber.js"
- if Amber.settings.auto_reload?
script src="/js/client_reload.js"
script type="module"
= "import { initialize } from 'amber'";
- if Amber.settings.auto_reload?
= "import { initializeWebSockets } from 'client_reload'";