webpacker-routes 0.1.2 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/lib/install/template.rb +1 -1
- data/lib/webpacker/routes/railtie.rb +1 -1
- data/lib/webpacker/routes/version.rb +1 -1
- data/lib/webpacker/routes.rb +63 -25
- metadata +24 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2df2a8394731fbcdcf3ca655bc2828445059ca63fb43f907f979f7aa086baa8a
|
4
|
+
data.tar.gz: b90011254262095c3ec086d0ed3f25b0de9b24ab5cc68e7f8d776e2610cbe8a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bad512988328796c14bd7c6049834d46d02bbdaf27d9fdd2e2f70d33c3f816c3e209ccbaf225a086fef1ad6bd25f4098c0b11fbb5e2641150603a3dc632730f
|
7
|
+
data.tar.gz: 44e7c1538a4078ce206cc769c852aa7907d867e46c4ba844966d8163702e5863c2a96eee99fca78186ca5cedbc2cd90983cc4ff5d48cb91dffbd23d9e73a1820
|
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# Webpacker::Routes
|
2
|
+
|
2
3
|
Webpacker Routes allows you to import Rails routes in your Webpacker javascript.
|
3
4
|
|
4
5
|
## Installation
|
6
|
+
|
5
7
|
Add this line to your application's Gemfile:
|
6
8
|
|
7
9
|
```ruby
|
@@ -9,16 +11,19 @@ gem 'webpacker-routes'
|
|
9
11
|
```
|
10
12
|
|
11
13
|
And then execute:
|
14
|
+
|
12
15
|
```bash
|
13
16
|
$ bundle
|
14
17
|
$ bundle exec rails webpacker:install:routes
|
15
18
|
```
|
16
19
|
|
17
20
|
## Usage
|
21
|
+
|
18
22
|
Import individual routes from any Webpacker-compiled file:
|
19
23
|
|
20
24
|
```javascript
|
21
25
|
import { root_path, root_url } from 'routes'
|
26
|
+
import { engine_path } from 'routes/engine_name'
|
22
27
|
|
23
28
|
root_path()
|
24
29
|
// /
|
@@ -51,6 +56,7 @@ The routes file is generated when Rails starts, including during `webpacker:comp
|
|
51
56
|
In development, routes will be updated when a file changes and a request is processed.
|
52
57
|
|
53
58
|
To generate routes manually, run:
|
59
|
+
|
54
60
|
```bash
|
55
61
|
$ bundle exec rails webpacker:routes:generate
|
56
62
|
```
|
@@ -61,4 +67,5 @@ $ bundle exec rails webpacker:routes:generate
|
|
61
67
|
- `config.webpacker.routes.camel_case` - convert route names to camel case. Default: `false`
|
62
68
|
|
63
69
|
## License
|
70
|
+
|
64
71
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/install/template.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
say "Creating JavaScript app source directory"
|
2
|
-
file Webpacker.config.routes_path.join('.gitignore'), "
|
2
|
+
file Webpacker.config.routes_path.join('.gitignore'), "*\n!.gitignore\n"
|
3
3
|
|
4
4
|
say "Installing all JavaScript dependencies"
|
5
5
|
run "yarn add webpacker-routes@#{Webpacker::Routes::VERSION} --exact"
|
@@ -8,7 +8,7 @@ module Webpacker
|
|
8
8
|
config.webpacker.routes.camel_case = false
|
9
9
|
|
10
10
|
config.after_initialize do |app|
|
11
|
-
if Rails
|
11
|
+
if Rails.gem_version >= Gem::Version.new('5.0.0')
|
12
12
|
app.reloader.to_run(:after) { Webpacker::Routes.generate(app) }
|
13
13
|
else
|
14
14
|
ActionDispatch::Reloader.to_prepare(:after) { Webpacker::Routes.generate(app) }
|
data/lib/webpacker/routes.rb
CHANGED
@@ -22,42 +22,71 @@ module Webpacker
|
|
22
22
|
default_url_options.merge!(config.default_url_options)
|
23
23
|
default_url_options.except!(*IGNORED_OPTIONS)
|
24
24
|
|
25
|
+
parent_spec_var = var_name.call('parent_spec')
|
25
26
|
default_url_options_var = var_name.call('default_url_options')
|
26
27
|
|
27
|
-
|
28
|
+
route_sets = [[app.routes, nil, Webpacker.config.routes_path]]
|
29
|
+
visited_directories = []
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
const #{default_url_options_var} = #{js(default_url_options)}
|
34
|
-
JAVASCRIPT
|
31
|
+
while (route_set, parent, directory = route_sets.shift)
|
32
|
+
directory.mkpath
|
33
|
+
visited_directories << directory
|
34
|
+
js_file = directory.join('index.js')
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
spec_var = var_name.call("#{name}_spec")
|
44
|
-
url_var = var_name.call("#{name}_url")
|
45
|
-
path_var = var_name.call("#{name}_path")
|
36
|
+
catch(:identical) do
|
37
|
+
File.atomic_write(js_file) do |temp_file|
|
38
|
+
parent_var_definition = if parent
|
39
|
+
"import { #{parent} as #{parent_spec_var} } from '../'"
|
40
|
+
else
|
41
|
+
"const #{parent_spec_var} = null"
|
42
|
+
end
|
46
43
|
|
47
44
|
temp_file.write(<<-JAVASCRIPT.strip_heredoc)
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
import { urlFor, pathFor } from 'webpacker-routes'
|
46
|
+
#{parent_var_definition}
|
47
|
+
const #{default_url_options_var} = #{js(default_url_options)}
|
51
48
|
JAVASCRIPT
|
52
|
-
end
|
53
49
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
50
|
+
route_set.named_routes.sort_by(&:first).each do |name, route|
|
51
|
+
raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
|
52
|
+
|
53
|
+
spec = route.path.spec.to_s
|
54
|
+
segment_keys = route.segment_keys.uniq
|
55
|
+
options = route.defaults.except(*IGNORED_OPTIONS)
|
56
|
+
|
57
|
+
spec_var = var_name.call("#{name}_spec")
|
58
|
+
url_var = var_name.call("#{name}_url")
|
59
|
+
path_var = var_name.call("#{name}_path")
|
60
|
+
|
61
|
+
temp_file.write(<<-JAVASCRIPT.strip_heredoc)
|
62
|
+
export const #{spec_var} = [#{js(spec)}, #{js(segment_keys)}, { ...#{default_url_options_var}, ...#{js(options)} }, #{parent_spec_var}]
|
63
|
+
export const #{url_var} = (...args) => urlFor(#{spec_var}, ...args)
|
64
|
+
export const #{path_var} = (...args) => pathFor(#{spec_var}, ...args)
|
65
|
+
JAVASCRIPT
|
66
|
+
|
67
|
+
if engine?(route)
|
68
|
+
engine = rack_app(route)
|
69
|
+
engine_name = engine.railtie_name
|
70
|
+
|
71
|
+
raise `Invalid engine name for javascript: ${engine_name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ engine_name
|
72
|
+
|
73
|
+
engine_name_var = var_name.call(engine_name)
|
74
|
+
|
75
|
+
route_sets << [engine.routes, spec_var, directory.join(engine_name_var)]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
temp_file.close
|
80
|
+
if identical?(js_file.to_s, temp_file.path)
|
81
|
+
temp_file.unlink
|
82
|
+
throw :identical
|
83
|
+
end
|
58
84
|
end
|
59
85
|
end
|
60
86
|
end
|
87
|
+
|
88
|
+
extra_directories = Webpacker.config.routes_path.glob('**/*').select(&:directory?) - visited_directories
|
89
|
+
extra_directories.sort_by { |directory| directory.to_s.size }.reverse_each(&:rmtree)
|
61
90
|
end
|
62
91
|
|
63
92
|
private
|
@@ -71,6 +100,15 @@ module Webpacker
|
|
71
100
|
rescue Errno::ENOENT
|
72
101
|
false
|
73
102
|
end
|
103
|
+
|
104
|
+
def rack_app(route)
|
105
|
+
route.app.app
|
106
|
+
end
|
107
|
+
|
108
|
+
def engine?(route)
|
109
|
+
app = rack_app(route)
|
110
|
+
app.is_a?(Class) && app < Rails::Engine
|
111
|
+
end
|
74
112
|
end
|
75
113
|
end
|
76
114
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webpacker-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Harsha
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: railties
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.2'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: sqlite3
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,7 +80,7 @@ dependencies:
|
|
66
80
|
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
69
|
-
description:
|
83
|
+
description:
|
70
84
|
email:
|
71
85
|
- davishmcclurg@gmail.com
|
72
86
|
executables: []
|
@@ -84,8 +98,9 @@ files:
|
|
84
98
|
homepage: https://github.com/davishmcclurg/webpacker-routes
|
85
99
|
licenses:
|
86
100
|
- MIT
|
87
|
-
metadata:
|
88
|
-
|
101
|
+
metadata:
|
102
|
+
source_code_uri: https://github.com/davishmcclurg/webpacker-routes
|
103
|
+
post_install_message:
|
89
104
|
rdoc_options: []
|
90
105
|
require_paths:
|
91
106
|
- lib
|
@@ -100,8 +115,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
115
|
- !ruby/object:Gem::Version
|
101
116
|
version: '0'
|
102
117
|
requirements: []
|
103
|
-
rubygems_version: 3.
|
104
|
-
signing_key:
|
118
|
+
rubygems_version: 3.3.7
|
119
|
+
signing_key:
|
105
120
|
specification_version: 4
|
106
121
|
summary: Convert Rails routes to JavaScript modules
|
107
122
|
test_files: []
|