ruby2js 4.1.6 → 4.1.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: 8f29494a95eb89f049d35d1a431e50e1ac4fb526fbe7544dff5b7c699b2aae9c
4
- data.tar.gz: cd87036fc9542ce81e7dedb379ee5eb874f7ea9144096ff7f1b496b2c3bad5a3
3
+ metadata.gz: 0e5086f1cd3eb09ea176eb28e0ad73eea87f47f111c24f5029b547883bda7fbd
4
+ data.tar.gz: d758cd6a5719aaba435ac7944bfbefc426e8d6ff98b41f7794c9dd6394847e6c
5
5
  SHA512:
6
- metadata.gz: 574b8d730585898be4ce8506b256f90a9ed092e88edcbe5af82cf5d0f70aa7177b31486deee3b615269fe8011f0b9ac0efb38381bf6df924d04cfddcd5dda799
7
- data.tar.gz: 68d618097efa0fc47509129242d84e72349dd3619b9238df83820ade29fca243d7cb19c6627d9fe5b14c64fb3fb6a5fa1d9f1cfa93bd2a5753acb84645a56fc4
6
+ metadata.gz: 405b1b6c7eb8d62d767a3d756378bf1226dfe204d89eb24743eb3af6727e5a2950a2ae16c0d9dc8b0e352fd2a2390964cfb4ef074edc7f4820676ece42ce819c
7
+ data.tar.gz: 16557065963d7ad981a07e2695fd756956866beb45eada9f3dcfbbe7544b84791e366ccc2e65f2beb4cad6de484c04dbc7465b222853792a1219dfc9997eb466
@@ -9,12 +9,12 @@ module Ruby2JS
9
9
 
10
10
  STIMULUS_IMPORT = s(:import,
11
11
  [s(:pair, s(:sym, :as), s(:const, nil, :Stimulus)),
12
- s(:pair, s(:sym, :from), s(:str, "stimulus"))],
12
+ s(:pair, s(:sym, :from), s(:str, "@hotwired/stimulus"))],
13
13
  s(:str, '*'))
14
14
 
15
15
  STIMULUS_IMPORT_SKYPACK = s(:import,
16
16
  [s(:pair, s(:sym, :as), s(:const, nil, :Stimulus)),
17
- s(:pair, s(:sym, :from), s(:str, "https://cdn.skypack.dev/stimulus"))],
17
+ s(:pair, s(:sym, :from), s(:str, "https://cdn.skypack.dev/@hotwired/stimulus"))],
18
18
  s(:str, '*'))
19
19
 
20
20
  def initialize(*args)
@@ -18,6 +18,12 @@ module Ruby2JS
18
18
  else
19
19
  super
20
20
  end
21
+ elsif [:take, :drop].include? method and node.is_method?
22
+ process S(:send, s(:lvar, :_), method, node.children[0], node.children[2])
23
+ elsif method == :each_slice and node.is_method?
24
+ process S(:send, s(:lvar, :_), :chunk, node.children[0], node.children[2])
25
+ elsif [:min, :max].include? method and node.children.length == 2
26
+ process S(:send, s(:lvar, :_), method, node.children[0])
21
27
  elsif method == :sample and node.children.length <= 3
22
28
  process S(:send, s(:lvar, :_), :sample, node.children[0],
23
29
  *node.children[2..-1])
@@ -62,14 +68,14 @@ module Ruby2JS
62
68
  elsif method == :reduce
63
69
  if node.children.length == 3 and node.children[2].type == :sym
64
70
  # input: a.reduce(:+)
65
- # output: _.reduce(_.rest(a),
71
+ # output: _.reduce(_.rest(a),
66
72
  # proc {|memo, item| return memo+item},
67
73
  # a[0])
68
- process S(:send, s(:lvar, :_), :reduce,
74
+ process S(:send, s(:lvar, :_), :reduce,
69
75
  s(:send, s(:lvar, :_), :rest, node.children.first),
70
- s(:block, s(:send, nil, :proc),
76
+ s(:block, s(:send, nil, :proc),
71
77
  s(:args, s(:arg, :memo), s(:arg, :item)),
72
- s(:autoreturn, s(:send, s(:lvar, :memo),
78
+ s(:autoreturn, s(:send, s(:lvar, :memo),
73
79
  node.children[2].children.first, s(:lvar, :item)))),
74
80
  s(:send, node.children.first, :[], s(:int, 0)))
75
81
  elsif node.children.last.type == :block_pass
@@ -79,9 +85,9 @@ module Ruby2JS
79
85
  # input: a.reduce(n, :+)
80
86
  # output: _.reduce(a, proc {|memo, item| return memo+item}, n)
81
87
  process S(:send, s(:lvar, :_), :reduce, node.children.first,
82
- s(:block, s(:send, nil, :proc),
88
+ s(:block, s(:send, nil, :proc),
83
89
  s(:args, s(:arg, :memo), s(:arg, :item)),
84
- s(:autoreturn, s(:send, s(:lvar, :memo),
90
+ s(:autoreturn, s(:send, s(:lvar, :memo),
85
91
  node.children[3].children.first, s(:lvar, :item)))),
86
92
  node.children[2])
87
93
  else
@@ -93,8 +99,8 @@ module Ruby2JS
93
99
  # input: a.compact!
94
100
  # output: a.splice(0, a.length, *a.compact)
95
101
  target = node.children.first
96
- process S(:send, target, :splice, s(:int, 0),
97
- s(:attr, target, :length), s(:splat, s(:send, target,
102
+ process S(:send, target, :splice, s(:int, 0),
103
+ s(:attr, target, :length), s(:splat, s(:send, target,
98
104
  :"#{method.to_s[0..-2]}", *node.children[2..-1])))
99
105
  else
100
106
  super
@@ -111,14 +117,14 @@ module Ruby2JS
111
117
  # output: _.sortBy {return expression}
112
118
  method = method.to_s.sub(/\_by$/,'By').to_sym
113
119
  process S(:block, s(:send, s(:lvar, :_), method,
114
- call.children.first), node.children[1],
120
+ call.children.first), node.children[1],
115
121
  s(:autoreturn, node.children[2]))
116
122
  elsif [:find, :reject].include? method
117
123
  if call.children.length == 2
118
124
  # input: a.find {|item| item > 0}
119
125
  # output: _.find(a) {|item| return item > 0}
120
- process S(:block, s(:send, s(:lvar, :_), method,
121
- call.children.first), node.children[1],
126
+ process S(:block, s(:send, s(:lvar, :_), method,
127
+ call.children.first), node.children[1],
122
128
  s(:autoreturn, node.children[2]))
123
129
  else
124
130
  super
@@ -127,25 +133,25 @@ module Ruby2JS
127
133
  elsif method == :times and call.children.length == 2
128
134
  # input: 5.times {|i| console.log i}
129
135
  # output: _.find(5) {|i| console.log(i)}
130
- process S(:block, s(:send, s(:lvar, :_), method,
136
+ process S(:block, s(:send, s(:lvar, :_), method,
131
137
  call.children.first), node.children[1], node.children[2])
132
138
 
133
139
  elsif method == :reduce
134
140
  if call.children.length == 2
135
141
  # input: a.reduce {|memo, item| memo+item}
136
- # output: _.reduce(_.rest(a),
142
+ # output: _.reduce(_.rest(a),
137
143
  # proc {|memo, item| return memo+item},
138
144
  # a[0])
139
- process S(:call, s(:lvar, :_), :reduce,
145
+ process S(:call, s(:lvar, :_), :reduce,
140
146
  s(:send, s(:lvar, :_), :rest, call.children.first),
141
- s(:block, s(:send, nil, :proc),
147
+ s(:block, s(:send, nil, :proc),
142
148
  node.children[1], s(:autoreturn, node.children[2])),
143
149
  s(:send, call.children.first, :[], s(:int, 0)))
144
150
  elsif call.children.length == 3
145
151
  # input: a.reduce(n) {|memo, item| memo+item}
146
152
  # output: _.reduce(a, proc {|memo, item| return memo+item}, n)
147
153
  process S(:call, s(:lvar, :_), :reduce, call.children.first,
148
- s(:block, s(:send, nil, :proc),
154
+ s(:block, s(:send, nil, :proc),
149
155
  node.children[1], s(:autoreturn, node.children[2])),
150
156
  call.children[2])
151
157
  end
@@ -155,7 +161,7 @@ module Ruby2JS
155
161
  # output: a.splice(0, a.length, *a.map {expression})
156
162
  method = :"#{method.to_s[0..-2]}"
157
163
  target = call.children.first
158
- process S(:call, target, :splice, s(:splat, s(:send, s(:array,
164
+ process S(:call, target, :splice, s(:splat, s(:send, s(:array,
159
165
  s(:int, 0), s(:attr, target, :length)), :concat,
160
166
  s(:block, s(:send, target, method, *call.children[2..-1]),
161
167
  *node.children[1..-1]))))
@@ -2,7 +2,7 @@ module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 4
4
4
  MINOR = 1
5
- TINY = 6
5
+ TINY = 7
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,43 @@
1
+ # install rollup plugin
2
+ run "yarn add @ruby2js/rollup-plugin"
3
+
4
+ # configure rollup for ruby2js with stimulus filters
5
+ insert_into_file Rails.root.join("rollup.config.js").to_s,
6
+ "import ruby2js from '@ruby2js/rollup-plugin';\n",
7
+ after: /import resolve from .*\n/
8
+
9
+ insert_into_file Rails.root.join("rollup.config.js").to_s,
10
+ <<-CONFIG, after: "resolve()\n"
11
+ ,ruby2js({
12
+ eslevel: 2020,
13
+ autoexports: 'default',
14
+ filters: ['stimulus', 'esm', 'functions']
15
+ })
16
+ CONFIG
17
+
18
+ # monkey patch stimulus:manifest:update to find .rb.js controllers too
19
+ append_to_file Rails.root.join('config/application.rb').to_s,
20
+ "\n" + <<~'CONFIG'
21
+ require 'stimulus/manifest'
22
+
23
+ module Stimulus::Manifest
24
+ def import_and_register_controller(controllers_path, controller_path)
25
+ controller_path = controller_path.relative_path_from(controllers_path).to_s
26
+ module_path = controller_path.split('.').first
27
+ controller_class_name = module_path.camelize.gsub(/::/, "__")
28
+ tag_name = module_path.remove(/_controller/).gsub(/_/, "-").gsub(/\//, "--")
29
+
30
+ <<~JS
31
+
32
+ import #{controller_class_name} from "./#{controller_path}"
33
+ application.register("#{tag_name}", #{controller_class_name})
34
+ JS
35
+ end
36
+
37
+ def extract_controllers_from(directory)
38
+ (directory.children.select { |e| e.to_s =~ /_controller\.js(\.\w+)?$/ } +
39
+ directory.children.select(&:directory?).collect { |d| extract_controllers_from(d) }
40
+ ).flatten.sort
41
+ end
42
+ end
43
+ CONFIG
@@ -1,32 +1,25 @@
1
1
  create_file Rails.root.join('config/initializers/ruby2js.rb').to_s,
2
2
  <<~CONFIG
3
- require 'ruby2js/filter/esm'
4
- require 'ruby2js/filter/functions'
5
- require 'ruby2js/filter/stimulus'
3
+ require 'stimulus/manifest'
6
4
 
7
- Ruby2JS::SprocketsTransformer.options = {
8
- autoexports: :default,
9
- eslevel: 2020
10
- }
5
+ module Stimulus::Manifest
6
+ def import_and_register_controller(controllers_path, controller_path)
7
+ controller_path = controller_path.relative_path_from(controllers_path).to_s
8
+ module_path = controller_path.split('.').first
9
+ controller_class_name = module_path.camelize.gsub(/::/, "__")
10
+ tag_name = module_path.remove(/_controller/).gsub(/_/, "-").gsub(/\//, "--")
11
11
 
12
- require 'stimulus/importmap_helper'
12
+ <<~JS
13
13
 
14
- module Stimulus::ImportmapHelper
15
- def find_javascript_files_in_tree(path)
16
- exts = {'.js' => '.js', '.jsm' => '.jsm'}.merge(
17
- Sprockets.mime_exts.map {|key, value|
18
- next unless Sprockets.transformers[value]["application/javascript"]
19
- [key, '.js']
20
- }.compact.to_h)
21
-
22
- Dir[path.join('**/*')].map {|file|
23
- file_ext, web_ext = Sprockets::PathUtils.match_path_extname(file, exts)
24
- next unless file_ext
25
-
26
- next unless File.file? file
14
+ import #{controller_class_name} from "./#{controller_path}"
15
+ application.register("#{tag_name}", #{controller_class_name})
16
+ JS
17
+ end
27
18
 
28
- Pathname.new(file.chomp(file_ext) + web_ext)
29
- }.compact
19
+ def extract_controllers_from(directory)
20
+ (directory.children.select { |e| e.to_s =~ /_controller\.js(\.\w+)?$/ } +
21
+ directory.children.select(&:directory?).collect { |d| extract_controllers_from(d) }
22
+ ).flatten.sort
30
23
  end
31
24
  end
32
25
  CONFIG
@@ -3,3 +3,6 @@ eval IO.read "#{__dir__}/webpacker.rb"
3
3
 
4
4
  insert_into_file Rails.root.join("app/javascript/controllers/index.js").to_s,
5
5
  '(\\.rb)?', after: '_controller\\.js'
6
+
7
+ insert_into_file Rails.root.join("app/javascript/packs/application.js").to_s,
8
+ "import \"../controllers\"\n", after: "import \"channels\"\n"
@@ -1,4 +1,6 @@
1
- Thor::Actions::WARNINGS[:unchanged_no_flag] = 'unchanged'
1
+ if defined? Thor
2
+ Thor::Actions::WARNINGS[:unchanged_no_flag] = 'unchanged'
3
+ end
2
4
 
3
5
  def template(location)
4
6
  system "#{RbConfig.ruby} #{Rails.root.join("bin")}/rails app:template " +
@@ -24,9 +26,9 @@ namespace :ruby2js do
24
26
  end
25
27
 
26
28
  namespace :stimulus do
27
- desc "Install Ruby2JS with Stimulus Sprockets support"
28
- task :sprockets => :"stimulus:install:asset_pipeline" do
29
- template 'install/stimulus-sprockets.rb'
29
+ desc "Install Ruby2JS with Stimulus Rollup support"
30
+ task :rollup do
31
+ template 'install/stimulus-rollup.rb'
30
32
  end
31
33
 
32
34
  desc "Install Ruby2JS with Stimulus Webpacker support"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2js
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.6
4
+ version: 4.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-08-19 00:00:00.000000000 Z
12
+ date: 2021-09-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parser
@@ -164,6 +164,7 @@ files:
164
164
  - lib/tasks/install/litelement.rb
165
165
  - lib/tasks/install/preact.rb
166
166
  - lib/tasks/install/react.rb
167
+ - lib/tasks/install/stimulus-rollup.rb
167
168
  - lib/tasks/install/stimulus-sprockets.rb
168
169
  - lib/tasks/install/stimulus-webpacker.rb
169
170
  - lib/tasks/install/webpacker.rb