stimulus-rails 0.3.11 → 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: 64761f6c52233208ab99ba06ecc3d99a4df3ec15a43bdda8e9059714a5e1a6ee
4
- data.tar.gz: 8a7416e6f82f089575a87b27fa7706cd0aa1f5ea01786aa0754cfce51e0c9d7c
3
+ metadata.gz: d527795451c1f97bf1bc540f6cb5ce4171a4f016df1d8f60b30da8e96f7adf27
4
+ data.tar.gz: 7478ece303c9b51ed819d7a04e8ff3e7f578b6debb17ebc7ea00a8afd2fbd2f3
5
5
  SHA512:
6
- metadata.gz: 929c0266cf1ed01dee781af2d228481b3e4f4164bdc09c3c9e1505ced0a40c4223fa54849ace773806d2be3df300103ed8099867735fbefa35e5288110ba799f
7
- data.tar.gz: 02d4931af10963beef58c26379499486326b355e325078e24649c6ab92e0351bfbabb936fc72202df31bb10de8c9a477bf698d27df10476260fd6604ae0e192d
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
 
@@ -1,21 +1,25 @@
1
- import { Application } from "@hotwired/stimulus"
1
+ // FIXME: es-module-shim won't shim the dynamic import without this explicit import
2
+ import "@hotwired/stimulus"
2
3
 
3
- function registerControllersFrom(under) {
4
- const paths = Object.keys(importmap).filter(path => path.match(new RegExp(`^${under}/.*_controller$`)))
5
- paths.forEach(path => registerControllerFromPath(path, under))
4
+ export function registerControllersFrom(under, application) {
5
+ const paths = Object.keys(parseImportmapJson())
6
+ .filter(path => path.match(new RegExp(`^${under}/.*_controller$`)))
7
+
8
+ paths.forEach(path => registerControllerFromPath(path, under, application))
9
+ }
10
+
11
+ export function parseImportmapJson() {
12
+ return JSON.parse(document.querySelector("script[type=importmap]").text).imports
6
13
  }
7
14
 
8
- function registerControllerFromPath(path, under) {
9
- const name = path.replace(`${under}/`, "").replace("_controller", "").replace(/\//g, "--").replace(/_/g, "-")
15
+ function registerControllerFromPath(path, under, application) {
16
+ const name = path
17
+ .replace(`${under}/`, "")
18
+ .replace("_controller", "")
19
+ .replace(/\//g, "--")
20
+ .replace(/_/g, "-")
10
21
 
11
22
  import(path)
12
23
  .then(module => application.register(name, module.default))
13
24
  .catch(error => console.log(`Failed to register controller: ${name} (${path})`, error))
14
25
  }
15
-
16
- const application = Application.start()
17
- const importmap = JSON.parse(document.querySelector("script[type=importmap]").text).imports
18
-
19
- registerControllersFrom("controllers")
20
-
21
- export { application, registerControllersFrom }
@@ -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 }
@@ -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"
@@ -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.3.11"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,26 +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
- elsif defined?(Importmap)
9
- Rake::Task["stimulus:install:asset_pipeline"].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
10
12
  else
11
- puts "You must either be running Webpacker or importmap-rails to use this gem."
13
+ puts "You must either be running with node (package.json) or importmap-rails (config/importmap.rb) to use this gem."
12
14
  end
13
15
  end
14
16
 
15
17
  namespace :install do
16
- desc "Install Stimulus on the app with the asset pipeline"
17
- task :asset_pipeline do
18
- 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"
19
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"))
20
37
 
21
- desc "Install Stimulus on the app with webpacker"
22
- task :webpacker do
23
- 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
24
45
  end
25
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.3.11
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-08-31 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,12 +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
- - lib/install/app/javascript/controllers/index.js
45
- - lib/install/stimulus_with_asset_pipeline.rb
46
- - lib/install/stimulus_with_webpacker.rb
48
+ - lib/install/app/javascript/controllers/index_for_importmap.js
49
+ - lib/install/app/javascript/controllers/index_for_node.js
50
+ - lib/install/stimulus_with_importmap.rb
51
+ - lib/install/stimulus_with_node.rb
47
52
  - lib/stimulus-rails.rb
48
53
  - lib/stimulus/engine.rb
54
+ - lib/stimulus/manifest.rb
49
55
  - lib/stimulus/version.rb
50
56
  - lib/tasks/stimulus_tasks.rake
51
57
  homepage: https://stimulus.hotwired.dev
@@ -1,9 +0,0 @@
1
- // Load all the controllers within this directory and all subdirectories.
2
- // Controller files must be named *_controller.js or *_controller.ts.
3
-
4
- import { Application } from "stimulus"
5
- import { definitionsFromContext } from "stimulus/webpack-helpers"
6
-
7
- window.Stimulus = Application.start()
8
- const context = require.context("controllers", true, /_controller\.(js|ts)$/)
9
- Stimulus.load(definitionsFromContext(context))
@@ -1,26 +0,0 @@
1
- APP_JS_ROOT = Rails.root.join("app/javascript")
2
- APP_JS_PATH = APP_JS_ROOT.join("application.js")
3
- IMPORTMAP_PATH = Rails.root.join("config/importmap.rb")
4
-
5
- if APP_JS_PATH.exist?
6
- say "Import Stimulus importmap autoloader in existing app/javascript/application.js"
7
- append_to_file APP_JS_PATH, %(\nimport "@hotwired/stimulus-importmap-autoloader"\n)
8
- else
9
- say <<~INSTRUCTIONS, :red
10
- You must import @hotwired/stimulus and @hotwired/stimulus-importmap-autoloader in your application.js.
11
- INSTRUCTIONS
12
- end
13
-
14
- say "Creating controllers directory"
15
- copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js", APP_JS_ROOT.join("controllers/hello_controller.js")
16
-
17
- if IMPORTMAP_PATH.exist?
18
- say "Pin @hotwired/stimulus and @hotwired/stimulus-importmap-autoloader in config/importmap.rb"
19
- append_to_file \
20
- IMPORTMAP_PATH.to_s,
21
- %(pin "@hotwired/stimulus", to: "stimulus.js"\npin "@hotwired/stimulus-importmap-autoloader", to: "stimulus-importmap-autoloader.js"\npin_all_from "app/javascript/controllers", under: "controllers"\n)
22
- else
23
- say <<~INSTRUCTIONS, :red
24
- You must add @hotwired/stimulus and @hotwired/stimulus-importmap-autoloader to your importmap to reference them via ESM.
25
- INSTRUCTIONS
26
- end
@@ -1,11 +0,0 @@
1
- say "Appending Stimulus setup code to #{Webpacker.config.source_entry_path}/application.js"
2
- append_to_file "#{Webpacker.config.source_entry_path}/application.js", %(\nimport "controllers")
3
-
4
- say "Creating controllers directory"
5
- directory "#{__dir__}/app/javascript/controllers", "#{Webpacker.config.source_path}/controllers"
6
-
7
- say "Using Stimulus NPM package"
8
- gsub_file "#{Webpacker.config.source_path}/controllers/hello_controller.js", /@hotwired\//, ''
9
-
10
- say "Installing all Stimulus dependencies"
11
- run "yarn add stimulus"