hyde-page-js 0.2.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hyde-page-js.rb +43 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71202952cbb7a6f848efb35f52b2567e0f5e71ff4c2bd03cb7900b646f76baf4
|
4
|
+
data.tar.gz: 89a466f9500aab52b11934ba3a7d4983727b8932322bb26142dd36e18c981188
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15e8b455e09fc47ed4c174588c2bd53b3fd38eb83a79fdc94515f7af6737292f3d7be05349e8010cb312243fb76d309e62bdcfe5905265b796c1571f278eaa01
|
7
|
+
data.tar.gz: 3692c60d4d2a9b2511e19db565fc411281bf265465e1c5be0fd0c28d6a37ea2a64ccf61430ee455c47453b253307040cbec83687c2dbb2b80d0bc462bce11964
|
data/lib/hyde-page-js.rb
CHANGED
@@ -2,14 +2,14 @@ require 'terser'
|
|
2
2
|
require 'digest'
|
3
3
|
require 'jekyll'
|
4
4
|
|
5
|
-
Jekyll::Hooks.register :pages, :
|
6
|
-
Hyde::Page::Js.new(page).run
|
5
|
+
Jekyll::Hooks.register :pages, :post_init do |page, payload|
|
6
|
+
Hyde::Page::Js.new(page).run if page.instance_of? Jekyll::Page
|
7
7
|
end
|
8
8
|
|
9
9
|
module Hyde
|
10
10
|
module Page
|
11
11
|
class Js
|
12
|
-
VERSION = "0.2
|
12
|
+
VERSION = "0.3.2"
|
13
13
|
end
|
14
14
|
|
15
15
|
class GeneratedJsFile < Jekyll::StaticFile
|
@@ -55,6 +55,7 @@ module Hyde
|
|
55
55
|
@page = page
|
56
56
|
@site = page.site
|
57
57
|
@config = fetch_config
|
58
|
+
@site.data['_hyde_pages_cache'] ||= {}
|
58
59
|
|
59
60
|
if keep_files? && !dev_mode?
|
60
61
|
@site.config.fetch("keep_files").push(destination)
|
@@ -64,34 +65,53 @@ module Hyde
|
|
64
65
|
def run
|
65
66
|
js = fetch_js(@page)
|
66
67
|
layout = fetch_layout(fetch_layout_name(@page))
|
67
|
-
|
68
|
+
js_groups = parent_layout_js(layout, js).reverse
|
69
|
+
return if js_groups.empty?
|
68
70
|
|
69
|
-
|
70
|
-
|
71
|
+
for group in js_groups
|
72
|
+
lookup_name = names_to_key(group)
|
73
|
+
cache_entry = @site.data['_hyde_pages_cache'].fetch(lookup_name, nil)
|
71
74
|
|
72
|
-
|
73
|
-
|
75
|
+
if cache_entry.nil?
|
76
|
+
data = concatenate_files(group)
|
77
|
+
return if data == ""
|
74
78
|
|
75
|
-
|
79
|
+
data = minify(data)
|
80
|
+
return if data == ""
|
76
81
|
|
77
|
-
|
78
|
-
return unless @site.static_files.find { |static_file| static_file.name == generated_file.name }.nil?
|
82
|
+
generated_file = generate_file(group, data)
|
79
83
|
|
80
|
-
|
81
|
-
|
84
|
+
# file already exists, so skip writing out the data to disk
|
85
|
+
return unless @site.static_files.find { |static_file| static_file.name == generated_file.name }.nil?
|
82
86
|
|
83
|
-
|
84
|
-
|
87
|
+
# place file data into the new file
|
88
|
+
generated_file.file_contents = data
|
85
89
|
|
86
|
-
|
87
|
-
|
90
|
+
# assign static file to list for jekyll to render
|
91
|
+
@site.static_files << generated_file
|
92
|
+
|
93
|
+
# add to cache
|
94
|
+
cache_entry = {
|
95
|
+
"url": generated_file.url,
|
96
|
+
"data": data
|
97
|
+
}
|
98
|
+
@site.data['_hyde_pages_cache'][lookup_name] = cache_entry
|
99
|
+
end
|
100
|
+
|
101
|
+
# assign to site.data.js_files for liquid output
|
102
|
+
add_to_urls(cache_entry.fetch(:url, nil)).compact
|
103
|
+
end
|
88
104
|
end
|
89
105
|
|
90
106
|
private
|
91
107
|
|
108
|
+
def names_to_key(names)
|
109
|
+
names.join('-')
|
110
|
+
end
|
111
|
+
|
92
112
|
def add_to_urls(url)
|
93
|
-
@
|
94
|
-
@
|
113
|
+
@page.data['js_files'] ||= []
|
114
|
+
@page.data['js_files'].push(url)
|
95
115
|
end
|
96
116
|
|
97
117
|
def fetch_config
|
@@ -127,7 +147,7 @@ module Hyde
|
|
127
147
|
end
|
128
148
|
|
129
149
|
def fetch_js(obj_with_data, default = [])
|
130
|
-
obj_with_data.data.fetch('js', [])
|
150
|
+
[obj_with_data.data.fetch('js', [])]
|
131
151
|
end
|
132
152
|
|
133
153
|
def fetch_layout(layout_name, default = nil)
|
@@ -144,7 +164,7 @@ module Hyde
|
|
144
164
|
|
145
165
|
def parent_layout_js(layout, js)
|
146
166
|
if layout.nil?
|
147
|
-
return js
|
167
|
+
return js
|
148
168
|
end
|
149
169
|
|
150
170
|
layout_name = fetch_layout_name(layout)
|
@@ -172,6 +192,8 @@ module Hyde
|
|
172
192
|
end
|
173
193
|
|
174
194
|
def minify(data)
|
195
|
+
return data unless minify?
|
196
|
+
|
175
197
|
converter_config = {mangle: mangle?, output: {comments: :copyright}}
|
176
198
|
js_converter = Terser.new(converter_config)
|
177
199
|
js_converter.compile(data)
|