ruby2js 4.0.2 → 4.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/lib/ruby2js.rb +1 -1
- data/lib/ruby2js/converter.rb +1 -0
- data/lib/ruby2js/converter/class.rb +7 -5
- data/lib/ruby2js/converter/class2.rb +4 -4
- data/lib/ruby2js/converter/def.rb +7 -1
- data/lib/ruby2js/converter/dstr.rb +3 -1
- data/lib/ruby2js/converter/return.rb +1 -1
- data/lib/ruby2js/converter/send.rb +1 -0
- data/lib/ruby2js/converter/taglit.rb +13 -2
- data/lib/ruby2js/filter/cjs.rb +100 -2
- data/lib/ruby2js/filter/functions.rb +11 -6
- data/lib/ruby2js/filter/lit-element.rb +218 -0
- data/lib/ruby2js/filter/react.rb +276 -88
- data/lib/ruby2js/filter/tagged_templates.rb +4 -2
- data/lib/ruby2js/rails.rb +10 -0
- data/lib/ruby2js/version.rb +2 -2
- data/lib/tasks/README.md +4 -0
- data/lib/tasks/install/app/javascript/elements/index.js +2 -0
- data/lib/tasks/install/config/webpack/loaders/ruby2js.js +20 -0
- data/lib/tasks/install/litelement.rb +9 -0
- data/lib/tasks/install/preact.rb +3 -0
- data/lib/tasks/install/react.rb +2 -0
- data/lib/tasks/install/stimulus-sprockets.rb +32 -0
- data/lib/tasks/install/stimulus-webpacker.rb +5 -0
- data/lib/tasks/install/webpacker.rb +80 -0
- data/lib/tasks/nodetest.js +47 -0
- data/lib/tasks/ruby2js_tasks.rake +47 -0
- metadata +15 -2
@@ -15,17 +15,19 @@ module Ruby2JS
|
|
15
15
|
|
16
16
|
tagged_methods = @options[:template_literal_tags] || [:html, :css]
|
17
17
|
|
18
|
-
if tagged_methods.include?(method) &&
|
18
|
+
if tagged_methods.include?(method) && args.length == 1
|
19
19
|
strnode = process args.first
|
20
20
|
if strnode.type == :str
|
21
21
|
# convert regular strings to literal strings
|
22
22
|
strnode = strnode.updated(:dstr, [s(:str, strnode.children.first.chomp("\n"))])
|
23
|
-
|
23
|
+
elsif strnode.type == :dstr
|
24
24
|
# for literal strings, chomp a newline off the end
|
25
25
|
if strnode.children.last.type == :str && strnode.children.last.children[0].end_with?("\n")
|
26
26
|
children = [*strnode.children.take(strnode.children.length - 1), s(:str, strnode.children.last.children[0].chomp)]
|
27
27
|
strnode = s(:dstr, *children)
|
28
28
|
end
|
29
|
+
else
|
30
|
+
return super
|
29
31
|
end
|
30
32
|
|
31
33
|
S(:taglit, s(:sym, method), strnode)
|
data/lib/ruby2js/version.rb
CHANGED
data/lib/tasks/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
const { environment } = require('@rails/webpacker')
|
2
|
+
|
3
|
+
module.exports = {
|
4
|
+
test: /\.js\.rb$/,
|
5
|
+
use: [
|
6
|
+
{
|
7
|
+
loader: "babel-loader",
|
8
|
+
options: environment.loaders.get('babel').use[0].options
|
9
|
+
},
|
10
|
+
|
11
|
+
{
|
12
|
+
loader: "@ruby2js/webpack-loader",
|
13
|
+
options: {
|
14
|
+
autoexports: "default",
|
15
|
+
eslevel: 2021,
|
16
|
+
filters: ["esm", "functions"]
|
17
|
+
}
|
18
|
+
}
|
19
|
+
]
|
20
|
+
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
@ruby2js_options = {filters: ['lit-element']}
|
2
|
+
@yarn_add='lit-element'
|
3
|
+
eval IO.read "#{__dir__}/webpacker.rb"
|
4
|
+
|
5
|
+
directory File.expand_path("app/javascript/elements", __dir__),
|
6
|
+
Rails.root.join('app/javascript/elements').to_s
|
7
|
+
|
8
|
+
append_to_file Rails.root.join('app/javascript/packs/application.js').to_s,
|
9
|
+
"\nimport 'elements'\n"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
create_file Rails.root.join('config/initializers/ruby2js.rb').to_s,
|
2
|
+
<<~CONFIG
|
3
|
+
require 'ruby2js/filter/esm'
|
4
|
+
require 'ruby2js/filter/functions'
|
5
|
+
require 'ruby2js/filter/stimulus'
|
6
|
+
|
7
|
+
Ruby2JS::SprocketsTransformer.options = {
|
8
|
+
autoexports: :default,
|
9
|
+
eslevel: 2020
|
10
|
+
}
|
11
|
+
|
12
|
+
require 'stimulus/importmap_helper'
|
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
|
27
|
+
|
28
|
+
Pathname.new(file.chomp(file_ext) + web_ext)
|
29
|
+
}.compact
|
30
|
+
end
|
31
|
+
end
|
32
|
+
CONFIG
|
@@ -0,0 +1,80 @@
|
|
1
|
+
#
|
2
|
+
# These instructions can be used standalone or in combination with other
|
3
|
+
# instructions.
|
4
|
+
#
|
5
|
+
# If used standalone on a repository which did not previously have the Ruby2JS
|
6
|
+
# webpack loader configured, it will add the '.js.rb' extension to
|
7
|
+
# `config/webpacker.yml`, install the Ruby2JS webpack-loader and add a minimal
|
8
|
+
# configuration which runs the Ruby2JS webpack loader then the babel loader to
|
9
|
+
# `config/webpacker.yml`.
|
10
|
+
#
|
11
|
+
# Other instructions can require these instructions. If they set the
|
12
|
+
# @ruby2js_options instance variable before they do so, the options provided
|
13
|
+
# will be merged and/or override the ones in the base configuration.
|
14
|
+
#
|
15
|
+
# If these instructions are run against a Rails application that was
|
16
|
+
# previously configured, the @ruby2js_options provided, if any, will be merged
|
17
|
+
# with the options found in the configuration.
|
18
|
+
#
|
19
|
+
|
20
|
+
# default options
|
21
|
+
options = @ruby2js_options || {}
|
22
|
+
|
23
|
+
# define .js.rb as a extension to webpacker
|
24
|
+
insert_into_file Rails.root.join("config/webpacker.yml").to_s,
|
25
|
+
" - .js.rb\n", after: "\n - .js\n"
|
26
|
+
|
27
|
+
# install webpack loader
|
28
|
+
run "yarn add @ruby2js/webpack-loader #{@yarn_add}"
|
29
|
+
|
30
|
+
target = Rails.root.join("config/webpack/loaders/ruby2js.js").to_s
|
31
|
+
|
32
|
+
# default config
|
33
|
+
if not File.exist? target
|
34
|
+
# may be called via eval, or directly. Resolve source either way.
|
35
|
+
source_paths.unshift __dir__
|
36
|
+
source_paths.unshift File.dirname(caller.first)
|
37
|
+
directory "config/webpack/loaders", File.dirname(target)
|
38
|
+
end
|
39
|
+
|
40
|
+
# load config
|
41
|
+
insert_into_file Rails.root.join("config/webpack/environment.js").to_s,
|
42
|
+
"environment.loaders.prepend('ruby2js', require('./loaders/ruby2js'))\n"
|
43
|
+
|
44
|
+
# read current configuration
|
45
|
+
before = IO.read(target)
|
46
|
+
|
47
|
+
# extract indentation and options
|
48
|
+
match = /^(\s*)options: (\{.*?\n\1\})/m.match(before)
|
49
|
+
|
50
|
+
# evaluate base options. Here it is handy that Ruby's syntax for hashes is
|
51
|
+
# fairly close to JavaScript's object literals. May run into problems in the
|
52
|
+
# future if there ever is a need for {"x": y} as that would need to be
|
53
|
+
# {"x" => y} in Ruby.
|
54
|
+
base = eval(match[2])
|
55
|
+
|
56
|
+
# Merge the options, initially having the new options override the original
|
57
|
+
# options.
|
58
|
+
merged = base.merge(options)
|
59
|
+
|
60
|
+
# Intelligently combine options if they are the same type.
|
61
|
+
options.keys.each do |key|
|
62
|
+
next unless base[key]
|
63
|
+
|
64
|
+
if options[key].is_a? Array and base[key].is_a? Array
|
65
|
+
merged[key] = (base[key] + options[key]).uniq
|
66
|
+
elsif options[key].is_a? Numeric and base[key].is_a? Numeric
|
67
|
+
merged[key] = [base[key], options[key]].max
|
68
|
+
elsif options[key].is_a? Hash and base[key].is_a? Hash
|
69
|
+
merged[key] = base[key].merge(options[key])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Serialize options as JavaScript, matching original indentation.
|
74
|
+
replacement = Ruby2JS.convert(merged.inspect + "\n").to_s.
|
75
|
+
gsub(/^/, match[1]).strip
|
76
|
+
|
77
|
+
# Update configuration
|
78
|
+
unless before.include? replacement
|
79
|
+
gsub_file target, match[2].to_s, replacement
|
80
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
// minimal sanity test to verify usage of Ruby2JS under Node
|
2
|
+
|
3
|
+
const assert = require('assert');
|
4
|
+
|
5
|
+
const Ruby2JS = require('@ruby2js/ruby2js');
|
6
|
+
// const Ruby2JS =
|
7
|
+
// require('/home/rubys/git/ruby2js/docs/src/demo/ruby2js.js');
|
8
|
+
|
9
|
+
function to_js(string, options={}) {
|
10
|
+
return Ruby2JS.convert(string, options).toString()
|
11
|
+
}
|
12
|
+
|
13
|
+
assert.strictEqual(
|
14
|
+
to_js('foo = 1'),
|
15
|
+
'var foo = 1');
|
16
|
+
|
17
|
+
assert.strictEqual(
|
18
|
+
to_js('foo = 1', {eslevel: 2015}),
|
19
|
+
'let foo = 1');
|
20
|
+
|
21
|
+
assert.strictEqual(
|
22
|
+
to_js('foo.empty?', {filters: ['functions']}),
|
23
|
+
'foo.length == 0');
|
24
|
+
|
25
|
+
let ast = Ruby2JS.convert('String', {file: 'a.rb'}).ast
|
26
|
+
assert.strictEqual(ast.constructor, Ruby2JS.AST.Node)
|
27
|
+
assert.strictEqual(ast.type, "const")
|
28
|
+
assert.strictEqual(ast.children.length, 2)
|
29
|
+
assert.strictEqual(ast.children[0], Ruby2JS.nil)
|
30
|
+
assert.strictEqual(ast.children[1], "String")
|
31
|
+
|
32
|
+
let sourcemap = Ruby2JS.convert('a=1', {file: 'a.rb'}).sourcemap
|
33
|
+
assert.strictEqual(sourcemap.version, 3)
|
34
|
+
assert.strictEqual(sourcemap.file, 'a.rb')
|
35
|
+
assert.strictEqual(sourcemap.sources.length, 1)
|
36
|
+
assert.strictEqual(sourcemap.sources[0], 'a.rb')
|
37
|
+
assert.strictEqual(sourcemap.mappings, 'QAAE')
|
38
|
+
|
39
|
+
console.log(to_js(`
|
40
|
+
class C < LitElement
|
41
|
+
def render
|
42
|
+
x.empty?
|
43
|
+
'<h1>title</h1>'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
`, {eslevel: 2020, filters: ['lit-element', 'esm', 'functions']}))
|
@@ -0,0 +1,47 @@
|
|
1
|
+
Thor::Actions::WARNINGS[:unchanged_no_flag] = 'unchanged'
|
2
|
+
|
3
|
+
def template(location)
|
4
|
+
system "#{RbConfig.ruby} #{Rails.root.join("bin")}/rails app:template " +
|
5
|
+
"LOCATION=#{File.expand_path(location, __dir__)}"
|
6
|
+
end
|
7
|
+
|
8
|
+
namespace :ruby2js do
|
9
|
+
namespace :install do
|
10
|
+
desc "Install Ruby2JS with LitElement support"
|
11
|
+
task :litelement do
|
12
|
+
template 'install/litelement.rb'
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Install Ruby2JS with Preact support"
|
16
|
+
task :preact do
|
17
|
+
template 'install/preact.rb'
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Install Ruby2JS with React support"
|
21
|
+
task :react do
|
22
|
+
template 'install/react.rb'
|
23
|
+
Rake::Task['webpacker:install:react'].invoke
|
24
|
+
end
|
25
|
+
|
26
|
+
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'
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "Install Ruby2JS with Stimulus Webpacker support"
|
33
|
+
task :webpacker => :"stimulus:install" do
|
34
|
+
template 'install/stimulus-webpacker.rb'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
namespace :webpacker do
|
41
|
+
namespace :install do
|
42
|
+
desc "Install everything needed for Ruby2JS"
|
43
|
+
task :ruby2js do
|
44
|
+
template 'install/webpacker.rb'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
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.
|
4
|
+
version: 4.1.1
|
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-03-
|
12
|
+
date: 2021-03-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parser
|
@@ -135,6 +135,7 @@ files:
|
|
135
135
|
- lib/ruby2js/filter/functions.rb
|
136
136
|
- lib/ruby2js/filter/jquery.rb
|
137
137
|
- lib/ruby2js/filter/jsx.rb
|
138
|
+
- lib/ruby2js/filter/lit-element.rb
|
138
139
|
- lib/ruby2js/filter/matchAll.rb
|
139
140
|
- lib/ruby2js/filter/minitest-jasmine.rb
|
140
141
|
- lib/ruby2js/filter/node.rb
|
@@ -150,11 +151,23 @@ files:
|
|
150
151
|
- lib/ruby2js/haml.rb
|
151
152
|
- lib/ruby2js/jsx.rb
|
152
153
|
- lib/ruby2js/namespace.rb
|
154
|
+
- lib/ruby2js/rails.rb
|
153
155
|
- lib/ruby2js/serializer.rb
|
154
156
|
- lib/ruby2js/sinatra.rb
|
155
157
|
- lib/ruby2js/sprockets.rb
|
156
158
|
- lib/ruby2js/strict.rb
|
157
159
|
- lib/ruby2js/version.rb
|
160
|
+
- lib/tasks/README.md
|
161
|
+
- lib/tasks/install/app/javascript/elements/index.js
|
162
|
+
- lib/tasks/install/config/webpack/loaders/ruby2js.js
|
163
|
+
- lib/tasks/install/litelement.rb
|
164
|
+
- lib/tasks/install/preact.rb
|
165
|
+
- lib/tasks/install/react.rb
|
166
|
+
- lib/tasks/install/stimulus-sprockets.rb
|
167
|
+
- lib/tasks/install/stimulus-webpacker.rb
|
168
|
+
- lib/tasks/install/webpacker.rb
|
169
|
+
- lib/tasks/nodetest.js
|
170
|
+
- lib/tasks/ruby2js_tasks.rake
|
158
171
|
- ruby2js.gemspec
|
159
172
|
homepage: http://github.com/rubys/ruby2js
|
160
173
|
licenses:
|