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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5194266884b788ff6ffd76ecf6408d7196fc8e4769381c6a63ef19ad49a30571
4
- data.tar.gz: 3fc7761af1ec667dd6f9cb742dd5131439bd42e36e801bc1f21f4d1bfd44e7b8
3
+ metadata.gz: c0593e1d38a8f7b8c3f5f8d46a27609b6c4552fdaa7030ad15e05734d9c0f4ba
4
+ data.tar.gz: 1399772ba3737f5fb38418eb17c7730dd606f36f16c84df146e0a7bbccf94656
5
5
  SHA512:
6
- metadata.gz: df569079481a998bec69b845602ee4f9ec35d6589fab7a91248608c9bcfeaa53a37ad2f25b35061b4be65ee427a6d188f98c5691526fa26deeca9d285c31d0d5
7
- data.tar.gz: acd55b6c961fa9372b311d96ff6ea9494213f8dbd83d1963f73b8b0b5b16d2f5a1134467ef4f5d3d4459de648e750219f76a7be4faa3950c41ccd916fb53c9bf
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
- console.log(root_path())
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
- ## License
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
- ## Todo
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
- - support all valid route names
40
- - relative_url_root
41
- - camelcase
64
+ ## License
65
+ 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"
@@ -25,7 +25,7 @@ namespace :webpacker do
25
25
 
26
26
  desc "Generate routes package"
27
27
  task generate: [:verify_install, :environment] do
28
- Webpacker::Routes.generate(Rails.application.routes)
28
+ Webpacker::Routes.generate(Rails.application)
29
29
  end
30
30
  end
31
31
  end
@@ -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(route_set)
17
- File.atomic_write(Webpacker.config.routes_path.join('index.js')) do |file|
18
- default_url_options = js(route_set.default_url_options.except(*IGNORED_OPTIONS))
19
-
20
- file.write(<<-JAVASCRIPT.strip_heredoc)
21
- import { urlFor, pathFor } from 'webpacker-routes'
22
- const default_url_options = #{default_url_options}
23
- JAVASCRIPT
24
-
25
- route_set.named_routes.sort_by(&:first).each do |name, route|
26
- raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
27
-
28
- spec = js(route.path.spec.to_s)
29
- segment_keys = js(route.segment_keys.uniq)
30
- options = js(route.defaults.except(*IGNORED_OPTIONS))
31
-
32
- file.write(<<-JAVASCRIPT.strip_heredoc)
33
- const #{name}_spec = [#{spec}, #{segment_keys}, { ...default_url_options, ...#{options} }]
34
- export const #{name}_url = (...args) => urlFor(#{name}_spec, ...args)
35
- export const #{name}_path = (...args) => pathFor(#{name}_spec, ...args)
36
- JAVASCRIPT
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(&generate)
12
+ app.reloader.to_run(:after) { Webpacker::Routes.generate(app) }
8
13
  else
9
- ActionDispatch::Reloader.to_prepare(&generate)
14
+ ActionDispatch::Reloader.to_prepare(:after) { Webpacker::Routes.generate(app) }
10
15
  end
11
- generate.call unless ENV['WEBPACKER_ROUTES_INSTALL'] == 'true'
16
+ Webpacker::Routes.generate(app.tap(&:reload_routes!)) unless ENV['WEBPACKER_ROUTES_INSTALL']
12
17
  end
13
18
  end
14
19
  end
@@ -1,5 +1,5 @@
1
1
  module Webpacker
2
2
  module Routes
3
- VERSION = '0.0.11'
3
+ VERSION = '0.1.4'
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.0.11
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: 2019-03-14 00:00:00.000000000 Z
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: