stimulus-rails 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e7e80a2e07dc6e40f109bc5c009bbea41b02057c8a7b489b638c0a964d14f22
4
- data.tar.gz: aa0979ef349cd0c4a49b27c11448663cb40728643bbf43f68a3136ae491e7ab7
3
+ metadata.gz: d527795451c1f97bf1bc540f6cb5ce4171a4f016df1d8f60b30da8e96f7adf27
4
+ data.tar.gz: 7478ece303c9b51ed819d7a04e8ff3e7f578b6debb17ebc7ea00a8afd2fbd2f3
5
5
  SHA512:
6
- metadata.gz: 694ab07e593aca69e7abfb0e22c4459ddc27be5d7a5f8cdaa35dfb957f05d6da45e580ec7b9c22af09911a0c2901b4b459a4fdad5ecbbf9165052c047d44d56e
7
- data.tar.gz: 3c1412141e0c2a9c2b29fea34f3946c0c14610d69b37c15557100d0a12422948f9be6d6d8977b7059d8b0aa233e1456eb3d82b78ec164881df39a2d1c3a8dc1d
6
+ metadata.gz: 8dfe74aba6df874a5f3d8f8fe701f78b79d3f31577383a673f3556c1607c9cf1d80e5df6301327aa5bfb816b7ee36c3916d9b9ea1c57bdfc4b0fedf96c0b8fa6
7
+ data.tar.gz: 4dec5a152ecac26df7008230f791bf4270518de1ab48379c405a30f4485d07c5443dcde5bb892baab33351949f2bd529e31df1180a4d217db7adcb902586fb88
data/README.md CHANGED
@@ -11,31 +11,24 @@ Stimulus for Rails makes it easy to use this modest framework with the asset pip
11
11
  2. Run `./bin/bundle install`.
12
12
  3. Run `./bin/rails stimulus:install`
13
13
 
14
- If using the asset pipeline to manage JavaScript, the last command will:
15
-
16
- 1. Create an example controller in `app/assets/javascripts/controllers/hello_controller.js`.
17
- 2. Append `import "@hotwired/stimulus-autoloader"` to your `app/assets/javascripts/application.js` entrypoint.
18
-
19
- Make sure you've already installed `importmap-rails` and that it's referenced before `stimulus-rails` (or `hotwire-rails`) in your Gemfile.
20
-
21
- If using Webpacker to manage JavaScript, the last command will:
22
-
23
- 1. Import the controllers directory in the application pack.
24
- 2. Create a controllers directory at `app/javascripts/controllers`.
25
- 3. Create an example controller in `app/javascripts/controllers/hello_controller.js`.
26
- 4. Install the Stimulus NPM package.
14
+ The installer will automatically detect whether you're using an import map or node to manage your application's JavaScript. If you're using an import map, the Stimulus dependencies will be pinned to the versions of the library included with this gem. If you're using node, yarn will add the dependencies to your package.json file.
27
15
 
28
16
 
29
17
  ## Usage
30
18
 
31
- With the installation done, you'll automatically have activated Stimulus through the controller autoloader. You can now easily add new Stimulus controllers that'll be loaded via ESM dynamic imports.
19
+ The installer amends your JavaScript entry point at `app/javascript/application.js` to import the `app/javascript/controllers/index.js` file, which is responsible for setting up your Stimulus application and registering your controllers.
20
+
21
+ With an import-mapped application, controllers are automatically pinned and registered based on the file structure. With a node application, controllers need to be imported and registered directly in the index.js file, but this is done automatically using either the Stimulus generator (`./bin/rails generate stimulus [controller]`) or the dedicated `stimulus:manifest:update` task. Either will overwrite the `controllers/index.js` file.
32
22
 
33
- For example, a more advanced `hello_controller` could look like this:
23
+ You're encouraged to use the generator to add new controllers like so:
34
24
 
35
25
  ```javascript
26
+ // Run "./bin/rails g stimulus hello" to create the file and update the index, then amend:
27
+
36
28
  // app/assets/javascripts/controllers/hello_controller.js
37
- import { Controller } from "stimulus"
29
+ import { Controller } from "@hotwired/stimulus"
38
30
 
31
+ // Connects with data-controller="hello"
39
32
  export default class extends Controller {
40
33
  static targets = [ "name", "output" ]
41
34
 
@@ -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,12 +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
5
  registerControllersFrom("controllers", application)
@@ -1,11 +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
- import("./hello_controller").then(c => application.register("hello", c.default))
6
+ import HelloController from "./hello_controller"
7
+ application.register("hello", HelloController)
@@ -2,6 +2,8 @@ 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
 
@@ -1,14 +1,9 @@
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
 
@@ -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.2"
2
+ VERSION = "0.5.0"
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.2
4
+ version: 0.5.0
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-04 00:00:00.000000000 Z
13
+ date: 2021-09-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -40,15 +40,18 @@ 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
+ - lib/generators/stimulus/USAGE
44
+ - lib/generators/stimulus/stimulus_generator.rb
45
+ - lib/generators/stimulus/templates/controller.js.tt
46
+ - lib/install/app/javascript/controllers/application.js
43
47
  - lib/install/app/javascript/controllers/hello_controller.js
44
48
  - lib/install/app/javascript/controllers/index_for_importmap.js
45
49
  - 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
50
  - lib/install/stimulus_with_importmap.rb
49
51
  - lib/install/stimulus_with_node.rb
50
52
  - lib/stimulus-rails.rb
51
53
  - lib/stimulus/engine.rb
54
+ - lib/stimulus/manifest.rb
52
55
  - lib/stimulus/version.rb
53
56
  - lib/tasks/stimulus_tasks.rake
54
57
  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"