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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hyde-page-js.rb +43 -21
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 867063daf19d510599c6474020f8f133c2564628a1eeb72b02a4aeb23a33d4ed
4
- data.tar.gz: ba3b8d908e8e3af80e8d31757bc9a39d28e10fb2408de974ca0fd31978197fad
3
+ metadata.gz: 71202952cbb7a6f848efb35f52b2567e0f5e71ff4c2bd03cb7900b646f76baf4
4
+ data.tar.gz: 89a466f9500aab52b11934ba3a7d4983727b8932322bb26142dd36e18c981188
5
5
  SHA512:
6
- metadata.gz: e8b40488499185989b1155c71c37e18bbb418f9b5017e2dd8b55e0b96600148bb86eab059d6289aaf93ec1eaab0a91052606194c4314f71a0b03b779f8a9d421
7
- data.tar.gz: d716cd8a8d0fb70ec94e3dec9cf4cbf12e66e6fda60f4338967fe52f8834e3d75ae1fd10feb1dbe3e0d3f97c6bdc672bd8194e7e91eae7ebb6088da329288a58
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, :pre_render do |page, payload|
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.0"
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
- results = parent_layout_js(layout, js).reverse
68
+ js_groups = parent_layout_js(layout, js).reverse
69
+ return if js_groups.empty?
68
70
 
69
- data = concatenate_files(results)
70
- return if data == ""
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
- data = minify(data)
73
- return if data == ""
75
+ if cache_entry.nil?
76
+ data = concatenate_files(group)
77
+ return if data == ""
74
78
 
75
- generated_file = generate_file(results, data)
79
+ data = minify(data)
80
+ return if data == ""
76
81
 
77
- # file already exists, so skip writing out the data to disk
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
- # place file data into the new file
81
- generated_file.file_contents = data
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
- # assign static file to list for jekyll to render
84
- @site.static_files << generated_file
87
+ # place file data into the new file
88
+ generated_file.file_contents = data
85
89
 
86
- # assign to site.data.js_files for liquid output
87
- add_to_urls(generated_file.url)
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
- @site.data['js_files'] ||= []
94
- @site.data['js_files'].push(url)
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', []).reverse
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.uniq.compact
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyde-page-js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory Daynes