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)


20
21
22
# File 'lib/react/server_rendering/webpacker_manifest_container.rb', line 20

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

Instance Method Details

#configObject



67
68
69
# File 'lib/react/server_rendering/webpacker_manifest_container.rb', line 67

def config
  Webpacker::Configuration
end

#file_path(path) ⇒ Object

1.0 and 1.1 support



85
86
87
# File 'lib/react/server_rendering/webpacker_manifest_container.rb', line 85

def file_path path
  manifest.lookup_path(path)
end

#find_asset(logical_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/react/server_rendering/webpacker_manifest_container.rb', line 25

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

#manifestObject



57
58
59
# File 'lib/react/server_rendering/webpacker_manifest_container.rb', line 57

def manifest
  Webpacker::Manifest
end

#output_pathObject



90
91
92
93
# File 'lib/react/server_rendering/webpacker_manifest_container.rb', line 90

def output_path
  # Webpack1 /:output/:entry, Webpack3 /public/:output
  config.respond_to?(:output_path) ? config.output_path : 'public'
end