js-routes 2.2.8 → 2.2.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -9
- data/Gemfile +5 -0
- data/Readme.md +60 -45
- data/bin/tapioca +27 -0
- data/js-routes.gemspec +4 -1
- data/lib/js_routes/configuration.rb +80 -34
- data/lib/js_routes/engine.rb +2 -0
- data/lib/js_routes/generators/base.rb +14 -0
- data/lib/js_routes/generators/middleware.rb +3 -6
- data/lib/js_routes/generators/webpacker.rb +1 -3
- data/lib/js_routes/instance.rb +42 -15
- data/lib/js_routes/middleware.rb +14 -3
- data/lib/js_routes/route.rb +57 -16
- data/lib/js_routes/types.rb +28 -0
- data/lib/js_routes/version.rb +2 -1
- data/lib/js_routes.rb +20 -7
- data/sorbet/config +4 -0
- data/sorbet/rbi/annotations/.gitattributes +1 -0
- data/sorbet/rbi/annotations/actionpack.rbi +428 -0
- data/sorbet/rbi/annotations/actionview.rbi +75 -0
- data/sorbet/rbi/annotations/activesupport.rbi +421 -0
- data/sorbet/rbi/annotations/railties.rbi +61 -0
- data/sorbet/rbi/gems/.gitattributes +1 -0
- data/sorbet/rbi/gems/actionpack@7.0.4.1.rbi +303 -0
- data/sorbet/rbi/gems/actionview@7.0.4.1.rbi +8 -0
- data/sorbet/rbi/gems/activesupport@7.0.4.1.rbi +16424 -0
- data/sorbet/rbi/gems/appraisal@2.4.1.rbi +584 -0
- data/sorbet/rbi/gems/builder@3.2.4.rbi +8 -0
- data/sorbet/rbi/gems/bump@0.10.0.rbi +168 -0
- data/sorbet/rbi/gems/byebug@11.1.3.rbi +3606 -0
- data/sorbet/rbi/gems/coderay@1.1.3.rbi +3426 -0
- data/sorbet/rbi/gems/concurrent-ruby@1.2.0.rbi +11570 -0
- data/sorbet/rbi/gems/crass@1.0.6.rbi +8 -0
- data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +1083 -0
- data/sorbet/rbi/gems/erubi@1.12.0.rbi +145 -0
- data/sorbet/rbi/gems/i18n@1.12.0.rbi +2296 -0
- data/sorbet/rbi/gems/libv8-node@16.10.0.0.rbi +8 -0
- data/sorbet/rbi/gems/loofah@2.19.1.rbi +8 -0
- data/sorbet/rbi/gems/method_source@1.0.0.rbi +272 -0
- data/sorbet/rbi/gems/mini_racer@0.6.3.rbi +224 -0
- data/sorbet/rbi/gems/minitest@5.17.0.rbi +1457 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
- data/sorbet/rbi/gems/nokogiri@1.14.0.rbi +8 -0
- data/sorbet/rbi/gems/parallel@1.24.0.rbi +280 -0
- data/sorbet/rbi/gems/prettier_print@1.2.1.rbi +951 -0
- data/sorbet/rbi/gems/prism@0.24.0.rbi +29744 -0
- data/sorbet/rbi/gems/pry-byebug@3.10.1.rbi +1150 -0
- data/sorbet/rbi/gems/pry@0.14.2.rbi +10075 -0
- data/sorbet/rbi/gems/racc@1.6.2.rbi +150 -0
- data/sorbet/rbi/gems/rack-test@2.0.2.rbi +8 -0
- data/sorbet/rbi/gems/rack@2.2.6.2.rbi +5585 -0
- data/sorbet/rbi/gems/rails-dom-testing@2.0.3.rbi +8 -0
- data/sorbet/rbi/gems/rails-html-sanitizer@1.5.0.rbi +8 -0
- data/sorbet/rbi/gems/railties@7.0.4.1.rbi +1959 -0
- data/sorbet/rbi/gems/rake@13.0.6.rbi +3072 -0
- data/sorbet/rbi/gems/rbi@0.1.9.rbi +3006 -0
- data/sorbet/rbi/gems/rspec-core@3.12.0.rbi +10868 -0
- data/sorbet/rbi/gems/rspec-expectations@3.12.2.rbi +8100 -0
- data/sorbet/rbi/gems/rspec-mocks@3.12.3.rbi +5299 -0
- data/sorbet/rbi/gems/rspec-support@3.12.0.rbi +1611 -0
- data/sorbet/rbi/gems/rspec@3.12.0.rbi +82 -0
- data/sorbet/rbi/gems/spoom@1.2.4.rbi +3777 -0
- data/sorbet/rbi/gems/sprockets-rails@3.4.2.rbi +14 -0
- data/sorbet/rbi/gems/sprockets@4.2.0.rbi +8 -0
- data/sorbet/rbi/gems/syntax_tree@6.2.0.rbi +23136 -0
- data/sorbet/rbi/gems/tapioca@0.12.0.rbi +3510 -0
- data/sorbet/rbi/gems/thor@1.2.1.rbi +3956 -0
- data/sorbet/rbi/gems/tzinfo@2.0.5.rbi +5914 -0
- data/sorbet/rbi/gems/yard-sorbet@0.8.1.rbi +428 -0
- data/sorbet/rbi/gems/yard@0.9.34.rbi +18332 -0
- data/sorbet/rbi/gems/zeitwerk@2.6.8.rbi +8 -0
- data/sorbet/rbi/todo.rbi +31 -0
- data/sorbet/tapioca/config.yml +13 -0
- data/sorbet/tapioca/require.rb +4 -0
- data/spec/js_routes/module_types/dts_spec.rb +17 -2
- data/spec/js_routes/module_types/umd_spec.rb +1 -1
- data/spec/js_routes/options_spec.rb +6 -6
- metadata +81 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d890e17e32f816311de89a16506fca4b8ff905ea773c7a8d198343e0270eb50a
|
4
|
+
data.tar.gz: 934c79a8b93e7129778e07789f93ac90d4183bd39ea578551f4ea9ca6cab22d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6adcfbfefbbf13c8f46198f9bf2ca47efdac35aa8d28a8b17be7b8af65d321a3ba0da30dd26e40c4a329c359f1a02b865adb74a1d5e3d024b61bae7fd6f8d9e0
|
7
|
+
data.tar.gz: 9cc86f32f79975aace5a264ef94d7bbe1afb41a7a141c738969c762479789fa74b1fb752139320f8ddd7c25b2eb164fe14cbcc3c6eb1a40046549538c3a6eb33
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
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
|
+
|
3
14
|
## v2.2.8
|
4
15
|
|
5
16
|
* Leave emoji symbols intact when encoding URI fragment [#312](https://github.com/railsware/js-routes/issues/312)
|
@@ -28,10 +39,10 @@
|
|
28
39
|
## v2.2.3
|
29
40
|
|
30
41
|
* Fixed NIL module type namespace defintion [#297](https://github.com/railsware/js-routes/issues/297).
|
31
|
-
* The patch may cause a problem with nested `namespace` option
|
42
|
+
* The patch may cause a problem with nested `namespace` option.
|
32
43
|
* Ex. Value like `MyProject.Routes` requires to define `window.MyProject` before importing the routes file
|
33
44
|
|
34
|
-
## v2.2.2
|
45
|
+
## v2.2.2
|
35
46
|
|
36
47
|
* Fix custom file path [#295](https://github.com/railsware/js-routes/issues/295)
|
37
48
|
|
@@ -156,10 +167,9 @@ See [UPGRADE TO 2.0](./VERSION_2_UPGRADE.md) for guidance.
|
|
156
167
|
|
157
168
|
* Fixed bug when js-routes is used in envs without window.location #224
|
158
169
|
|
159
|
-
|
160
170
|
## v1.4.0
|
161
171
|
|
162
|
-
*
|
172
|
+
* Implemented Routes.config() and Routes.configure instead of Routes.defaults
|
163
173
|
|
164
174
|
New methods support 4 options at the moment:
|
165
175
|
|
@@ -245,7 +255,7 @@ Routes.configure({
|
|
245
255
|
|
246
256
|
* Support host, port and protocol inline parameters
|
247
257
|
* Support host, port and protocol parameters given to a route explicitly
|
248
|
-
* Remove all incompatibilities between
|
258
|
+
* Remove all incompatibilities between Actiondispatch and js-routes
|
249
259
|
|
250
260
|
## v1.1.2
|
251
261
|
|
@@ -271,10 +281,10 @@ Routes.configure({
|
|
271
281
|
|
272
282
|
## v1.0.0
|
273
283
|
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
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)
|
278
288
|
|
279
289
|
## v0.9.9
|
280
290
|
|
data/Gemfile
CHANGED
data/Readme.md
CHANGED
@@ -16,22 +16,22 @@ gem "js-routes"
|
|
16
16
|
|
17
17
|
There are several possible ways to setup JsRoutes:
|
18
18
|
|
19
|
-
|
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
|
-
|
23
|
+
2. [Advanced Setup](#advanced-setup)
|
24
24
|
* Allows very custom setups
|
25
25
|
* Automatic updates need to be customized
|
26
|
-
|
26
|
+
3. [Webpacker ERB Loader](#webpacker) - Legacy
|
27
27
|
* Requires ESM module system (the default)
|
28
28
|
* Doesn't support typescript definitions
|
29
|
-
|
29
|
+
4. [Sprockets](#sprockets) - Legacy
|
30
30
|
* Deprecated and not recommended for modern apps
|
31
31
|
|
32
|
-
<div id=
|
32
|
+
<div id="quick-start"></div>
|
33
33
|
|
34
|
-
### Quick Start
|
34
|
+
### Quick Start
|
35
35
|
|
36
36
|
Setup [Rack Middleware](https://guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack)
|
37
37
|
to automatically generate and maintain `routes.js` file and corresponding
|
@@ -77,13 +77,12 @@ Add js-routes files to `.gitignore`:
|
|
77
77
|
/app/javascript/routes.d.ts
|
78
78
|
```
|
79
79
|
|
80
|
-
<div id=
|
80
|
+
<div id="webpack"></div>
|
81
81
|
|
82
82
|
### Webpacker ERB loader
|
83
83
|
|
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
|
-
|
87
86
|
#### Use a Generator
|
88
87
|
|
89
88
|
Run a command:
|
@@ -133,7 +132,7 @@ Create routes file `app/javascript/routes.js.erb`:
|
|
133
132
|
<%= JsRoutes.generate() %>
|
134
133
|
```
|
135
134
|
|
136
|
-
Use routes wherever you need them:
|
135
|
+
Use routes wherever you need them:
|
137
136
|
|
138
137
|
``` javascript
|
139
138
|
import {post_path} from 'routes.js.erb';
|
@@ -141,62 +140,78 @@ import {post_path} from 'routes.js.erb';
|
|
141
140
|
alert(post_path(2));
|
142
141
|
```
|
143
142
|
|
144
|
-
<div id=
|
143
|
+
<div id="advanced-setup"></div>
|
145
144
|
|
146
145
|
### Advanced Setup
|
147
146
|
|
148
|
-
|
149
|
-
|
150
|
-
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:
|
151
148
|
|
152
|
-
```
|
153
|
-
|
154
|
-
|
155
|
-
|
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)
|
156
156
|
```
|
157
157
|
|
158
|
-
|
159
|
-
If your application has an `admin` and an `application` namespace for example:
|
160
|
-
|
161
|
-
**IMPORTANT**: Requires [Webpacker ERB Loader](#webpacker) setup.
|
158
|
+
They can also be used in ERB context
|
162
159
|
|
163
160
|
``` erb
|
164
|
-
|
165
|
-
<%= JsRoutes.generate(
|
161
|
+
<script>
|
162
|
+
var AdminRoutes = <%= JsRoutes.generate(
|
163
|
+
include: /admin/, module_type: nil, namespace: nil
|
164
|
+
) %>;
|
165
|
+
</script>
|
166
166
|
```
|
167
167
|
|
168
|
-
|
169
|
-
// app/javascript/customer/routes.js.erb
|
170
|
-
<%= JsRoutes.generate(exclude: /admin/) %>
|
171
|
-
```
|
168
|
+
Routes can be returns via API:
|
172
169
|
|
173
|
-
|
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
|
174
185
|
|
175
|
-
``` erb
|
176
|
-
export const routes = <%= JsRoutes.generate(module_type: nil, namespace: nil) %>
|
177
186
|
```
|
178
187
|
|
179
|
-
|
188
|
+
Default auto-update middleware for development
|
189
|
+
doesn't support configuration out of the box,
|
190
|
+
but it can be extended through inheritence:
|
180
191
|
|
181
192
|
``` ruby
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
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
|
193
208
|
```
|
194
209
|
|
195
|
-
<div id=
|
210
|
+
<div id="definitions"></div>
|
196
211
|
|
197
212
|
#### Typescript Definitions
|
198
213
|
|
199
|
-
JsRoutes has typescript support out of the box.
|
214
|
+
JsRoutes has typescript support out of the box.
|
200
215
|
|
201
216
|
Restrictions:
|
202
217
|
|
@@ -208,7 +223,7 @@ More advanced setup would involve calling manually:
|
|
208
223
|
|
209
224
|
``` ruby
|
210
225
|
JsRoutes.definitions! # to output to file
|
211
|
-
# or
|
226
|
+
# or
|
212
227
|
JsRoutes.definitions # to output to string
|
213
228
|
```
|
214
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>, [">=
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
}
|
21
|
-
|
22
|
-
|
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
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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 = defined?(Webpacker) ?
|
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"
|
data/lib/js_routes/engine.rb
CHANGED
@@ -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
|
@@ -13,4 +15,16 @@ class JsRoutes::Generators::Base < Rails::Generators::Base
|
|
13
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
|
24
|
-
|
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 =
|
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,8 +2,6 @@ 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"
|
@@ -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
|
-
|
22
|
+
alert(Routes.root_path())
|
25
23
|
JS
|
26
24
|
end
|
27
25
|
|