optipipe 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee49c3430395ba01e41e7ae659d5c6f0751288b6
4
+ data.tar.gz: 0e9d8b86848478d8293b608e416d989c7ce2f9b4
5
+ SHA512:
6
+ metadata.gz: 3566d85e227cddfabb05a49132805b109c47e1f1c33959fe32c0d4e29d0dac2147dd2743b764c464323eb8c4cf5b055e8cc00a27a4befb2a5ec80958e02de57e
7
+ data.tar.gz: 026474edf7a76374a16b2720e92505db71f794fbc462bb07c750150687951b09deb15f5a2bb33aad7aa8765432b3c15ecd50945222ee92f27a264c6025c671e4
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 TODO: Write your name
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.ja.md ADDED
@@ -0,0 +1,110 @@
1
+ # Optipipe
2
+ Optipipeは、precompile済みのアセットに関して、必要なもののみを読み込む仕組みや、遅延読み込み(onload後等)に簡単に対応できるGemです。
3
+ 具体的には、
4
+
5
+ - 複数のprecompile済みファイルを使用したり、アセットの(主にjavascriptの)依存関係をわかりやすく管理することを前提としたディレクトリ構成
6
+ - controller毎に指定した必要なファイルのみ読み込む仕組み
7
+ - onload後に読み込めれば問題ないファイルに関して、簡単に遅延読み込みを設定できる仕組み
8
+
9
+ を提供します。
10
+
11
+ これらによって、特に`HTTP/2`プロトコルを採用した際の並列読み込みに対して速度最適化を行うことができます。
12
+
13
+ *Read this in other languages: [日本語](README.ja.md)*
14
+
15
+ ## Installation
16
+ Gemfileに以下を追記して、gemを追加して下さい。
17
+
18
+ ```ruby
19
+ gem 'optipipe'
20
+ ```
21
+
22
+ そしてbundle installを行って下さい。
23
+ ```bash
24
+ $ bundle
25
+ ```
26
+
27
+ 最後に以下のコマンドを実行して初期設定を行って下さい。
28
+ ```bash
29
+ $ rails g optipipe:install
30
+ ```
31
+
32
+ ## Configure
33
+ 初期設定のコマンドを叩くことで、
34
+
35
+ ```
36
+ app/assets/
37
+ ├── javascripts
38
+ │   └── optipipe
39
+ │   ├── files
40
+ │   │   ├── example.js
41
+ │   │   └── example_after_onload.js
42
+ │   ├── mappers
43
+ │   │   └── example_mapper.yml
44
+ │   └── precompiles
45
+ │   ├── example_precompile.js
46
+ │   └── example_precompile_after_onload.js
47
+ └── stylesheets
48
+ └── optipipe
49
+ ├── files
50
+ │   └── example.css
51
+ ├── mappers
52
+ │   └── example_mapper.yml
53
+ └── precompiles
54
+ └── example_precompile.css
55
+ ```
56
+
57
+ 上記のような構造でファイルとディレクトリが生成されます。
58
+ 初期で生成されるファイルは全てサンプルなので、書き換えまたは削除を行って下さい。
59
+
60
+ ## BasicUsage
61
+ ### Helper
62
+ layoutのviewファイルで下記のように実行することで、controller毎に指定したアセットファイルが読み込まれます。
63
+
64
+ ```erb
65
+ <%= optipipe_javascript_include_tag %>
66
+ <%= optipipe_stylesheet_link_tag %>
67
+ ```
68
+
69
+ 下記のように当てはまるMapperが無い場合にデフォルトで使用するMapperを指定できます。
70
+ 指定するのはcontroller_pathに指定している値になります。
71
+
72
+ ```erb
73
+ <%= optipipe_javascript_include_tag(default: 'welcome') %>
74
+ ```
75
+
76
+ 引数にHash形式でmapperのオプション条件を下記のように追加できます。
77
+
78
+ ```erb
79
+ <%= optipipe_javascript_include_tag(mapper_conditions: {device: 'pc'}) %>
80
+ ```
81
+ ```yml
82
+ controller_path: 'welcome'
83
+ load_files:
84
+ - example_precompile
85
+ after_onload:
86
+ - example_precompile_after_onload
87
+ device: 'pc'
88
+ ```
89
+
90
+ ### Mapper
91
+ `Mapper`では、controller毎に読み込むprecompile済みのassetを設定できるようになっています。
92
+
93
+ #### controller_path
94
+ controller名を設定する。
95
+ namespase付きのcontrollerの場合は、`/`で区切って指定して下さい。(`ActionController`の`controller_path`の値を使用しています)
96
+
97
+ #### load_files
98
+ 読み込むprecompile済みのassetファイル名を指定します。
99
+ `optipipe/precompiles`配下にあるファイルを指定します。
100
+
101
+ #### after_onload
102
+ javascriptのonload後に読み込むassetファイル名を指定します。
103
+ `optipipe/precompiles`配下にあるファイルを指定します。
104
+ これはjavascripts用のMapperのみの設定です。
105
+
106
+ ## License
107
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
108
+
109
+ ## Author
110
+ [ykogure](https://github.com/ykogure)
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Optipipe
2
+ Optipipe enables you to correspond to load only required assets, load assets lazy, and so on, about precompiled files.
3
+
4
+ Specifically, Optipipe provide following functions.
5
+
6
+ - Structure of directories to manage using some precompiled assets and being easy to understand dependencies of assets (principally javascripts).
7
+ - A funcion that load only required asset files.
8
+ - A funcion that configure lazyloading asset files that have no problem even if load after `onload`.
9
+
10
+ A funcion that load lazy asset files that have no problem even if load after `onload`
11
+
12
+ Optipipe enables you to fix your web application's performance by these funtions, when use `HTTP/2` protocol.
13
+
14
+ *Read this in other languages: [日本語](README.ja.md)*
15
+
16
+ ## Installation
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'optipipe'
21
+ ```
22
+
23
+ And then execute:
24
+ ```bash
25
+ $ bundle
26
+ ```
27
+
28
+ And then run initial settings:
29
+ ```bash
30
+ $ rails g optipipe:install
31
+ ```
32
+
33
+ ## Configure
34
+ Running initial settings create files and directories like following structure.
35
+
36
+ ```
37
+ app/assets/
38
+ ├── javascripts
39
+ │   └── optipipe
40
+ │   ├── files
41
+ │   │   ├── example.js
42
+ │   │   └── example_after_onload.js
43
+ │   ├── mappers
44
+ │   │   └── example_mapper.yml
45
+ │   └── precompiles
46
+ │   ├── example_precompile.js
47
+ │   └── example_precompile_after_onload.js
48
+ └── stylesheets
49
+ └── optipipe
50
+ ├── files
51
+ │   └── example.css
52
+ ├── mappers
53
+ │   └── example_mapper.yml
54
+ └── precompiles
55
+ └── example_precompile.css
56
+ ```
57
+
58
+ All files created initial is sample, please rewriting or delete.
59
+
60
+ ## BasicUsage
61
+ ### Helper
62
+ Run helper method like following in layout's view files, and you can load asset files each controllers.
63
+
64
+ ```erb
65
+ <%= optipipe_javascript_include_tag %>
66
+ <%= optipipe_stylesheet_link_tag %>
67
+ ```
68
+
69
+ You can configure like following using default mapper when there aren't any mappers meeting a condition. This value is controller_path.
70
+
71
+ ```erb
72
+ <%= optipipe_javascript_include_tag(default: 'welcome') %>
73
+ ```
74
+
75
+ You can configure mapper's additional conditions by arguments like following.
76
+
77
+ ```erb
78
+ <%= optipipe_javascript_include_tag(device: 'pc') %>
79
+ ```
80
+ ```yml
81
+ controller_path: 'welcome'
82
+ load_files:
83
+ - example_precompile
84
+ after_onload:
85
+ - example_precompile_after_onload
86
+ device: 'pc'
87
+ ```
88
+
89
+ ### Mapper
90
+ You can configure precompiled asset files each controllers by `Mapper`.
91
+
92
+ #### controller_path
93
+ Please set controller path.
94
+ If use controller with namespace, join `/`. (same value of `controller_path` in `ActionController`.)
95
+
96
+ #### load_files
97
+ Please set precompiled asset file's names.
98
+ Files exist at under `optipipe/precompiles`.
99
+
100
+ #### after_onload
101
+ Please set asset file's names loaded after onload on javascript.
102
+ Files exist at under `optipipe/precompiles`.
103
+ This setting is only Javascript's Mapper.
104
+
105
+ ## License
106
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
107
+
108
+ ## Author
109
+ [ykogure](https://github.com/ykogure)
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Optipipe'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,9 @@
1
+ <script type="text/javascript">
2
+ window.onload = function() {
3
+ <% paths.each do |path| %>
4
+ var script = document.createElement("script");
5
+ script.src = "<%= path %>";
6
+ document.body.appendChild(script);
7
+ <% end %>
8
+ };
9
+ </script>
@@ -0,0 +1,31 @@
1
+ module Optipipe
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Creates a Optipipe initializer to your application."
7
+
8
+ def copy_initializer
9
+ # javascript's templates
10
+ empty_directory 'app/assets/javascripts/optipipe'
11
+ empty_directory 'app/assets/javascripts/optipipe/files'
12
+ empty_directory 'app/assets/javascripts/optipipe/mappers'
13
+ empty_directory 'app/assets/javascripts/optipipe/precompiles'
14
+ template 'files/example.js', 'app/assets/javascripts/optipipe/files/example.js'
15
+ template 'files/example_after_onload.js', 'app/assets/javascripts/optipipe/files/example_after_onload.js'
16
+ template 'precompiles/example_precompile.js', 'app/assets/javascripts/optipipe/precompiles/example_precompile.js'
17
+ template 'precompiles/example_precompile_after_onload.js', 'app/assets/javascripts/optipipe/precompiles/example_precompile_after_onload.js'
18
+ template 'mappers/example_mapper_for_javascripts.yml', 'app/assets/javascripts/optipipe/mappers/example_mapper.yml'
19
+
20
+ # stylesheet's templates
21
+ empty_directory 'app/assets/stylesheets/optipipe'
22
+ empty_directory 'app/assets/stylesheets/optipipe/files'
23
+ empty_directory 'app/assets/stylesheets/optipipe/mappers'
24
+ empty_directory 'app/assets/stylesheets/optipipe/precompiles'
25
+ template 'files/example.css', 'app/assets/stylesheets/optipipe/files/example.css'
26
+ template 'precompiles/example_precompile.css', 'app/assets/stylesheets/optipipe/precompiles/example_precompile.css'
27
+ template 'mappers/example_mapper_for_stylesheets.yml', 'app/assets/stylesheets/optipipe/mappers/example_mapper.yml'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ .example{
2
+ color: red;
3
+ }
@@ -0,0 +1 @@
1
+ console.log('example');
@@ -0,0 +1 @@
1
+ console.log('example_after_onload');
@@ -0,0 +1,5 @@
1
+ controller_path: 'welcome'
2
+ load_files:
3
+ - example_precompile
4
+ after_onload:
5
+ - example_precompile_after_onload
@@ -0,0 +1,3 @@
1
+ controller_path: 'welcome'
2
+ load_files:
3
+ - example_precompile
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require ../files/example
3
+ */
@@ -0,0 +1 @@
1
+ //= require ../files/example
@@ -0,0 +1 @@
1
+ //= require ../files/example_after_onload
@@ -0,0 +1,38 @@
1
+ require 'optipipe/models/mapper'
2
+ require 'optipipe/helpers/tag_helper'
3
+
4
+ module Optipipe
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace Optipipe
7
+
8
+ # Add precompile paths.
9
+ initializer "optipipe.assets.precompile" do |app|
10
+ app.config.assets.precompile = app.config.assets.precompile.map do |precompile_setting|
11
+ next precompile_setting unless precompile_setting.is_a?(Proc)
12
+
13
+ if Rails::VERSION::MAJOR == 3
14
+ lambda do |path|
15
+ precompile_setting.call(path) &&
16
+ !(path =~ /optipipe\/mappers\// &&
17
+ ['.yml'].include?(File.extname(path)))
18
+ end
19
+ else
20
+ lambda do |logical_path, filename|
21
+ precompile_setting.call(logical_path, filename) &&
22
+ !(filename.start_with?(::Rails.root.join("app/assets/optipipe/mappers").to_s) &&
23
+ ['.yml'].include?(File.extname(logical_path)))
24
+ end
25
+ end
26
+ end
27
+ app.config.assets.precompile << 'optipipe/precompiles/*.js'
28
+ app.config.assets.precompile << 'optipipe/precompiles/*.css'
29
+ end
30
+
31
+ # Add helpers
32
+ initializer 'gtm_on_rails.action_view_helpers' do
33
+ ActiveSupport.on_load :action_view do
34
+ include Optipipe::TagHelper
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,69 @@
1
+ module Optipipe
2
+ module TagHelper
3
+ # options
4
+ # mapper_conditions: Limiting conditions when select mapper.
5
+ # default: A mapper used when there aren't any one meeting a condition. This value is controller_path.
6
+ def optipipe_javascript_include_tag(*args)
7
+ # set args
8
+ options = args[0] || {}
9
+ include_tag_options = args[1] || {}
10
+ mapper_conditions = options.delete(:mapper_conditions) || {}
11
+
12
+ mapper_conditions.except!('controller_path', 'load_files', 'after_onload') # Using several key names at mapper_conditions are not allowed.
13
+ mapper = get_mapper(Optipipe::Mapper.get_javascript_mappers(controller.controller_path), mapper_conditions)
14
+
15
+ # default_mapper
16
+ mapper = (Optipipe::Mapper.get_javascript_mappers(options[:default]) || []).first if mapper.blank?
17
+
18
+ return if mapper.blank?
19
+
20
+ script = (mapper['load_files'] || []).map do |file_name|
21
+ javascript_include_tag("optipipe/precompiles/#{file_name}", include_tag_options)
22
+ end.join("\n").html_safe
23
+
24
+ # after onload
25
+ paths = (mapper['after_onload'] || []).map do |file_name|
26
+ asset_path("optipipe/precompiles/#{file_name}")
27
+ end
28
+ script += render(partial: 'optipipe/after_onload', locals: {paths: paths}) if paths.present?
29
+
30
+ script
31
+ end
32
+
33
+ # options
34
+ # mapper_conditions: Limiting conditions when select mapper.
35
+ # default: A mapper used when there aren't any one meeting a condition. This value is controller_path.
36
+ def optipipe_stylesheet_link_tag(*args)
37
+ # set args
38
+ options = args[0] || {}
39
+ include_tag_options = args[1] || {}
40
+ mapper_conditions = options.delete(:mapper_conditions) || {}
41
+
42
+ mapper_conditions.except!('controller_path', 'load_files') # Using several key names at mapper_conditions are not allowed.
43
+ mapper = get_mapper(Optipipe::Mapper.get_stylesheet_mappers(controller.controller_path), mapper_conditions)
44
+
45
+ # default_mapper
46
+ mapper = (Optipipe::Mapper.get_stylesheet_mappers(options[:default]) || []).first if mapper.blank?
47
+
48
+ return if mapper.blank?
49
+
50
+ (mapper['load_files'] || []).map do |file_name|
51
+ stylesheet_link_tag("optipipe/precompiles/#{file_name}", {media: 'all'}.merge(include_tag_options))
52
+ end.join.html_safe
53
+ end
54
+
55
+ private
56
+
57
+ def get_mapper(mappers, conditions = {})
58
+ return if mappers.blank?
59
+
60
+ conditions.each do |key, value|
61
+ mappers.select! do |mapper|
62
+ mapper[key.to_s] == value
63
+ end
64
+ end
65
+
66
+ mappers.first
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,32 @@
1
+ module Optipipe
2
+ class Mapper
3
+ class << self
4
+ def get_javascript_mappers(controller_path)
5
+ get_yaml_data('javascripts')[controller_path]
6
+ end
7
+
8
+ def get_stylesheet_mappers(controller_path)
9
+ get_yaml_data('stylesheets')[controller_path]
10
+ end
11
+
12
+ private
13
+
14
+ def get_yaml_data(type)
15
+ const_name = :"#{type.upcase}_YAML_DATA"
16
+ if self.const_defined?(const_name) && Rails.env.production?
17
+ yaml_data = self.const_get(const_name)
18
+ else
19
+ yaml_data = {}
20
+ Dir.glob(Rails.root.join('app', 'assets', type, 'optipipe', 'mappers', '*')).each do |mapper_yaml|
21
+ data = YAML.load(File.open(mapper_yaml))
22
+ yaml_data[data['controller_path']] ||= []
23
+ yaml_data[data['controller_path']] << data
24
+ end
25
+ self.const_set(const_name, yaml_data)
26
+ end
27
+
28
+ yaml_data
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Optipipe
2
+ VERSION = '0.4.1'
3
+ end
data/lib/optipipe.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "optipipe/engine"
2
+
3
+ module Optipipe
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :optipipe do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optipipe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ platform: ruby
6
+ authors:
7
+ - ykogure
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.22
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.22
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This is an plugin that load precompiled asset files each controllers.
42
+ This plugin is created for optimize to load asset files parallel when had to use
43
+ `HTTP/2` protocol.
44
+ email:
45
+ - renkin1008@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - MIT-LICENSE
51
+ - README.ja.md
52
+ - README.md
53
+ - Rakefile
54
+ - app/views/optipipe/_after_onload.html.erb
55
+ - lib/generators/optipipe/install_generator.rb
56
+ - lib/generators/templates/files/example.css
57
+ - lib/generators/templates/files/example.js
58
+ - lib/generators/templates/files/example_after_onload.js
59
+ - lib/generators/templates/mappers/example_mapper_for_javascripts.yml
60
+ - lib/generators/templates/mappers/example_mapper_for_stylesheets.yml
61
+ - lib/generators/templates/precompiles/example_precompile.css
62
+ - lib/generators/templates/precompiles/example_precompile.js
63
+ - lib/generators/templates/precompiles/example_precompile_after_onload.js
64
+ - lib/optipipe.rb
65
+ - lib/optipipe/engine.rb
66
+ - lib/optipipe/helpers/tag_helper.rb
67
+ - lib/optipipe/models/mapper.rb
68
+ - lib/optipipe/version.rb
69
+ - lib/tasks/optipipe_tasks.rake
70
+ homepage: https://github.com/ZIGExN/optipipe
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.6.11
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: An plugin that load precompiled asset files each controllers.
94
+ test_files: []