opal-optimizer 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bac974d0150297fd084bdc1a40e9eb41d859f2231545d543294de2e3de0ac073
4
+ data.tar.gz: 0cda5d5d3ac92adef47a98e7c82b9c1233e7ff18d218e23bd4838cc812b82ae6
5
+ SHA512:
6
+ metadata.gz: cce52551506f12e9bdf5c0f05ee80df69b6c9c1d3887c57a316a019b108abe0441fb5b9f0a70bf02ef2fe2fe5fff637ec06563639b8e295ad4371b4567b8b2a1
7
+ data.tar.gz: 11460f5faf89d08dc4965d5c788b216b0ef55c16125e0a1ea9a617ac7ce0111d3eb99f8b60334ff0cfc7cedd5f0883573d7583f408087b0f98b35a8e27973d86
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in opal-optimizer.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+
9
+ gem "opal", path: "../opal"
10
+ gem "opal-sprockets", path: "../opal-sprockets"
11
+ gem "opal-browser", path: "../opal-browser"
12
+
13
+ gem "rkelly-remix"
14
+
15
+ gem "pry"
@@ -0,0 +1,79 @@
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 (0.5.0.1.0.4.0)
12
+ opal (~> 1.0.0)
13
+ sprockets (~> 4.0)
14
+ tilt (>= 1.4)
15
+
16
+ PATH
17
+ remote: ../opal
18
+ specs:
19
+ opal (1.0.0)
20
+ ast (>= 2.3.0)
21
+ parser (~> 2.6)
22
+
23
+ PATH
24
+ remote: .
25
+ specs:
26
+ opal-optimizer (0.1.0)
27
+ opal (>= 1.0.0)
28
+ rkelly-remix
29
+
30
+ GEM
31
+ remote: https://rubygems.org/
32
+ specs:
33
+ ast (2.4.1)
34
+ coderay (1.1.3)
35
+ concurrent-ruby (1.1.7)
36
+ diff-lcs (1.4.4)
37
+ method_source (1.0.0)
38
+ paggio (0.2.6)
39
+ parser (2.7.1.4)
40
+ ast (~> 2.4.1)
41
+ pry (0.13.1)
42
+ coderay (~> 1.1)
43
+ method_source (~> 1.0)
44
+ rack (2.2.3)
45
+ rake (12.3.3)
46
+ rkelly-remix (0.0.7)
47
+ rspec (3.9.0)
48
+ rspec-core (~> 3.9.0)
49
+ rspec-expectations (~> 3.9.0)
50
+ rspec-mocks (~> 3.9.0)
51
+ rspec-core (3.9.2)
52
+ rspec-support (~> 3.9.3)
53
+ rspec-expectations (3.9.2)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.9.0)
56
+ rspec-mocks (3.9.1)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.9.0)
59
+ rspec-support (3.9.3)
60
+ sprockets (4.0.2)
61
+ concurrent-ruby (~> 1.0)
62
+ rack (> 1, < 3)
63
+ tilt (2.0.10)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ opal!
70
+ opal-browser!
71
+ opal-optimizer!
72
+ opal-sprockets!
73
+ pry
74
+ rake (~> 12.0)
75
+ rkelly-remix
76
+ rspec (~> 3.0)
77
+
78
+ BUNDLED WITH
79
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Opal::Optimizer
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'opal-optimizer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install opal-optimizer
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/opal-optimizer.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "opal/optimizer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,6 @@
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,61 @@
1
+ require "opal/optimizer/version"
2
+ require "rkelly"
3
+ require "opal/optimizer/helpers"
4
+ require "opal/compiler"
5
+
6
+ require "pry"
7
+
8
+ module Opal
9
+ class Optimizer
10
+ include Helpers
11
+
12
+ attr_accessor :ast, :opal_version, :corelib, :corelib_source, :corelib_calls,
13
+ :function_calls
14
+
15
+ attr_accessor :exports
16
+
17
+ def initialize(code, exports: "")
18
+ @ast = parse_js(code)
19
+
20
+ @corelib = @ast.value.find do |i|
21
+ es = i.to_ecma
22
+ if es.start_with?("(function(undefined) {\n var global_object") &&
23
+ es.end_with?(").call(this);")
24
+ @opal_version = 1.0
25
+ elsif es.start_with?("(function(global_object) {\n \"use strict\";\n"+
26
+ " var console;\n if(typeof (globalThis) !== "+
27
+ "'undefined') {\n") &&
28
+ es.end_with?(").call(this);")
29
+ @opal_version = 1.1
30
+ end
31
+ end
32
+
33
+ @corelib_source = @corelib.value.value.value.value.function_body.value if @corelib
34
+
35
+ reload
36
+
37
+ # Are exports js or do we need to compile them first?
38
+ unless [nil, ""].include?(exports) || exports.start_with?("(function(")
39
+ exports = Opal::Compiler.new(exports).compile
40
+ end
41
+ @exports = Opal::Optimizer.new(exports, exports: nil) unless exports == nil
42
+ end
43
+
44
+ def reload
45
+ @function_calls = ast.pointcut(FunctionCallNode).matches
46
+ @corelib_calls = @function_calls.select do |i|
47
+ i.value_path?(DotAccessorNode, ResolveNode, "Opal")
48
+ end.group_by { |i| i.value.accessor }
49
+ @corelib_calls = Hash.new { [] }.merge(@corelib_calls)
50
+ end
51
+
52
+ def optimize
53
+ Step::TreeShaking.new(self).run
54
+ Step::CollapseStubs.new(self).run
55
+
56
+ @ast.to_ecma
57
+ end
58
+ end
59
+ end
60
+
61
+ require "opal/optimizer/step"
@@ -0,0 +1,35 @@
1
+ module Opal; class Optimizer
2
+
3
+ module Helpers
4
+ include RKelly::Nodes
5
+
6
+ def parse_js(code)
7
+ RKelly.parse(code)
8
+ end
9
+ end
10
+
11
+ end; end
12
+
13
+ class RKelly::Nodes::Node
14
+ def value_path?(*patterns)
15
+ current = self
16
+ patterns.all? do |pattern|
17
+ current = current.value
18
+ pattern === current
19
+ end
20
+ end
21
+
22
+ def accept (visitor, &block)
23
+ if @destroyed_by
24
+ if RKelly::Visitors::ECMAVisitor === visitor
25
+ "/* destroyed: #{@destroyed_by} */0"
26
+ end
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ def destroy! by
33
+ @destroyed_by = by
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ require 'opal/optimizer'
2
+
3
+ require 'opal-sprockets'
4
+
5
+ module Opal; class Optimizer
6
+
7
+ module Sprockets
8
+ def self.call input
9
+ Opal::Optimizer.new(input[:data]).optimize
10
+ end
11
+ end
12
+
13
+ end; end
14
+
15
+ Sprockets.register_bundle_processor 'application/javascript', Opal::Optimizer::Sprockets
@@ -0,0 +1,23 @@
1
+ require 'forwardable'
2
+
3
+ module Opal; class Optimizer
4
+
5
+ class Step
6
+ include Helpers
7
+ extend Forwardable
8
+
9
+ def_delegators :@optimizer, *Optimizer.public_instance_methods(false)
10
+
11
+ def initialize optimizer
12
+ @optimizer = optimizer
13
+ end
14
+
15
+ def run
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+
20
+ end; end
21
+
22
+ require 'opal/optimizer/step/tree_shaking'
23
+ require 'opal/optimizer/step/collapse_stubs'
@@ -0,0 +1,41 @@
1
+ require 'set'
2
+
3
+ module Opal; class Optimizer; class Step
4
+
5
+ class CollapseStubs < Step
6
+ def run
7
+ # We can't collapse when no corelib present.
8
+ return unless corelib
9
+
10
+ stubs = Set.new
11
+
12
+ nodes = corelib_calls["add_stubs"] || []
13
+ nodes.each do |node|
14
+ stubs += node.arguments.value.first.value.map do |i|
15
+ i.value.value.gsub(/['"]/, '')
16
+ end
17
+
18
+ node.destroy! "CollapseStubs"
19
+ end
20
+
21
+ stubs -= ["__send__", "class", "!=", "equal?", "==", "__id__", "!",
22
+ "initialize", "eql?", "instance_eval", "instance_exec",
23
+ "singleton_method_added", "singleton_method_removed",
24
+ "singleton_method_undefined", "method_missing"].map(&"$".method(:+))
25
+
26
+ stubs -= ["$respond_to_missing?"] if opal_version >= 1.1
27
+
28
+ new_stub_code = <<~end
29
+ var stubs = '#{stubs.to_a.join('#')}'.split('#'), stubs_obj = {};
30
+ for (var i = 0; i < stubs.length; i++)
31
+ stubs_obj[stubs[i]] = {value: Opal.stub_for(stubs[i]), enumerable: false, configurable: true, writable: true};
32
+ Object.defineProperties(Opal.BasicObject.$$prototype, stubs_obj);
33
+ end
34
+
35
+ new_stub_code = parse_js(new_stub_code)
36
+
37
+ corelib_source.value += new_stub_code.value
38
+ end
39
+ end
40
+
41
+ end; end; end
@@ -0,0 +1,83 @@
1
+ require "set"
2
+
3
+ module Opal; class Optimizer; class Step
4
+
5
+ class TreeShaking < Step
6
+ def shake_methods
7
+ aliases = corelib_calls["alias"].map do |i|
8
+ old = i.arguments.value[2]
9
+ [i, "$"+old.value[1..-2]] if StringNode === old
10
+ end.compact.to_h
11
+
12
+ method_defs = corelib_calls["def"] +
13
+ corelib_calls["defs"] +
14
+ corelib_calls["defn"] +
15
+ aliases.keys
16
+
17
+ method_calls = Set.new
18
+
19
+ method_calls += ( function_calls + exports.function_calls ).map do |i|
20
+ out = if i.value_path?(DotAccessorNode) && i.value.accessor.start_with?("$")
21
+ i.value.accessor
22
+ elsif i.value_path?(BracketAccessorNode) &&
23
+ StringNode === i.value.accessor &&
24
+ i.value.accessor.value[1] == '$'
25
+ i.value.accessor.value[1..-2]
26
+ elsif i.value_path?(ResolveNode, "$send")
27
+ old = i.arguments.value[1]
28
+ "$" + old.value[1..-2] if StringNode === old
29
+ end
30
+
31
+ out = [out]
32
+
33
+ case out.first
34
+ when /\A\$(public_|private_|protected_)?(class_|instance_|singleton_)?(send|method(_defined\?)?)\z/,
35
+ '$__send__'
36
+
37
+ old = i.arguments.value[0]
38
+ out << "$" + old.value[1..-2] if StringNode === old
39
+ end
40
+
41
+ out
42
+ end.flatten.compact
43
+
44
+ method_calls += aliases.values
45
+
46
+ # Protected methods
47
+ method_calls += ["$register", "$negative?"]
48
+
49
+ removed = Set.new
50
+
51
+ method_defs.each do |m|
52
+ 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"
57
+
58
+ next if method_calls.include? name
59
+
60
+ removed << name
61
+ m.destroy! "TreeShaking#shake_methods/#{name}"
62
+ end
63
+
64
+ corelib_calls["add_stubs"].each do |stubcall|
65
+ stubcall.arguments.value.first.value.reject! do |i|
66
+ removed.include? i.value.value[1..-2]
67
+ end
68
+ end
69
+
70
+ removed
71
+ end
72
+
73
+ def run
74
+ loop do
75
+ removed = shake_methods
76
+ #$stdout.puts removed.inspect
77
+ reload
78
+ break if removed.length == 0
79
+ end
80
+ end
81
+ end
82
+
83
+ end; end; end
@@ -0,0 +1,5 @@
1
+ module Opal
2
+ class Optimizer
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/opal/optimizer/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "opal-optimizer"
5
+ spec.version = Opal::Optimizer::VERSION
6
+ spec.authors = ["hmdne"]
7
+ spec.email = []
8
+
9
+ spec.summary = %q{Optimize Opal's resulting javascript code}
10
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
11
+ spec.homepage = "https://github.com/hmdne/opal-optimizer"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/hmdne/opal-optimizer"
17
+ spec.metadata["changelog_uri"] = "https://github.com/hmdne/opal-optimizer/commits/master"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "rkelly-remix"
29
+ spec.add_dependency "opal", ">= 1.0.0"
30
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opal-optimizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - hmdne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rkelly-remix
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: opal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ description:
42
+ email: []
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - ".rspec"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/opal-optimizer
57
+ - bin/setup
58
+ - lib/opal/optimizer.rb
59
+ - lib/opal/optimizer/helpers.rb
60
+ - lib/opal/optimizer/sprockets.rb
61
+ - lib/opal/optimizer/step.rb
62
+ - lib/opal/optimizer/step/collapse_stubs.rb
63
+ - lib/opal/optimizer/step/tree_shaking.rb
64
+ - lib/opal/optimizer/version.rb
65
+ - opal-optimizer.gemspec
66
+ homepage: https://github.com/hmdne/opal-optimizer
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/hmdne/opal-optimizer
71
+ source_code_uri: https://github.com/hmdne/opal-optimizer
72
+ changelog_uri: https://github.com/hmdne/opal-optimizer/commits/master
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.3.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.1.4
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Optimize Opal's resulting javascript code
92
+ test_files: []