js-routes 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Readme.md +87 -49
- data/lib/js_routes/generators/webpacker.rb +32 -0
- data/lib/js_routes/version.rb +1 -1
- data/lib/js_routes.rb +12 -2
- data/lib/templates/erb.js +11 -0
- data/lib/templates/initializer.rb +5 -0
- data/lib/templates/routes.js.erb +1 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64ddd33bda1dffe333670e22c7abe2e578d960709c5ddb42a1559fea57495288
|
4
|
+
data.tar.gz: af6b923046c1d3e0e838be4502355d9a2c8646af03c90b084e0bf04ec41cbce5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15d336c5dae45e63e7ca1700a58ca9d9146c867a59034f9a1346fbdb03153162055fee77d018a8b206bf5ead8a813730ca8618332d1fd5b1a42e0336db8c4ece
|
7
|
+
data.tar.gz: fe9f55283a5b5d71a25b117f35cf9c2562128a4ca6d1efe4022a3f0108fb93779dd1ec13183b019cbf895df30ea52657c4c0f8c9df780ec7dcd486ad6eb71932
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
## master
|
2
2
|
|
3
|
+
## v2.1.1
|
4
|
+
|
5
|
+
* Added webpacker generator `./bin/rails generate js_routes:webpacker`
|
6
|
+
* Reorganized Readme to describe different setups with their pros and cons more clearly
|
7
|
+
|
3
8
|
## v2.1.0
|
4
9
|
|
5
10
|
* Support typescript defintions file aka `routes.d.ts`. See [Readme.md](./Readme.md#definitions) for more information.
|
data/Readme.md
CHANGED
@@ -16,6 +16,18 @@ gem "js-routes"
|
|
16
16
|
|
17
17
|
## Setup
|
18
18
|
|
19
|
+
There are 3 possible ways to setup JsRoutes:
|
20
|
+
|
21
|
+
* [Quick and easy](#quick-start)
|
22
|
+
* Requires rake task to be run each time route file is updated
|
23
|
+
* [Webpacker](#webpacker) automatic updates
|
24
|
+
* Requires ESM module system (the default)
|
25
|
+
* Doesn't support typescript definitions
|
26
|
+
* [Sprockets](#sprockets) legacy
|
27
|
+
* Deprecated and not recommended for modern apps
|
28
|
+
|
29
|
+
<div id='quick-start'></div>
|
30
|
+
|
19
31
|
### Quick Start
|
20
32
|
|
21
33
|
Run:
|
@@ -26,6 +38,7 @@ rake js:routes
|
|
26
38
|
rake js:routes:typescript
|
27
39
|
```
|
28
40
|
|
41
|
+
**IMPORTANT**: that this setup requires the rake task to be run each time routes file is updated.
|
29
42
|
|
30
43
|
Individual routes can be imported using:
|
31
44
|
|
@@ -42,14 +55,24 @@ import * as Routes from 'routes';
|
|
42
55
|
window.Routes = Routes;
|
43
56
|
```
|
44
57
|
|
45
|
-
**Note**: that this setup requires `rake js:routes` to be run each time routes file is updated.
|
46
|
-
|
47
58
|
<div id='webpacker'></div>
|
48
59
|
|
49
|
-
|
60
|
+
### Webpacker + automatic updates - Typescript
|
61
|
+
|
62
|
+
**IMPORTANT**: this setup doesn't support IDE autocompletion with [Typescript](#definitions)
|
63
|
+
|
64
|
+
|
65
|
+
#### Use a Generator
|
50
66
|
|
67
|
+
Run a command:
|
51
68
|
|
52
|
-
|
69
|
+
``` sh
|
70
|
+
./bin/rails generate js_routes:webpacker
|
71
|
+
```
|
72
|
+
|
73
|
+
#### Setup manually
|
74
|
+
|
75
|
+
The routes files can be automatically updated without `rake` task being called manually.
|
53
76
|
It requires [rails-erb-loader](https://github.com/usabilityhub/rails-erb-loader) npm package to work.
|
54
77
|
|
55
78
|
Add `erb` loader to webpacker:
|
@@ -63,16 +86,16 @@ Create webpack ERB config `config/webpack/loaders/erb.js`:
|
|
63
86
|
|
64
87
|
``` javascript
|
65
88
|
module.exports = {
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
}
|
75
|
-
}
|
89
|
+
module: {
|
90
|
+
rules: [
|
91
|
+
{
|
92
|
+
test: /\.erb$/,
|
93
|
+
enforce: 'pre',
|
94
|
+
loader: 'rails-erb-loader'
|
95
|
+
},
|
96
|
+
]
|
97
|
+
}
|
98
|
+
};
|
76
99
|
```
|
77
100
|
|
78
101
|
Enable `erb` extension in `config/webpack/environment.js`:
|
@@ -97,21 +120,30 @@ window.Routes = Routes;
|
|
97
120
|
|
98
121
|
<div id='definitions'></div>
|
99
122
|
|
100
|
-
|
123
|
+
### Typescript Definitions
|
124
|
+
|
125
|
+
JsRoutes has typescript support out of the box.
|
126
|
+
|
127
|
+
Restrictions:
|
101
128
|
|
102
|
-
|
129
|
+
* Only available if `module_type` is set to `ESM` (strongly recommended and default).
|
130
|
+
* Webpacker Automatic Updates are not available because typescript compiler can not be configured to understand `.erb` extensions.
|
131
|
+
|
132
|
+
For the basic setup of typscript definitions see [Quick Start](#quick-start) setup.
|
133
|
+
More advanced setup would involve calling manually:
|
103
134
|
|
104
135
|
``` ruby
|
105
|
-
JsRoutes.definitions!
|
136
|
+
JsRoutes.definitions! # to output to file
|
137
|
+
# or
|
138
|
+
JsRoutes.definitions # to output to string
|
106
139
|
```
|
107
140
|
|
108
|
-
|
141
|
+
Even more advanced setups can be achieved by setting `module_type` to `DTS` inside [configuration](#module_type)
|
142
|
+
which will cause any `JsRoutes` instance to generate defintions instead of routes themselves.
|
109
143
|
|
110
|
-
|
111
|
-
<%= JsRoutes.defintions %>
|
112
|
-
```
|
144
|
+
<div id="sprockets"></div>
|
113
145
|
|
114
|
-
|
146
|
+
### Sprockets (Deprecated)
|
115
147
|
|
116
148
|
If you are using [Sprockets](https://github.com/rails/sprockets-rails) you may configure js-routes in the following way.
|
117
149
|
|
@@ -140,7 +172,7 @@ This cache is not flushed on server restart in development environment.
|
|
140
172
|
|
141
173
|
**Important:** If routes.js file is not updated after some configuration change you need to run this rake task again.
|
142
174
|
|
143
|
-
|
175
|
+
## Configuration
|
144
176
|
|
145
177
|
You can configure JsRoutes in two main ways. Either with an initializer (e.g. `config/initializers/js_routes.rb`):
|
146
178
|
|
@@ -150,7 +182,7 @@ JsRoutes.setup do |config|
|
|
150
182
|
end
|
151
183
|
```
|
152
184
|
|
153
|
-
Or dynamically in JavaScript, although only [Formatter Options](#formatter-options) are supported
|
185
|
+
Or dynamically in JavaScript, although only [Formatter Options](#formatter-options) are supported:
|
154
186
|
|
155
187
|
``` js
|
156
188
|
import * as Routes from 'routes'
|
@@ -160,12 +192,14 @@ Routes.configure({
|
|
160
192
|
Routes.config(); // current config
|
161
193
|
```
|
162
194
|
|
163
|
-
|
195
|
+
### Available Options
|
164
196
|
|
165
|
-
|
197
|
+
#### Generator Options
|
166
198
|
|
167
199
|
Options to configure JavaScript file generator. These options are only available in Ruby context but not JavaScript.
|
168
200
|
|
201
|
+
<div id='module-type'></div>
|
202
|
+
|
169
203
|
* `module_type` - JavaScript module type for generated code. [Article](https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm)
|
170
204
|
* Options: `ESM`, `UMD`, `CJS`, `AMD`, `DTS`, `nil`.
|
171
205
|
* Default: `ESM`
|
@@ -196,7 +230,7 @@ Options to configure JavaScript file generator. These options are only available
|
|
196
230
|
* `file` - a file location where generated routes are stored
|
197
231
|
* Default: `app/javascript/routes.js` if setup with Webpacker, otherwise `app/assets/javascripts/routes.js` if setup with Sprockets.
|
198
232
|
|
199
|
-
|
233
|
+
#### Formatter Options
|
200
234
|
|
201
235
|
Options to configure routes formatting. These options are available both in Ruby and JavaScript context.
|
202
236
|
|
@@ -214,7 +248,7 @@ Options to configure routes formatting. These options are available both in Ruby
|
|
214
248
|
* This option exists because JS doesn't provide a difference between an object and a hash
|
215
249
|
* Default: `_options`
|
216
250
|
|
217
|
-
|
251
|
+
## Advanced Setup
|
218
252
|
|
219
253
|
In case you need multiple route files for different parts of your application, you have to create the files manually.
|
220
254
|
If your application has an `admin` and an `application` namespace for example:
|
@@ -252,22 +286,34 @@ Configuration above will create a nice javascript file with `Routes` object that
|
|
252
286
|
``` js
|
253
287
|
import * as Routes from 'routes';
|
254
288
|
|
255
|
-
Routes.users_path()
|
256
|
-
|
257
|
-
Routes.user_path(1, {format: 'json'}) // => "/users/1.json"
|
258
|
-
Routes.user_path(1, {anchor: 'profile'}) // => "/users/1#profile"
|
259
|
-
Routes.new_user_project_path(1, {format: 'json'}) // => "/users/1/projects/new.json"
|
260
|
-
Routes.user_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true"
|
261
|
-
Routes.user_project_path(1,2, {hello: ['world', 'mars']}) // => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars"
|
262
|
-
```
|
289
|
+
Routes.users_path()
|
290
|
+
// => "/users"
|
263
291
|
|
264
|
-
|
292
|
+
Routes.user_path(1)
|
293
|
+
// => "/users/1"
|
294
|
+
|
295
|
+
Routes.user_path(1, {format: 'json'})
|
296
|
+
// => "/users/1.json"
|
297
|
+
|
298
|
+
Routes.user_path(1, {anchor: 'profile'})
|
299
|
+
// => "/users/1#profile"
|
300
|
+
|
301
|
+
Routes.new_user_project_path(1, {format: 'json'})
|
302
|
+
// => "/users/1/projects/new.json"
|
303
|
+
|
304
|
+
Routes.user_project_path(1,2, {q: 'hello', custom: true})
|
305
|
+
// => "/users/1/projects/2?q=hello&custom=true"
|
306
|
+
|
307
|
+
Routes.user_project_path(1,2, {hello: ['world', 'mars']})
|
308
|
+
// => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars"
|
265
309
|
|
266
|
-
``` js
|
267
310
|
var google = {id: 1, name: "Google"};
|
268
|
-
Routes.company_path(google)
|
311
|
+
Routes.company_path(google)
|
312
|
+
// => "/companies/1"
|
313
|
+
|
269
314
|
var google = {id: 1, name: "Google", to_param: "google"};
|
270
|
-
Routes.company_path(google)
|
315
|
+
Routes.company_path(google)
|
316
|
+
// => "/companies/google"
|
271
317
|
```
|
272
318
|
|
273
319
|
In order to make routes helpers available globally:
|
@@ -276,7 +322,7 @@ In order to make routes helpers available globally:
|
|
276
322
|
jQuery.extend(window, Routes)
|
277
323
|
```
|
278
324
|
|
279
|
-
|
325
|
+
### Get spec of routes and required params
|
280
326
|
|
281
327
|
Possible to get `spec` of route by function `toString`:
|
282
328
|
|
@@ -361,14 +407,6 @@ Advantages of this one are:
|
|
361
407
|
* Support Rails `#to_param` convention for seo optimized paths
|
362
408
|
* Well tested
|
363
409
|
|
364
|
-
## Version 2 TODO
|
365
|
-
|
366
|
-
* Add routes generation .d.ts file
|
367
|
-
* Add config option on the output format: js, ts, d.ts
|
368
|
-
* Add prettier
|
369
|
-
* Add eslint
|
370
|
-
* Add development guide
|
371
|
-
|
372
410
|
#### Thanks to [contributors](https://github.com/railsware/js-routes/contributors)
|
373
411
|
|
374
412
|
#### Have fun
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
class JsRoutes::Webpacker < Rails::Generators::Base
|
4
|
+
|
5
|
+
source_root File.expand_path(__FILE__ + "/../../../templates")
|
6
|
+
|
7
|
+
def create_webpack
|
8
|
+
copy_file "initializer.rb", "config/initializers/js_routes.rb"
|
9
|
+
copy_file "erb.js", "config/webpack/loaders/erb.js"
|
10
|
+
copy_file "routes.js.erb", "app/javascript/routes.js.erb"
|
11
|
+
inject_into_file "config/webpack/environment.js", loader_content
|
12
|
+
inject_into_file "app/javascript/packs/application.js", pack_content
|
13
|
+
command = Rails.root.join("./bin/yarn add rails-erb-loader")
|
14
|
+
run command
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def pack_content
|
20
|
+
<<-JS
|
21
|
+
import * as Routes from 'routes.js.erb';
|
22
|
+
window.Routes = Routes;
|
23
|
+
JS
|
24
|
+
end
|
25
|
+
|
26
|
+
def loader_content
|
27
|
+
<<-JS
|
28
|
+
const erb = require('./loaders/erb')
|
29
|
+
environment.loaders.append('erb', erb)
|
30
|
+
JS
|
31
|
+
end
|
32
|
+
end
|
data/lib/js_routes/version.rb
CHANGED
data/lib/js_routes.rb
CHANGED
@@ -66,6 +66,10 @@ class JsRoutes
|
|
66
66
|
esm? || dts?
|
67
67
|
end
|
68
68
|
|
69
|
+
def require_esm
|
70
|
+
raise "ESM module type is required" unless modern?
|
71
|
+
end
|
72
|
+
|
69
73
|
def source_file
|
70
74
|
File.dirname(__FILE__) + "/" + default_file_name
|
71
75
|
end
|
@@ -115,7 +119,7 @@ class JsRoutes
|
|
115
119
|
@configuration ||= Configuration.new
|
116
120
|
end
|
117
121
|
|
118
|
-
def generate(opts
|
122
|
+
def generate(**opts)
|
119
123
|
new(opts).generate
|
120
124
|
end
|
121
125
|
|
@@ -123,9 +127,13 @@ class JsRoutes
|
|
123
127
|
new(file: file_name, **opts).generate!
|
124
128
|
end
|
125
129
|
|
130
|
+
def definitions(**opts)
|
131
|
+
generate(module_type: 'DTS', **opts)
|
132
|
+
end
|
133
|
+
|
126
134
|
def definitions!(file_name = nil, **opts)
|
127
135
|
file_name ||= configuration.file&.sub!(%r{\.(j|t)s\Z}, ".d.ts")
|
128
|
-
|
136
|
+
generate!(file_name, module_type: 'DTS', **opts)
|
129
137
|
end
|
130
138
|
|
131
139
|
def json(string)
|
@@ -449,3 +457,5 @@ JS
|
|
449
457
|
end
|
450
458
|
end
|
451
459
|
end
|
460
|
+
|
461
|
+
require "js_routes/generators/webpacker"
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= JsRoutes.generate %>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -171,11 +171,15 @@ files:
|
|
171
171
|
- lib/js-routes.rb
|
172
172
|
- lib/js_routes.rb
|
173
173
|
- lib/js_routes/engine.rb
|
174
|
+
- lib/js_routes/generators/webpacker.rb
|
174
175
|
- lib/js_routes/version.rb
|
175
176
|
- lib/routes.d.ts
|
176
177
|
- lib/routes.js
|
177
178
|
- lib/routes.ts
|
178
179
|
- lib/tasks/js_routes.rake
|
180
|
+
- lib/templates/erb.js
|
181
|
+
- lib/templates/initializer.rb
|
182
|
+
- lib/templates/routes.js.erb
|
179
183
|
- package.json
|
180
184
|
- spec/dummy/app/assets/config/manifest.js
|
181
185
|
- spec/dummy/app/assets/javascripts/.gitkeep
|