Lucky - Stimulus

Public
Install StimulusJS in a Lucky Application
Used 21 times
Created by
S Stephen Dolan

Usage

From within a Lucky application with the `bloat` gem installed, run:
bloat with https://railsbytes.com/script/zl0sKQ

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/zl0sKQ"

Template Source

Review the code before running this template on your machine.

def build_sample_controller
  create_file "src/js/controllers/hello_controller.js" do<<~EOF
    import { Controller } from "stimulus";

    export default class extends Controller {
      // Defines a `this.helloTarget` that you can use in your functions.
      static get targets() {
        return ["hello"];
      }
      
      // Called whenever the controller is attached to an element on the page.
      initialize() {
        console.log("Hello! StimulusJS is working!");
      }
      
      // Can be called from within this file with `this.hello()`, or in an action in your HTML.
      hello() {
        console.log("Hello! You're in a StimulusJS function!");
      }
    }
  	EOF
  end

end

def yarn(*packages)
  run("yarn add #{packages.join(" ")}")
end

# Add StimulusJS to package.json
yarn "stimulus"

append_to_file "src/js/app.js" do<<~EOF
  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));
  EOF
end

if yes?("Would you like to add a sample StimulusJS controller to verify things are working? [y/N]")
  build_sample_controller
end
Mark
© 2020 GoRails, LLC