js-routes 1.4.9 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +15 -0
  3. data/.gitignore +5 -0
  4. data/.nvmrc +1 -0
  5. data/.travis.yml +2 -1
  6. data/CHANGELOG.md +85 -0
  7. data/Rakefile +5 -2
  8. data/Readme.md +274 -98
  9. data/VERSION_2_UPGRADE.md +66 -0
  10. data/js-routes.gemspec +8 -5
  11. data/lib/js_routes/engine.rb +0 -1
  12. data/lib/js_routes/generators/middleware.rb +33 -0
  13. data/lib/js_routes/generators/webpacker.rb +32 -0
  14. data/lib/js_routes/middleware.rb +36 -0
  15. data/lib/js_routes/version.rb +1 -1
  16. data/lib/js_routes.rb +341 -171
  17. data/lib/routes.d.ts +79 -0
  18. data/lib/routes.js +501 -519
  19. data/lib/routes.ts +737 -0
  20. data/lib/tasks/js_routes.rake +8 -2
  21. data/lib/templates/erb.js +11 -0
  22. data/lib/templates/initializer.rb +5 -0
  23. data/lib/templates/routes.js.erb +1 -0
  24. data/package.json +37 -0
  25. data/spec/dummy/app/assets/config/manifest.js +2 -0
  26. data/spec/js_routes/default_serializer_spec.rb +19 -3
  27. data/spec/js_routes/{amd_compatibility_spec.rb → module_types/amd_spec.rb} +7 -14
  28. data/spec/js_routes/module_types/cjs_spec.rb +15 -0
  29. data/spec/js_routes/module_types/dts/routes.spec.d.ts +114 -0
  30. data/spec/js_routes/module_types/dts/test.spec.ts +56 -0
  31. data/spec/js_routes/module_types/dts_spec.rb +111 -0
  32. data/spec/js_routes/module_types/esm_spec.rb +45 -0
  33. data/spec/js_routes/module_types/nil_spec.rb +86 -0
  34. data/spec/js_routes/{generated_javascript_spec.rb → module_types/umd_spec.rb} +33 -27
  35. data/spec/js_routes/options_spec.rb +67 -56
  36. data/spec/js_routes/rails_routes_compatibility_spec.rb +69 -25
  37. data/spec/js_routes/zzz_last_post_rails_init_spec.rb +4 -4
  38. data/spec/spec_helper.rb +34 -17
  39. data/spec/support/routes.rb +10 -4
  40. data/spec/tsconfig.json +4 -0
  41. data/tsconfig.json +28 -0
  42. data/yarn.lock +2145 -0
  43. metadata +42 -22
  44. data/lib/routes.js.coffee +0 -411
@@ -0,0 +1,66 @@
1
+ ## Version 2.0 upgrade notes
2
+
3
+ ### Using ESM module by default
4
+
5
+ New version of JsRoutes doesn't try to guess your javascript environment module system because JS has generated a ton of legacy module systems in the past.
6
+ [ESM+Webpacker](/Readme.md#webpacker) upgrade is recommended.
7
+
8
+ However, if you don't want to follow that pass, specify `module_type` configuration option instead based on module system available in your JS environment.
9
+ Here are supported values:
10
+
11
+ * CJS
12
+ * UMD
13
+ * AMD
14
+ * ESM
15
+ * nil
16
+
17
+ [Explaination Article](https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm)
18
+
19
+ If you don't want to use any JS module system and make routes available via a **global variable**, specify `nil` as a `module_type` and use `namespace` option:
20
+
21
+ ``` ruby
22
+ JsRoutes.setup do |config|
23
+ config.module_type = nil
24
+ config.namespace = "Routes"
25
+ end
26
+ ```
27
+
28
+ ### JSDoc comment
29
+
30
+ New version of js-routes generates function comment in the [JSDoc](https://jsdoc.app) format.
31
+ If you have any problems with that, you can disable it like this:
32
+
33
+
34
+ ``` ruby
35
+ JsRoutes.setup do |config|
36
+ config.documentation = false
37
+ end
38
+ ```
39
+
40
+ ### `required_params` renamed
41
+
42
+ In case you are using `required_params` property, it is now renamed and converted to a method:
43
+
44
+ ``` javascript
45
+ // Old style
46
+ Routes.post_path.required_params // => ['id']
47
+ // New style
48
+ Routes.post_path.requiredParams() // => ['id']
49
+ ```
50
+
51
+ ### ParameterMissing error rework
52
+
53
+ `ParameterMissing` is renamed to `ParametersMissing` error and now list all missing parameters instead of just first encountered in its message. Missing parameters are now available via `ParametersMissing#keys` property.
54
+
55
+ ``` javascript
56
+ try {
57
+ return Routes.inbox_path();
58
+ } catch(error) {
59
+ if (error.name === 'ParametersMissing') {
60
+ console.warn(`Missing route keys ${error.keys.join(', ')}. Ignoring.`);
61
+ return "#";
62
+ } else {
63
+ throw error;
64
+ }
65
+ }
66
+ ```
data/js-routes.gemspec CHANGED
@@ -7,13 +7,16 @@ Gem::Specification.new do |s|
7
7
  s.name = %q{js-routes}
8
8
  s.version = JsRoutes::VERSION
9
9
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ if s.respond_to? :required_rubygems_version=
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
12
+ end
11
13
  s.authors = ["Bogdan Gusiev"]
12
14
  s.description = %q{Generates javascript file that defines all Rails named routes as javascript helpers}
13
15
  s.email = %q{agresso@gmail.com}
14
16
  s.extra_rdoc_files = [
15
17
  "LICENSE.txt"
16
18
  ]
19
+ s.required_ruby_version = '>= 2.4.0'
17
20
  s.files = `git ls-files`.split("\n")
18
21
  s.homepage = %q{http://github.com/railsware/js-routes}
19
22
  s.licenses = ["MIT"]
@@ -21,16 +24,16 @@ Gem::Specification.new do |s|
21
24
  s.summary = %q{Brings Rails named routes to javascript}
22
25
 
23
26
  s.add_runtime_dependency(%q<railties>, [">= 4"])
24
- s.add_runtime_dependency(%q<sprockets-rails>)
25
- s.add_development_dependency(%q<rspec>, [">= 3.0.0"])
27
+ s.add_development_dependency(%q<sprockets-rails>)
28
+ s.add_development_dependency(%q<rspec>, [">= 3.10.0"])
26
29
  s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
27
- s.add_development_dependency(%q<coffee-script>, [">= 0"])
28
30
  s.add_development_dependency(%q<appraisal>, [">= 0.5.2"])
31
+ s.add_development_dependency(%q<bump>, [">= 0.10.0"])
29
32
  if defined?(JRUBY_VERSION)
30
33
  s.add_development_dependency(%q<therubyrhino>, [">= 2.0.4"])
31
34
  else
32
35
  s.add_development_dependency(%q<byebug>)
33
36
  s.add_development_dependency(%q<pry-byebug>)
34
- s.add_development_dependency(%q<mini_racer>, [">= 0.2.4"])
37
+ s.add_development_dependency(%q<mini_racer>, [">= 0.4.0"])
35
38
  end
36
39
  end
@@ -47,7 +47,6 @@ class Engine < ::Rails::Engine
47
47
  when -> (v) { v2.match?('', v) },
48
48
  -> (v) { vgte3.match?('', v) }
49
49
 
50
- # Other rails version, assumed newer
51
50
  Rails.application.config.assets.configure do |config|
52
51
  config.register_preprocessor(
53
52
  "application/javascript",
@@ -0,0 +1,33 @@
1
+ require "rails/generators"
2
+
3
+ class JsRoutes::Generators::Middleware < Rails::Generators::Base
4
+
5
+ source_root File.expand_path(__FILE__ + "/../../../templates")
6
+
7
+ def create_middleware
8
+ copy_file "initializer.rb", "config/initializers/js_routes.rb"
9
+ # copy_file "erb.js", "config/webpack/loaders/erb.js"
10
+ # copy_file "routes.js.erb", "app/javascript/routes.js.erb"
11
+ # inject_into_file "config/webpack/environment.js", loader_content
12
+ inject_into_file "app/javascript/packs/application.js", pack_content
13
+ inject_into_file "config/environments/development.rb", middleware_content, before: /^end\n\z/
14
+ end
15
+
16
+ protected
17
+
18
+ def pack_content
19
+ <<-JS
20
+ import * as Routes from '../routes';
21
+ window.Routes = Routes;
22
+ JS
23
+ end
24
+
25
+ def middleware_content
26
+ <<-RB
27
+
28
+ # Automatically update routes.js file
29
+ # when routes.rb is changed
30
+ config.middleware.use(JsRoutes::Middleware)
31
+ RB
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ require "rails/generators"
2
+
3
+ class JsRoutes::Generators::Webpacker < Rails::Generators::Base
4
+
5
+ source_root File.expand_path(__FILE__ + "/../../../templates")
6
+
7
+ def create_webpack
8
+ copy_file "initializer.rb", "config/initializers/js_routes.rb"
9
+ copy_file "erb.js", "config/webpack/loaders/erb.js"
10
+ copy_file "routes.js.erb", "app/javascript/routes.js.erb"
11
+ inject_into_file "config/webpack/environment.js", loader_content
12
+ inject_into_file "app/javascript/packs/application.js", pack_content
13
+ command = Rails.root.join("./bin/yarn add rails-erb-loader")
14
+ run command
15
+ end
16
+
17
+ protected
18
+
19
+ def pack_content
20
+ <<-JS
21
+ import * as Routes from 'routes.js.erb';
22
+ window.Routes = Routes;
23
+ JS
24
+ end
25
+
26
+ def loader_content
27
+ <<-JS
28
+ const erb = require('./loaders/erb')
29
+ environment.loaders.append('erb', erb)
30
+ JS
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ class JsRoutes
2
+ # A Rack middleware that automatically updates routes file
3
+ # whenever routes.rb is modified
4
+ #
5
+ # Inspired by
6
+ # https://github.com/fnando/i18n-js/blob/main/lib/i18n/js/middleware.rb
7
+ class Middleware
8
+ def initialize(app)
9
+ @app = app
10
+ @routes_file = Rails.root.join("config/routes.rb")
11
+ @mtime = nil
12
+ end
13
+
14
+ def call(env)
15
+ update_js_routes
16
+ @app.call(env)
17
+ end
18
+
19
+ protected
20
+
21
+ def update_js_routes
22
+ new_mtime = routes_mtime
23
+ unless new_mtime == @mtime
24
+ JsRoutes.generate!
25
+ JsRoutes.definitions!
26
+ @mtime = new_mtime
27
+ end
28
+ end
29
+
30
+ def routes_mtime
31
+ File.mtime(@routes_file)
32
+ rescue Errno::ENOENT
33
+ nil
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "1.4.9"
2
+ VERSION = "2.2.0"
3
3
  end