Class: React::ServerRendering::WebpackerManifestContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/react/server_rendering/webpacker_manifest_container.rb

Overview

Get a compiled file from Webpacker. It may come from:

  • webpack-dev-server

  • compiled pack

Constant Summary collapse

CLIENT_REQUIRE =

This pattern matches the code that initializes the dev-server client.

%r{__webpack_require__\(.*webpack-dev-server\/client\/index\.js.*\n}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compatible?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/react/server_rendering/webpacker_manifest_container.rb', line 29

def self.compatible?
  !!defined?(Webpacker)
end

Instance Method Details

#find_asset(logical_path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/react/server_rendering/webpacker_manifest_container.rb', line 13

def find_asset(logical_path)
  # raises if not found
  asset_path = Webpacker::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 = Webpacker::Manifest.lookup_path(logical_path).to_s
    File.read(full_path)
  end
end