jekyll-workbox-plugin 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll-workbox-plugin.rb +97 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 073d7c6b7b494b9a1e4958b22108e998bfcfb40a
4
+ data.tar.gz: 1bc21f65b26ca7a33ae2d3fa7f035f5238711490
5
+ SHA512:
6
+ metadata.gz: d5da36555245318273478130a6744b4103a15bc0bcf4418b75780448bebdaee71533c05f1dafce714e990ff6eb0c2a9ab6a71976d5b171347259aba2fe18e50d
7
+ data.tar.gz: 1a26d9c655caeeaea9dbd90cda12c155a76ad6877ccc042582558a72c2bcdfe276c6259c7a95e7de4859bd3f392f90616eb64cb09d0c25d989b562e1cc0c9cf9
@@ -0,0 +1,97 @@
1
+ class WorkboxHelper
2
+ WORKBOX_VERSION = '3.6.3'
3
+
4
+ def initialize(site, config)
5
+ @site = site
6
+ @config = config
7
+ @sw_filename = @config['sw_dest_filename'] || 'sw.js'
8
+ @sw_src_filepath = @config['sw_src_filepath'] || 'sw.js'
9
+ end
10
+
11
+ def generate_workbox_precache()
12
+ directory = @config['precache_glob_directory'] || '/'
13
+ directory = @site.in_dest_dir(directory)
14
+ patterns = @config['precache_glob_patterns'] || ['**/*.{html,js,css,eot,svg,ttf,woff}']
15
+ ignores = @config['precache_glob_ignores'] || []
16
+ recent_posts_num = @config['precache_recent_posts_num']
17
+
18
+ # according to workbox precache {url: 'main.js', revision: 'xxxx'}
19
+ @precache_list = []
20
+
21
+ # find precache files with glob
22
+ precache_files = []
23
+ patterns.each do |pattern|
24
+ Dir.glob(File.join(directory, pattern)) do |filepath|
25
+ precache_files.push(filepath)
26
+ end
27
+ end
28
+ precache_files = precache_files.uniq
29
+
30
+ # precache recent n posts
31
+ posts_path_url_map = {}
32
+ if recent_posts_num
33
+ precache_files.concat(
34
+ @site.posts.docs
35
+ .reverse.take(recent_posts_num)
36
+ .map do |post|
37
+ posts_path_url_map[post.path] = post.url
38
+ post.path
39
+ end
40
+ )
41
+ end
42
+
43
+ # filter with ignores
44
+ ignores.each do |pattern|
45
+ Dir.glob(File.join(directory, pattern)) do |ignored_filepath|
46
+ precache_files.delete(ignored_filepath)
47
+ end
48
+ end
49
+
50
+ # generate md5 for each precache file
51
+ md5 = Digest::MD5.new
52
+ precache_files.each do |filepath|
53
+ md5.reset
54
+ md5 << File.read(filepath)
55
+ if posts_path_url_map[filepath]
56
+ url = posts_path_url_map[filepath]
57
+ else
58
+ url = filepath.sub(@site.dest, '')
59
+ end
60
+ @precache_list.push({
61
+ url: @site.baseurl.to_s + url,
62
+ revision: md5.hexdigest
63
+ })
64
+ end
65
+ end
66
+
67
+ def write_sw()
68
+ # read the sw.js source file
69
+ sw_js_str = File.read(@site.in_source_dir(@sw_src_filepath))
70
+
71
+ # prepend the import scripts
72
+ sw_js_str = "importScripts('https://storage.googleapis.com/workbox-cdn/releases/#{WorkboxHelper::WORKBOX_VERSION}/workbox-sw.js');\n#{sw_js_str}"
73
+
74
+ # generate precache list and inject it into the sw.js
75
+ precache_list_str = @precache_list.map do |precache_item|
76
+ precache_item.to_json
77
+ end.join(",")
78
+ sw_js_str = sw_js_str.sub(
79
+ "workbox.precaching.precacheAndRoute([])",
80
+ "workbox.precaching.precacheAndRoute([#{precache_list_str}])")
81
+
82
+ # write sw.js
83
+ sw_dest_file = File.new(@site.in_dest_dir(@sw_filename), 'w')
84
+ sw_dest_file.puts(sw_js_str)
85
+ sw_dest_file.close
86
+ end
87
+ end
88
+
89
+ module Jekyll
90
+ Hooks.register :site, :post_write do |site|
91
+ pwa_config = site.config['workbox'] || {}
92
+ sw_helper = WorkboxHelper.new(site, pwa_config)
93
+
94
+ sw_helper.generate_workbox_precache()
95
+ sw_helper.write_sw()
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-workbox-plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benedikt Meurer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This plugin provides workbox support for Jekyll. Generate a service worker
14
+ and provides precache with Google Workbox.
15
+ email: benedikt.meurer@googlemail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/jekyll-workbox-plugin.rb
21
+ homepage: https://github.com/bmeurer/jekyll-workbox-plugin
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.2.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Workbox support for Jekyll.
45
+ test_files: []