rails, stimulus, engine
mkdir hello_world && cd hello_world
echo 3.2.2 > .ruby-version
gem install bundler rails
rails plugin new . --mountable --database=sqlite3 --no-skip-javascript
require_relative "lib/hello_world/version" Gem::Specification.new do |spec| spec.name = "hello_world" spec.version = HelloWorld::VERSION spec.authors = ["Tomasz Kowalewski"] spec.email = ["me@tkowalewski.pl"] spec.homepage = "https://hello-world.com" spec.summary = "Summary of HelloWorld." spec.description = "Description of HelloWorld." spec.license = "MIT" # Prevent pushing this gem to RubyGems.org. To allow pushes either set the "allowed_push_host" # to allow pushing to a single host or delete this section to allow pushing to any host. spec.metadata["allowed_push_host"] = "https://rubygems.org" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = "https://github.com/tkowalewski/hello_world" spec.metadata["changelog_uri"] = "https://github.com/tkowalewski/hello_world/blob/main/CHANGELOG.md" spec.files = Dir.chdir(File.expand_path(__dir__)) do Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] end spec.add_dependency "rails", ">= 7.1.2" end
touch CHANGELOG.md
spec.add_dependency "importmap-rails", "~> 1.2", ">= 1.2.3" spec.add_dependency "stimulus-rails", "~> 1.3"
bundle install
//= link_directory ../javascripts/hello_world .js
//= link_tree ../javascripts/hello_world .js
import { Controller } from "@hotwired/stimulus" export default class extends Controller { connect() { console.log("Connected") this.element.textContent = "Test" } }
module HelloWorld class HomeController < ApplicationController def index; end end end
HelloWorld::Engine.routes.draw do root to: 'home#index' end
<div data-controller="test"></div>
<%= javascript_importmap_tags %>
pin_all_from File.expand_path("../app/assets/javascripts", __dir__)
module HelloWorld class Engine < ::Rails::Engine isolate_namespace HelloWorld initializer 'hello_world-engine.importmap', before: 'importmap' do |app| app.config.importmap.paths += [Engine.root.join('config/importmap.rb')] end initializer "hello_world-engine.assets" do |app| app.config.assets.precompile += %w[hello_world_manifest.js] end end end
gem 'importmap-rails', '~> 1.2', '>= 1.2.3' gem 'stimulus-rails', '~> 1.3'
bundle install
cd test/dummy && bin/rails importmap:install stimulus:install
mkdir app/assets/javascripts && touch app/assets/javascripts/.keep
eagerLoadControllersFrom("hello_world", application)
// Import and register all your controllers from the importmap under controllers/* import { application } from "controllers/application" // Eager load all controllers defined in the import map under controllers/**/*_controller import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading" eagerLoadControllersFrom("controllers", application) eagerLoadControllersFrom("hello_world", application) // Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!) // import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading" // lazyLoadControllersFrom("controllers", application)
cd ../../ && bin/rails s
gem "hello_world"
bundle install
mount HelloWorld::Engine => "/hello_world"
eagerLoadControllersFrom("hello_world", application)