roda-assets_manifest 0.1.0 → 0.1.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 +4 -4
- data/lib/roda/assets_manifest.rb +1 -0
- data/lib/roda/plugins/assets_manifest.rb +96 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2b2b362aa226ba858aec860220456bed7f85be41e9056a913fa9a5ce5570766a
|
|
4
|
+
data.tar.gz: 71930327f96243c284ceefea5abffb8eb7783ec43b6c1d74092b99383d5e5d5a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f7efda3174f71568649a054af7f58e6f43d3517cb27f5dd043d5052a0751544a356c9292fad038eff181687c16a0117aaf0606ec2c64be5ca840ee45a1c5dccb
|
|
7
|
+
data.tar.gz: 075053ae4a259f7d8cc91eff7ec4e932d64b870e0fd90fd69ca9b706440071797d1419b74e9fdbc109cfc474102b3de1600f7690f0f237e634b62c1141b90939
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require_relative "plugins/assets_manifest"
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
class Roda
|
|
6
|
+
module RodaPlugins
|
|
7
|
+
# plugin :assets_manifest,
|
|
8
|
+
# host: { development: "/public/assets", production: "" },
|
|
9
|
+
# host: "/public/assets"
|
|
10
|
+
# location: "../../public/assets/manifest.json",
|
|
11
|
+
# manifest: proc do
|
|
12
|
+
# {
|
|
13
|
+
# "app.js" => "app-123.js"
|
|
14
|
+
# }
|
|
15
|
+
# end,
|
|
16
|
+
# is_production: ENV["RACK_ENV"] == "production"
|
|
17
|
+
#
|
|
18
|
+
# js_entrypoint_tag('/name/someting') # /public/assets/name/something.js and /public/assets/name/something-DIGESTED.js
|
|
19
|
+
|
|
20
|
+
module AssetsManifest
|
|
21
|
+
DEFAULT_HREF_HOST = '/public/assets'
|
|
22
|
+
DEFAULT_MANIFEST_LOCATION = '/public/assets/manifest.json'
|
|
23
|
+
JS_ENTRYPOINT_TAG_CACHE = {}
|
|
24
|
+
CSS_ENTRYPOINT_TAG_CACHE = {}
|
|
25
|
+
|
|
26
|
+
def self.configure(app, opts = {})
|
|
27
|
+
opts[:host] ||= DEFAULT_HREF_HOST
|
|
28
|
+
|
|
29
|
+
if opts[:host].is_a?(String)
|
|
30
|
+
app.opts[:assets_manifest_host_development] ||= opts[:host]
|
|
31
|
+
app.opts[:assets_manifest_host_production] ||= opts[:host]
|
|
32
|
+
elsif opts[:host].is_a?(Hash)
|
|
33
|
+
app.opts[:assets_manifest_host_development] ||= opts.dig(:host, :development) || DEFAULT_HREF_HOST
|
|
34
|
+
app.opts[:assets_manifest_host_production] ||=
|
|
35
|
+
opts.dig(:host, :production) || app.opts[:assets_manifest_host_development]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
app.opts[:assets_manifest_is_production] = if opts.key?(:is_production)
|
|
39
|
+
opts[:is_production]
|
|
40
|
+
else
|
|
41
|
+
ENV["RACK_ENV"] == "production"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
if opts[:manifest].respond_to?(:call)
|
|
45
|
+
app.opts[:assets_manifest] = opts[:manifest].call
|
|
46
|
+
else
|
|
47
|
+
location = File.expand_path((opts[:location] || DEFAULT_MANIFEST_LOCATION), __dir__)
|
|
48
|
+
if File.exist?(location)
|
|
49
|
+
app.opts[:assets_manifest] = JSON.parse(File.read(location))
|
|
50
|
+
app.opts[:assets_version] = app.opts[:assets_manifest].hash.to_s
|
|
51
|
+
else
|
|
52
|
+
app.opts[:assets_manifest] = {}
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
module InstanceMethods
|
|
58
|
+
def js_entrypoint_tag(entrypoint)
|
|
59
|
+
self.class.js_entrypoint_tag(entrypoint)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def css_entrypoint_tag(entrypoint)
|
|
63
|
+
self.class.css_entrypoint_tag(entrypoint)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def assets_version
|
|
67
|
+
self.class.assets_version
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
module ClassMethods
|
|
72
|
+
def assets_version = opts[:assets_version]
|
|
73
|
+
|
|
74
|
+
def js_entrypoint_tag(entrypoint)
|
|
75
|
+
Roda::RodaPlugins::AssetsManifest::JS_ENTRYPOINT_TAG_CACHE[entrypoint] ||=
|
|
76
|
+
if opts[:assets_manifest_is_production]
|
|
77
|
+
"<script type=\"module\" src=\"#{opts[:assets_manifest_host_production]}/#{opts[:assets_manifest]["#{entrypoint}.js"]}\"></script>"
|
|
78
|
+
else
|
|
79
|
+
"<script type=\"module\" src=\"#{opts[:assets_manifest_host_development]}/#{entrypoint}.js\"></script>"
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def css_entrypoint_tag(entrypoint)
|
|
84
|
+
Roda::RodaPlugins::AssetsManifest::CSS_ENTRYPOINT_TAG_CACHE[entrypoint] ||=
|
|
85
|
+
if opts[:assets_manifest_is_production]
|
|
86
|
+
"<link rel=\"stylesheet\" href=\"#{opts[:assets_manifest_host_production]}/#{opts[:assets_manifest]["#{entrypoint}.css"]}\" />"
|
|
87
|
+
else
|
|
88
|
+
"<link rel=\"stylesheet\" href=\"#{opts[:assets_manifest_host_development]}/#{entrypoint}.css\" />"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
register_plugin(:assets_manifest, AssetsManifest)
|
|
95
|
+
end
|
|
96
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: roda-assets_manifest
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Henrique F. Teixeira
|
|
@@ -28,7 +28,9 @@ email:
|
|
|
28
28
|
executables: []
|
|
29
29
|
extensions: []
|
|
30
30
|
extra_rdoc_files: []
|
|
31
|
-
files:
|
|
31
|
+
files:
|
|
32
|
+
- lib/roda/assets_manifest.rb
|
|
33
|
+
- lib/roda/plugins/assets_manifest.rb
|
|
32
34
|
homepage: https://github.com/henrique-ft/roda-assets_manifest
|
|
33
35
|
licenses:
|
|
34
36
|
- MIT
|