Module: ReactOnRails::PackerUtils

Defined in:
lib/react_on_rails/packer_utils.rb

Class Method Summary collapse

Class Method Details

.asset_uri_from_packer(asset_name) ⇒ Object

The function doesn’t ensure that the asset exists.

  • It just returns url to the asset if dev server is running

  • Otherwise it returns file path to the asset



76
77
78
79
80
81
82
# File 'lib/react_on_rails/packer_utils.rb', line 76

def self.asset_uri_from_packer(asset_name)
  if dev_server_running?
    "#{dev_server_url}/#{public_output_uri_path}#{asset_name}"
  else
    File.join(packer_public_output_path, asset_name).to_s
  end
end

.bundle_js_uri_from_packer(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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/react_on_rails/packer_utils.rb', line 51

def self.bundle_js_uri_from_packer(bundle_name)
  hashed_bundle_name = ::Shakapacker.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_bundle_running_on_server = (bundle_name == ReactOnRails.configuration.server_bundle_js_file) ||
                                (bundle_name == ReactOnRails.configuration.rsc_bundle_js_file)

  if ::Shakapacker.dev_server.running? && (!is_bundle_running_on_server ||
    ReactOnRails.configuration.same_bundle_for_client_and_server)
    "#{dev_server_url}#{hashed_bundle_name}"
  else
    File.expand_path(File.join("public", hashed_bundle_name)).to_s
  end
end

.check_manifest_not_cachedObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/react_on_rails/packer_utils.rb', line 112

def self.check_manifest_not_cached
  return unless ::Shakapacker.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/shakapacker.yml file and set cache_manifest to false for test.
  MSG
  puts wrap_message(msg)
  exit!
end

.dev_server_running?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/react_on_rails/packer_utils.rb', line 7

def self.dev_server_running?
  Shakapacker.dev_server.running?
end

.dev_server_urlObject



11
12
13
# File 'lib/react_on_rails/packer_utils.rb', line 11

def self.dev_server_url
  "#{Shakapacker.dev_server.protocol}://#{Shakapacker.dev_server.host_with_port}"
end

.manifest_exists?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/react_on_rails/packer_utils.rb', line 104

def self.manifest_exists?
  ::Shakapacker.config.public_manifest_path.exist?
end

.nested_entries?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/react_on_rails/packer_utils.rb', line 96

def self.nested_entries?
  ::Shakapacker.config.nested_entries?
end

.packer_public_output_pathObject



100
101
102
# File 'lib/react_on_rails/packer_utils.rb', line 100

def self.packer_public_output_path
  ::Shakapacker.config.public_output_path.to_s
end

.packer_source_entry_pathObject



92
93
94
# File 'lib/react_on_rails/packer_utils.rb', line 92

def self.packer_source_entry_path
  ::Shakapacker.config.source_entry_path
end

.packer_source_pathObject



88
89
90
# File 'lib/react_on_rails/packer_utils.rb', line 88

def self.packer_source_path
  ::Shakapacker.config.source_path
end

.packer_source_path_explicit?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/react_on_rails/packer_utils.rb', line 108

def self.packer_source_path_explicit?
  ::Shakapacker.config.send(:data)[:source_path].present?
end

.precompile?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/react_on_rails/packer_utils.rb', line 84

def self.precompile?
  ::Shakapacker.config.shakapacker_precompile?
end

.public_output_uri_pathObject



69
70
71
# File 'lib/react_on_rails/packer_utils.rb', line 69

def self.public_output_uri_path
  "#{::Shakapacker.config.public_output_path.relative_path_from(::Shakapacker.config.public_path)}/"
end

.raise_nested_entries_disabledObject



136
137
138
139
140
141
142
143
144
# File 'lib/react_on_rails/packer_utils.rb', line 136

def self.raise_nested_entries_disabled
  msg = <<~MSG
    **ERROR** ReactOnRails: `nested_entries` is configured to be disabled in shakapacker. Please update \
    config/shakapacker.yml to enable nested entries. for more information read
    https://www.shakacode.com/react-on-rails/docs/guides/file-system-based-automated-bundle-generation.md#enable-nested_entries-for-shakapacker
  MSG

  raise ReactOnRails::Error, msg
end

.raise_shakapacker_version_incompatible_for_autobundlingObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/react_on_rails/packer_utils.rb', line 146

def self.raise_shakapacker_version_incompatible_for_autobundling
  msg = <<~MSG
    **ERROR** ReactOnRails: Please upgrade ::Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_BUNDLING} or \
    above to use the automated bundle generation feature (which requires nested_entries support). \
    The currently installed version is #{ReactOnRails::PackerUtils.shakapacker_version}. \
    Basic pack generation requires ::Shakapacker #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or above.
  MSG

  raise ReactOnRails::Error, msg
end

.raise_shakapacker_version_incompatible_for_basic_pack_generationObject



157
158
159
160
161
162
163
164
# File 'lib/react_on_rails/packer_utils.rb', line 157

def self.raise_shakapacker_version_incompatible_for_basic_pack_generation
  msg = <<~MSG
    **ERROR** ReactOnRails: Please upgrade ::Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or \
    above to use basic pack generation features. The currently installed version is #{ReactOnRails::PackerUtils.shakapacker_version}.
  MSG

  raise ReactOnRails::Error, msg
end

.shakapacker_versionObject



15
16
17
18
19
# File 'lib/react_on_rails/packer_utils.rb', line 15

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

  @shakapacker_version = Gem.loaded_specs["shakapacker"].version.to_s
end

.shakapacker_version_as_arrayObject



21
22
23
24
25
26
27
28
# File 'lib/react_on_rails/packer_utils.rb', line 21

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

  match = shakapacker_version.match(ReactOnRails::VersionChecker::VERSION_PARTS_REGEX)

  # match[4] is the pre-release version, not normally a number but something like "beta.1" or `nil`
  @shakapacker_version_as_array = [match[1].to_i, match[2].to_i, match[3].to_i, match[4]].compact
end

.shakapacker_version_requirement_met?(required_version) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/react_on_rails/packer_utils.rb', line 30

def self.shakapacker_version_requirement_met?(required_version)
  @version_checks ||= {}
  @version_checks[required_version] ||= Gem::Version.new(shakapacker_version) >= Gem::Version.new(required_version)
end

.supports_async_loading?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/react_on_rails/packer_utils.rb', line 35

def self.supports_async_loading?
  shakapacker_version_requirement_met?("8.2.0")
end

.supports_autobundling?Boolean

Returns:

  • (Boolean)


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

def self.supports_autobundling?
  min_version = ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_BUNDLING
  ::Shakapacker.config.respond_to?(:nested_entries?) && shakapacker_version_requirement_met?(min_version)
end

.supports_basic_pack_generation?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/react_on_rails/packer_utils.rb', line 39

def self.supports_basic_pack_generation?
  shakapacker_version_requirement_met?(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION)
end

.webpack_assets_status_checkerObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/react_on_rails/packer_utils.rb', line 124

def self.webpack_assets_status_checker
  source_path = ReactOnRails::Utils.source_path
  generated_assets_full_path = ReactOnRails::Utils.generated_assets_full_path
  webpack_generated_files = ReactOnRails.configuration.webpack_generated_files

  @webpack_assets_status_checker ||= ReactOnRails::TestHelper::WebpackAssetsStatusChecker.new(
    source_path: source_path,
    generated_assets_full_path: generated_assets_full_path,
    webpack_generated_files: webpack_generated_files
  )
end