I’ve been setting up a new app, without webpack and using the asset pipeline. Turbo + stimulus.
I have a folder for a library that I created for some common page functions, CSS functions, etc. Took me a while, but I successfully got it working and everything importing in dev. It’s structured like so:
- app / assets / javascripts
- controllers - normal controllers here
- libraries
- sm - my library folder
- sm.js - entry point that exports my functions.
- sm - my library folder
In my controller, I reference my sm.js exports with a normal import:
import {Animation, CSS} from 'sm'
Works great in dev. Once I deploy to production (Heroku) or run it locally with RAILS_ENV=production, the autoloader cannot find my sm library at all. In the console it generates the usual errors when it can’t find a library. BUT:
- public/assets/libraries has the files I expect generated from the precompile
- public/assets/importmap.json has all the files I would expect listed, and the paths with the randomly generated strings all exist.
So basically, there is something not joining up somewhere but I’m not sure why and not sure what to look at next.
Update
One thing I didn’t explain above is that my sm.js file also includes two files animation.js and css.js that are in the same folder. Just now looking at the browser console it appears that Importmap IS loading the sm.js file fine, but when that file tries to import its dependencies, it using the base filename and NOT including the asset precompiled file name with the unique code.
I’m guessing that I need to convert sm.js into sm.js.erb, and use <% asset_path %> to reference those included files?