stimulus-rails 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ Description:
2
+ ============
3
+ Generates a new Stimulus controller at the passed path. If running under node,
4
+ it'll also call the stimulus:manifest:update task to update the controllers/index.js.
5
+
6
+ Examples:
7
+ =========
8
+ bin/rails generate stimulus chat
9
+
10
+ creates: app/javascript/controllers/chat_controller.js
11
+
12
+
13
+ bin/rails generate stimulus nested/chat
14
+
15
+ creates: app/javascript/controllers/nested/chat_controller.js
@@ -0,0 +1,16 @@
1
+ require "rails/generators/named_base"
2
+
3
+ class StimulusGenerator < Rails::Generators::NamedBase # :nodoc:
4
+ source_root File.expand_path("templates", __dir__)
5
+
6
+ def copy_view_files
7
+ @attribute = stimulus_attribute_value(name)
8
+ template "controller.js", "app/javascript/controllers/#{name}_controller.js"
9
+ rails_command "stimulus:manifest:update" if Rails.root.join("package.json").exist?
10
+ end
11
+
12
+ private
13
+ def stimulus_attribute_value(name)
14
+ name.gsub(/\//, "--")
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // Connects to data-controller="<%= @attribute %>"
4
+ export default class extends Controller {
5
+ connect() {
6
+ }
7
+ }
@@ -0,0 +1,10 @@
1
+ import { Application } from "@hotwired/stimulus"
2
+
3
+ const application = Application.start()
4
+
5
+ // Configure Stimulus development experience
6
+ application.warnings = true
7
+ application.debug = false
8
+ window.Stimulus = application
9
+
10
+ export { application }
@@ -1,13 +1,5 @@
1
- import { Application } from "@hotwired/stimulus"
2
-
3
- const application = Application.start()
4
-
5
- // Configure Stimulus development experience
6
- application.warnings = true
7
- application.debug = false
8
- window.Stimulus = application
9
-
10
1
  // Import and register all your controllers from the importmap under controllers/*
2
+
3
+ import { application } from "controllers/application"
11
4
  import { registerControllersFrom } from "@hotwired/stimulus-importmap-autoloader"
12
- const importmap = JSON.parse(document.querySelector("script[type=importmap]").text).imports
13
- registerControllersFrom("controllers", importmap, application)
5
+ registerControllersFrom("controllers", application)
@@ -1,12 +1,7 @@
1
- import { Application } from "@hotwired/stimulus"
1
+ // This file is auto-generated by ./bin/rails stimulus:manifest:update
2
+ // Run that command whenever you add a new controller
2
3
 
3
- const application = Application.start()
4
+ import { application } from "./application"
4
5
 
5
- // Configure Stimulus development experience
6
- application.warnings = true
7
- application.debug = false
8
- window.Stimulus = application
9
-
10
- // Import and register all your controllers
11
6
  import HelloController from "./hello_controller"
12
7
  application.register("hello", HelloController)
@@ -2,11 +2,13 @@ say "Create controllers directory"
2
2
  empty_directory "app/javascript/controllers"
3
3
  copy_file "#{__dir__}/app/javascript/controllers/index_for_importmap.js",
4
4
  "app/javascript/controllers/index.js"
5
+ copy_file "#{__dir__}/app/javascript/controllers/application.js",
6
+ "app/javascript/controllers/application.js"
5
7
  copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js",
6
8
  "app/javascript/controllers/hello_controller.js"
7
9
 
8
10
  say "Import Stimulus controllers"
9
- append_to_file "app/javascript/application.js", %(import "./controllers"\n)
11
+ append_to_file "app/javascript/application.js", %(import "controllers"\n)
10
12
 
11
13
  say "Pin Stimulus"
12
14
  append_to_file "config/importmap.rb" do <<-RUBY
@@ -1,19 +1,18 @@
1
1
  say "Create controllers directory"
2
2
  empty_directory "app/javascript/controllers"
3
-
4
- if Rails.root.join("config/webpacker.yml").exist?
5
- copy_file "#{__dir__}/app/javascript/controllers/index_for_webpacker.js",
6
- "app/javascript/controllers/index.js"
7
- else
8
- copy_file "#{__dir__}/app/javascript/controllers/index_for_node.js",
9
- "app/javascript/controllers/index.js"
10
- end
11
-
3
+ copy_file "#{__dir__}/app/javascript/controllers/index_for_node.js",
4
+ "app/javascript/controllers/index.js"
5
+ copy_file "#{__dir__}/app/javascript/controllers/application.js",
6
+ "app/javascript/controllers/application.js"
12
7
  copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js",
13
8
  "app/javascript/controllers/hello_controller.js"
14
9
 
15
- say "Import Stimulus controllers"
16
- append_to_file "app/javascript/application.js", %(import "./controllers"\n)
10
+ if (Rails.root.join("app/javascript/application.js")).exist?
11
+ say "Import Stimulus controllers"
12
+ append_to_file "app/javascript/application.js", %(import "./controllers"\n)
13
+ else
14
+ say %(You must import "./controllers" in your JavaScript entrypoint), :red
15
+ end
17
16
 
18
17
  say "Install Stimulus"
19
18
  run "yarn add @hotwired/stimulus"
@@ -0,0 +1,23 @@
1
+ module Stimulus::Manifest
2
+ extend self
3
+
4
+ def generate_from(controllers_path)
5
+ extract_controllers_from(controllers_path).collect do |controller_path|
6
+ module_path = controller_path.relative_path_from(controllers_path).to_s.remove(".js")
7
+ controller_class_name = module_path.camelize.gsub(/::/, "__")
8
+ tag_name = module_path.remove("_controller").gsub(/\//, "--")
9
+
10
+ <<-JS
11
+
12
+ import #{controller_class_name} from "./#{module_path}"
13
+ application.register("#{tag_name}", #{controller_class_name})
14
+ JS
15
+ end
16
+ end
17
+
18
+ def extract_controllers_from(directory)
19
+ (directory.children.select { |e| e.to_s =~ /_controller.js$/ } +
20
+ directory.children.select(&:directory?).collect { |d| extract_controllers_from(d) }
21
+ ).flatten
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Stimulus
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -1,3 +1,5 @@
1
+ require "stimulus/manifest"
2
+
1
3
  def run_stimulus_install_template(path) system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}" end
2
4
 
3
5
  namespace :stimulus do
@@ -23,4 +25,23 @@ namespace :stimulus do
23
25
  run_stimulus_install_template "stimulus_with_node"
24
26
  end
25
27
  end
28
+
29
+ namespace :manifest do
30
+ task :display do
31
+ puts Stimulus::Manifest.generate_from(Rails.root.join("app/javascript/controllers"))
32
+ end
33
+
34
+ task :update do
35
+ manifest =
36
+ Stimulus::Manifest.generate_from(Rails.root.join("app/javascript/controllers"))
37
+
38
+ File.open(Rails.root.join("app/javascript/controllers/index.js"), "w+") do |index|
39
+ index.puts "// This file is auto-generated by ./bin/rails stimulus:manifest:update"
40
+ index.puts "// Run that command whenever you add a new controller"
41
+ index.puts
42
+ index.puts %(import { application } from "./application")
43
+ index.puts manifest
44
+ end
45
+ end
46
+ end
26
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Stephenson
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-09-03 00:00:00.000000000 Z
13
+ date: 2021-09-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -40,15 +40,19 @@ files:
40
40
  - app/assets/javascripts/stimulus.js
41
41
  - app/assets/javascripts/stimulus@2.js
42
42
  - app/assets/javascripts/stimulus@3.0.0-beta.1.js
43
+ - app/assets/javascripts/stimulus@3.0.0-beta.2.js
44
+ - lib/generators/stimulus/USAGE
45
+ - lib/generators/stimulus/stimulus_generator.rb
46
+ - lib/generators/stimulus/templates/controller.js.tt
47
+ - lib/install/app/javascript/controllers/application.js
43
48
  - lib/install/app/javascript/controllers/hello_controller.js
44
49
  - lib/install/app/javascript/controllers/index_for_importmap.js
45
50
  - lib/install/app/javascript/controllers/index_for_node.js
46
- - lib/install/app/javascript/controllers/index_for_webpacker.js
47
- - lib/install/install.rb
48
51
  - lib/install/stimulus_with_importmap.rb
49
52
  - lib/install/stimulus_with_node.rb
50
53
  - lib/stimulus-rails.rb
51
54
  - lib/stimulus/engine.rb
55
+ - lib/stimulus/manifest.rb
52
56
  - lib/stimulus/version.rb
53
57
  - lib/tasks/stimulus_tasks.rake
54
58
  homepage: https://stimulus.hotwired.dev
@@ -1,14 +0,0 @@
1
- import { Application } from "@hotwired/stimulus"
2
-
3
- const application = Application.start()
4
-
5
- // Configure Stimulus development experience
6
- application.warnings = true
7
- application.debug = false
8
- window.Stimulus = application
9
-
10
- // Import and register all your controllers within this directory and all subdirectories
11
- // Controller files must be named *_controller.js or *_controller.ts
12
- import { definitionsFromContext } from "@hotwired/stimulus"
13
- const context = require.context("controllers", true, /_controller\.(js|ts)$/)
14
- application.load(definitionsFromContext(context))
@@ -1,10 +0,0 @@
1
- if (js_entrypoint_path = Rails.root.join("app/javascript/application.js")).exist?
2
- say "Appending Stimulus setup code to JavaScript entrypoint"
3
- append_to_file "app/javascript/application.js", %(\nimport "controllers")
4
- else
5
- end
6
-
7
- say "Creating controllers directory"
8
- directory "#{__dir__}/app/javascript/controllers", "app/javascript/controllers"
9
-
10
- say "Add @hotwired/stimulus package"