webpacker-routes 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/install/template.rb +1 -1
- data/lib/webpacker/routes.rb +63 -25
- 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: 055ecdf48cffd4ebd36c9b3564203c9c6a461555a4bac52094b7883150708f58
|
4
|
+
data.tar.gz: 9fbab7703efe9952af0ac6ac3747480cf9e6e92e7c5023e48d9e5c247f53ac28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e71f761d6fa2a2e5acee218f952827938a5c1c36f6ad7d7575d474a5d474f1cbbb18736371f01a0aebde3eae11b274c4a76ea5b1985c726bb8c4b9919e7969d
|
7
|
+
data.tar.gz: 8dce45f1f1e0b7c9dc9d8ecf816544f92a5810d80d7227f32a4616d8aa910b6dea142a86ea63b032776a195044027e1662d50b1a701fa6fed3655ba4e50de717
|
data/README.md
CHANGED
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
@@ -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,14 +1,14 @@
|
|
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.3
|
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-02-23 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:
|