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 +4 -4
- data/CHANGELOG.md +10 -0
- data/Readme.md +1 -0
- data/lib/js_routes/engine.rb +27 -25
- data/lib/js_routes/instance.rb +21 -2
- data/lib/js_routes/version.rb +1 -1
- data/lib/js_routes.rb +1 -1
- data/lib/routes.d.ts +0 -1
- data/lib/routes.js +9 -25
- data/lib/routes.ts +9 -30
- data/spec/js_routes/module_types/dts/routes.spec.d.ts +0 -1
- data/spec/js_routes/module_types/nil_spec.rb +1 -0
- data/spec/js_routes/options_spec.rb +6 -3
- data/spec/js_routes/rails_routes_compatibility_spec.rb +4 -1
- data/spec/spec_helper.rb +12 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b706c51040d12aaa69182dad9d25304ba947fe1e80cc5171775864a27d6f4ed8
|
4
|
+
data.tar.gz: 1a105e8a4dbc9cfde5a787a0714b6e0bf0758b6291f6945f0a16ca9c1e7a47a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
data/lib/js_routes/engine.rb
CHANGED
@@ -29,32 +29,34 @@ end
|
|
29
29
|
|
30
30
|
|
31
31
|
class Engine < ::Rails::Engine
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
{
|
39
|
-
|
40
|
-
{
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
config.
|
52
|
-
|
53
|
-
|
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
|
data/lib/js_routes/instance.rb
CHANGED
@@ -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' =>
|
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
|
data/lib/js_routes/version.rb
CHANGED
data/lib/js_routes.rb
CHANGED
data/lib/routes.d.ts
CHANGED
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((
|
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
|
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(
|
74
|
-
|
72
|
+
define() {
|
73
|
+
// Defined using RubyVariables.WRAPPER
|
75
74
|
},
|
76
75
|
isSupported() {
|
77
|
-
return
|
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 (
|
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 ((
|
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 (
|
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
|
-
})(
|
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
|
-
(
|
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
|
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(
|
190
|
-
|
187
|
+
define() {
|
188
|
+
// Defined using RubyVariables.WRAPPER
|
191
189
|
},
|
192
190
|
isSupported() {
|
193
|
-
return
|
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 (
|
629
|
+
return (isBrowser && window?.location?.hostname) || "";
|
632
630
|
}
|
633
631
|
|
634
632
|
current_protocol(): string {
|
635
633
|
return (
|
636
|
-
(
|
634
|
+
(isBrowser && window?.location?.protocol?.replace(/:$/, "")) || "http"
|
637
635
|
);
|
638
636
|
}
|
639
637
|
|
640
638
|
current_port(): string {
|
641
|
-
return (
|
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
|
-
)(
|
716
|
+
)();
|
@@ -399,13 +399,16 @@ describe JsRoutes, "options" do
|
|
399
399
|
end
|
400
400
|
|
401
401
|
let(:_presetup) do
|
402
|
-
|
402
|
+
location = {
|
403
403
|
protocol: current_protocol,
|
404
404
|
hostname: current_hostname,
|
405
405
|
port: current_port,
|
406
406
|
host: current_host,
|
407
|
-
}
|
408
|
-
|
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(
|
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
|
-
|
46
|
-
_, _, line, _ =
|
47
|
-
|
48
|
-
|
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.
|
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-
|
11
|
+
date: 2022-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|