stimulus-rails 0.2.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +11 -22
  4. data/app/assets/javascripts/{stimulus/loaders/autoloader.js → stimulus-autoloader.js} +2 -2
  5. data/app/assets/javascripts/stimulus-importmap-autoloader.js +25 -0
  6. data/app/assets/javascripts/stimulus.js +1926 -0
  7. data/lib/generators/stimulus/USAGE +15 -0
  8. data/lib/generators/stimulus/stimulus_generator.rb +16 -0
  9. data/lib/generators/stimulus/templates/controller.js.tt +7 -0
  10. data/lib/install/app/javascript/controllers/application.js +10 -0
  11. data/lib/install/app/{assets/javascripts → javascript}/controllers/hello_controller.js +1 -1
  12. data/lib/install/app/javascript/controllers/index_for_importmap.js +5 -0
  13. data/lib/install/app/javascript/controllers/index_for_node.js +7 -0
  14. data/lib/install/stimulus_with_importmap.rb +19 -0
  15. data/lib/install/stimulus_with_node.rb +18 -0
  16. data/lib/stimulus/engine.rb +1 -17
  17. data/lib/stimulus/manifest.rb +27 -0
  18. data/lib/stimulus/version.rb +1 -1
  19. data/lib/tasks/stimulus_tasks.rake +32 -9
  20. metadata +21 -22
  21. data/app/assets/javascripts/stimulus/libraries/es-module-shims.js +0 -1
  22. data/app/assets/javascripts/stimulus/libraries/es-module-shims@0.7.1.js +0 -475
  23. data/app/assets/javascripts/stimulus/libraries/stimulus.js +0 -1
  24. data/app/assets/javascripts/stimulus/libraries/stimulus@2.js +0 -1699
  25. data/app/assets/javascripts/stimulus/manifest.js +0 -2
  26. data/app/helpers/stimulus/stimulus_helper.rb +0 -10
  27. data/lib/install/app/assets/javascripts/controllers/index.js +0 -9
  28. data/lib/install/app/assets/javascripts/importmap.json.erb +0 -5
  29. data/lib/install/application.js +0 -1
  30. data/lib/install/stimulus_with_asset_pipeline.rb +0 -23
  31. data/lib/install/stimulus_with_webpacker.rb +0 -10
  32. data/lib/stimulus/importmap_helper.rb +0 -33
@@ -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,4 +1,4 @@
1
- import { Controller } from "stimulus"
1
+ import { Controller } from "@hotwired/stimulus"
2
2
 
3
3
  export default class extends Controller {
4
4
  connect() {
@@ -0,0 +1,5 @@
1
+ // Import and register all your controllers from the importmap under controllers/*
2
+
3
+ import { application } from "controllers/application"
4
+ import { registerControllersFrom } from "@hotwired/stimulus-importmap-autoloader"
5
+ registerControllersFrom("controllers", application)
@@ -0,0 +1,7 @@
1
+ // This file is auto-generated by ./bin/rails stimulus:manifest:update
2
+ // Run that command whenever you add a new controller
3
+
4
+ import { application } from "./application"
5
+
6
+ import HelloController from "./hello_controller"
7
+ application.register("hello", HelloController)
@@ -0,0 +1,19 @@
1
+ say "Create controllers directory"
2
+ empty_directory "app/javascript/controllers"
3
+ copy_file "#{__dir__}/app/javascript/controllers/index_for_importmap.js",
4
+ "app/javascript/controllers/index.js"
5
+ copy_file "#{__dir__}/app/javascript/controllers/application.js",
6
+ "app/javascript/controllers/application.js"
7
+ copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js",
8
+ "app/javascript/controllers/hello_controller.js"
9
+
10
+ say "Import Stimulus controllers"
11
+ append_to_file "app/javascript/application.js", %(import "controllers"\n)
12
+
13
+ say "Pin Stimulus"
14
+ append_to_file "config/importmap.rb" do <<-RUBY
15
+ pin "@hotwired/stimulus", to: "stimulus.js"
16
+ pin "@hotwired/stimulus-importmap-autoloader", to: "stimulus-importmap-autoloader.js"
17
+ pin_all_from "app/javascript/controllers", under: "controllers"
18
+ RUBY
19
+ end
@@ -0,0 +1,18 @@
1
+ say "Create controllers directory"
2
+ empty_directory "app/javascript/controllers"
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"
7
+ copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js",
8
+ "app/javascript/controllers/hello_controller.js"
9
+
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
16
+
17
+ say "Install Stimulus"
18
+ run "yarn add @hotwired/stimulus"
@@ -1,24 +1,8 @@
1
- require "stimulus/importmap_helper"
2
-
3
1
  module Stimulus
4
2
  class Engine < ::Rails::Engine
5
- config.autoload_once_paths = %w( #{root}/app/helpers )
6
-
7
3
  initializer "stimulus.assets" do
8
4
  if Rails.application.config.respond_to?(:assets)
9
- Rails.application.config.assets.precompile += %w( importmap.json stimulus/manifest )
10
- end
11
- end
12
-
13
- initializer "stimulus.helpers" do
14
- ActiveSupport.on_load(:action_controller_base) do
15
- helper Stimulus::StimulusHelper
16
- end
17
-
18
- if Rails.application.config.respond_to?(:assets)
19
- Rails.application.config.assets.configure do |env|
20
- env.context_class.class_eval { include Stimulus::ImportmapHelper }
21
- end
5
+ Rails.application.config.assets.precompile += %w( stimulus )
22
6
  end
23
7
  end
24
8
  end
@@ -0,0 +1,27 @@
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
+ import_and_register_controller(controllers_path, controller_path)
7
+ end
8
+ end
9
+
10
+ def import_and_register_controller(controllers_path, controller_path)
11
+ module_path = controller_path.relative_path_from(controllers_path).to_s.remove(".js")
12
+ controller_class_name = module_path.camelize.gsub(/::/, "__")
13
+ tag_name = module_path.remove("_controller").gsub(/_/, "-").gsub(/\//, "--")
14
+
15
+ <<-JS
16
+
17
+ import #{controller_class_name} from "./#{module_path}"
18
+ application.register("#{tag_name}", #{controller_class_name})
19
+ JS
20
+ end
21
+
22
+ def extract_controllers_from(directory)
23
+ (directory.children.select { |e| e.to_s =~ /_controller.js$/ } +
24
+ directory.children.select(&:directory?).collect { |d| extract_controllers_from(d) }
25
+ ).flatten.sort
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Stimulus
2
- VERSION = "0.2.4"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -1,24 +1,47 @@
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
4
6
  desc "Install Stimulus into the app"
5
7
  task :install do
6
- if defined?(Webpacker::Engine)
7
- Rake::Task["stimulus:install:webpacker"].invoke
8
+ if Rails.root.join("config/importmap.rb").exist?
9
+ Rake::Task["stimulus:install:importmap"].invoke
10
+ elsif Rails.root.join("package.json").exist?
11
+ Rake::Task["stimulus:install:node"].invoke
8
12
  else
9
- Rake::Task["stimulus:install:asset_pipeline"].invoke
13
+ puts "You must either be running with node (package.json) or importmap-rails (config/importmap.rb) to use this gem."
10
14
  end
11
15
  end
12
16
 
13
17
  namespace :install do
14
- desc "Install Stimulus on the app with the asset pipeline"
15
- task :asset_pipeline do
16
- run_stimulus_install_template "stimulus_with_asset_pipeline"
18
+ desc "Install Stimulus on an app running importmap-rails"
19
+ task :importmap do
20
+ run_stimulus_install_template "stimulus_with_importmap"
21
+ end
22
+
23
+ desc "Install Stimulus on an app running node"
24
+ task :node do
25
+ run_stimulus_install_template "stimulus_with_node"
17
26
  end
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"))
18
37
 
19
- desc "Install Stimulus on the app with webpacker"
20
- task :webpacker do
21
- run_stimulus_install_template "stimulus_with_webpacker"
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
22
45
  end
23
46
  end
24
47
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Stephenson
8
8
  - Javan Mahkmali
9
9
  - David Heinemeier Hansson
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-06-12 00:00:00.000000000 Z
13
+ date: 2021-09-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -26,7 +26,7 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: 6.0.0
29
- description:
29
+ description:
30
30
  email: david@loudthinking.com
31
31
  executables: []
32
32
  extensions: []
@@ -35,31 +35,30 @@ files:
35
35
  - MIT-LICENSE
36
36
  - README.md
37
37
  - Rakefile
38
- - app/assets/javascripts/stimulus/libraries/es-module-shims.js
39
- - app/assets/javascripts/stimulus/libraries/es-module-shims@0.7.1.js
40
- - app/assets/javascripts/stimulus/libraries/stimulus.js
41
- - app/assets/javascripts/stimulus/libraries/stimulus@2.js
42
- - app/assets/javascripts/stimulus/loaders/autoloader.js
43
- - app/assets/javascripts/stimulus/manifest.js
44
- - app/helpers/stimulus/stimulus_helper.rb
45
- - lib/install/app/assets/javascripts/controllers/hello_controller.js
46
- - lib/install/app/assets/javascripts/controllers/index.js
47
- - lib/install/app/assets/javascripts/importmap.json.erb
48
- - lib/install/application.js
49
- - lib/install/stimulus_with_asset_pipeline.rb
50
- - lib/install/stimulus_with_webpacker.rb
38
+ - app/assets/javascripts/stimulus-autoloader.js
39
+ - app/assets/javascripts/stimulus-importmap-autoloader.js
40
+ - app/assets/javascripts/stimulus.js
41
+ - lib/generators/stimulus/USAGE
42
+ - lib/generators/stimulus/stimulus_generator.rb
43
+ - lib/generators/stimulus/templates/controller.js.tt
44
+ - lib/install/app/javascript/controllers/application.js
45
+ - lib/install/app/javascript/controllers/hello_controller.js
46
+ - lib/install/app/javascript/controllers/index_for_importmap.js
47
+ - lib/install/app/javascript/controllers/index_for_node.js
48
+ - lib/install/stimulus_with_importmap.rb
49
+ - lib/install/stimulus_with_node.rb
51
50
  - lib/stimulus-rails.rb
52
51
  - lib/stimulus/engine.rb
53
- - lib/stimulus/importmap_helper.rb
52
+ - lib/stimulus/manifest.rb
54
53
  - lib/stimulus/version.rb
55
54
  - lib/tasks/stimulus_tasks.rake
56
- homepage: https://stimulus.hotwire.dev
55
+ homepage: https://stimulus.hotwired.dev
57
56
  licenses:
58
57
  - MIT
59
58
  metadata:
60
- homepage_uri: https://stimulus.hotwire.dev
59
+ homepage_uri: https://stimulus.hotwired.dev
61
60
  source_code_uri: https://github.com/hotwired/stimulus-rails
62
- post_install_message:
61
+ post_install_message:
63
62
  rdoc_options: []
64
63
  require_paths:
65
64
  - lib
@@ -75,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
74
  version: '0'
76
75
  requirements: []
77
76
  rubygems_version: 3.1.4
78
- signing_key:
77
+ signing_key:
79
78
  specification_version: 4
80
79
  summary: A modest JavaScript framework for the HTML you already have.
81
80
  test_files: []
@@ -1 +0,0 @@
1
- //= require ./es-module-shims@0.7.1.js