stimulus-rails 0.3.11 → 0.4.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: aa026a69205d5d12a6f7dfe43892b6e832ac6d7331cb87fab2e1ae6ea6fd3a1f
4
+ data.tar.gz: 0b4534ef0afb61a881a8e702b6ccb61e8c24fddff649992269f2b7348546a412
5
5
  SHA512:
6
- metadata.gz: 929c0266cf1ed01dee781af2d228481b3e4f4164bdc09c3c9e1505ced0a40c4223fa54849ace773806d2be3df300103ed8099867735fbefa35e5288110ba799f
7
- data.tar.gz: 02d4931af10963beef58c26379499486326b355e325078e24649c6ab92e0351bfbabb936fc72202df31bb10de8c9a477bf698d27df10476260fd6604ae0e192d
6
+ metadata.gz: 4512fadf940a7893da2ac9b906f6591a2829bca71f957d46693c5e1085480c9088877c70adb4c72e10b93a8c6f995d8525441bfcbb29677d1f47dea9e539dbfc
7
+ data.tar.gz: 3ba7838345956e406dc51183365e05f78e0b92b7102debb74b17a702d5e82b24e2173861f899704532ab0ab1fc06d0312e82f6969734f687d9b6a0186e2cbc0d
@@ -1,21 +1,21 @@
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, importmap, application) {
5
+ const paths = Object.keys(importmap)
6
+ .filter(path => path.match(new RegExp(`^${under}/.*_controller$`)))
7
+
8
+ paths.forEach(path => registerControllerFromPath(path, under, application))
6
9
  }
7
10
 
8
- function registerControllerFromPath(path, under) {
9
- const name = path.replace(`${under}/`, "").replace("_controller", "").replace(/\//g, "--").replace(/_/g, "-")
11
+ function registerControllerFromPath(path, under, application) {
12
+ const name = path
13
+ .replace(`${under}/`, "")
14
+ .replace("_controller", "")
15
+ .replace(/\//g, "--")
16
+ .replace(/_/g, "-")
10
17
 
11
18
  import(path)
12
19
  .then(module => application.register(name, module.default))
13
20
  .catch(error => console.log(`Failed to register controller: ${name} (${path})`, error))
14
21
  }
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,13 @@
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 from the importmap under controllers/*
11
+ import { registerControllersFrom } from "@hotwired/stimulus-importmap-autoloader"
12
+ const importmap = JSON.parse(document.querySelector("script[type=importmap]").text).imports
13
+ registerControllersFrom("controllers", importmap, application)
@@ -0,0 +1,12 @@
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
11
+ import HelloController from "./hello_controller"
12
+ application.register("hello", HelloController)
@@ -0,0 +1,14 @@
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))
@@ -0,0 +1,10 @@
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"
@@ -0,0 +1,17 @@
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/hello_controller.js",
6
+ "app/javascript/controllers/hello_controller.js"
7
+
8
+ say "Import Stimulus controllers"
9
+ append_to_file "app/javascript/application.js", %(import "./controllers"\n)
10
+
11
+ say "Pin Stimulus"
12
+ append_to_file "config/importmap.rb" do <<-RUBY
13
+ pin "@hotwired/stimulus", to: "stimulus.js"
14
+ pin "@hotwired/stimulus-importmap-autoloader", to: "stimulus-importmap-autoloader.js"
15
+ pin_all_from "app/javascript/controllers", under: "controllers"
16
+ RUBY
17
+ end
@@ -0,0 +1,19 @@
1
+ say "Create controllers directory"
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
+
12
+ copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js",
13
+ "app/javascript/controllers/hello_controller.js"
14
+
15
+ say "Import Stimulus controllers"
16
+ append_to_file "app/javascript/application.js", %(import "./controllers"\n)
17
+
18
+ say "Install Stimulus"
19
+ run "yarn add @hotwired/stimulus"
@@ -1,3 +1,3 @@
1
1
  module Stimulus
2
- VERSION = "0.3.11"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -3,24 +3,24 @@ def run_stimulus_install_template(path) system "#{RbConfig.ruby} ./bin/rails app
3
3
  namespace :stimulus do
4
4
  desc "Install Stimulus into the app"
5
5
  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
6
+ if Rails.root.join("config/importmap.rb").exist?
7
+ Rake::Task["stimulus:install:importmap"].invoke
8
+ elsif Rails.root.join("package.json").exist?
9
+ Rake::Task["stimulus:install:node"].invoke
10
10
  else
11
- puts "You must either be running Webpacker or importmap-rails to use this gem."
11
+ puts "You must either be running with node (package.json) or importmap-rails (config/importmap.rb) to use this gem."
12
12
  end
13
13
  end
14
14
 
15
15
  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"
16
+ desc "Install Stimulus on an app running importmap-rails"
17
+ task :importmap do
18
+ run_stimulus_install_template "stimulus_with_importmap"
19
19
  end
20
20
 
21
- desc "Install Stimulus on the app with webpacker"
22
- task :webpacker do
23
- run_stimulus_install_template "stimulus_with_webpacker"
21
+ desc "Install Stimulus on an app running node"
22
+ task :node do
23
+ run_stimulus_install_template "stimulus_with_node"
24
24
  end
25
25
  end
26
26
  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.4.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-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -41,9 +41,12 @@ files:
41
41
  - app/assets/javascripts/stimulus@2.js
42
42
  - app/assets/javascripts/stimulus@3.0.0-beta.1.js
43
43
  - 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
44
+ - lib/install/app/javascript/controllers/index_for_importmap.js
45
+ - 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
+ - lib/install/stimulus_with_importmap.rb
49
+ - lib/install/stimulus_with_node.rb
47
50
  - lib/stimulus-rails.rb
48
51
  - lib/stimulus/engine.rb
49
52
  - lib/stimulus/version.rb
@@ -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"