Description
I tried to migrate from webpacker
to importmap-rails
and managed to port everything, including migrating from turbolinks to turbo-rails
. Everything worked well in development mode, but broke as soon as I deployed the application. Specifically, a JavaScript file i loaded from application.js
was not found. Here's my setup:
app/javascript/application.js
import "@hotwired/turbo-rails"
import { Filter } from './src/Filter.js';
/* some more js code skipped for brevity */
app/javascript/src/filter.js
class Filter {
/* some more js code skipped for brevity */
}
As soon as I run the application in production mode, I get the following browser error when loading the page: Uncaught Error: 404 Not Found https://example.com/assets/src/Filter.js
I tried the following mitigations and many possible combinations:
- Adding
pin_all_from "app/javascript/src", under: "src"
toconfig/importmap.rb
- Moving the filter.js file to app/assets/javascripts/src, and adding
//= link_tree ../../../app/assets/javascripts/src .js
toapp/assets/config/manifest.js
, as well as addingpin_all_from "app/assets/javascripts/src", under: "src"
toconfig/importmap.rb
- Adding
Rails.application.config.assets.precompile += %w( src/*.js )
toconfig/initializers/assets.rb
I noticed that I can get the asset pipeline to compile filter.js
(I do get a digested version in public/assets/src
), but when the JS code is run in production, it always tries to load the non-digested version.
How can I import files that are available locally using importmaps?
I can provide a minimal example if the issue is unknown and if that helps.