Non easy start
See original GitHub issueExample of my experience trying this for the first time, written as a beginner to see if we can find improvements:
- While working on a new Rails app I thought about using Stimulus.
- I already have a Rails (5.2) project and do not want another new project. I want to enhance my project with some Javascript goodness.
- As a inexperienced user with Stimulus I hoped it would feel more welcoming than for example React.
- And I expected a smooth integration with Rails. Something like: Add this to your Gemfile, hit bundle and go.
But I got dragged into: https://stimulusjs.org/handbook/installing and other manuals and encounter:
Stimulus integrates with the webpack asset packager to automatically load controller files from a folder in your app.
add the stimulus npm package to your JavaScript bundle
(Note that the stimulus-starter project uses the Yarn package manager for dependency management, so make sure you have that installed first.)
Some code I really have no clue of why it’s needed:
// src/application.js
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
const application = Application.start()
const context = require.context("./controllers", true, /\.js$/)
application.load(definitionsFromContext(context))
It’s using paths like /src which seem familiar with React, coming from a Rails project they don’t make sense.
It feels like it does not optimally support the nagging sense:
If, on the other hand, you have nagging sense that what you’re working on does not warrant the intense complexity and application separation such contemporary techniques imply, then you’re likely to find refuge in our approach. https://stimulusjs.org/handbook/origin
I don’t know whether I want NPM or Yarn, or how they mix. I don’t want to understand how controllers are loaded. I want to add some javascript feature like the example at the homepage: https://stimulusjs.org
Wouldn’t it be possible to give the installation experience a more Rails like experience?
After a quite long time I found the Discourse forum via one of the issues. There seem to be more people experiencing this at first:
https://discourse.stimulusjs.org/t/organize-controllers-in-rails-app-without-webpacker/224 https://discourse.stimulusjs.org/t/work-with-rails/43
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:5 (2 by maintainers)
Top GitHub Comments
@adrienpoly Thanks for the link and command! That did it!
The steps I took:
I run:
rails webpacker:install:stimulus
I get error:
What I needed to do:
Add this to Gemfile:
gem 'webpacker', '~> 3.5'
Then:
bundle install
Then I got error:
Then I go to: https://yarnpkg.com/lang/en/docs/install/ with a complicated story but luckily I had brew installed. So I run:
brew install yarn
That one fails too:
Then I run:
brew upgrade yarn
Then I run again:
rails webpacker:install:stimulus
Fails:
So I run:
rails webpacker:install
Then I run again:
rails webpacker:install:stimulus
🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉
I then manually added this into application.html.erb layout:
<%= javascript_pack_tag 'application' %>
See, it doesn’t really feel as smooth as installing Rails things. Hopefully this may become more easy in the future. I think for sure that the manual of Stimulus needs to have a clear direction towards your command:
rails webpacker:install:stimulus
Which is essentially the correct installation command but is missing.
well to install stimulus in rails you can use the official installer
https://github.com/rails/webpacker#stimulus
for a new app
rails new myapp --webpack=stimulus
or
to add it to an existing app
rails webpacker:install:stimulus
While it is well documented in webpacker a link in the handbook would be nice