js-routes 2.2.2 → 2.2.3

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: 52db2ffdef9bea714377b9c00e152177b17e40de0fd8ef57ba09a84fee2f206e
4
- data.tar.gz: 8f899234c70859fee6a7c0486825c12ce501a01052e2b5d9a038f08544ac6df9
3
+ metadata.gz: b706c51040d12aaa69182dad9d25304ba947fe1e80cc5171775864a27d6f4ed8
4
+ data.tar.gz: 1a105e8a4dbc9cfde5a787a0714b6e0bf0758b6291f6945f0a16ca9c1e7a47a9
5
5
  SHA512:
6
- metadata.gz: e697c45b1908906c40cdc4591f5914dd415226de79a42a8e948f6095e0f09eb5edb5bfb0c5fd61953fde7121d9a025b3472b644ddb17ca2c13cb075c97311000
7
- data.tar.gz: 7f48edacee6659d4e4b5be0d71b5127f26e473119a8bf13a73c55a7b3c295dbee7fc34b9d3f515725c5871baad319ceb0bb32ed176900845b48a84c786ba3f9c
6
+ metadata.gz: f053d1f48cb8a4f9f7ac7f1e68d37f4816d09e42e0a91660fb84c1e9f4ad36d439019f6f402f1e2fce30467e6b0d5b799fd08bbe70bfc2aa8423c63f053f0bdb
7
+ data.tar.gz: bdcf72700542350da423e9cf62b4abcc0ba326400c4077c70c5f6174edb75a7e61b6b6a765a0454b4cd1a2fc5ddbf91f3a16294ce995d9ab31d9984153c29b4c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  ## master
2
2
 
3
+ ## v2.2.4
4
+
5
+ * Fix rails engine loading if sprockets is not in Gemfile. Fixes [#294](https://github.com/railsware/js-routes/issues/294)
6
+
7
+ ## v2.2.3
8
+
9
+ * Fixed NIL module type namespace defintion [#297](https://github.com/railsware/js-routes/issues/297).
10
+ * The patch may cause a problem with nested `namespace` option
11
+ * Ex. Value like `MyProject.Routes` requires to define `window.MyProject` before importing the routes file
12
+
3
13
  ## v2.2.2.
4
14
 
5
15
  * Fix custom file path [#295](https://github.com/railsware/js-routes/issues/295)
data/Readme.md CHANGED
@@ -20,6 +20,7 @@ There are several possible ways to setup JsRoutes:
20
20
 
21
21
  * [Quick and easy](#quick-start)
22
22
  * Uses Rack Middleware to automatically update routes locally
23
+ * Automatically generates routes files on `assets:precompile` in production
23
24
  * Works great for a simple Rails application
24
25
  * [Webpacker ERB Loader](#webpacker)
25
26
  * Requires ESM module system (the default)
@@ -29,32 +29,34 @@ end
29
29
 
30
30
 
31
31
  class Engine < ::Rails::Engine
32
- require 'sprockets/version'
33
- v2 = Gem::Dependency.new('', ' ~> 2')
34
- vgte3 = Gem::Dependency.new('', ' >= 3')
35
- sprockets_version = Gem::Version.new(::Sprockets::VERSION).release
36
- initializer_args = case sprockets_version
37
- when -> (v) { v2.match?('', v) }
38
- { after: "sprockets.environment" }
39
- when -> (v) { vgte3.match?('', v) }
40
- { after: :engines_blank_point, before: :finisher_hook }
41
- else
42
- raise StandardError, "Sprockets version #{sprockets_version} is not supported"
43
- end
44
-
45
- initializer 'js-routes.dependent_on_routes', initializer_args do
46
- case sprockets_version
47
- when -> (v) { v2.match?('', v) },
48
- -> (v) { vgte3.match?('', v) }
49
-
50
- Rails.application.config.assets.configure do |config|
51
- config.register_preprocessor(
52
- "application/javascript",
53
- SprocketsExtension,
54
- )
32
+ if defined?(::Sprockets::Railtie)
33
+ require 'sprockets/version'
34
+ v2 = Gem::Dependency.new('', ' ~> 2')
35
+ vgte3 = Gem::Dependency.new('', ' >= 3')
36
+ sprockets_version = Gem::Version.new(::Sprockets::VERSION).release
37
+ initializer_args = case sprockets_version
38
+ when -> (v) { v2.match?('', v) }
39
+ { after: "sprockets.environment" }
40
+ when -> (v) { vgte3.match?('', v) }
41
+ { after: :engines_blank_point, before: :finisher_hook }
42
+ else
43
+ raise StandardError, "Sprockets version #{sprockets_version} is not supported"
44
+ end
45
+
46
+ initializer 'js-routes.dependent_on_routes', initializer_args do
47
+ case sprockets_version
48
+ when -> (v) { v2.match?('', v) },
49
+ -> (v) { vgte3.match?('', v) }
50
+
51
+ Rails.application.config.assets.configure do |config|
52
+ config.register_preprocessor(
53
+ "application/javascript",
54
+ SprocketsExtension,
55
+ )
56
+ end
57
+ else
58
+ raise StandardError, "Sprockets version #{sprockets_version} is not supported"
55
59
  end
56
- else
57
- raise StandardError, "Sprockets version #{sprockets_version} is not supported"
58
60
  end
59
61
  end
60
62
  end
@@ -57,16 +57,35 @@ module JsRoutes
57
57
  'DEPRECATED_GLOBBING_BEHAVIOR' => ActionPack::VERSION::MAJOR == 4 && ActionPack::VERSION::MINOR == 0,
58
58
 
59
59
  'APP_CLASS' => application.class.to_s,
60
- 'NAMESPACE' => json(@configuration.namespace),
61
60
  'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
62
61
  'PREFIX' => json(@configuration.prefix),
63
62
  'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
64
63
  'SERIALIZER' => @configuration.serializer || json(nil),
65
64
  'MODULE_TYPE' => json(@configuration.module_type),
66
- 'WRAPPER' => @configuration.esm? ? 'const __jsr = ' : '',
65
+ 'WRAPPER' => wrapper_variable,
67
66
  }
68
67
  end
69
68
 
69
+ def wrapper_variable
70
+ case @configuration.module_type
71
+ when 'ESM'
72
+ 'const __jsr = '
73
+ when 'NIL'
74
+ namespace = @configuration.namespace
75
+ if namespace
76
+ if namespace.include?('.')
77
+ "#{namespace} = "
78
+ else
79
+ "(typeof window !== 'undefined' ? window : this).#{namespace} = "
80
+ end
81
+ else
82
+ ''
83
+ end
84
+ else
85
+ ''
86
+ end
87
+ end
88
+
70
89
  def application
71
90
  @configuration.application
72
91
  end
@@ -1,3 +1,3 @@
1
1
  module JsRoutes
2
- VERSION = "2.2.2"
2
+ VERSION = "2.2.3"
3
3
  end
data/lib/js_routes.rb CHANGED
@@ -1,4 +1,4 @@
1
- if defined?(::Rails) && defined?(::Sprockets::Railtie)
1
+ if defined?(::Rails)
2
2
  require 'js_routes/engine'
3
3
  end
4
4
  require 'js_routes/version'
data/lib/routes.d.ts CHANGED
@@ -66,7 +66,6 @@ declare const RubyVariables: {
66
66
  SPECIAL_OPTIONS_KEY: string;
67
67
  DEFAULT_URL_OPTIONS: RouteParameters;
68
68
  SERIALIZER: Serializer;
69
- NAMESPACE: string;
70
69
  ROUTES_OBJECT: RouteHelpers;
71
70
  MODULE_TYPE: ModuleType;
72
71
  WRAPPER: <T>(callback: T) => T;
data/lib/routes.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * File generated by js-routes RubyVariables.GEM_VERSION
3
3
  * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
4
  */
5
- RubyVariables.WRAPPER((that) => {
5
+ RubyVariables.WRAPPER(() => {
6
6
  const hasProp = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
7
7
  let NodeTypes;
8
8
  (function (NodeTypes) {
@@ -15,8 +15,7 @@ RubyVariables.WRAPPER((that) => {
15
15
  NodeTypes[NodeTypes["SLASH"] = 7] = "SLASH";
16
16
  NodeTypes[NodeTypes["DOT"] = 8] = "DOT";
17
17
  })(NodeTypes || (NodeTypes = {}));
18
- const Root = that;
19
- const isBroswer = typeof window !== "undefined";
18
+ const isBrowser = typeof window !== "undefined";
20
19
  const ModuleReferences = {
21
20
  CJS: {
22
21
  define(routes) {
@@ -70,11 +69,11 @@ RubyVariables.WRAPPER((that) => {
70
69
  },
71
70
  },
72
71
  NIL: {
73
- define(routes) {
74
- Utils.namespace(Root, RubyVariables.NAMESPACE, routes);
72
+ define() {
73
+ // Defined using RubyVariables.WRAPPER
75
74
  },
76
75
  isSupported() {
77
- return !RubyVariables.NAMESPACE || !!Root;
76
+ return true;
78
77
  },
79
78
  },
80
79
  DTS: {
@@ -422,15 +421,15 @@ RubyVariables.WRAPPER((that) => {
422
421
  }
423
422
  current_host() {
424
423
  var _a;
425
- return (isBroswer && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.hostname)) || "";
424
+ return (isBrowser && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.hostname)) || "";
426
425
  }
427
426
  current_protocol() {
428
427
  var _a, _b;
429
- return ((isBroswer && ((_b = (_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.protocol) === null || _b === void 0 ? void 0 : _b.replace(/:$/, ""))) || "http");
428
+ return ((isBrowser && ((_b = (_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.protocol) === null || _b === void 0 ? void 0 : _b.replace(/:$/, ""))) || "http");
430
429
  }
431
430
  current_port() {
432
431
  var _a;
433
- return (isBroswer && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.port)) || "";
432
+ return (isBrowser && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.port)) || "";
434
433
  }
435
434
  is_object(value) {
436
435
  return (typeof value === "object" &&
@@ -445,21 +444,6 @@ RubyVariables.WRAPPER((that) => {
445
444
  is_reserved_option(key) {
446
445
  return ReservedOptions.includes(key);
447
446
  }
448
- namespace(object, namespace, routes) {
449
- const parts = (namespace === null || namespace === void 0 ? void 0 : namespace.split(".")) || [];
450
- if (parts.length === 0) {
451
- return;
452
- }
453
- for (let index = 0; index < parts.length; index++) {
454
- const part = parts[index];
455
- if (index < parts.length - 1) {
456
- object = object[part] || (object[part] = {});
457
- }
458
- else {
459
- object[part] = routes;
460
- }
461
- }
462
- }
463
447
  configure(new_config) {
464
448
  this.configuration = { ...this.configuration, ...new_config };
465
449
  return this.configuration;
@@ -502,4 +486,4 @@ RubyVariables.WRAPPER((that) => {
502
486
  };
503
487
  Utils.define_module(RubyVariables.MODULE_TYPE, result);
504
488
  return result;
505
- })(this);
489
+ })();
data/lib/routes.ts CHANGED
@@ -84,7 +84,6 @@ declare const RubyVariables: {
84
84
  SPECIAL_OPTIONS_KEY: string;
85
85
  DEFAULT_URL_OPTIONS: RouteParameters;
86
86
  SERIALIZER: Serializer;
87
- NAMESPACE: string;
88
87
  ROUTES_OBJECT: RouteHelpers;
89
88
  MODULE_TYPE: ModuleType;
90
89
  WRAPPER: <T>(callback: T) => T;
@@ -97,7 +96,7 @@ declare const define:
97
96
  declare const module: { exports: any } | undefined;
98
97
 
99
98
  RubyVariables.WRAPPER(
100
- (that: unknown): RouterExposedMethods => {
99
+ (): RouterExposedMethods => {
101
100
  const hasProp = (value: unknown, key: string) =>
102
101
  Object.prototype.hasOwnProperty.call(value, key);
103
102
  enum NodeTypes {
@@ -128,8 +127,7 @@ RubyVariables.WRAPPER(
128
127
  [T in keyof RouteNodes]: RouteNode<T>;
129
128
  }[keyof RouteNodes];
130
129
 
131
- const Root = that;
132
- const isBroswer = typeof window !== "undefined";
130
+ const isBrowser = typeof window !== "undefined";
133
131
  type ModuleDefinition = {
134
132
  define: (routes: RouterExposedMethods) => void;
135
133
  isSupported: () => boolean;
@@ -186,11 +184,11 @@ RubyVariables.WRAPPER(
186
184
  },
187
185
  },
188
186
  NIL: {
189
- define(routes) {
190
- Utils.namespace(Root, RubyVariables.NAMESPACE, routes);
187
+ define() {
188
+ // Defined using RubyVariables.WRAPPER
191
189
  },
192
190
  isSupported() {
193
- return !RubyVariables.NAMESPACE || !!Root;
191
+ return true;
194
192
  },
195
193
  },
196
194
  DTS: {
@@ -628,17 +626,17 @@ RubyVariables.WRAPPER(
628
626
  }
629
627
 
630
628
  current_host(): string {
631
- return (isBroswer && window?.location?.hostname) || "";
629
+ return (isBrowser && window?.location?.hostname) || "";
632
630
  }
633
631
 
634
632
  current_protocol(): string {
635
633
  return (
636
- (isBroswer && window?.location?.protocol?.replace(/:$/, "")) || "http"
634
+ (isBrowser && window?.location?.protocol?.replace(/:$/, "")) || "http"
637
635
  );
638
636
  }
639
637
 
640
638
  current_port(): string {
641
- return (isBroswer && window?.location?.port) || "";
639
+ return (isBrowser && window?.location?.port) || "";
642
640
  }
643
641
 
644
642
  is_object(value: unknown): value is Record<string, unknown> {
@@ -660,25 +658,6 @@ RubyVariables.WRAPPER(
660
658
  return ReservedOptions.includes(key as any);
661
659
  }
662
660
 
663
- namespace(
664
- object: any,
665
- namespace: string | null | undefined,
666
- routes: unknown
667
- ): void {
668
- const parts = namespace?.split(".") || [];
669
- if (parts.length === 0) {
670
- return;
671
- }
672
- for (let index = 0; index < parts.length; index++) {
673
- const part = parts[index];
674
- if (index < parts.length - 1) {
675
- object = object[part] || (object[part] = {});
676
- } else {
677
- object[part] = routes;
678
- }
679
- }
680
- }
681
-
682
661
  configure(new_config: Partial<Configuration>): Configuration {
683
662
  this.configuration = { ...this.configuration, ...new_config };
684
663
  return this.configuration;
@@ -734,4 +713,4 @@ RubyVariables.WRAPPER(
734
713
  Utils.define_module(RubyVariables.MODULE_TYPE, result);
735
714
  return result;
736
715
  }
737
- )(this);
716
+ )();
@@ -66,7 +66,6 @@ declare const RubyVariables: {
66
66
  SPECIAL_OPTIONS_KEY: string;
67
67
  DEFAULT_URL_OPTIONS: RouteParameters;
68
68
  SERIALIZER: Serializer;
69
- NAMESPACE: string;
70
69
  ROUTES_OBJECT: RouteHelpers;
71
70
  MODULE_TYPE: ModuleType;
72
71
  WRAPPER: <T>(callback: T) => T;
@@ -45,6 +45,7 @@ describe JsRoutes, "compatibility with NIL (legacy browser)" do
45
45
  let(:_presetup) { "" }
46
46
  before do
47
47
  evaljs("var window = this;")
48
+ evaljs("window.PHM = {}")
48
49
  evaljs(_presetup)
49
50
  evaljs(generated_js)
50
51
  end
@@ -399,13 +399,16 @@ describe JsRoutes, "options" do
399
399
  end
400
400
 
401
401
  let(:_presetup) do
402
- window = {location: {
402
+ location = {
403
403
  protocol: current_protocol,
404
404
  hostname: current_hostname,
405
405
  port: current_port,
406
406
  host: current_host,
407
- }}
408
- "const window = #{ActiveSupport::JSON.encode(window)}"
407
+ }
408
+ [
409
+ "const window = this;",
410
+ "window.location = #{ActiveSupport::JSON.encode(location)};",
411
+ ].join("\n")
409
412
  end
410
413
 
411
414
  context "without specifying a default host" do
@@ -2,8 +2,11 @@ require "spec_helper"
2
2
 
3
3
  describe JsRoutes, "compatibility with Rails" do
4
4
 
5
+ let(:generated_js) do
6
+ JsRoutes.generate({module_type: nil, namespace: 'Routes'})
7
+ end
5
8
  before(:each) do
6
- evaljs(JsRoutes.generate({module_type: nil, namespace: 'Routes'}))
9
+ evaljs(generated_js)
7
10
  end
8
11
 
9
12
  it "should generate collection routing" do
data/spec/spec_helper.rb CHANGED
@@ -42,10 +42,14 @@ end
42
42
  def evaljs(string, force: false, filename: 'context.js')
43
43
  jscontext(force).eval(string, filename: filename)
44
44
  rescue MiniRacer::ParseError => e
45
- message = e.message
46
- _, _, line, _ = message.split(':')
47
- code = line && string.split("\n")[line.to_i-1]
48
- raise "#{message}. Code: #{code.strip}";
45
+ trace = e.message
46
+ _, _, line, _ = trace.split(':')
47
+ if line
48
+ code = string.split("\n")[line.to_i-1]
49
+ raise "#{trace}. Code: #{code.strip}";
50
+ else
51
+ raise e
52
+ end
49
53
  rescue MiniRacer::RuntimeError => e
50
54
  raise e
51
55
  end
@@ -62,6 +66,10 @@ def planner_routes
62
66
  Planner::Engine.routes.url_helpers
63
67
  end
64
68
 
69
+ def log(string)
70
+ evaljs("console.log(#{string})")
71
+ end
72
+
65
73
  ActiveSupport::Inflector.inflections do |inflect|
66
74
  inflect.irregular "budgie", "budgies"
67
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-01 00:00:00.000000000 Z
11
+ date: 2022-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties