-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Hello Stimulus <3 #1244
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
Hello Stimulus <3 #1244
Conversation
Use Rails 5 API when available, making easier to drop support of Rails 4.2 in future.
hello.setAttribute("data-controller", "hello") | ||
hello.setAttribute("data-target", "hello.output") | ||
document.body.appendChild(hello) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating an element with JavaScript isn't a very good introduction to Stimulus since you'll almost always be working with server-rendered HTML. Let's ditch this file and start people out right by appending the setup code to app/javascript/packs/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))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌 ok, ready for another look.
Appended the setup code to app/javascript/packs/application.js
and ditched the previous file.
…egrity-config Add line break after check_yarn_integrity template config
From CI "Use the new Ruby 1.9 hash syntax. inject_into_file "#{Webpacker.config.source_entry_path}/application.js", :after => "console.log('Hello World from Webpacker')" do"
lib/install/stimulus.rb
Outdated
@@ -0,0 +1,12 @@ | |||
say "Appending Stimulus setup code to #{Webpacker.config.source_entry_path}/application.js" | |||
inject_into_file "#{Webpacker.config.source_entry_path}/application.js", after: "console.log('Hello World from Webpacker')" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Injecting after console.log('Hello World from Webpacker')
feels kind of brittle. Maybe just append to the end?
@@ -0,0 +1,8 @@ | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename this file to application.js
since that's the file it ends up in. And move this leading whitespace to the injection.
import { Controller } from "stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = [ "output" ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a \n
lib/tasks/webpacker.rake
Outdated
"webpacker:install:erb" => "Installs Erb loader with an example", | ||
"webpacker:install:coffee" => "Installs CoffeeScript loader with an example", | ||
"webpacker:install:typescript" => "Installs Typescript loader with an example" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
static targets = [ "output" ] | ||
connect() { | ||
this.outputTarget.textContent = | ||
`Hello Stimulus!` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the \n
here or the interpolated string: this.outputTarget.textContent = 'Hello Stimulus!'
`warn` messages are silenced when `$VERBOSE=nil` (verbosity level 0). We're well beyond casual warning territory here and refusing to boot with an error.
Fix typo in the hello_* examples
add whitespace
remove whitespace
this.outputTarget.textContent = 'Hello Stimulus!'
👌 ready |
import { definitionsFromContext } from "stimulus/webpack-helpers" | ||
|
||
const application = Application.start() | ||
const context = require.context("./controllers", true, /.js$/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does "./controllers"
work? That's not the relative path to the controllers directory. When I tested, "controllers"
worked as expected since webpacker adds all root app/javascript/
directories to the load path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out. I uninstalled the old webpacker and now, it's working for me 🤷♂️
@@ -0,0 +1,12 @@ | |||
// Visit The Stimulus Handbook for more details | |||
// https://stimulusjs.org/handbook/introduction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to show the HTML this controller expects in the comments here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this Related??
Can you update the PR description, please? Just need to describe the webpacker changes and link to Stimulus where appropriate. |
// Visit The Stimulus Handbook for more details | ||
// https://stimulusjs.org/handbook/introduction | ||
// | ||
// This controller is expecting the following HTML. Paste it with the javascript_pack_tag helper. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
// This example controller works with specially annotated HTML like:
//
// <div data-controller="hello>
// <h1 data-target="hello.output"></h1>
// </div>
static targets = [ "output" ] | ||
|
||
connect() { | ||
this.outputTarget.textContent = 'Hello Stimulus!' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-'Hello Stimulus!'
+'Hello, Stimulus!'
// <div data-controller="hello" data-target="hello.output"> | ||
// </div> | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️extra newline
Updated description and pushed in the updated commit. If there are new reviews, I will push in tomorrow morning. Thanks for reviewing! |
lib/install/stimulus.rb
Outdated
end | ||
|
||
say "Creating controllers directory" | ||
directory "#{__dir__}/examples/stimulus/controllers", "#{Webpacker.config.source_entry_path}/controllers" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should create an app/javascript/controllers/
directory here, not app/javascript/packs/controllers/
. So use Webpacker.config.source_path
instead of Webpacker.config.source_entry_path
.
That's why #1244 (comment) worked for me and not you.
Once #1244 (comment) is fixed, please squash your commits and then we're good to go. |
webpacker:install:stimulus added controllers directory and hello_controller.js HELLO stimulus! Wrapping it up with stimulus.rb include stimulus in README.md Remove hello_stimulus.js Append setup code to application.js 👮♂️Satisfy robocop From CI "Use the new Ruby 1.9 hash syntax. inject_into_file "#{Webpacker.config.source_entry_path}/application.js", :after => "console.log('Hello World from Webpacker')" do" use append_to_file instead of inject_into_file add whitespace rename setup.js to application.js remove whitespace added a \n this.outputTarget.textContent = 'Hello Stimulus!' ✂️ ✂️ to appease robocop fix: cannot find module "controllers" revert back to "controllers" Added the expectant HTML in comments. 🤰 ✂️, Edit comments, and changed to 'Hello, Stimulus!' Use source_path instead of source_entry_path missing "
Good to go 👍🏻 |
It looks like you merged master. Can you rebase and squash instead, please? There's a linked guide in my last comment. |
lib/tasks/webpacker.rake
Outdated
@@ -11,6 +11,7 @@ tasks = { | |||
"webpacker:install:vue" => "Installs and setup example Vue component", | |||
"webpacker:install:angular" => "Installs and setup example Angular component", | |||
"webpacker:install:elm" => "Installs and setup example Elm component", | |||
"webpacker:install:stimulus" => "Install and setup Stimulus component", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-"Install and setup Stimulus component
+"Installs and setup example Stimulus component
Is more consistent to the rest od task description.
This is merged at #1255