-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack_helper.rb
76 lines (60 loc) · 1.76 KB
/
webpack_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# manifest.jsonから実体を取り出すhelper
#
# developmentではwebpack-dev-serverから取得
# from: webpack-dev-server --mode=development --hot --port=3035 --contentBase=./public
# productionではpublic/packsから取得
#
# see: https://inside.pixiv.blog/subal/4615
# see: https://numb86-tech.hatenablog.com/entry/2019/01/10/211416
module WebpackHelper
def asset_pack_path(entry, **options)
raise "assets not found => #{entry}" unless manifest.key?(entry)
target = manifest.fetch(entry)
asset_path("#{asset_host}/#{target}", **options)
end
def javascript_pack_tag(entry, **options)
path = asset_pack_path("#{entry}.js")
options = {
src: path,
defer: true
}.merge(options)
options.delete(:defer) if options[:async]
javascript_include_tag '', **options
end
def stylesheet_pack_tag(entry, **options)
path = asset_pack_path("#{entry}.css")
options = {
href: path
}.merge(options)
stylesheet_link_tag '', **options
end
private
def asset_host
Rails.env.production? ? '' : webpack_server
end
# rubocop:disable Rails/HelperInstanceVariable
def manifest
@manifest ||= lambda do
json = Rails.env.production? ? manifest_for_production : manifest_for_development
begin
Oj.load(json)
rescue
{}
end
end.call
@manifest
end
# rubocop:enable Rails/HelperInstanceVariable
def manifest_for_production
file = Rails.root.join('public', 'packs', 'manifest.json')
raise 'manifest not found' unless File.exist?(file)
File.read(file)
end
def manifest_for_development
require 'open-uri'
OpenURI.open_uri("#{webpack_server}/packs/manifest.json").read
end
def webpack_server
"http://#{request.host}:3035"
end
end