hanami / assets-js

esbuild plugin for Hanami Assets
3 stars 5 forks source link

Fix type error with assets run not being a function #13

Closed bkuhlmann closed 1 year ago

bkuhlmann commented 1 year ago

Why

Since the introduction of assets in 2.1.0 Beta 2, it hasn't been possible to launch the app using Overmind due to the following error:

system | Tmux socket name: overmind-demo-5EvsvjGrcnWYJKXyhdFNx
system | Tmux session ID: demo
system | Listening at ./.overmind.sock
web    | Started with pid 57386...
assets | Started with pid 57387...
web    |
web    | 11:03:20 [rerun] Demo launched
web    | 11:03:20 [rerun] Rerun (57388) running Demo (57397)
web    | [57397] Puma starting in cluster mode...
web    | [57397] * Puma version: 6.4.0 (ruby 3.2.2-p53) ("The Eagle of Durango")
web    | [57397] *  Min threads: 5
web    | [57397] *  Max threads: 5
web    | [57397] *  Environment: development
web    | [57397] *   Master PID: 57397
web    | [57397] *      Workers: 8
web    | [57397] *     Restarts: (✔) hot (✖) phased
web    | [57397] * Preloading application
assets | file:///Users/bkuhlmann/Scratch/demo/config/assets.mjs:3
assets | await assets.run();
assets |              ^
assets |
assets | TypeError: assets.run is not a function
assets |     at file:///Users/bkuhlmann/Scratch/demo/config/assets.mjs:3:14
assets |     at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
assets |     at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
assets |     at async loadESM (node:internal/process/esm_loader:34:7)
assets |     at async handleMainPromise (node:internal/modules/run_main:113:12)
assets |
assets | Node.js v21.1.0
assets | Exited
web    | Interrupting...
web    | [57397] * Listening on http://0.0.0.0:2300
web    |
web    | 11:03:21 [rerun] Demo stopping
web    | Exited

The above is true when using Foreman as well so this isn't specific to Overmind.

How

To recreate, run:

hanami new demo
cd demo
overmind start --procfile Procfile.dev

On the other hand, using hanami server works since assets are not being used.

Notes

Here's my environment:

timriley commented 1 year ago

@bkuhlmann I don't think this is a problem with Overmind. I can use both foreman and overmind to launch from my Procfile.dev and they both work.

I think you hit a known issue that we plan to release a fix for in an rc2 release ASAP.

If you inspect your newly demo/ app, you might notice there is no package-lock.json. This is why node isn't loading hanami-assets properly: it's not installed, because we generated an incorrectly formatted version for the hanami-assets package in package.json.

If you go into package.json and change the version for hanami-assets to "^2.1.0-rc.1" and then run nom install, you should be good to go again. Let me know.

If you're looking to test, there's one more thing you'll need to fix too. Just go replace the contents of your app/templates/layouts/app.html.erb with the following (this fixes some helper names):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bookshelf</title>
    <%= favicon_tag %>
    <%= stylesheet_tag "app" %>
  </head>
  <body>
    <% if flash[:alert] %>
      <p><%= flash[:alert] %></p>
    <% end %>
    <% if flash[:notice] %>
      <p><%= flash[:notice] %></p>
    <% end %>

    <%= yield %>
    <%= javascript_tag "app" %>
  </body>
</html>

Let me know how you go! And thanks for testing this out :)

bkuhlmann commented 1 year ago

Thanks. Yes, I see the problem now. I had to use the following to properly confirm since I had installed JavaScript dependencies earlier:

rm -f package-lock.json
npm install

Using latest -- which is what I was originally using -- also works (didn't realize the package-lock.json wasn't being updated):

"dependencies": {
  "hanami-assets": "latest"
}

Closing this since I can make this work locally, now.