-
Notifications
You must be signed in to change notification settings - Fork 86
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
How to import vendor library to use in Stimulus controller, Asset Pipeline version #51
Comments
This engine is built to leverage ECMAScript Modules. If your project has an external dependency, you have several options. First, declare the import statement in the module that needs it: Next, configure your server so that it knows how to resolve the
Have you tried any of these other alternatives? |
Ok i've finally figured out 👏 how to import a library. Maybe this is common knowledge for others but ES6 is a brave new world for me. I'm generally stuck back in Year 2000 Javascript. I didn't want to use a gem that bundled the library for the asset pipeline, and I didn't want to call skypack or a remote CDN. I wanted to serve it locally.
Thanks for a prod that led me in the right direction 🥳 |
Better yet, I've just realised that I don't need to do this required "date logic" and calculations in javascript anymore, because I should now be able to use a turbo frame and keep the logic in Rails 🎉 |
I'm using Stimulus in Rails 6 via the Asset Pipeline and want to use either momentjs or dayjs library within my controller. How do I import those?
I've tried saving the dayjs.min.js file to vendors/assets/javascripts/dayjs/dayjs.min.js, but do I need to add anything to app/assets/config/manifest.js? or maybe app/assets/javascripts/importmap.json.erb
manifest.js looks like this:
importmap.json.erb looks like this:
This is the bit where I'm stuck. I get the same error:
I think then once the library is "available", I should be import it to be available in my Stimulus controller with 🙏 :
import { Controller } from "stimulus" + import dayjs from 'dayjs' export default class extends Controller { ...
If I try the following with importmap.json.erb:
{ "imports": { "turbo": "<%= asset_path "turbo" %>", + "dayjs": "<%= asset_path "dayjs/dayjs.min.js" %>", <%= importmap_list_with_stimulus_from "app/assets/javascripts/controllers", "app/assets/javascripts/libraries" %> } }
The error changes to the following when I reload the page:
The text was updated successfully, but these errors were encountered: