-
Notifications
You must be signed in to change notification settings - Fork 756
/
Copy pathwebpacker_manifest_container.rb
38 lines (34 loc) · 1.16 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
require "open-uri"
module React
module ServerRendering
# Get a compiled file from Webpacker. It may come from:
#
# - webpack-dev-server
# - compiled pack
class WebpackerManifestContainer
# This pattern matches the code that initializes the dev-server client.
CLIENT_REQUIRE = %r{__webpack_require__\(.*webpack-dev-server\/client\/index\.js.*\n}
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 = manifest.lookup_path(logical_path).to_s
File.read(full_path)
end
end
def manifest
Webpacker.respond_to?(:manifest) ? Webpacker.manifest : Webpacker::Manifest
end
def self.compatible?
!!defined?(Webpacker)
end
end
end
end