webpacker-routes 0.0.4

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 870c1f1a66d6273a647b5c278d7985d07e23a6814bce47f3626a69e8b46d3e64
4
+ data.tar.gz: 0afca05fd9696f6a14bad00d51a3be56180b80a8a55e8ec5875fc75352d313d3
5
+ SHA512:
6
+ metadata.gz: 355ae6d680169c228079bb5e93287ef82bdd1521c075ba748528869fefa56d9ad2f6e063150efe864f815cf8f2711233aeaa000e15f4a690d967093bd565871c
7
+ data.tar.gz: 41bb64fe9f9fa95a0dd86f42e432c6e710815d3ac83a62c1b6c86fb12eb226cd3fbec3115f13b717427ee641e6d0c0ee2dbcf5a517e536a13bc07619a4c22c5a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 David Harsha
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Webpacker::Routes
2
+ Webpacker Routes allows you to import Rails routes in your Webpacker javascript.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'webpacker-routes'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ $ bundle exec rails webpacker:install:routes
15
+ ```
16
+
17
+ Finally, generate your routes file:
18
+ ```bash
19
+ $ bundle exec rails webpacker:routes:generate
20
+ ```
21
+
22
+ You will need to regenerate the routes file whenever you add a new Rails route.
23
+
24
+ ## Usage
25
+ Import individual routes from any Webpacker-compiled file:
26
+
27
+ ```javascript
28
+ import { root_path } from 'routes'
29
+
30
+ console.log(root_path())
31
+ ```
32
+
33
+ ## License
34
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
35
+
36
+ ## TODO
37
+
38
+ - fix handle_positional_args in rails
39
+ - somehow generate routes automatically in development
40
+ - to_prepare?
41
+ - support all valid route names
42
+ - default_url_options
43
+ - relative_url_root
44
+ - camelcase
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Webpacker::Routes'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1 @@
1
+ // run `webpacker:routes:generate`
@@ -0,0 +1,5 @@
1
+ say "Creating JavaScript app source directory"
2
+ directory "#{__dir__}/javascript", Webpacker.config.source_path.join('routes')
3
+
4
+ say "Installing all JavaScript dependencies"
5
+ run "yarn add webpacker-routes@0.0.4 --exact"
@@ -0,0 +1,33 @@
1
+ namespace :webpacker do
2
+ namespace :install do
3
+ desc "Install everything needed for routes"
4
+ task routes: ["webpacker:verify_install"] do
5
+ template = File.expand_path("../../install/template.rb", __dir__)
6
+ if Rails::VERSION::MAJOR >= 5
7
+ exec "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{template}"
8
+ else
9
+ exec "#{RbConfig.ruby} ./bin/rake rails:template LOCATION=#{template}"
10
+ end
11
+ end
12
+ end
13
+
14
+ namespace :routes do
15
+ desc "Verifies if routes is installed"
16
+ task verify_install: ["webpacker:verify_install"] do
17
+ if Webpacker.config.routes_path.exist?
18
+ $stdout.puts "Webpacker Routes is installed 🎉 🍰"
19
+ $stdout.puts "Using #{Webpacker.config.routes_path} file for generating routes"
20
+ else
21
+ $stderr.puts "Webpacker Routes directory not found. \n"\
22
+ "Make sure webpacker:install:routes is run successfully before " \
23
+ "running dependent tasks"
24
+ exit!
25
+ end
26
+ end
27
+
28
+ desc "Generate routes package"
29
+ task generate: [:verify_install, :environment] do
30
+ Webpacker::Routes.generate(Rails.application.routes)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ require "webpacker/routes/railtie"
2
+
3
+ module Webpacker
4
+ class Configuration
5
+ def routes_path
6
+ source_path.join('routes')
7
+ end
8
+ end
9
+
10
+ module Routes
11
+ JAVASCRIPT_VARIABLE_NAME_REGEX = /\A[_$a-z][_$a-z0-9]*\z/i
12
+
13
+ def self.generate(route_set)
14
+ File.atomic_write(Webpacker.config.routes_path.join('index.js')) do |file|
15
+ file.write(<<-JAVASCRIPT.strip_heredoc)
16
+ import { urlFor, pathFor } from 'webpacker-routes'
17
+ JAVASCRIPT
18
+
19
+ route_set.named_routes.sort_by(&:first).each do |name, route|
20
+ raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
21
+
22
+ spec = route.path.spec.to_s.to_json
23
+ segment_keys = route.segment_keys.uniq.to_json
24
+ defaults = route.defaults.except(:controller, :action).to_json
25
+
26
+ file.write(<<-JAVASCRIPT.strip_heredoc)
27
+ const #{name} = [#{spec}, #{segment_keys}, #{defaults}]
28
+ export const #{name}_url = (...args) => urlFor(#{name}, ...args)
29
+ export const #{name}_path = (...args) => pathFor(#{name}, ...args)
30
+ JAVASCRIPT
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ module Webpacker
2
+ module Routes
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Webpacker
2
+ module Routes
3
+ VERSION = '0.0.4'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpacker-routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - David Harsha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: webpacker
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: execjs
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - davishmcclurg@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - lib/install/javascript/index.js
80
+ - lib/install/template.rb
81
+ - lib/tasks/webpacker/routes_tasks.rake
82
+ - lib/webpacker/routes.rb
83
+ - lib/webpacker/routes/railtie.rb
84
+ - lib/webpacker/routes/version.rb
85
+ homepage: https://github.com/davishmcclurg/webpacker-routes
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.7.6
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Convert Rails routes to JavaScript modules
109
+ test_files: []