js-routes 2.0.7 → 2.0.8

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: 4c4859fef1627aff464cbfd2c4cede3be5890c402d6bcb20afe1617059996d85
4
- data.tar.gz: 04135b7c60e37f964f7f3904f04714fe76e6297560c7526b9ff5d3dd6069aa1c
3
+ metadata.gz: 4f78bfaf0cba479c48245d0606cc10b262481e854f3fc82e7195a8b58548bf7a
4
+ data.tar.gz: 118cafad50e73568ba4b1758821af072194e9570178f1f9caf067785e9f5a0f5
5
5
  SHA512:
6
- metadata.gz: 77a1e9c70ee04c5e01a64cd910ea724f68f5eaec46977bb2d2f3bb2677482688ca11a6efb32ba54e397f7f6267aa1eccc3797d86f9ce629a4c988731ba48bbaf
7
- data.tar.gz: cb8166142e036e1232f517c25e6a3d515f8ecc75c87aa356562e3db1199a9962d51f64f21932a502de75c0b0fba94202efb0f40574f6c16b8fb6910a6a4c3577
6
+ metadata.gz: 95490b886a00861b0fe4a5e2419c67ec804bbb94a916edefe333acd2e0c94b407137952b97e4bf70d928ad1ebc5cf47333b5b5083f0dc227a43be2d3ebba7648
7
+ data.tar.gz: 666d2862d8a86e760176afdfdd62711a34202a4725d08609e5f5bf69732becb8829826ddeccaca0f8cacffa6a78022e69a05872ea26ce550e7352a9bfde0d0e4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## master
2
2
 
3
+ ## v2.0.8
4
+
5
+ * Forbid usage of `namespace` option if `module_type` is not `nil`. [#281](https://github.com/railsware/js-routes/issues/281).
6
+
3
7
  ## v2.0.7
4
8
 
5
9
  * Remove source map annotation from JS file. Fixes [#277](https://github.com/railsware/js-routes/issues/277)
data/Readme.md CHANGED
@@ -159,6 +159,7 @@ Options to configure JavaScript file generator. These options are only available
159
159
  * Default: `[]`
160
160
  * The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
161
161
  * `namespace` - global object used to access routes.
162
+ * Only available if `module_type` option is set to `nil`.
162
163
  * Supports nested namespace like `MyProject.routes`
163
164
  * Default: `nil`
164
165
  * `camel_case` - specifies if route helpers should be generated in camel case instead of underscore case.
@@ -314,6 +315,9 @@ import {
314
315
  } from 'routes.js.erb'
315
316
  ```
316
317
 
318
+ Such import structure allows for moddern JS bundlers like [Webpack](https://webpack.js.org/) to only include explicitly imported routes into JS bundle file.
319
+ See [Tree Shaking](https://webpack.js.org/guides/tree-shaking/) for more information.
320
+
317
321
  ### Exclude option
318
322
 
319
323
  Split your routes into multiple files related to each section of your website like:
@@ -325,18 +329,6 @@ Split your routes into multiple files related to each section of your website li
325
329
  <%= JsRoutes.generate(exclude: /^admin_/)
326
330
  ```
327
331
 
328
- ## JsRoutes and Heroku
329
-
330
- When using this setup on Heroku, it is impossible to use the asset pipeline. You should use the "Very Advanced Setup" schema in this case.
331
-
332
- For example create routes.js.erb in assets folder with needed content:
333
-
334
- ``` erb
335
- <%= JsRoutes.generate(options) %>
336
- ```
337
-
338
- This should just work.
339
-
340
332
  ## Advantages over alternatives
341
333
 
342
334
  There are some alternatives available. Most of them has only basic feature and don't reach the level of quality I accept.
data/VERSION_2_UPGRADE.md CHANGED
@@ -2,28 +2,40 @@
2
2
 
3
3
  ### Using ESM module by default
4
4
 
5
- The default setting are now changed:
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.
6
7
 
7
- Setting | Old | New
8
- --- | --- | ---
9
- module\_type | nil | ESM
10
- namespace | Routes | nil
11
- documentation | false | true
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:
12
10
 
13
- This is more optimized setup for WebPacker.
14
- New version of js-routes generates function comment in the [JSDoc](https://jsdoc.app) format.
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)
15
18
 
16
- You can restore the old configuration like this:
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:
17
20
 
18
21
  ``` ruby
19
22
  JsRoutes.setup do |config|
20
23
  config.module_type = nil
21
- config.namespace = 'Routes'
22
- config.documentation = false
24
+ config.namespace = "Routes"
23
25
  end
24
26
  ```
25
27
 
26
- However, [ESM+Webpacker](/Readme.md#webpacker) upgrade is recommended.
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
+ ```
27
39
 
28
40
  ### `required_params` renamed
29
41
 
data/lib/js_routes.rb CHANGED
@@ -60,6 +60,7 @@ class JsRoutes
60
60
  value = value.call if value.is_a?(Proc)
61
61
  send(:"#{attribute}=", value)
62
62
  end
63
+ normalize_and_verify
63
64
  self
64
65
  end
65
66
 
@@ -76,7 +77,24 @@ class JsRoutes
76
77
  end
77
78
 
78
79
  def esm?
79
- self.module_type === 'ESM'
80
+ module_type === 'ESM'
81
+ end
82
+
83
+ def normalize_and_verify
84
+ normalize
85
+ verify
86
+ end
87
+
88
+ protected
89
+
90
+ def normalize
91
+ self.module_type = module_type&.upcase || 'NIL'
92
+ end
93
+
94
+ def verify
95
+ if module_type != 'NIL' && namespace
96
+ raise "JsRoutes namespace option can only be used if module_type is nil"
97
+ end
80
98
  end
81
99
  end
82
100
 
@@ -87,6 +105,7 @@ class JsRoutes
87
105
  class << self
88
106
  def setup(&block)
89
107
  configuration.tap(&block) if block
108
+ configuration.normalize_and_verify
90
109
  end
91
110
 
92
111
  def configuration
@@ -248,12 +267,14 @@ class JsRoutes
248
267
  end
249
268
  end
250
269
 
270
+ protected
271
+
251
272
  def body(absolute)
252
- "__jsr.r(#{arguments(absolute).join(', ')})"
273
+ "__jsr.r(#{arguments(absolute).map{|a| json(a)}.join(', ')})"
253
274
  end
254
275
 
255
276
  def arguments(absolute)
256
- absolute ? base_arguments + [json(true)] : base_arguments
277
+ absolute ? base_arguments + [true] : base_arguments
257
278
  end
258
279
 
259
280
  def match_configuration?
@@ -298,10 +319,11 @@ JS
298
319
  route.required_parts
299
320
  end
300
321
 
301
- protected
302
-
303
322
  def base_arguments
304
- return @base_arguments if defined?(@base_arguments)
323
+ @base_arguments ||= [parts_table, serialize(spec, parent_spec)]
324
+ end
325
+
326
+ def parts_table
305
327
  parts_table = {}
306
328
  route.parts.each do |part, hash|
307
329
  parts_table[part] ||= {}
@@ -318,11 +340,7 @@ JS
318
340
  parts_table[part][:d] = value
319
341
  end
320
342
  end
321
- @base_arguments = [
322
- parts_table, serialize(spec, parent_spec)
323
- ].map do |argument|
324
- json(argument)
325
- end
343
+ parts_table
326
344
  end
327
345
 
328
346
  def documentation_params
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "2.0.7"
2
+ VERSION = "2.0.8"
3
3
  end
data/lib/routes.d.ts CHANGED
@@ -37,7 +37,7 @@ declare type PartsTable = Record<string, {
37
37
  r?: boolean;
38
38
  d?: unknown;
39
39
  }>;
40
- declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM";
40
+ declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "NIL";
41
41
  declare const RubyVariables: {
42
42
  PREFIX: string;
43
43
  DEPRECATED_GLOBBING_BEHAVIOR: boolean;
@@ -46,7 +46,7 @@ declare const RubyVariables: {
46
46
  SERIALIZER: Serializer;
47
47
  NAMESPACE: string;
48
48
  ROUTES_OBJECT: RouteHelpers;
49
- MODULE_TYPE: ModuleType | null;
49
+ MODULE_TYPE: ModuleType;
50
50
  WRAPPER: <T>(callback: T) => T;
51
51
  };
52
52
  declare const define: undefined | (((arg: unknown[], callback: () => unknown) => void) & {
data/lib/routes.js CHANGED
@@ -18,25 +18,32 @@ RubyVariables.WRAPPER((that) => {
18
18
  const Root = that;
19
19
  const ModuleReferences = {
20
20
  CJS: {
21
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
22
- define: (routes) => (module.exports = routes),
23
- support: () => typeof module === "object",
21
+ define(routes) {
22
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
23
+ module.exports = routes;
24
+ },
25
+ isSupported() {
26
+ return typeof module === "object";
27
+ },
24
28
  },
25
29
  AMD: {
26
- define: (routes) =>
27
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
28
- define([], function () {
29
- return routes;
30
- }),
31
- support: () => typeof define === "function" && !!define.amd,
30
+ define(routes) {
31
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32
+ define([], function () {
33
+ return routes;
34
+ });
35
+ },
36
+ isSupported() {
37
+ return typeof define === "function" && !!define.amd;
38
+ },
32
39
  },
33
40
  UMD: {
34
- define: (routes) => {
35
- if (ModuleReferences.AMD.support()) {
41
+ define(routes) {
42
+ if (ModuleReferences.AMD.isSupported()) {
36
43
  ModuleReferences.AMD.define(routes);
37
44
  }
38
45
  else {
39
- if (ModuleReferences.CJS.support()) {
46
+ if (ModuleReferences.CJS.isSupported()) {
40
47
  try {
41
48
  ModuleReferences.CJS.define(routes);
42
49
  }
@@ -47,11 +54,27 @@ RubyVariables.WRAPPER((that) => {
47
54
  }
48
55
  }
49
56
  },
50
- support: () => ModuleReferences.AMD.support() || ModuleReferences.CJS.support(),
57
+ isSupported() {
58
+ return (ModuleReferences.AMD.isSupported() ||
59
+ ModuleReferences.CJS.isSupported());
60
+ },
51
61
  },
52
62
  ESM: {
53
- define: () => null,
54
- support: () => true,
63
+ define() {
64
+ // Module can only be defined using ruby code generation
65
+ },
66
+ isSupported() {
67
+ // Its impossible to check if "export" keyword is supported
68
+ return true;
69
+ },
70
+ },
71
+ NIL: {
72
+ define(routes) {
73
+ Utils.namespace(Root, RubyVariables.NAMESPACE, routes);
74
+ },
75
+ isSupported() {
76
+ return !!Root;
77
+ },
55
78
  },
56
79
  };
57
80
  class ParametersMissing extends Error {
@@ -62,7 +85,6 @@ RubyVariables.WRAPPER((that) => {
62
85
  this.name = ParametersMissing.name;
63
86
  }
64
87
  }
65
- const DeprecatedGlobbingBehavior = RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR;
66
88
  const UriEncoderSegmentRegex = /[^a-zA-Z0-9\-._~!$&'()*+,;=:@]/g;
67
89
  const ReservedOptions = [
68
90
  "anchor",
@@ -342,7 +364,9 @@ RubyVariables.WRAPPER((that) => {
342
364
  value = value.join("/");
343
365
  }
344
366
  const result = this.path_identifier(value);
345
- return DeprecatedGlobbingBehavior ? result : encodeURI(result);
367
+ return RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR
368
+ ? result
369
+ : encodeURI(result);
346
370
  }
347
371
  get_prefix() {
348
372
  const prefix = this.configuration.prefix;
@@ -449,7 +473,7 @@ RubyVariables.WRAPPER((that) => {
449
473
  return { ...this.configuration };
450
474
  }
451
475
  is_module_supported(name) {
452
- return ModuleReferences[name].support();
476
+ return ModuleReferences[name].isSupported();
453
477
  }
454
478
  ensure_module_supported(name) {
455
479
  if (!this.is_module_supported(name)) {
@@ -457,9 +481,6 @@ RubyVariables.WRAPPER((that) => {
457
481
  }
458
482
  }
459
483
  define_module(name, module) {
460
- if (!name) {
461
- return;
462
- }
463
484
  this.ensure_module_supported(name);
464
485
  ModuleReferences[name].define(module);
465
486
  }
@@ -484,9 +505,6 @@ RubyVariables.WRAPPER((that) => {
484
505
  },
485
506
  ...RubyVariables.ROUTES_OBJECT,
486
507
  };
487
- Utils.namespace(Root, RubyVariables.NAMESPACE, result);
488
- if (RubyVariables.MODULE_TYPE) {
489
- Utils.define_module(RubyVariables.MODULE_TYPE, result);
490
- }
508
+ Utils.define_module(RubyVariables.MODULE_TYPE, result);
491
509
  return result;
492
510
  })(this);
data/lib/routes.ts CHANGED
@@ -39,7 +39,7 @@ type KeywordUrlOptions = Optional<{
39
39
 
40
40
  type PartsTable = Record<string, { r?: boolean; d?: unknown }>;
41
41
 
42
- type ModuleType = "CJS" | "AMD" | "UMD" | "ESM";
42
+ type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "NIL";
43
43
 
44
44
  declare const RubyVariables: {
45
45
  PREFIX: string;
@@ -49,7 +49,7 @@ declare const RubyVariables: {
49
49
  SERIALIZER: Serializer;
50
50
  NAMESPACE: string;
51
51
  ROUTES_OBJECT: RouteHelpers;
52
- MODULE_TYPE: ModuleType | null;
52
+ MODULE_TYPE: ModuleType;
53
53
  WRAPPER: <T>(callback: T) => T;
54
54
  };
55
55
 
@@ -94,28 +94,35 @@ RubyVariables.WRAPPER(
94
94
  const Root = that;
95
95
  type ModuleDefinition = {
96
96
  define: (routes: unknown) => void;
97
- support: () => boolean;
97
+ isSupported: () => boolean;
98
98
  };
99
99
  const ModuleReferences: Record<ModuleType, ModuleDefinition> = {
100
100
  CJS: {
101
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
102
- define: (routes) => (module!.exports = routes),
103
- support: () => typeof module === "object",
101
+ define(routes) {
102
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
103
+ module!.exports = routes;
104
+ },
105
+ isSupported() {
106
+ return typeof module === "object";
107
+ },
104
108
  },
105
109
  AMD: {
106
- define: (routes) =>
110
+ define(routes) {
107
111
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
108
112
  define!([], function () {
109
113
  return routes;
110
- }),
111
- support: () => typeof define === "function" && !!define.amd,
114
+ });
115
+ },
116
+ isSupported() {
117
+ return typeof define === "function" && !!define.amd;
118
+ },
112
119
  },
113
120
  UMD: {
114
- define: (routes) => {
115
- if (ModuleReferences.AMD.support()) {
121
+ define(routes) {
122
+ if (ModuleReferences.AMD.isSupported()) {
116
123
  ModuleReferences.AMD.define(routes);
117
124
  } else {
118
- if (ModuleReferences.CJS.support()) {
125
+ if (ModuleReferences.CJS.isSupported()) {
119
126
  try {
120
127
  ModuleReferences.CJS.define(routes);
121
128
  } catch (error) {
@@ -124,12 +131,29 @@ RubyVariables.WRAPPER(
124
131
  }
125
132
  }
126
133
  },
127
- support: () =>
128
- ModuleReferences.AMD.support() || ModuleReferences.CJS.support(),
134
+ isSupported() {
135
+ return (
136
+ ModuleReferences.AMD.isSupported() ||
137
+ ModuleReferences.CJS.isSupported()
138
+ );
139
+ },
129
140
  },
130
141
  ESM: {
131
- define: () => null,
132
- support: () => true,
142
+ define() {
143
+ // Module can only be defined using ruby code generation
144
+ },
145
+ isSupported() {
146
+ // Its impossible to check if "export" keyword is supported
147
+ return true;
148
+ },
149
+ },
150
+ NIL: {
151
+ define(routes) {
152
+ Utils.namespace(Root, RubyVariables.NAMESPACE, routes);
153
+ },
154
+ isSupported() {
155
+ return !!Root;
156
+ },
133
157
  },
134
158
  };
135
159
 
@@ -143,9 +167,6 @@ RubyVariables.WRAPPER(
143
167
  }
144
168
  }
145
169
 
146
- const DeprecatedGlobbingBehavior =
147
- RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR;
148
-
149
170
  const UriEncoderSegmentRegex = /[^a-zA-Z0-9\-._~!$&'()*+,;=:@]/g;
150
171
 
151
172
  const ReservedOptions = [
@@ -498,7 +519,9 @@ RubyVariables.WRAPPER(
498
519
  value = value.join("/");
499
520
  }
500
521
  const result = this.path_identifier(value as any);
501
- return DeprecatedGlobbingBehavior ? result : encodeURI(result);
522
+ return RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR
523
+ ? result
524
+ : encodeURI(result);
502
525
  }
503
526
 
504
527
  get_prefix(): string {
@@ -635,7 +658,7 @@ RubyVariables.WRAPPER(
635
658
  }
636
659
 
637
660
  is_module_supported(name: ModuleType): boolean {
638
- return ModuleReferences[name].support();
661
+ return ModuleReferences[name].isSupported();
639
662
  }
640
663
 
641
664
  ensure_module_supported(name: ModuleType): void {
@@ -644,13 +667,7 @@ RubyVariables.WRAPPER(
644
667
  }
645
668
  }
646
669
 
647
- define_module(
648
- name: ModuleType | null,
649
- module: RouterExposedMethods
650
- ): void {
651
- if (!name) {
652
- return;
653
- }
670
+ define_module(name: ModuleType, module: RouterExposedMethods): void {
654
671
  this.ensure_module_supported(name);
655
672
  ModuleReferences[name].define(module);
656
673
  }
@@ -683,11 +700,7 @@ RubyVariables.WRAPPER(
683
700
  ...RubyVariables.ROUTES_OBJECT,
684
701
  };
685
702
 
686
- Utils.namespace(Root, RubyVariables.NAMESPACE, result);
687
-
688
- if (RubyVariables.MODULE_TYPE) {
689
- Utils.define_module(RubyVariables.MODULE_TYPE, result);
690
- }
703
+ Utils.define_module(RubyVariables.MODULE_TYPE, result);
691
704
  return result;
692
705
  }
693
706
  )(this);
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.0.7
4
+ version: 2.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-09 00:00:00.000000000 Z
11
+ date: 2021-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties