webpacker-routes 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0ace4cd3e92b5e779788f42c032b8227f8352cc124f72d5725ac08280125097
4
- data.tar.gz: ca63b493a4a7f3f3115ca19a87883628cfdf864e2862b00d35c9e807af795e63
3
+ metadata.gz: 055ecdf48cffd4ebd36c9b3564203c9c6a461555a4bac52094b7883150708f58
4
+ data.tar.gz: 9fbab7703efe9952af0ac6ac3747480cf9e6e92e7c5023e48d9e5c247f53ac28
5
5
  SHA512:
6
- metadata.gz: b2dec026375850eda7b48f59c0ffa89e17ccb36fc7f4ea87311047416aabd7fdfbaac2f53ec47a552c0e8c39b551a0ad8859df8a7dba5f4106a2bd53f23781de
7
- data.tar.gz: ccf44fcf0f9e8c02b1de95b392a9992e3ed6646bd08bf11301d36bda1317347b256bcab93a83a197fd094436c8b7f27da742f499304b879b1d5b6662fce4b67d
6
+ metadata.gz: 5e71f761d6fa2a2e5acee218f952827938a5c1c36f6ad7d7575d474a5d474f1cbbb18736371f01a0aebde3eae11b274c4a76ea5b1985c726bb8c4b9919e7969d
7
+ data.tar.gz: 8dce45f1f1e0b7c9dc9d8ecf816544f92a5810d80d7227f32a4616d8aa910b6dea142a86ea63b032776a195044027e1662d50b1a701fa6fed3655ba4e50de717
data/README.md CHANGED
@@ -19,6 +19,7 @@ Import individual routes from any Webpacker-compiled file:
19
19
 
20
20
  ```javascript
21
21
  import { root_path, root_url } from 'routes'
22
+ import { engine_path } from 'routes/engine_name'
22
23
 
23
24
  root_path()
24
25
  // /
@@ -1,5 +1,5 @@
1
1
  say "Creating JavaScript app source directory"
2
- file Webpacker.config.routes_path.join('.gitignore'), "/index.js\n"
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"
@@ -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
- js_file = Webpacker.config.routes_path.join('index.js')
28
+ route_sets = [[app.routes, nil, Webpacker.config.routes_path]]
29
+ visited_directories = []
28
30
 
29
- catch(:identical) do
30
- File.atomic_write(js_file) do |temp_file|
31
- temp_file.write(<<-JAVASCRIPT.strip_heredoc)
32
- import { urlFor, pathFor } from 'webpacker-routes'
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
- app.routes.named_routes.sort_by(&:first).each do |name, route|
37
- raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
38
-
39
- spec = route.path.spec.to_s
40
- segment_keys = route.segment_keys.uniq
41
- options = route.defaults.except(*IGNORED_OPTIONS)
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
- const #{spec_var} = [#{js(spec)}, #{js(segment_keys)}, { ...#{default_url_options_var}, ...#{js(options)} }]
49
- export const #{url_var} = (...args) => urlFor(#{spec_var}, ...args)
50
- export const #{path_var} = (...args) => pathFor(#{spec_var}, ...args)
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
- temp_file.close
55
- if identical?(js_file.to_s, temp_file.path)
56
- temp_file.unlink
57
- throw :identical
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
@@ -1,5 +1,5 @@
1
1
  module Webpacker
2
2
  module Routes
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  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.2
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: 2019-12-18 00:00:00.000000000 Z
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: