web_components_rails 2.0.0 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/lib/web_components_rails/html_bundle_processor.rb +81 -0
- data/lib/web_components_rails/html_import_processor.rb +24 -17
- data/lib/web_components_rails/railtie.rb +2 -1
- data/lib/web_components_rails/version.rb +1 -1
- data/lib/web_components_rails.rb +5 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b09f5c09128952911e23c7d01d9c32a15646d442
|
4
|
+
data.tar.gz: 02876ac28c8bcb83da2530d22e814bf613675e7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84aa01e3227cf376f61dc8c4682ccbec450325b014b42946701ea7c9049ba07894ddcc2e4ee9cc776091f3af5407d5991ce0a17d6ab9e371e847587f5ae50990
|
7
|
+
data.tar.gz: be43d982b2c169077db220d449f0be06a91d33ddcb6e3f11a6ff009baab445a9274dc4ab2ce28a02828ac22ad1886aea5509fdb768df91aae5f41f36f4ee44dd
|
data/README.md
CHANGED
@@ -18,6 +18,15 @@ gem 'web_components_rails'
|
|
18
18
|
<%= html_import_tag 'my_component' %>
|
19
19
|
```
|
20
20
|
|
21
|
+
### Optimized JavaScript
|
22
|
+
|
23
|
+
If you'd like the scripts in your HTML bundles to be combined, minified using Uglifier, and data-src encoded for performance:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# In an initializer or somewhere on rails load
|
27
|
+
WebComponentsRails.optimize_scripts = true
|
28
|
+
```
|
29
|
+
|
21
30
|
## Building
|
22
31
|
|
23
32
|
After adding new features or fixes, please update the version in version.rb, and then tag the repo appropriately:
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
require 'uglifier'
|
3
|
+
|
4
|
+
# Compresses HTML bundles
|
5
|
+
class WebComponentsRails::HTMLBundleProcessor
|
6
|
+
|
7
|
+
VERSION = '1'
|
8
|
+
|
9
|
+
def self.instance
|
10
|
+
@instance ||= new
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.call(input)
|
14
|
+
instance.call(input)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.cache_key
|
18
|
+
instance.cache_key
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :cache_key
|
22
|
+
|
23
|
+
def initialize(options = {})
|
24
|
+
@cache_key = [self.class.name, VERSION, options, WebComponentsRails.optimize_scripts].freeze
|
25
|
+
end
|
26
|
+
|
27
|
+
def call(input)
|
28
|
+
# Sprockets::Environment for looking up assets, etc.
|
29
|
+
@environment = input[:environment]
|
30
|
+
@context = @environment.context_class.new(input)
|
31
|
+
@data = input[:data]
|
32
|
+
|
33
|
+
@data = process_bundle(@data)
|
34
|
+
|
35
|
+
@context.metadata.merge(data: @data)
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
def process_bundle(html)
|
42
|
+
# Don't do anything if we have optimization off
|
43
|
+
return html unless !!WebComponentsRails.optimize_scripts
|
44
|
+
|
45
|
+
doc = Nokogiri::HTML5.fragment(html)
|
46
|
+
|
47
|
+
combined_js = ''
|
48
|
+
|
49
|
+
doc.css('script:not([src])').map do |script|
|
50
|
+
combined_js << script.content
|
51
|
+
combined_js << "\n"
|
52
|
+
script.remove
|
53
|
+
end
|
54
|
+
|
55
|
+
combined_script_tag = optimized_script_tag(doc, combined_js)
|
56
|
+
|
57
|
+
body = doc.at_css('html body')
|
58
|
+
if body
|
59
|
+
body << combined_script_tag
|
60
|
+
else
|
61
|
+
doc << combined_script_tag
|
62
|
+
end
|
63
|
+
|
64
|
+
WebComponentsRails::HTMLImportProcessor.doc_to_html(doc)
|
65
|
+
end
|
66
|
+
|
67
|
+
def optimized_script_tag(doc, raw_js)
|
68
|
+
combined_script_tag = Nokogiri::XML::Element.new('script', doc)
|
69
|
+
|
70
|
+
# Assume uglifier is fine with defaults for minification
|
71
|
+
minified_js = Uglifier.compile(raw_js)
|
72
|
+
|
73
|
+
# Data-URI encode the JS (helps a lot in browsers like IE)
|
74
|
+
inlined_js = 'data:text/javascript;base64,'
|
75
|
+
inlined_js << Base64.strict_encode64(minified_js).gsub('=', '%3D')
|
76
|
+
combined_script_tag['src'] = inlined_js
|
77
|
+
|
78
|
+
combined_script_tag
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -20,6 +20,29 @@ class WebComponentsRails::HTMLImportProcessor
|
|
20
20
|
instance.cache_key
|
21
21
|
end
|
22
22
|
|
23
|
+
def self.doc_to_html(doc)
|
24
|
+
# Nokogiri/Nokogumbo are hard-coded to URI-escape certain attributes (src, href, action, and a[name]),
|
25
|
+
# so we have to put in placeholders, and fix the values in the HTML string output afterwards
|
26
|
+
# This doesn't work so well with framework-specific syntax (eg. <foo src="{{bar}}">)
|
27
|
+
placeholder_mapping = {}
|
28
|
+
%w(src href action).each do |name|
|
29
|
+
doc.css("[#{name}]").each do |node|
|
30
|
+
# The placeholders are just random strings
|
31
|
+
placeholder = SecureRandom.hex(40)
|
32
|
+
attr = node.attributes[name]
|
33
|
+
placeholder_mapping[placeholder] = attr.value
|
34
|
+
attr.value = placeholder
|
35
|
+
end
|
36
|
+
end
|
37
|
+
new_html = doc.to_html
|
38
|
+
placeholder_mapping.each do |placeholder, value|
|
39
|
+
new_html.sub!(placeholder, value)
|
40
|
+
end
|
41
|
+
|
42
|
+
new_html
|
43
|
+
end
|
44
|
+
|
45
|
+
|
23
46
|
attr_reader :cache_key
|
24
47
|
|
25
48
|
def initialize(options = {})
|
@@ -112,23 +135,7 @@ class WebComponentsRails::HTMLImportProcessor
|
|
112
135
|
end
|
113
136
|
end
|
114
137
|
|
115
|
-
|
116
|
-
# so we have to put in placeholders, and fix the values in the HTML string output afterwards
|
117
|
-
# This doesn't work so well with framework-specific syntax (eg. <foo src="{{bar}}">)
|
118
|
-
placeholder_mapping = {}
|
119
|
-
%w(src href action).each do |name|
|
120
|
-
doc.css("[#{name}]").each do |node|
|
121
|
-
# The placeholders are just random strings
|
122
|
-
placeholder = SecureRandom.hex(40)
|
123
|
-
attr = node.attributes[name]
|
124
|
-
placeholder_mapping[placeholder] = attr.value
|
125
|
-
attr.value = placeholder
|
126
|
-
end
|
127
|
-
end
|
128
|
-
new_html = doc.to_html
|
129
|
-
placeholder_mapping.each do |placeholder, value|
|
130
|
-
new_html.sub!(placeholder, value)
|
131
|
-
end
|
138
|
+
new_html = self.class.doc_to_html(doc)
|
132
139
|
|
133
140
|
[new_html, dependencies]
|
134
141
|
end
|
@@ -21,7 +21,8 @@ class WebComponentsRails::Railtie < Rails::Railtie
|
|
21
21
|
env.register_mime_type 'text/html', extensions: ['.html', '.haml']
|
22
22
|
env.register_preprocessor 'text/html', Sprockets::DirectiveProcessor
|
23
23
|
env.register_preprocessor 'text/html', WebComponentsRails::HTMLImportProcessor
|
24
|
-
env.register_bundle_processor 'text/html',
|
24
|
+
env.register_bundle_processor 'text/html', Sprockets::Bundle
|
25
|
+
env.register_bundle_processor 'text/html', WebComponentsRails::HTMLBundleProcessor
|
25
26
|
end
|
26
27
|
end
|
27
28
|
end
|
data/lib/web_components_rails.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
+
require 'active_support'
|
1
2
|
require 'nokogumbo'
|
2
3
|
require 'sprockets'
|
3
4
|
|
4
5
|
module WebComponentsRails
|
6
|
+
|
7
|
+
mattr_accessor :optimize_scripts
|
8
|
+
|
5
9
|
end
|
6
10
|
|
7
11
|
require 'web_components_rails/asset_tag_helper'
|
12
|
+
require 'web_components_rails/html_bundle_processor'
|
8
13
|
require 'web_components_rails/html_import_processor'
|
9
14
|
require 'web_components_rails/railtie'
|
10
15
|
require 'web_components_rails/version'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: web_components_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Botelho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogumbo
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: uglifier
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.0.0
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,6 +96,7 @@ files:
|
|
82
96
|
- README.md
|
83
97
|
- lib/web_components_rails.rb
|
84
98
|
- lib/web_components_rails/asset_tag_helper.rb
|
99
|
+
- lib/web_components_rails/html_bundle_processor.rb
|
85
100
|
- lib/web_components_rails/html_import_processor.rb
|
86
101
|
- lib/web_components_rails/railtie.rb
|
87
102
|
- lib/web_components_rails/version.rb
|
@@ -104,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
119
|
version: '0'
|
105
120
|
requirements: []
|
106
121
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.4.8
|
108
123
|
signing_key:
|
109
124
|
specification_version: 4
|
110
125
|
summary: Web components utils for rails
|