js-routes 2.2.6 → 2.2.7

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: 55d34a6a511e250d1fc59aa2e868a3148734bc6fafc6f156ff86eea1f0d6f81e
4
- data.tar.gz: 94143965ae4a2a14db535dd484521c3348c18eee2ca6f61ee72186e908cdc786
3
+ metadata.gz: cc558c858345b705d2ca048eaab529acf4d4ffe20299e152f1c23fcbeb31388e
4
+ data.tar.gz: 70dc5ab9c6f629d3236d45a5cf202c6bb8f00893041857f137b9569a3467d48c
5
5
  SHA512:
6
- metadata.gz: d727f254cec5e5269e92ef1180309aacb3362fc4aa8065c2fb604237d9096918c1e0ab534b7b443df656fb5218962e949522ce3338b4be9d99e17d0268f6efbb
7
- data.tar.gz: 9ce12dc8aefab9a646aadc2318e541f6b422fc9340856e69488889374727c7de7f7e70fa8fbe510263b9d7f5edf2097657ae62fac0a90d0206b9b2be5481b48a
6
+ metadata.gz: 0536cff5b25734ae0c0399cba8d65fb48503e7038c6e5f7396acb69497505ab8849b109e3348bc2c7a010b073cb3cd4aebed1798c538e8bd0e0270f8949dd53c
7
+ data.tar.gz: 99e9d0487c9062cf3789a8018beba7bcc0b9f985251921cb6f042c20c571d74fab8a4c79f82ad0a6530dde42ee8a678fb5dbfae184dd64e8756ef47d06805b0b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## master
2
2
 
3
+ ## v2.2.7
4
+
5
+ * Fix ESM Tree Shaking [#306](https://github.com/railsware/js-routes/issues/306)
6
+
3
7
  ## v2.2.6
4
8
 
5
9
  * Prefer to extend `javascript:build` instead of `assets:precompile`. [#305](https://github.com/railsware/js-routes/issues/305)
data/Readme.md CHANGED
@@ -18,7 +18,7 @@ There are several possible ways to setup JsRoutes:
18
18
 
19
19
  * [Quick and easy](#quick-start)
20
20
  * Uses Rack Middleware to automatically update routes locally
21
- * Automatically generates routes files on `assets:precompile` in production
21
+ * Automatically generates routes files on javascript build
22
22
  * Works great for a simple Rails application
23
23
  * [Webpacker ERB Loader](#webpacker)
24
24
  * Requires ESM module system (the default)
@@ -414,9 +414,14 @@ JsRoutes itself does not have security holes.
414
414
  It makes URLs without access protection more reachable by potential attacker.
415
415
  If that is an issue for you, you may use one of the following solutions:
416
416
 
417
- ### Explicit Import + ESM Tree shaking
417
+ ### ESM Tree shaking
418
418
 
419
- Make sure `module_type` is set to `ESM` (the default) and JS files import only required routes into the file like:
419
+ Make sure `module_type` is set to `ESM` (the default). Modern JS bundlers like
420
+ [Webpack](https://webpack.js.org) can statically determine which ESM exports are used, and remove
421
+ the unused exports to reduce bundle size. This is known as [Tree
422
+ Shaking](https://webpack.js.org/guides/tree-shaking/).
423
+
424
+ JS files can use named imports to import only required routes into the file, like:
420
425
 
421
426
  ``` javascript
422
427
  import {
@@ -428,8 +433,15 @@ import {
428
433
  } from '../routes'
429
434
  ```
430
435
 
431
- Such import structure allows for moddern JS bundlers like [Webpack](https://webpack.js.org/) to only include explicitly imported routes into JS bundle file.
432
- See [Tree Shaking](https://webpack.js.org/guides/tree-shaking/) for more information.
436
+ JS files can also use star imports (`import * as`) for tree shaking, as long as only explicit property accesses are used.
437
+
438
+ ``` javascript
439
+ import * as routes from '../routes';
440
+
441
+ console.log(routes.inbox_path); // OK, only `inbox_path` is included in the bundle
442
+
443
+ console.log(Object.keys(routes)); // forces bundler to include all exports, breaking tree shaking
444
+ ```
433
445
 
434
446
  ### Exclude option
435
447
 
@@ -33,8 +33,15 @@ module JsRoutes
33
33
  end
34
34
 
35
35
  def body(absolute)
36
- @configuration.dts? ?
37
- definition_body : "__jsr.r(#{arguments(absolute).map{|a| json(a)}.join(', ')})"
36
+ if @configuration.dts?
37
+ definition_body
38
+ else
39
+ # For tree-shaking ESM, add a #__PURE__ comment informing Webpack/minifiers that the call to `__jsr.r`
40
+ # has no side-effects (e.g. modifying global variables) and is safe to remove when unused.
41
+ # https://webpack.js.org/guides/tree-shaking/#clarifying-tree-shaking-and-sideeffects
42
+ pure_comment = @configuration.esm? ? '/*#__PURE__*/ ' : ''
43
+ "#{pure_comment}__jsr.r(#{arguments(absolute).map{|a| json(a)}.join(', ')})"
44
+ end
38
45
  end
39
46
 
40
47
  def definition_body
@@ -1,3 +1,3 @@
1
1
  module JsRoutes
2
- VERSION = "2.2.6"
2
+ VERSION = "2.2.7"
3
3
  end
data/lib/routes.d.ts CHANGED
@@ -32,8 +32,8 @@ declare type RequiredParameters<T extends number> = T extends 1 ? [RequiredRoute
32
32
  RequiredRouteParameter,
33
33
  RequiredRouteParameter
34
34
  ] : RequiredRouteParameter[];
35
- declare type RouteHelperOptions<T extends string> = RouteOptions & Optional<Record<T, OptionalRouteParameter>>;
36
- declare type RouteHelper<T extends number = number, U extends string = string> = ((...args: [...RequiredParameters<T>, RouteHelperOptions<U>]) => string) & RouteHelperExtras;
35
+ declare type RouteHelperOptions = RouteOptions & Record<string, OptionalRouteParameter>;
36
+ declare type RouteHelper<T extends number = number> = ((...args: [...RequiredParameters<T>, RouteHelperOptions]) => string) & RouteHelperExtras;
37
37
  declare type RouteHelpers = Record<string, RouteHelper>;
38
38
  declare type Configuration = {
39
39
  prefix: string;
data/lib/routes.ts CHANGED
@@ -40,11 +40,10 @@ type RequiredParameters<T extends number> = T extends 1
40
40
  ]
41
41
  : RequiredRouteParameter[];
42
42
 
43
- type RouteHelperOptions<T extends string> = RouteOptions &
44
- Optional<Record<T, OptionalRouteParameter>>;
43
+ type RouteHelperOptions = RouteOptions & Record<string, OptionalRouteParameter>;
45
44
 
46
- type RouteHelper<T extends number = number, U extends string = string> = ((
47
- ...args: [...RequiredParameters<T>, RouteHelperOptions<U>]
45
+ type RouteHelper<T extends number = number> = ((
46
+ ...args: [...RequiredParameters<T>, RouteHelperOptions]
48
47
  ) => string) &
49
48
  RouteHelperExtras;
50
49
 
@@ -539,7 +538,7 @@ RubyVariables.WRAPPER(
539
538
  let key: string;
540
539
  switch (route[0]) {
541
540
  case NodeTypes.GROUP:
542
- return "(" + this.build_path_spec(route[1]) + ")";
541
+ return `(${this.build_path_spec(route[1])})`;
543
542
  case NodeTypes.CAT:
544
543
  return (
545
544
  this.build_path_spec(route[1]) + this.build_path_spec(route[2])
@@ -32,8 +32,8 @@ declare type RequiredParameters<T extends number> = T extends 1 ? [RequiredRoute
32
32
  RequiredRouteParameter,
33
33
  RequiredRouteParameter
34
34
  ] : RequiredRouteParameter[];
35
- declare type RouteHelperOptions<T extends string> = RouteOptions & Optional<Record<T, OptionalRouteParameter>>;
36
- declare type RouteHelper<T extends number = number, U extends string = string> = ((...args: [...RequiredParameters<T>, RouteHelperOptions<U>]) => string) & RouteHelperExtras;
35
+ declare type RouteHelperOptions = RouteOptions & Record<string, OptionalRouteParameter>;
36
+ declare type RouteHelper<T extends number = number> = ((...args: [...RequiredParameters<T>, RouteHelperOptions]) => string) & RouteHelperExtras;
37
37
  declare type RouteHelpers = Record<string, RouteHelper>;
38
38
  declare type Configuration = {
39
39
  prefix: string;
@@ -105,7 +105,7 @@ DOC
105
105
  describe "compiled javascript asset" do
106
106
  subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) }
107
107
  it "should have js routes code" do
108
- is_expected.to include("export const inbox_message_path = __jsr.r(")
108
+ is_expected.to include("export const inbox_message_path = /*#__PURE__*/ __jsr.r(")
109
109
  end
110
110
  end
111
111
  end
@@ -24,7 +24,7 @@ describe JsRoutes, "compatibility with ESM" do
24
24
  * @param {object | undefined} options
25
25
  * @returns {string} route path
26
26
  */
27
- export const inboxes_path = __jsr.r
27
+ export const inboxes_path = /*#__PURE__*/ __jsr.r(
28
28
  DOC
29
29
  end
30
30
 
@@ -39,7 +39,7 @@ DOC
39
39
  describe "compiled javascript asset" do
40
40
  subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) }
41
41
  it "should have js routes code" do
42
- is_expected.to include("export const inbox_message_path = __jsr.r(")
42
+ is_expected.to include("export const inbox_message_path = /*#__PURE__*/ __jsr.r(")
43
43
  end
44
44
  end
45
45
  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.6
4
+ version: 2.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-05 00:00:00.000000000 Z
11
+ date: 2023-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -222,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
222
  - !ruby/object:Gem::Version
223
223
  version: '0'
224
224
  requirements: []
225
- rubygems_version: 3.3.7
225
+ rubygems_version: 3.4.15
226
226
  signing_key:
227
227
  specification_version: 4
228
228
  summary: Brings Rails named routes to javascript