This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
manifest.rb
102 lines (88 loc) · 3.58 KB
/
manifest.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'net/http'
require 'uri'
module Webpack
module Rails
# Webpack manifest loading, caching & entry point retrieval
class Manifest
# Raised if we can't read our webpack manifest for whatever reason
class ManifestLoadError < StandardError
def initialize(message, orig)
super "#{message} (original error #{orig})"
end
end
# Raised if webpack couldn't build one of your entry points
class WebpackError < StandardError
def initialize(errors)
super "Error in webpack compile, details follow below:\n#{errors.join("\n\n")}"
end
end
# Raised if a supplied entry point does not exist in the webpack manifest
class EntryPointMissingError < StandardError
end
class << self
# :nodoc:
def asset_paths(source)
raise WebpackError, manifest["errors"] unless manifest_bundled?
paths = manifest["assetsByChunkName"][source]
if paths
# Can be either a string or an array of strings.
# Do not include source maps as they are not javascript
[paths].flatten.reject { |p| p =~ /.*\.map$/ }.map do |p|
"/#{::Rails.configuration.webpack.public_path}/#{p}"
end
else
raise EntryPointMissingError, "Can't find entry point '#{source}' in webpack manifest"
end
end
private
def manifest_bundled?
!manifest["errors"].any? { |error| error.include? "Module build failed" }
end
def manifest
if ::Rails.configuration.webpack.dev_server.enabled
# Don't cache if we're in dev server mode, manifest may change ...
load_manifest
else
# ... otherwise cache at class level, as JSON loading/parsing can be expensive
@manifest ||= load_manifest
end
end
def load_manifest
data = if ::Rails.configuration.webpack.dev_server.enabled
load_dev_server_manifest
else
load_static_manifest
end
JSON.parse(data)
end
def load_dev_server_manifest
host = ::Rails.configuration.webpack.dev_server.manifest_host
port = ::Rails.configuration.webpack.dev_server.manifest_port
http = Net::HTTP.new(host, port)
http.use_ssl = ::Rails.configuration.webpack.dev_server.https
http.verify_mode = ::Rails.configuration.webpack.dev_server.https_verify_peer ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
http.get(dev_server_path).body
rescue => e
raise ManifestLoadError.new("Could not load manifest from webpack-dev-server at http://#{host}:#{port}#{dev_server_path} - is it running, and is stats-webpack-plugin loaded?", e)
end
def load_static_manifest
File.read(static_manifest_path)
rescue => e
raise ManifestLoadError.new("Could not load compiled manifest from #{static_manifest_path} - have you run `rake webpack:compile`?", e)
end
def static_manifest_path
::Rails.root.join(
::Rails.configuration.webpack.output_dir,
::Rails.configuration.webpack.manifest_filename
)
end
def dev_server_path
"/#{::Rails.configuration.webpack.public_path}/#{::Rails.configuration.webpack.manifest_filename}"
end
def dev_server_url
"http://#{::Rails.configuration.webpack.dev_server.host}:#{::Rails.configuration.webpack.dev_server.port}#{dev_server_path}"
end
end
end
end
end