Comments (3)
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: import day from "dayjs"
.
Next, configure your server so that it knows how to resolve the dayjs
package name. You have several options, including:
- downloading the source as a vendored package or a gem that bundles its assets, configuring how to resolve the name in your
importmap.json
with a project-relative path (e.g."dayjs": asset_path("dayjs/dayjs.min.js")
), then serving it from the Rails application - downloading the source as a node_modules package, configuring how to resolve the name in your
importmap.json
with a project-relative Webpacker path (e.g."dayjs": asset_pack_path("dayjs/dayjs.min.js")
), then serving it from the Rails application - configuring how to resolve the name in your
importmap.json
with an absolute URL (e.g."dayjs": "https://cdn.skypack.dev/dayjs"
) - declaring the
import
statement directly in your module with an absolute URL (e.g.import day from "https://cdn.skypack.dev/dayjs"
)
Have you tried any of these other alternatives?
from stimulus-rails.
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.
-
I went to Skypack to get their browser-optimized packaged version of Dayjs: https://cdn.skypack.dev/dayjs and saved the content of that to
/app/assets/javascripts/libraries/dayjs/dayjs.js.erb
(note.erb
). That file looked like this on Skypack.export * from '/-/[email protected]/dist=es2020,mode=imports/optimized/dayjs.js'; export {default} from '/-/[email protected]/dist=es2020,mode=imports/optimized/dayjs.js';
which I changed to the following, using
asset_path
:export * from '<%= asset_path "libraries/dayjs/dayjs.export.js" %>'; export {default} from '<%= asset_path "libraries/dayjs/dayjs.export.js" %>';
-
That originally referenced https://cdn.skypack.dev//-/[email protected]/dist=es2020,mode=imports/optimized/dayjs.js file, I saved unchanged to
/app/assets/javascripts/libraries/dayjs/dayjs.export.js
-
In my
importmap.json.erb
I added:{ "imports": { "turbo": "<%= asset_path "turbo" %>", <%= importmap_list_with_stimulus_from "app/assets/javascripts/controllers", "app/assets/javascripts/libraries" %>, + "dayjs": "<%= asset_path "libraries/dayjs/dayjs.js" %>" } }
-
In my Stimulus controller, I added:
import { Controller } from "stimulus" + import dayjs from 'dayjs';
Thanks for a prod that led me in the right direction 🥳
from stimulus-rails.
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 🎉
from stimulus-rails.
Related Issues (20)
- Controllers written in Coffeescript? HOT 1
- why not auto "rails stimulus:manifest:update"
- Failing to register controllers does not bubble the error to handleError HOT 1
- stimulus-rails installation rails - undefined method Stimulus::Rails.application HOT 1
- manifest:update configuration for controller segmentation HOT 1
- Should controller names be registered as camelCase? HOT 2
- rails stimulus:manifest:update replacing our index.js configs HOT 1
- 404 on assets/stimulus-loading.js with config.assets.digest set to false HOT 11
- Mutation Observer not recognising DOM change when using Turbo Streams HOT 2
- Browser Back button is not working for some feature in stimulus HOT 2
- Console error with lazyLoadControllersFrom and external controller HOT 1
- Add typescript support for rails stimulus generator
- Action params not populating HOT 1
- Debugging `Failed to auto-load controller` ... `Unable to resolve specifier` HOT 2
- Error when passing JSON formatted data from rails to stimulus HOT 1
- Stimulus broken in Firefox 115+ with importmap-rails HOT 4
- Is it possible to use controllers stored in a GEM ?
- ActiveModel not working with value to Object HOT 2
- Add stimulus:manifest:update to the tasks description HOT 1
- Error in lib/install/stimulus_with_node.rb
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stimulus-rails.