Module: ReactOnRails::WebpackerUtils

Defined in:
lib/react_on_rails/webpacker_utils.rb

Class Method Summary collapse

Class Method Details

.bundle_js_uri_from_webpacker(bundle_name) ⇒ Object

This returns either a URL for the webpack-dev-server, non-server bundle or the hashed server bundle if using the same bundle for the client. Otherwise returns a file path.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/react_on_rails/webpacker_utils.rb', line 23

def self.bundle_js_uri_from_webpacker(bundle_name)
  # Note Webpacker 3.4.3 manifest lookup is inside of the public_output_path
  # [2] (pry) ReactOnRails::WebpackerUtils: 0> Webpacker.manifest.lookup("app-bundle.js")
  # "/webpack/development/app-bundle-c1d2b6ab73dffa7d9c0e.js"
  # Next line will throw if the file or manifest does not exist
  hashed_bundle_name = Webpacker.manifest.lookup!(bundle_name)

  # Support for hashing the server-bundle and having that built
  # the webpack-dev-server is provided by the config value
  # "same_bundle_for_client_and_server" where a value of true
  # would mean that the bundle is created by the webpack-dev-server
  is_server_bundle = bundle_name == ReactOnRails.configuration.server_bundle_js_file

  if Webpacker.dev_server.running? && (!is_server_bundle ||
    ReactOnRails.configuration.same_bundle_for_client_and_server)
    "#{Webpacker.dev_server.protocol}://#{Webpacker.dev_server.host_with_port}#{hashed_bundle_name}"
  else
    File.expand_path(File.join("public", hashed_bundle_name)).to_s
  end
end

.check_manifest_not_cachedObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/react_on_rails/webpacker_utils.rb', line 63

def self.check_manifest_not_cached
  return unless using_webpacker? && Webpacker.config.cache_manifest?

  msg = <<-MSG.strip_heredoc
      ERROR: you have enabled cache_manifest in the #{Rails.env} env when using the
      ReactOnRails::TestHelper.configure_rspec_to_compile_assets helper
      To fix this: edit your config/webpacker.yml file and set cache_manifest to false for test.
  MSG
  puts wrap_message(msg)
  exit!
end

.dev_server_running?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/react_on_rails/webpacker_utils.rb', line 14

def self.dev_server_running?
  return false unless using_webpacker?

  Webpacker.dev_server.running?
end

.manifest_exists?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/react_on_rails/webpacker_utils.rb', line 53

def self.manifest_exists?
  Webpacker.config.public_manifest_path.exist?
end

.using_webpacker?Boolean

TODO: V13 code should be cleaned up so that the webpacker gem is required. This check should only be done at startup.

Returns:

  • (Boolean)


7
8
9
10
11
12
# File 'lib/react_on_rails/webpacker_utils.rb', line 7

def self.using_webpacker?
  return @using_webpacker if defined?(@using_webpacker)

  @using_webpacker = ReactOnRails::Utils.gem_available?("webpacker") ||
                     ReactOnRails::Utils.gem_available?("shakapacker")
end

.webpacker_public_output_pathObject



48
49
50
51
# File 'lib/react_on_rails/webpacker_utils.rb', line 48

def self.webpacker_public_output_path
  # Webpacker has the full absolute path of webpacker output files in a Pathname
  Webpacker.config.public_output_path.to_s
end

.webpacker_source_pathObject



44
45
46
# File 'lib/react_on_rails/webpacker_utils.rb', line 44

def self.webpacker_source_path
  Webpacker.config.source_path
end

.webpacker_source_path_explicit?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/react_on_rails/webpacker_utils.rb', line 57

def self.webpacker_source_path_explicit?
  # WARNING: Calling private method `data` on Webpacker::Configuration, lib/webpacker/configuration.rb
  config_webpacker_yml = Webpacker.config.send(:data)
  config_webpacker_yml[:source_path].present?
end