js-routes 2.2.7 → 2.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -9
  3. data/Gemfile +5 -0
  4. data/Readme.md +63 -47
  5. data/bin/tapioca +27 -0
  6. data/js-routes.gemspec +4 -1
  7. data/lib/js_routes/configuration.rb +80 -34
  8. data/lib/js_routes/engine.rb +2 -0
  9. data/lib/js_routes/generators/base.rb +15 -1
  10. data/lib/js_routes/generators/middleware.rb +3 -6
  11. data/lib/js_routes/generators/webpacker.rb +2 -4
  12. data/lib/js_routes/instance.rb +42 -15
  13. data/lib/js_routes/middleware.rb +14 -3
  14. data/lib/js_routes/route.rb +57 -16
  15. data/lib/js_routes/types.rb +28 -0
  16. data/lib/js_routes/version.rb +2 -1
  17. data/lib/js_routes.rb +20 -7
  18. data/lib/routes.js +25 -3
  19. data/lib/routes.ts +30 -6
  20. data/sorbet/config +4 -0
  21. data/sorbet/rbi/annotations/.gitattributes +1 -0
  22. data/sorbet/rbi/annotations/actionpack.rbi +428 -0
  23. data/sorbet/rbi/annotations/actionview.rbi +75 -0
  24. data/sorbet/rbi/annotations/activesupport.rbi +421 -0
  25. data/sorbet/rbi/annotations/railties.rbi +61 -0
  26. data/sorbet/rbi/gems/.gitattributes +1 -0
  27. data/sorbet/rbi/gems/actionpack@7.0.4.1.rbi +303 -0
  28. data/sorbet/rbi/gems/actionview@7.0.4.1.rbi +8 -0
  29. data/sorbet/rbi/gems/activesupport@7.0.4.1.rbi +16424 -0
  30. data/sorbet/rbi/gems/appraisal@2.4.1.rbi +584 -0
  31. data/sorbet/rbi/gems/builder@3.2.4.rbi +8 -0
  32. data/sorbet/rbi/gems/bump@0.10.0.rbi +168 -0
  33. data/sorbet/rbi/gems/byebug@11.1.3.rbi +3606 -0
  34. data/sorbet/rbi/gems/coderay@1.1.3.rbi +3426 -0
  35. data/sorbet/rbi/gems/concurrent-ruby@1.2.0.rbi +11570 -0
  36. data/sorbet/rbi/gems/crass@1.0.6.rbi +8 -0
  37. data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +1083 -0
  38. data/sorbet/rbi/gems/erubi@1.12.0.rbi +145 -0
  39. data/sorbet/rbi/gems/i18n@1.12.0.rbi +2296 -0
  40. data/sorbet/rbi/gems/libv8-node@16.10.0.0.rbi +8 -0
  41. data/sorbet/rbi/gems/loofah@2.19.1.rbi +8 -0
  42. data/sorbet/rbi/gems/method_source@1.0.0.rbi +272 -0
  43. data/sorbet/rbi/gems/mini_racer@0.6.3.rbi +224 -0
  44. data/sorbet/rbi/gems/minitest@5.17.0.rbi +1457 -0
  45. data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
  46. data/sorbet/rbi/gems/nokogiri@1.14.0.rbi +8 -0
  47. data/sorbet/rbi/gems/parallel@1.24.0.rbi +280 -0
  48. data/sorbet/rbi/gems/prettier_print@1.2.1.rbi +951 -0
  49. data/sorbet/rbi/gems/prism@0.24.0.rbi +29744 -0
  50. data/sorbet/rbi/gems/pry-byebug@3.10.1.rbi +1150 -0
  51. data/sorbet/rbi/gems/pry@0.14.2.rbi +10075 -0
  52. data/sorbet/rbi/gems/racc@1.6.2.rbi +150 -0
  53. data/sorbet/rbi/gems/rack-test@2.0.2.rbi +8 -0
  54. data/sorbet/rbi/gems/rack@2.2.6.2.rbi +5585 -0
  55. data/sorbet/rbi/gems/rails-dom-testing@2.0.3.rbi +8 -0
  56. data/sorbet/rbi/gems/rails-html-sanitizer@1.5.0.rbi +8 -0
  57. data/sorbet/rbi/gems/railties@7.0.4.1.rbi +1959 -0
  58. data/sorbet/rbi/gems/rake@13.0.6.rbi +3072 -0
  59. data/sorbet/rbi/gems/rbi@0.1.9.rbi +3006 -0
  60. data/sorbet/rbi/gems/rspec-core@3.12.0.rbi +10868 -0
  61. data/sorbet/rbi/gems/rspec-expectations@3.12.2.rbi +8100 -0
  62. data/sorbet/rbi/gems/rspec-mocks@3.12.3.rbi +5299 -0
  63. data/sorbet/rbi/gems/rspec-support@3.12.0.rbi +1611 -0
  64. data/sorbet/rbi/gems/rspec@3.12.0.rbi +82 -0
  65. data/sorbet/rbi/gems/spoom@1.2.4.rbi +3777 -0
  66. data/sorbet/rbi/gems/sprockets-rails@3.4.2.rbi +14 -0
  67. data/sorbet/rbi/gems/sprockets@4.2.0.rbi +8 -0
  68. data/sorbet/rbi/gems/syntax_tree@6.2.0.rbi +23136 -0
  69. data/sorbet/rbi/gems/tapioca@0.12.0.rbi +3510 -0
  70. data/sorbet/rbi/gems/thor@1.2.1.rbi +3956 -0
  71. data/sorbet/rbi/gems/tzinfo@2.0.5.rbi +5914 -0
  72. data/sorbet/rbi/gems/yard-sorbet@0.8.1.rbi +428 -0
  73. data/sorbet/rbi/gems/yard@0.9.34.rbi +18332 -0
  74. data/sorbet/rbi/gems/zeitwerk@2.6.8.rbi +8 -0
  75. data/sorbet/rbi/todo.rbi +31 -0
  76. data/sorbet/tapioca/config.yml +13 -0
  77. data/sorbet/tapioca/require.rb +4 -0
  78. data/spec/js_routes/default_serializer_spec.rb +1 -1
  79. data/spec/js_routes/module_types/dts_spec.rb +17 -2
  80. data/spec/js_routes/module_types/umd_spec.rb +2 -2
  81. data/spec/js_routes/options_spec.rb +6 -6
  82. data/spec/js_routes/rails_routes_compatibility_spec.rb +17 -12
  83. data/spec/js_routes/route_specification_spec.rb +1 -4
  84. data/spec/js_routes/{zzz_last_post_rails_init_spec.rb → zzz_sprockets_spec.rb} +1 -1
  85. data/spec/spec_helper.rb +4 -0
  86. metadata +82 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc558c858345b705d2ca048eaab529acf4d4ffe20299e152f1c23fcbeb31388e
4
- data.tar.gz: 70dc5ab9c6f629d3236d45a5cf202c6bb8f00893041857f137b9569a3467d48c
3
+ metadata.gz: d890e17e32f816311de89a16506fca4b8ff905ea773c7a8d198343e0270eb50a
4
+ data.tar.gz: 934c79a8b93e7129778e07789f93ac90d4183bd39ea578551f4ea9ca6cab22d2
5
5
  SHA512:
6
- metadata.gz: 0536cff5b25734ae0c0399cba8d65fb48503e7038c6e5f7396acb69497505ab8849b109e3348bc2c7a010b073cb3cd4aebed1798c538e8bd0e0270f8949dd53c
7
- data.tar.gz: 99e9d0487c9062cf3789a8018beba7bcc0b9f985251921cb6f042c20c571d74fab8a4c79f82ad0a6530dde42ee8a678fb5dbfae184dd64e8756ef47d06805b0b
6
+ metadata.gz: 6adcfbfefbbf13c8f46198f9bf2ca47efdac35aa8d28a8b17be7b8af65d321a3ba0da30dd26e40c4a329c359f1a02b865adb74a1d5e3d024b61bae7fd6f8d9e0
7
+ data.tar.gz: 9cc86f32f79975aace5a264ef94d7bbe1afb41a7a141c738969c762479789fa74b1fb752139320f8ddd7c25b2eb164fe14cbcc3c6eb1a40046549538c3a6eb33
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
+ # Changelog
2
+
1
3
  ## master
2
4
 
5
+ ## v2.2.10
6
+
7
+ * Fix middleware error for non-modern setup.
8
+
9
+ ## v2.2.9
10
+
11
+ * Added [Sorbet](https://sorbet.org/) method signatures.
12
+ * Always use DTS module type when calling JsRoutes.definitions or .definitions!. [#313](https://github.com/railsware/js-routes/issues/313)
13
+
14
+ ## v2.2.8
15
+
16
+ * Leave emoji symbols intact when encoding URI fragment [#312](https://github.com/railsware/js-routes/issues/312)
17
+ * Use webpacker config variable instead of hardcode [#309](https://github.com/railsware/js-routes/issues/309)
18
+ * Use `File.exist?` to be compatible with all versions of ruby [#310](https://github.com/railsware/js-routes/issues/310)
19
+
3
20
  ## v2.2.7
4
21
 
5
22
  * Fix ESM Tree Shaking [#306](https://github.com/railsware/js-routes/issues/306)
@@ -22,10 +39,10 @@
22
39
  ## v2.2.3
23
40
 
24
41
  * Fixed NIL module type namespace defintion [#297](https://github.com/railsware/js-routes/issues/297).
25
- * The patch may cause a problem with nested `namespace` option
42
+ * The patch may cause a problem with nested `namespace` option.
26
43
  * Ex. Value like `MyProject.Routes` requires to define `window.MyProject` before importing the routes file
27
44
 
28
- ## v2.2.2.
45
+ ## v2.2.2
29
46
 
30
47
  * Fix custom file path [#295](https://github.com/railsware/js-routes/issues/295)
31
48
 
@@ -150,10 +167,9 @@ See [UPGRADE TO 2.0](./VERSION_2_UPGRADE.md) for guidance.
150
167
 
151
168
  * Fixed bug when js-routes is used in envs without window.location #224
152
169
 
153
-
154
170
  ## v1.4.0
155
171
 
156
- * __breaking change!__ Implemented Routes.config() and Routes.configure instead of Routes.defaults
172
+ * Implemented Routes.config() and Routes.configure instead of Routes.defaults
157
173
 
158
174
  New methods support 4 options at the moment:
159
175
 
@@ -239,7 +255,7 @@ Routes.configure({
239
255
 
240
256
  * Support host, port and protocol inline parameters
241
257
  * Support host, port and protocol parameters given to a route explicitly
242
- * Remove all incompatibilities between actiondispatch and js-routes in handling route URLs
258
+ * Remove all incompatibilities between Actiondispatch and js-routes
243
259
 
244
260
  ## v1.1.2
245
261
 
@@ -265,10 +281,10 @@ Routes.configure({
265
281
 
266
282
  ## v1.0.0
267
283
 
268
- * Add the compact mode [#125](https://github.com/railsware/js-routes/pull/125)
269
- * Add support for host, protocol, and port configuration [#137](https://github.com/railsware/js-routes/pull/137)
270
- * Routes path specs [#135](https://github.com/railsware/js-routes/pull/135)
271
- * Support Rails 4.2 and Ruby 2.2 [#140](https://github.com/railsware/js-routes/pull/140)
284
+ * Add the compact mode [#125](https://github.com/railsware/js-routes/pull/125)
285
+ * Add support for host, protocol, and port configuration [#137](https://github.com/railsware/js-routes/pull/137)
286
+ * Routes path specs [#135](https://github.com/railsware/js-routes/pull/135)
287
+ * Support Rails 4.2 and Ruby 2.2 [#140](https://github.com/railsware/js-routes/pull/140)
272
288
 
273
289
  ## v0.9.9
274
290
 
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in js-routes.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem 'sorbet', '>= 0.5.11518'
8
+ gem 'tapioca'
9
+ end
data/Readme.md CHANGED
@@ -16,21 +16,22 @@ gem "js-routes"
16
16
 
17
17
  There are several possible ways to setup JsRoutes:
18
18
 
19
- * [Quick and easy](#quick-start)
19
+ 1. [Quick and easy](#quick-start) - Recommended
20
20
  * Uses Rack Middleware to automatically update routes locally
21
21
  * Automatically generates routes files on javascript build
22
22
  * Works great for a simple Rails application
23
- * [Webpacker ERB Loader](#webpacker)
23
+ 2. [Advanced Setup](#advanced-setup)
24
+ * Allows very custom setups
25
+ * Automatic updates need to be customized
26
+ 3. [Webpacker ERB Loader](#webpacker) - Legacy
24
27
  * Requires ESM module system (the default)
25
28
  * Doesn't support typescript definitions
26
- * [Advanced Setup](#advanced-setup)
27
- * Allows very custom setups
28
- * [Sprockets](#sprockets) legacy
29
+ 4. [Sprockets](#sprockets) - Legacy
29
30
  * Deprecated and not recommended for modern apps
30
31
 
31
- <div id='quick-start'></div>
32
+ <div id="quick-start"></div>
32
33
 
33
- ### Quick Start
34
+ ### Quick Start
34
35
 
35
36
  Setup [Rack Middleware](https://guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack)
36
37
  to automatically generate and maintain `routes.js` file and corresponding
@@ -76,12 +77,11 @@ Add js-routes files to `.gitignore`:
76
77
  /app/javascript/routes.d.ts
77
78
  ```
78
79
 
79
- <div id='webpacker'></div>
80
+ <div id="webpack"></div>
80
81
 
81
82
  ### Webpacker ERB loader
82
83
 
83
- **IMPORTANT**: this setup doesn't support IDE autocompletion with [Typescript](https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html)
84
-
84
+ **IMPORTANT**: the setup doesn't support IDE autocompletion with [Typescript](https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html)
85
85
 
86
86
  #### Use a Generator
87
87
 
@@ -132,7 +132,7 @@ Create routes file `app/javascript/routes.js.erb`:
132
132
  <%= JsRoutes.generate() %>
133
133
  ```
134
134
 
135
- Use routes wherever you need them:
135
+ Use routes wherever you need them:
136
136
 
137
137
  ``` javascript
138
138
  import {post_path} from 'routes.js.erb';
@@ -140,62 +140,78 @@ import {post_path} from 'routes.js.erb';
140
140
  alert(post_path(2));
141
141
  ```
142
142
 
143
- <div id='advanced-setup'></div>
143
+ <div id="advanced-setup"></div>
144
144
 
145
145
  ### Advanced Setup
146
146
 
147
- **IMPORTANT**: that this setup requires the JS routes file to be updates manually
148
-
149
- Routes file can be generated with a `rake` task:
147
+ In case you need multiple route files for different parts of your application, there are low level methods:
150
148
 
151
- ``` sh
152
- rake js:routes
153
- # OR for typescript support
154
- rake js:routes:typescript
149
+ ``` ruby
150
+ # Returns a routes file as a string
151
+ JsRoutes.generate(options)
152
+ # Writes routes to specific file location
153
+ JsRoutes.generate!(file_name, options)
154
+ # Writes Typescript definitions file for routes
155
+ JsRoutes.definitions!(file_name, options)
155
156
  ```
156
157
 
157
- In case you need multiple route files for different parts of your application, you have to create the files manually.
158
- If your application has an `admin` and an `application` namespace for example:
159
-
160
- **IMPORTANT**: Requires [Webpacker ERB Loader](#webpacker) setup.
158
+ They can also be used in ERB context
161
159
 
162
160
  ``` erb
163
- // app/javascript/admin/routes.js.erb
164
- <%= JsRoutes.generate(include: /admin/) %>
161
+ <script>
162
+ var AdminRoutes = <%= JsRoutes.generate(
163
+ include: /admin/, module_type: nil, namespace: nil
164
+ ) %>;
165
+ </script>
165
166
  ```
166
167
 
167
- ``` erb
168
- // app/javascript/customer/routes.js.erb
169
- <%= JsRoutes.generate(exclude: /admin/) %>
170
- ```
168
+ Routes can be returns via API:
171
169
 
172
- You can manipulate the generated helper manually by injecting ruby into javascript:
170
+ ``` ruby
171
+ class Api::RoutesController < Api::BaseController
172
+ def index
173
+ options = {
174
+ include: /\Aapi_/,
175
+ default_url_options: { format: 'json' },
176
+ }
177
+ render json: {
178
+ routes: {
179
+ source: JsRoutes.generate(options),
180
+ definitions: JsRoutes.definitions(options),
181
+ }
182
+ }
183
+ end
184
+ end
173
185
 
174
- ``` erb
175
- export const routes = <%= JsRoutes.generate(module_type: nil, namespace: nil) %>
176
186
  ```
177
187
 
178
- If you want to generate the routes files manually with custom options, you can use `JsRoutes.generate!`:
188
+ Default auto-update middleware for development
189
+ doesn't support configuration out of the box,
190
+ but it can be extended through inheritence:
179
191
 
180
192
  ``` ruby
181
- path = Rails.root.join("app/javascript")
182
-
183
- JsRoutes.generate!(
184
- "#{path}/app_routes.js", exclude: [/^admin_/, /^api_/]
185
- )
186
- JsRoutes.generate!(
187
- "#{path}/adm_routes.js", include: /^admin_/
188
- )
189
- JsRoutes.generate!(
190
- "#{path}/api_routes.js", include: /^api_/, default_url_options: {format: "json"}
191
- )
193
+ class AdvancedJsRoutesMiddleware < JsRoutes::Middleware
194
+ def regenerate
195
+ path = Rails.root.join("app/javascript")
196
+
197
+ JsRoutes.generate!(
198
+ "#{path}/app_routes.js", exclude: [/^admin_/, /^api_/]
199
+ )
200
+ JsRoutes.generate!(
201
+ "#{path}/adm_routes.js", include: /^admin_/
202
+ )
203
+ JsRoutes.generate!(
204
+ "#{path}/api_routes.js", include: /^api_/, default_url_options: {format: "json"}
205
+ )
206
+ end
207
+ end
192
208
  ```
193
209
 
194
- <div id='definitions'></div>
210
+ <div id="definitions"></div>
195
211
 
196
212
  #### Typescript Definitions
197
213
 
198
- JsRoutes has typescript support out of the box.
214
+ JsRoutes has typescript support out of the box.
199
215
 
200
216
  Restrictions:
201
217
 
@@ -207,7 +223,7 @@ More advanced setup would involve calling manually:
207
223
 
208
224
  ``` ruby
209
225
  JsRoutes.definitions! # to output to file
210
- # or
226
+ # or
211
227
  JsRoutes.definitions # to output to string
212
228
  ```
213
229
 
data/bin/tapioca ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'tapioca' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12
+
13
+ bundle_binstub = File.expand_path("bundle", __dir__)
14
+
15
+ if File.file?(bundle_binstub)
16
+ if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17
+ load(bundle_binstub)
18
+ else
19
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21
+ end
22
+ end
23
+
24
+ require "rubygems"
25
+ require "bundler/setup"
26
+
27
+ load Gem.bin_path("tapioca", "tapioca")
data/js-routes.gemspec CHANGED
@@ -22,11 +22,14 @@ Gem::Specification.new do |s|
22
22
  s.licenses = ["MIT"]
23
23
  s.require_paths = ["lib"]
24
24
  s.summary = %q{Brings Rails named routes to javascript}
25
+ s.metadata = { "github_repo" => "ssh://github.com/railsware/js-routes" }
25
26
 
26
27
  s.add_runtime_dependency(%q<railties>, [">= 4"])
28
+ s.add_runtime_dependency(%q<sorbet-runtime>)
29
+
27
30
  s.add_development_dependency(%q<sprockets-rails>)
28
31
  s.add_development_dependency(%q<rspec>, [">= 3.10.0"])
29
- s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
32
+ s.add_development_dependency(%q<bundler>, [">= 2.2.25"])
30
33
  s.add_development_dependency(%q<appraisal>, [">= 0.5.2"])
31
34
  s.add_development_dependency(%q<bump>, [">= 0.10.0"])
32
35
  if defined?(JRUBY_VERSION)
@@ -1,81 +1,122 @@
1
+ # typed: strict
1
2
  require "pathname"
3
+ require "js_routes/types"
2
4
 
3
5
  module JsRoutes
4
6
  class Configuration
5
- DEFAULTS = {
6
- namespace: nil,
7
- exclude: [],
8
- include: //,
9
- file: nil,
10
- prefix: -> { Rails.application.config.relative_url_root || "" },
11
- url_links: false,
12
- camel_case: false,
13
- default_url_options: {},
14
- compact: false,
15
- serializer: nil,
16
- special_options_key: "_options",
17
- application: -> { Rails.application },
18
- module_type: 'ESM',
19
- documentation: true,
20
- } #:nodoc:
21
-
22
- attr_accessor(*DEFAULTS.keys)
23
-
7
+ include JsRoutes::Types
8
+ extend T::Sig
9
+
10
+ sig { returns(T.nilable(String)) }
11
+ attr_accessor :namespace
12
+ sig { returns(Clusivity) }
13
+ attr_accessor :exclude
14
+ sig { returns(Clusivity) }
15
+ attr_accessor :include
16
+ sig { returns(FileName) }
17
+ attr_accessor :file
18
+ sig { returns(Prefix) }
19
+ attr_accessor :prefix
20
+ sig { returns(T::Boolean) }
21
+ attr_accessor :url_links
22
+ sig { returns(T::Boolean) }
23
+ attr_accessor :camel_case
24
+ sig { returns(Options) }
25
+ attr_accessor :default_url_options
26
+ sig { returns(T::Boolean) }
27
+ attr_accessor :compact
28
+ sig { returns(T.nilable(String)) }
29
+ attr_accessor :serializer
30
+ sig { returns(Literal) }
31
+ attr_accessor :special_options_key
32
+ sig { returns(ApplicationCaller) }
33
+ attr_accessor :application
34
+ sig { returns(T::Boolean) }
35
+ attr_accessor :documentation
36
+ sig { returns(T.nilable(String)) }
37
+ attr_accessor :module_type
38
+
39
+ sig {params(attributes: T.nilable(Options)).void }
24
40
  def initialize(attributes = nil)
25
- assign(DEFAULTS)
41
+ @namespace = nil
42
+ @exclude = T.let([], Clusivity)
43
+ @include = T.let([//], Clusivity)
44
+ @file = T.let(nil, FileName)
45
+ @prefix = T.let(-> { Rails.application.config.relative_url_root || "" }, T.untyped)
46
+ @url_links = T.let(false, T::Boolean)
47
+ @camel_case = T.let(false, T::Boolean)
48
+ @default_url_options = T.let(T.unsafe({}), Options)
49
+ @compact = T.let(false, T::Boolean)
50
+ @serializer = T.let(nil, T.nilable(String))
51
+ @special_options_key = T.let("_options", Literal)
52
+ @application = T.let(-> { Rails.application }, ApplicationCaller)
53
+ @module_type = T.let('ESM', T.nilable(String))
54
+ @documentation = T.let(true, T::Boolean)
55
+
26
56
  return unless attributes
27
57
  assign(attributes)
28
58
  end
29
59
 
30
- def assign(attributes = nil, &block)
31
- if !attributes && !block
32
- raise "Provide attributes or block"
33
- end
34
- tap(&block) if block
60
+ sig do
61
+ params(
62
+ attributes: Options,
63
+ ).returns(JsRoutes::Configuration)
64
+ end
65
+ def assign(attributes)
35
66
  if attributes
36
67
  attributes.each do |attribute, value|
37
- value = value.call if value.is_a?(Proc)
38
- send(:"#{attribute}=", value)
68
+ public_send(:"#{attribute}=", value)
39
69
  end
40
70
  end
41
71
  normalize_and_verify
42
72
  self
43
73
  end
44
74
 
75
+ sig { params(block: ConfigurationBlock).returns(T.self_type) }
76
+ def setup(&block)
77
+ tap(&block)
78
+ end
79
+
80
+ sig { params(attribute: Literal).returns(T.untyped) }
45
81
  def [](attribute)
46
- send(attribute)
82
+ public_send(attribute)
47
83
  end
48
84
 
85
+ sig { params(attributes: Options).returns(JsRoutes::Configuration) }
49
86
  def merge(attributes)
50
87
  clone.assign(attributes)
51
88
  end
52
89
 
53
- def to_hash
54
- Hash[*members.zip(values).flatten(1)].symbolize_keys
55
- end
56
-
90
+ sig {returns(T::Boolean)}
57
91
  def esm?
58
92
  module_type === 'ESM'
59
93
  end
60
94
 
95
+ sig {returns(T::Boolean)}
61
96
  def dts?
62
97
  self.module_type === 'DTS'
63
98
  end
64
99
 
100
+ sig {returns(T::Boolean)}
65
101
  def modern?
66
102
  esm? || dts?
67
103
  end
68
104
 
105
+ sig { void }
69
106
  def require_esm
70
107
  raise "ESM module type is required" unless modern?
71
108
  end
72
109
 
110
+ sig { returns(String) }
73
111
  def source_file
74
112
  File.dirname(__FILE__) + "/../" + default_file_name
75
113
  end
76
114
 
115
+ sig { returns(Pathname) }
77
116
  def output_file
78
- webpacker_dir = pathname('app', 'javascript')
117
+ webpacker_dir = defined?(::Webpacker) ?
118
+ T.unsafe(::Webpacker).config.source_path :
119
+ pathname('app', 'javascript')
79
120
  sprockets_dir = pathname('app','assets','javascripts')
80
121
  file_name = file || default_file_name
81
122
  sprockets_file = sprockets_dir.join(file_name)
@@ -85,23 +126,28 @@ module JsRoutes
85
126
 
86
127
  protected
87
128
 
129
+ sig { void }
88
130
  def normalize_and_verify
89
131
  normalize
90
132
  verify
91
133
  end
92
134
 
135
+ sig { params(parts: String).returns(Pathname) }
93
136
  def pathname(*parts)
94
- Pathname.new(File.join(*parts))
137
+ Pathname.new(File.join(*T.unsafe(parts)))
95
138
  end
96
139
 
140
+ sig { returns(String) }
97
141
  def default_file_name
98
142
  dts? ? "routes.d.ts" : "routes.js"
99
143
  end
100
144
 
145
+ sig {void}
101
146
  def normalize
102
147
  self.module_type = module_type&.upcase || 'NIL'
103
148
  end
104
149
 
150
+ sig { void }
105
151
  def verify
106
152
  if module_type != 'NIL' && namespace
107
153
  raise "JsRoutes namespace option can only be used if module_type is nil"
@@ -1,3 +1,5 @@
1
+ require "rails/engine"
2
+
1
3
  module JsRoutes
2
4
  class SprocketsExtension
3
5
  def initialize(filename, &block)
@@ -3,6 +3,8 @@ require "rails/generators"
3
3
 
4
4
  class JsRoutes::Generators::Base < Rails::Generators::Base
5
5
 
6
+ source_root File.expand_path(__FILE__ + "/../../../templates")
7
+
6
8
  protected
7
9
 
8
10
  def application_js_path
@@ -10,7 +12,19 @@ class JsRoutes::Generators::Base < Rails::Generators::Base
10
12
  "app/javascript/packs/application.js",
11
13
  "app/javascript/controllers/application.js",
12
14
  ].find do |path|
13
- File.exists?(Rails.root.join(path))
15
+ File.exist?(Rails.root.join(path))
14
16
  end
15
17
  end
18
+
19
+ def depends_on?(gem_name)
20
+ !!Bundler.load.gems.find {|g| g.name == gem_name}
21
+ end
22
+
23
+ def depends_on_js_bundling?
24
+ depends_on?('jsbundling-rails')
25
+ end
26
+
27
+ def depends_on_webpacker?
28
+ depends_on?('webpacker')
29
+ end
16
30
  end
@@ -2,8 +2,6 @@ require "js_routes/generators/base"
2
2
 
3
3
  class JsRoutes::Generators::Middleware < JsRoutes::Generators::Base
4
4
 
5
- source_root File.expand_path(__FILE__ + "/../../../templates")
6
-
7
5
  def create_middleware
8
6
  copy_file "initializer.rb", "config/initializers/js_routes.rb"
9
7
  inject_into_file "config/environments/development.rb", middleware_content, before: /^end\n\z/
@@ -20,8 +18,8 @@ class JsRoutes::Generators::Middleware < JsRoutes::Generators::Base
20
18
 
21
19
  def pack_content
22
20
  <<-JS
23
- import * as Routes from '../routes';
24
- window.Routes = Routes;
21
+ import {root_path} from '../routes';
22
+ alert(`JsRoutes installed.\\nYour root path is ${root_path()}`)
25
23
  JS
26
24
  end
27
25
 
@@ -35,8 +33,7 @@ window.Routes = Routes;
35
33
  end
36
34
 
37
35
  def rakefile_content
38
- enhanced_task = Bundler.load.gems.find {|g| g.name = 'jsbundling-rails'} ?
39
- "javascript:build" : "assets:precompile"
36
+ enhanced_task = depends_on_js_bundling? ? "javascript:build" : "assets:precompile"
40
37
  <<-RB
41
38
  # Update js-routes file before javascript build
42
39
  task "#{enhanced_task}" => "js:routes:typescript"
@@ -2,12 +2,10 @@ require "rails/generators"
2
2
 
3
3
  class JsRoutes::Generators::Webpacker < Rails::Generators::Base
4
4
 
5
- source_root File.expand_path(__FILE__ + "/../../../templates")
6
-
7
5
  def create_webpack
8
6
  copy_file "initializer.rb", "config/initializers/js_routes.rb"
9
7
  copy_file "erb.js", "config/webpack/loaders/erb.js"
10
- copy_file "routes.js.erb", "app/javascript/routes.js.erb"
8
+ copy_file "routes.js.erb", "#{Webpacker.config.source_path}/routes.js.erb"
11
9
  inject_into_file "config/webpack/environment.js", loader_content
12
10
  if path = application_js_path
13
11
  inject_into_file path, pack_content
@@ -21,7 +19,7 @@ class JsRoutes::Generators::Webpacker < Rails::Generators::Base
21
19
  def pack_content
22
20
  <<-JS
23
21
  import * as Routes from 'routes.js.erb';
24
- window.Routes = Routes;
22
+ alert(Routes.root_path())
25
23
  JS
26
24
  end
27
25