opal-optimizer 0.1.2 → 0.1.6

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: d76a6684777438ef696216358695d66b0e2ddd212f5b9d28d482843c912a2ac4
4
- data.tar.gz: 536b978277f2af33f5ac47ad8661629e151d54adf11d88f8e0a63487327956d2
3
+ metadata.gz: 6498304a0ea20dac60d150ea41df0876ade32476a48a489daca24396686de528
4
+ data.tar.gz: e453ead6b82eedb0fd1c03fc38cd2de64a5d1ceda31a55a541e83f50b9ed76d8
5
5
  SHA512:
6
- metadata.gz: 3e87db8dbc1976b663dfc0fcbbc25b3317058e06c72f759b131e16caeaf589355f85e029e2307392ba386f8c7787128a70a39743570a2d854f327f8b054187fb
7
- data.tar.gz: f29200eb79910e28c08acf486e3881468ec0b5cdc8718787a0e719193e6a2ac1b96c9b43acfc5fbcfe2158e710ce19371d67795521119fbd448db216f87512f4
6
+ metadata.gz: bcbf6fb6a5f209e3cea950be73637504ce830c16a9ef8e7402156b64d82e8905c6406a12e0acbc52feb2a1770a4f869ca1c52b78c6f08b590c6c64df94d7c9c7
7
+ data.tar.gz: 941e429601ce081347bad1179f152f0dd5aa0f3b20374cf683e021ca1c00e5e078cd63e44476af127fcc8d21eb0f34b35c605df5ae9a129c2e28c3917c970a59
@@ -0,0 +1,63 @@
1
+ name: build
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ - "*-stable"
8
+ - "*/ci-check"
9
+ pull_request: {}
10
+
11
+ jobs:
12
+ rake:
13
+ name: ${{ matrix.combo.name }}
14
+ strategy:
15
+ fail-fast: false
16
+ matrix:
17
+ combo:
18
+ - name: Opal-master
19
+ opal: master
20
+ - name: Opal-1.4
21
+ opal: 1.4
22
+ - name: Opal-1.3
23
+ opal: 1.3
24
+ - name: Opal-1.2
25
+ opal: 1.2
26
+ - name: Opal-1.1
27
+ opal: 1.1
28
+ - name: Opal-1.0
29
+ opal: '1.0'
30
+ - name: Ruby-head
31
+ ruby: head
32
+ - name: Windows
33
+ os: windows-latest
34
+ - name: macOS
35
+ os: macos-latest
36
+
37
+ runs-on: ${{ matrix.combo.os || 'ubuntu-latest' }}
38
+
39
+ steps:
40
+ - uses: actions/checkout@v2
41
+ - name: set environment variables
42
+ run: |
43
+ echo "OPAL_VERSION=${{ matrix.combo.opal || '1.3' }}" >> $GITHUB_ENV
44
+ - uses: ruby/setup-ruby@v1
45
+ with:
46
+ ruby-version: ${{ matrix.combo.ruby || '3.0' }}
47
+ - run: bundle lock
48
+ - uses: actions/cache@v2
49
+ with:
50
+ path: ./vendor/bundle
51
+ key: ${{ runner.os }}-${{ matrix.combo.ruby || '3.0' }}-gems-${{ matrix.combo.opal || '1.3' }}-${{ github.ref }}-${{ hashFiles('**/Gemfile.lock') }}
52
+ restore-keys: |
53
+ ${{ runner.os }}-${{ matrix.combo.ruby || '3.0' }}-gems-${{ matrix.combo.opal || '1.3' }}-${{ github.ref }}
54
+ ${{ runner.os }}-${{ matrix.combo.ruby || '3.0' }}-gems-${{ matrix.combo.opal || '1.3' }}-master
55
+ ${{ runner.os }}-${{ matrix.combo.ruby || '3.0' }}-gems-${{ matrix.combo.opal || '1.3' }}-
56
+ ${{ runner.os }}-${{ matrix.combo.ruby || '3.0' }}-gems-
57
+ - name: bundle install
58
+ run: |
59
+ bundle config path $PWD/vendor/bundle
60
+ bundle install --jobs 4 --retry 3
61
+ bundle clean
62
+ - run: bundle exec rake
63
+
data/.gitignore CHANGED
@@ -7,5 +7,7 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
+ /Gemfile.lock
11
+
10
12
  # rspec failure tracking
11
13
  .rspec_status
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## v0.1.6
2
+
3
+ * Further Opal 1.4 compatibility
4
+
5
+ ## v0.1.5
6
+
7
+ * Opal 1.4 compatibility
data/Gemfile CHANGED
@@ -3,14 +3,20 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in opal-optimizer.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
6
+ gem "rake"
7
+ gem "rspec"
8
8
 
9
- gem "opal", path: "../opal"
10
- gem "opal-sprockets", path: "../opal-sprockets"
11
- gem "opal-browser", path: "../opal-browser"
9
+ case ENV['OPAL_VERSION']
10
+ when nil
11
+ when /\A[0-9.]+\z/
12
+ gem 'opal', "~> #{ENV['OPAL_VERSION']}.0a"
13
+ else
14
+ gem 'opal', github: 'opal/opal', ref: ENV['OPAL_VERSION']
15
+ end
16
+ gem "opal-sprockets"
17
+ gem "opal-browser"
12
18
 
13
- gem "rkelly-turbo", path: "../rkelly-turbo"
19
+ gem "rkelly-turbo"
14
20
 
15
21
  gem "ruby-prof"
16
22
  gem "pry"
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # Opal::Optimizer
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/opal/optimizer`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A utility to optimize the JavaScript output from Opal with a help of RKelly-Turbo JavaScript parser.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ As of now it contains two steps:
6
+
7
+ * Tree shaking for Opal methods - if a named method is not called anywhere in the code, it's removed
8
+ * Collapsing stubs
6
9
 
7
10
  ## Installation
8
11
 
@@ -22,7 +25,7 @@ Or install it yourself as:
22
25
 
23
26
  ## Usage
24
27
 
25
- TODO: Write usage instructions here
28
+ For Sprockets environments, all you need is to `require "opal/optimizer/sprockets"` in your pipeline.
26
29
 
27
30
  ## Development
28
31
 
@@ -32,7 +35,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
35
 
33
36
  ## Contributing
34
37
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/opal-optimizer.
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hmdne/opal-optimizer.
36
39
 
37
40
 
38
41
  ## License
data/bin/opal-optimizer CHANGED
@@ -1,6 +1,2 @@
1
1
  #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "opal/optimizer"
5
-
6
- print Opal::Optimizer.new(ARGV[0] ? File.read(ARGV[0]) : STDIN.read).optimize
2
+ load __dir__+"/../exe/opal-optimizer"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "opal/optimizer"
5
+
6
+ print Opal::Optimizer.new(ARGF.read).optimize
@@ -11,8 +11,12 @@ class CollapseStubs < Step
11
11
 
12
12
  nodes = corelib_calls["add_stubs"] || []
13
13
  nodes.each do |node|
14
- stubs += node.arguments.value.first.value.map do |i|
15
- i.value.value.gsub(/['"]/, '')
14
+ if opal_version >= 1.4
15
+ stubs += node.arguments.value.first.value[1..-2].split(',').map(&"$".method(:+))
16
+ else
17
+ stubs += node.arguments.value.first.value.map do |i|
18
+ i.value.value.gsub(/['"]/, '')
19
+ end
16
20
  end
17
21
 
18
22
  node.destroy! "CollapseStubs"
@@ -25,12 +29,13 @@ class CollapseStubs < Step
25
29
 
26
30
  stubs -= ["$respond_to_missing?"] if opal_version >= 1.1
27
31
 
28
- new_stub_code = <<~end
29
- var stubs = '#{stubs.to_a.join('#')}'.split('#'), stubs_obj = {};
32
+
33
+ new_stub_code = <<~JAVASCRIPT
34
+ var stubs = '#{stubs.to_a.join(',')}'.split(','), stubs_obj = {};
30
35
  for (var i = 0; i < stubs.length; i++)
31
36
  stubs_obj[stubs[i]] = {value: Opal.stub_for(stubs[i]), enumerable: false, configurable: true, writable: true};
32
37
  Object.defineProperties(Opal.BasicObject.$$prototype, stubs_obj);
33
- end
38
+ JAVASCRIPT
34
39
 
35
40
  new_stub_code = parse_js(new_stub_code)
36
41
 
@@ -12,9 +12,10 @@ class TreeShaking < Step
12
12
  method_defs = corelib_calls["def"] +
13
13
  corelib_calls["defs"] +
14
14
  corelib_calls["defn"] +
15
+ corelib_calls["udef"] +
15
16
  aliases.keys
16
17
 
17
- method_calls = Set.new
18
+ method_calls = Set.new(['$method_missing'])
18
19
 
19
20
  method_calls += ( function_calls + exports.function_calls ).map do |i|
20
21
  out = if i.value_path?(DotAccessorNode) && i.value.accessor.start_with?("$")
@@ -23,8 +24,12 @@ class TreeShaking < Step
23
24
  StringNode === i.value.accessor &&
24
25
  i.value.accessor.value[1] == '$'
25
26
  i.value.accessor.value[1..-2]
26
- elsif i.value_path?(ResolveNode, "$send")
27
- old = i.arguments.value[1]
27
+ elsif i.value_path?(ResolveNode, ->(i) { %w[$send $send2 $refined_send].include? i })
28
+ if i.value.value == '$send'
29
+ old = i.arguments.value[1]
30
+ else
31
+ old = i.arguments.value[2]
32
+ end
28
33
  "$" + old.value[1..-2] if StringNode === old
29
34
  end
30
35
 
@@ -44,16 +49,30 @@ class TreeShaking < Step
44
49
  method_calls += aliases.values
45
50
 
46
51
  # Protected methods
47
- method_calls += ["$register", "$negative?"]
52
+ method_calls += []
48
53
 
49
54
  removed = Set.new
50
55
 
51
56
  method_defs.each do |m|
52
57
  name = m.arguments.value[1]
53
- next unless StringNode === name
54
-
55
- name = name.value[1..-2]
56
- name = "$" + name if m.value.accessor == "alias"
58
+ case name
59
+ when AddNode
60
+ if StringNode === name.left && StringNode === name.value
61
+ name = name.left.value[1..-2] + name.value.value[1..-2]
62
+ else
63
+ next
64
+ end
65
+ when StringNode
66
+ name = name.value[1..-2]
67
+ case m.value
68
+ when ResolveNode
69
+ name = "$" + name if m.value.value == "$alias"
70
+ when DotAccessorNode
71
+ name = "$" + name if m.value.accessor == "alias"
72
+ end
73
+ else
74
+ next
75
+ end
57
76
 
58
77
  next if method_calls.include? name
59
78
 
@@ -62,8 +81,17 @@ class TreeShaking < Step
62
81
  end
63
82
 
64
83
  corelib_calls["add_stubs"].each do |stubcall|
65
- stubcall.arguments.value.first.value.reject! do |i|
66
- removed.include? i.value.value[1..-2]
84
+ if opal_version >= 1.4
85
+ stubs = stubcall.arguments.value.first.value
86
+ new_stubs = stubs[1..-2].split(",").reject do |i|
87
+ removed.include? i[1..-1]
88
+ end.join(',')
89
+ new_stubs = "'#{new_stubs}'"
90
+ stubs.replace(new_stubs)
91
+ else
92
+ stubcall.arguments.value.first.value.reject! do |i|
93
+ removed.include? i.value.value[1..-2]
94
+ end
67
95
  end
68
96
  end
69
97
 
@@ -73,7 +101,7 @@ class TreeShaking < Step
73
101
  def run
74
102
  loop do
75
103
  removed = shake_methods
76
- #$stdout.puts removed.inspect
104
+ # $stdout.puts removed.inspect
77
105
  reload
78
106
  break if removed.length == 0
79
107
  end
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  class Optimizer
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.6"
4
4
  end
5
5
  end
@@ -26,7 +26,11 @@ module Opal
26
26
  elsif es.start_with?("(function(global_object) {\n \"use strict\";\n\n // @note\n "\
27
27
  "// A few conventions for the documentation of this file:") &&
28
28
  es.end_with?("TypeError.$$super = Error;\n}).call(this);")
29
- @opal_version = 1.1
29
+ if es.include? 'function $prop(object,'
30
+ @opal_version = 1.4
31
+ else
32
+ @opal_version = 1.1
33
+ end
30
34
  end
31
35
  end
32
36
 
@@ -46,8 +50,9 @@ module Opal
46
50
  def reload
47
51
  @function_calls = ast.pointcut(FunctionCallNode).matches
48
52
  @corelib_calls = @function_calls.select do |i|
49
- i.value_path?(DotAccessorNode, ResolveNode, "Opal")
50
- end.group_by { |i| i.value.accessor }
53
+ i.value_path?(DotAccessorNode, ResolveNode, "Opal") ||
54
+ i.value_path?(ResolveNode, /\A\$/)
55
+ end.group_by { |i| (i.value.value.is_a?(String) ? i.value.value : i.value.accessor).gsub('$', '') }
51
56
  @corelib_calls = Hash.new { [] }.merge(@corelib_calls)
52
57
  end
53
58
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-optimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - hmdne
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-26 00:00:00.000000000 Z
11
+ date: 2021-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rkelly-turbo
@@ -40,21 +40,24 @@ dependencies:
40
40
  version: 1.0.0
41
41
  description:
42
42
  email: []
43
- executables: []
43
+ executables:
44
+ - opal-optimizer
44
45
  extensions: []
45
46
  extra_rdoc_files: []
46
47
  files:
48
+ - ".github/workflows/build.yml"
47
49
  - ".gitignore"
48
50
  - ".rspec"
49
51
  - ".travis.yml"
52
+ - CHANGELOG.md
50
53
  - Gemfile
51
- - Gemfile.lock
52
54
  - LICENSE.txt
53
55
  - README.md
54
56
  - Rakefile
55
57
  - bin/console
56
58
  - bin/opal-optimizer
57
59
  - bin/setup
60
+ - exe/opal-optimizer
58
61
  - lib/opal/optimizer.rb
59
62
  - lib/opal/optimizer/helpers.rb
60
63
  - lib/opal/optimizer/sprockets.rb
@@ -85,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
88
  - !ruby/object:Gem::Version
86
89
  version: '0'
87
90
  requirements: []
88
- rubygems_version: 3.2.15
91
+ rubygems_version: 3.2.22
89
92
  signing_key:
90
93
  specification_version: 4
91
94
  summary: Optimize Opal's resulting javascript code
data/Gemfile.lock DELETED
@@ -1,85 +0,0 @@
1
- PATH
2
- remote: ../opal-browser
3
- specs:
4
- opal-browser (0.2.0)
5
- opal (>= 1.0, < 2.0)
6
- paggio
7
-
8
- PATH
9
- remote: ../opal-sprockets
10
- specs:
11
- opal-sprockets (1.0.0)
12
- opal (>= 1.0, < 1.2)
13
- sprockets (~> 4.0)
14
- tilt (>= 1.4)
15
-
16
- PATH
17
- remote: ../opal
18
- specs:
19
- opal (1.1.1)
20
- ast (>= 2.3.0)
21
- parser (~> 3.0)
22
-
23
- PATH
24
- remote: ../rkelly-turbo
25
- specs:
26
- rkelly-turbo (0.1.1.20210526224136)
27
-
28
- PATH
29
- remote: .
30
- specs:
31
- opal-optimizer (0.1.2)
32
- opal (>= 1.0.0)
33
- rkelly-turbo
34
-
35
- GEM
36
- remote: https://rubygems.org/
37
- specs:
38
- ast (2.4.2)
39
- coderay (1.1.3)
40
- concurrent-ruby (1.1.8)
41
- diff-lcs (1.4.4)
42
- method_source (1.0.0)
43
- paggio (0.2.6)
44
- parser (3.0.1.1)
45
- ast (~> 2.4.1)
46
- pry (0.13.1)
47
- coderay (~> 1.1)
48
- method_source (~> 1.0)
49
- rack (2.2.3)
50
- rake (12.3.3)
51
- rspec (3.9.0)
52
- rspec-core (~> 3.9.0)
53
- rspec-expectations (~> 3.9.0)
54
- rspec-mocks (~> 3.9.0)
55
- rspec-core (3.9.2)
56
- rspec-support (~> 3.9.3)
57
- rspec-expectations (3.9.2)
58
- diff-lcs (>= 1.2.0, < 2.0)
59
- rspec-support (~> 3.9.0)
60
- rspec-mocks (3.9.1)
61
- diff-lcs (>= 1.2.0, < 2.0)
62
- rspec-support (~> 3.9.0)
63
- rspec-support (3.9.3)
64
- ruby-prof (1.4.3)
65
- sprockets (4.0.2)
66
- concurrent-ruby (~> 1.0)
67
- rack (> 1, < 3)
68
- tilt (2.0.10)
69
-
70
- PLATFORMS
71
- ruby
72
-
73
- DEPENDENCIES
74
- opal!
75
- opal-browser!
76
- opal-optimizer!
77
- opal-sprockets!
78
- pry
79
- rake (~> 12.0)
80
- rkelly-turbo!
81
- rspec (~> 3.0)
82
- ruby-prof
83
-
84
- BUNDLED WITH
85
- 2.1.4