webpacker-routes 0.0.11 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -8
- data/lib/install/template.rb +1 -1
- data/lib/tasks/webpacker/routes_tasks.rake +1 -1
- data/lib/webpacker/routes.rb +88 -22
- data/lib/webpacker/routes/railtie.rb +9 -4
- data/lib/webpacker/routes/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0593e1d38a8f7b8c3f5f8d46a27609b6c4552fdaa7030ad15e05734d9c0f4ba
|
4
|
+
data.tar.gz: 1399772ba3737f5fb38418eb17c7730dd606f36f16c84df146e0a7bbccf94656
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c6916c6d80e316f5d73bd8217cbac538fc1a4caa23a4b950d8af566fce20776b97fc2534448ff059353ad3946ea94fc4721315e8ac643f6600876d64d0e0238
|
7
|
+
data.tar.gz: a129f2c9afe03ff317b1e0f9c42ec045edd53680d16b08bca3a4cc1d26e869b4666e947a9ef199ee6fae3bcca93056aa657cd5913404a07bfdbae19b73d4b052
|
data/README.md
CHANGED
@@ -18,9 +18,34 @@ $ bundle exec rails webpacker:install:routes
|
|
18
18
|
Import individual routes from any Webpacker-compiled file:
|
19
19
|
|
20
20
|
```javascript
|
21
|
-
import { root_path } from 'routes'
|
21
|
+
import { root_path, root_url } from 'routes'
|
22
|
+
import { engine_path } from 'routes/engine_name'
|
22
23
|
|
23
|
-
|
24
|
+
root_path()
|
25
|
+
// /
|
26
|
+
|
27
|
+
root_path({ foo: 'bar' })
|
28
|
+
// /?foo=bar
|
29
|
+
|
30
|
+
root_url({ host: 'https://example.com' })
|
31
|
+
// https://example.com/
|
32
|
+
|
33
|
+
root_url({ host: 'https://example.com', bar: 'baz' })
|
34
|
+
// https://example.com/?bar=baz
|
35
|
+
|
36
|
+
root_url({
|
37
|
+
anchor: 'abc',
|
38
|
+
host: 'example.com',
|
39
|
+
params: {
|
40
|
+
foo: 'bar'
|
41
|
+
},
|
42
|
+
port: 3000,
|
43
|
+
protocol: 'https',
|
44
|
+
relative_url_root: '/rel',
|
45
|
+
trailing_slash: true,
|
46
|
+
bar: 'baz'
|
47
|
+
})
|
48
|
+
// https://example.com:3000/rel/?bar=baz&foo=bar#abc
|
24
49
|
```
|
25
50
|
|
26
51
|
The routes file is generated when Rails starts, including during `webpacker:compile` (or `assets:precompile`).
|
@@ -31,11 +56,10 @@ To generate routes manually, run:
|
|
31
56
|
$ bundle exec rails webpacker:routes:generate
|
32
57
|
```
|
33
58
|
|
34
|
-
|
35
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
59
|
+
### Options
|
36
60
|
|
37
|
-
|
61
|
+
- `config.webpacker.routes.default_url_options` - defaults used for generating urls. These are merged with `Rails.application.default_url_options`. Default: `{}`
|
62
|
+
- `config.webpacker.routes.camel_case` - convert route names to camel case. Default: `false`
|
38
63
|
|
39
|
-
|
40
|
-
|
41
|
-
- camelcase
|
64
|
+
## License
|
65
|
+
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"
|
data/lib/webpacker/routes.rb
CHANGED
@@ -4,7 +4,7 @@ require "webpacker/routes/railtie"
|
|
4
4
|
module Webpacker
|
5
5
|
class Configuration
|
6
6
|
def routes_path
|
7
|
-
source_path.join('routes')
|
7
|
+
fetch(:routes_path) || source_path.join('routes')
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -13,29 +13,80 @@ module Webpacker
|
|
13
13
|
IGNORED_OPTIONS = %i[controller action]
|
14
14
|
|
15
15
|
class << self
|
16
|
-
def generate(
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
16
|
+
def generate(app)
|
17
|
+
config = app.config.webpacker.routes
|
18
|
+
var_name = -> (name) { config.camel_case ? name.camelize(:lower) : name }
|
19
|
+
|
20
|
+
default_url_options = app.default_url_options.dup
|
21
|
+
default_url_options[:relative_url_root] = app.config.relative_url_root if app.config.relative_url_root
|
22
|
+
default_url_options.merge!(config.default_url_options)
|
23
|
+
default_url_options.except!(*IGNORED_OPTIONS)
|
24
|
+
|
25
|
+
parent_spec_var = var_name.call('parent_spec')
|
26
|
+
default_url_options_var = var_name.call('default_url_options')
|
27
|
+
|
28
|
+
route_sets = [[app.routes, nil, Webpacker.config.routes_path]]
|
29
|
+
visited_directories = []
|
30
|
+
|
31
|
+
while (route_set, parent, directory = route_sets.shift)
|
32
|
+
directory.mkpath
|
33
|
+
visited_directories << directory
|
34
|
+
js_file = directory.join('index.js')
|
35
|
+
|
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
|
43
|
+
|
44
|
+
temp_file.write(<<-JAVASCRIPT.strip_heredoc)
|
45
|
+
import { urlFor, pathFor } from 'webpacker-routes'
|
46
|
+
#{parent_var_definition}
|
47
|
+
const #{default_url_options_var} = #{js(default_url_options)}
|
48
|
+
JAVASCRIPT
|
49
|
+
|
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
|
84
|
+
end
|
37
85
|
end
|
38
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)
|
39
90
|
end
|
40
91
|
|
41
92
|
private
|
@@ -43,6 +94,21 @@ module Webpacker
|
|
43
94
|
def js(obj)
|
44
95
|
ERB::Util.json_escape(obj.to_json)
|
45
96
|
end
|
97
|
+
|
98
|
+
def identical?(path1, path2)
|
99
|
+
FileUtils.compare_file(path1, path2)
|
100
|
+
rescue Errno::ENOENT
|
101
|
+
false
|
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
|
46
112
|
end
|
47
113
|
end
|
48
114
|
end
|
@@ -1,14 +1,19 @@
|
|
1
|
+
require 'webpacker/railtie'
|
2
|
+
|
1
3
|
module Webpacker
|
2
4
|
module Routes
|
3
5
|
class Engine < ::Rails::Engine
|
6
|
+
config.webpacker.routes = ActiveSupport::OrderedOptions.new
|
7
|
+
config.webpacker.routes.default_url_options = {}
|
8
|
+
config.webpacker.routes.camel_case = false
|
9
|
+
|
4
10
|
config.after_initialize do |app|
|
5
|
-
generate = -> { Webpacker::Routes.generate(app.tap(&:reload_routes!).routes) }
|
6
11
|
if Rails::VERSION::MAJOR >= 5
|
7
|
-
app.reloader.to_run(
|
12
|
+
app.reloader.to_run(:after) { Webpacker::Routes.generate(app) }
|
8
13
|
else
|
9
|
-
ActionDispatch::Reloader.to_prepare(
|
14
|
+
ActionDispatch::Reloader.to_prepare(:after) { Webpacker::Routes.generate(app) }
|
10
15
|
end
|
11
|
-
generate.
|
16
|
+
Webpacker::Routes.generate(app.tap(&:reload_routes!)) unless ENV['WEBPACKER_ROUTES_INSTALL']
|
12
17
|
end
|
13
18
|
end
|
14
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webpacker-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Harsha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -84,7 +84,8 @@ files:
|
|
84
84
|
homepage: https://github.com/davishmcclurg/webpacker-routes
|
85
85
|
licenses:
|
86
86
|
- MIT
|
87
|
-
metadata:
|
87
|
+
metadata:
|
88
|
+
source_code_uri: https://github.com/davishmcclurg/webpacker-routes
|
88
89
|
post_install_message:
|
89
90
|
rdoc_options: []
|
90
91
|
require_paths:
|