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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0ace4cd3e92b5e779788f42c032b8227f8352cc124f72d5725ac08280125097
4
- data.tar.gz: ca63b493a4a7f3f3115ca19a87883628cfdf864e2862b00d35c9e807af795e63
3
+ metadata.gz: 2df2a8394731fbcdcf3ca655bc2828445059ca63fb43f907f979f7aa086baa8a
4
+ data.tar.gz: b90011254262095c3ec086d0ed3f25b0de9b24ab5cc68e7f8d776e2610cbe8a1
5
5
  SHA512:
6
- metadata.gz: b2dec026375850eda7b48f59c0ffa89e17ccb36fc7f4ea87311047416aabd7fdfbaac2f53ec47a552c0e8c39b551a0ad8859df8a7dba5f4106a2bd53f23781de
7
- data.tar.gz: ccf44fcf0f9e8c02b1de95b392a9992e3ed6646bd08bf11301d36bda1317347b256bcab93a83a197fd094436c8b7f27da742f499304b879b1d5b6662fce4b67d
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).
@@ -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"
@@ -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::VERSION::MAJOR >= 5
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) }
@@ -1,5 +1,5 @@
1
1
  module Webpacker
2
2
  module Routes
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.5'
4
4
  end
5
5
  end
@@ -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
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.2
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: 2019-12-18 00:00:00.000000000 Z
11
+ date: 2022-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
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
- post_install_message:
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.0.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: []