-
Notifications
You must be signed in to change notification settings - Fork 759
/
Copy pathwebpacker_manifest_container.rb
94 lines (85 loc) · 2.64 KB
/
webpacker_manifest_container.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
require 'open-uri'
module React
module ServerRendering
# Get a compiled file from Webpacker. It may come from:
#
# - webpack-dev-server
# - compiled pack
class WebpackerManifestContainer
begin
MAJOR, MINOR, PATCH, _ = Bundler.locked_gems.specs.find { |gem_spec| gem_spec.name == 'webpacker' }.version.segments
rescue
MAJOR, MINOR, PATCH, _ = [0,0,0]
end
# This pattern matches the code that initializes the dev-server client.
CLIENT_REQUIRE = %r{__webpack_require__\(.*webpack-dev-server\/client\/index\.js.*\n}
def self.compatible?
!!defined?(Webpacker)
end
if MAJOR < 3
def find_asset(logical_path)
# raises if not found
asset_path = manifest.lookup(logical_path).to_s
if asset_path.start_with?('http')
# Get a file from the webpack-dev-server
dev_server_asset = open(asset_path).read
# Remove `webpack-dev-server/client/index.js` code which causes ExecJS to 💥
dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
dev_server_asset
else
# Read the already-compiled pack:
full_path = file_path(logical_path).to_s
File.read(full_path)
end
end
else
def find_asset(logical_path)
asset_path = Webpacker.manifest.lookup(logical_path).to_s
if Webpacker.dev_server.running?
ds = Webpacker.dev_server
dev_server_asset = open("#{ds.protocol}://#{ds.host_with_port}#{asset_path}").read
dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
dev_server_asset
else
File.read(file_path(logical_path))
end
end
end
if MAJOR < 3
def manifest
Webpacker::Manifest
end
else
def manifest
Webpacker.manifest
end
end
if MAJOR < 3
def config
Webpacker::Configuration
end
else
def config
Webpacker.config
end
end
if (MAJOR == 1 && MINOR >= 2) || MAJOR == 2
def file_path path
manifest.lookup_path(path)
end
elsif MAJOR == 3
def file_path path
::Rails.root.join('public', manifest.lookup(path)[1..-1])
end
else # 1.0 and 1.1 support
def file_path path
File.join(output_path, manifest.lookup(path).split('/')[2..-1])
end
end
def output_path
# Webpack1 /:output/:entry, Webpack3 /public/:output
config.respond_to?(:output_path) ? config.output_path : 'public'
end
end
end
end