stimulus-rails 0.3.1 → 0.3.5

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: 19ab1007269aa440db2fb6a93b7ace6b4a3998e4dd6cb52e5a90b839df9a9902
4
- data.tar.gz: c3ea9e2693c48c103b2dea61b80f6ba9ec3ce54c25be65dd1305876e8149284e
3
+ metadata.gz: 137cfce6bd7f22e364b007518b204f8d4f910190e9aa3eb02325bc72df4126a3
4
+ data.tar.gz: fbdfaa35d93547b47c7a1ec9beb41ce55f5b513db29a5e94c66c3de639fa9834
5
5
  SHA512:
6
- metadata.gz: 01c93b2f7cc12890a42be77fea0d6e044392d5c144933bcbf5cfe31b6c7311141a4859ffbb7dd192d980089f18242c3e27963dc40b0fd1d89a5e808b80f05364
7
- data.tar.gz: 7cae42a9c82935007b292f524720e080805a076dde248e533f6fb1600421c192ebcf6050a3ec7843a348f593a4efbea1b5b89ff045d0be28d5ece53fff8fbcaa
6
+ metadata.gz: 3d72a10a00514a1c3dc0466f653fc9f40ebe8044ab11d3b8d4704e3e56dde8bd9d9733d25634206adeef373a4c5fae8abd2b44e1a458e16969981073889de781
7
+ data.tar.gz: b8f826cd66e344b302f893d88659471cd503b63a530613cd6fa03ce7697dc9060d2b7f8f2ed23b1123baf5f0cb7ddd3f833472f6591116f4c7ac74f53c5d8007
@@ -0,0 +1,14 @@
1
+ import { Application } from "@hotwired/stimulus"
2
+
3
+ const application = Application.start()
4
+
5
+ const importmap = await JSON.parse(document.querySelector("script[type=importmap]").text)
6
+ const importedControllerPaths = Object.keys(importmap.imports).filter((e) => e.match("controllers/"))
7
+
8
+ importedControllerPaths.forEach(function(path) {
9
+ const name = path.replace("controllers/", "").replace("_controller", "").replace("/", "--").replace("_", "-")
10
+
11
+ import(path)
12
+ .then(module => application.register(name, module.default))
13
+ .catch(error => console.log(`Failed to autoload controller: ${name}`, error))
14
+ })
@@ -1,7 +1,27 @@
1
- APP_JS_ROOT = Rails.root.join("app/assets/javascripts")
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/initializers/importmap.rb")
2
4
 
3
- say "Import Stimulus autoloader in existing app/assets/javascripts/application.js"
4
- append_to_file APP_JS_ROOT.join("application.js"), %(import "@hotwired/stimulus-autoloader"\n)
5
+ if APP_JS_PATH.exist?
6
+ say "Import Stimulus autoloader in existing app/javascript/application.js"
7
+ append_to_file APP_JS_PATH, %(\nimport "@hotwired/stimulus-autoloader"\n)
8
+ else
9
+ say <<~INSTRUCTIONS, :red
10
+ You must import @hotwire/stimulus and @hotwire/stimulus-importmap-autoloader in your application.js.
11
+ INSTRUCTIONS
12
+ end
5
13
 
6
14
  say "Creating controllers directory"
7
- copy_file "#{__dir__}/app/assets/javascripts/controllers/hello_controller.js", APP_JS_ROOT.join("controllers/hello_controller.js")
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/initializers/importmap.rb"
19
+ insert_into_file \
20
+ IMPORTMAP_PATH.to_s,
21
+ %( pin "@hotwired/stimulus", to: "stimulus.js"\n pin "@hotwired/stimulus-importmap-autoloader", to: "stimulus-importmap-autoloader.js"\n\n),
22
+ after: "Rails.application.config.importmap.draw do\n"
23
+ else
24
+ say <<~INSTRUCTIONS, :red
25
+ You must add @rails/actiontext and trix to your importmap to reference them via ESM.
26
+ INSTRUCTIONS
27
+ end
@@ -2,7 +2,7 @@ say "Appending Stimulus setup code to #{Webpacker.config.source_entry_path}/appl
2
2
  append_to_file "#{Webpacker.config.source_entry_path}/application.js", %(\nimport "controllers")
3
3
 
4
4
  say "Creating controllers directory"
5
- directory "#{__dir__}/app/assets/javascripts/controllers", "#{Webpacker.config.source_path}/controllers"
5
+ directory "#{__dir__}/app/javascript/controllers", "#{Webpacker.config.source_path}/controllers"
6
6
 
7
7
  say "Using Stimulus NPM package"
8
8
  gsub_file "#{Webpacker.config.source_path}/controllers/hello_controller.js", /@hotwired\//, ''
@@ -2,16 +2,7 @@ module Stimulus
2
2
  class Engine < ::Rails::Engine
3
3
  initializer "stimulus.assets" do
4
4
  if Rails.application.config.respond_to?(:assets)
5
- Rails.application.config.assets.precompile += %w( stimulus stimulus-autoloader )
6
- end
7
- end
8
-
9
- initializer "stimulus.importmap" do
10
- if Rails.application.config.respond_to?(:importmap)
11
- Rails.application.config.importmap.paths.tap do |paths|
12
- paths.asset "@hotwired/stimulus", path: "stimulus"
13
- paths.asset "@hotwired/stimulus-autoloader", path: "stimulus-autoloader"
14
- end
5
+ Rails.application.config.assets.precompile += %w( stimulus stimulus-autoloader stimulus-importmap-autoloader )
15
6
  end
16
7
  end
17
8
  end
@@ -1,3 +1,3 @@
1
1
  module Stimulus
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.5"
3
3
  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.1
4
+ version: 0.3.5
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-12 00:00:00.000000000 Z
13
+ date: 2021-08-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -36,10 +36,11 @@ files:
36
36
  - README.md
37
37
  - Rakefile
38
38
  - app/assets/javascripts/stimulus-autoloader.js
39
+ - app/assets/javascripts/stimulus-importmap-autoloader.js
39
40
  - app/assets/javascripts/stimulus.js
40
41
  - app/assets/javascripts/stimulus@2.js
41
- - lib/install/app/assets/javascripts/controllers/hello_controller.js
42
- - lib/install/app/assets/javascripts/controllers/index.js
42
+ - lib/install/app/javascript/controllers/hello_controller.js
43
+ - lib/install/app/javascript/controllers/index.js
43
44
  - lib/install/stimulus_with_asset_pipeline.rb
44
45
  - lib/install/stimulus_with_webpacker.rb
45
46
  - lib/stimulus-rails.rb