Module: ReactOnRails::PackerUtils
- Defined in:
- lib/react_on_rails/packer_utils.rb
Class Method Summary collapse
-
.asset_uri_from_packer(asset_name) ⇒ Object
The function doesn’t ensure that the asset exists.
-
.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.
- .check_manifest_not_cached ⇒ Object
- .dev_server_running? ⇒ Boolean
- .dev_server_url ⇒ Object
- .manifest_exists? ⇒ Boolean
- .nested_entries? ⇒ Boolean
- .packer ⇒ Object
- .packer_public_output_path ⇒ Object
- .packer_source_entry_path ⇒ Object
- .packer_source_path ⇒ Object
- .packer_source_path_explicit? ⇒ Boolean
- .packer_type ⇒ Object
- .precompile? ⇒ Boolean
- .public_output_uri_path ⇒ Object
- .raise_nested_entries_disabled ⇒ Object
- .raise_shakapacker_not_installed ⇒ Object
- .raise_shakapacker_version_incompatible_for_autobundling ⇒ Object
- .shakapacker_version ⇒ Object
- .shakapacker_version_as_array ⇒ Object
- .shakapacker_version_requirement_met?(required_version) ⇒ Boolean
- .using_packer? ⇒ Boolean
- .using_shakapacker_const? ⇒ Boolean
- .using_webpacker_const? ⇒ Boolean
- .webpack_assets_status_checker ⇒ Object
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
100 101 102 103 104 105 106 |
# File 'lib/react_on_rails/packer_utils.rb', line 100 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/react_on_rails/packer_utils.rb', line 75 def self.bundle_js_uri_from_packer(bundle_name) hashed_bundle_name = packer.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 packer.dev_server.running? && (!is_bundle_running_on_server || ReactOnRails.configuration.same_bundle_for_client_and_server) "#{dev_server_url}#{hashed_bundle_name}" else File.(File.join("public", hashed_bundle_name)).to_s end end |
.check_manifest_not_cached ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/react_on_rails/packer_utils.rb', line 139 def self.check_manifest_not_cached return unless using_packer? && packer.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/#{packer_type}.yml file and set cache_manifest to false for test. MSG puts (msg) exit! end |
.dev_server_running? ⇒ Boolean
42 43 44 45 46 |
# File 'lib/react_on_rails/packer_utils.rb', line 42 def self.dev_server_running? return false unless using_packer? packer.dev_server.running? end |
.dev_server_url ⇒ Object
48 49 50 |
# File 'lib/react_on_rails/packer_utils.rb', line 48 def self.dev_server_url "#{packer.dev_server.protocol}://#{packer.dev_server.host_with_port}" end |
.manifest_exists? ⇒ Boolean
131 132 133 |
# File 'lib/react_on_rails/packer_utils.rb', line 131 def self.manifest_exists? packer.config.public_manifest_path.exist? end |
.nested_entries? ⇒ Boolean
123 124 125 |
# File 'lib/react_on_rails/packer_utils.rb', line 123 def self.nested_entries? packer.config.nested_entries? end |
.packer ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/react_on_rails/packer_utils.rb', line 31 def self.packer return nil unless using_packer? if using_shakapacker_const? require "shakapacker" return ::Shakapacker end require "webpacker" ::Webpacker end |
.packer_public_output_path ⇒ Object
127 128 129 |
# File 'lib/react_on_rails/packer_utils.rb', line 127 def self.packer_public_output_path packer.config.public_output_path.to_s end |
.packer_source_entry_path ⇒ Object
119 120 121 |
# File 'lib/react_on_rails/packer_utils.rb', line 119 def self.packer_source_entry_path packer.config.source_entry_path end |
.packer_source_path ⇒ Object
115 116 117 |
# File 'lib/react_on_rails/packer_utils.rb', line 115 def self.packer_source_path packer.config.source_path end |
.packer_source_path_explicit? ⇒ Boolean
135 136 137 |
# File 'lib/react_on_rails/packer_utils.rb', line 135 def self.packer_source_path_explicit? packer.config.send(:data)[:source_path].present? end |
.packer_type ⇒ Object
24 25 26 27 28 29 |
# File 'lib/react_on_rails/packer_utils.rb', line 24 def self.packer_type return "shakapacker" if using_shakapacker_const? return "webpacker" if using_webpacker_const? nil end |
.precompile? ⇒ Boolean
108 109 110 111 112 113 |
# File 'lib/react_on_rails/packer_utils.rb', line 108 def self.precompile? return ::Webpacker.config.webpacker_precompile? if using_webpacker_const? return ::Shakapacker.config.shakapacker_precompile? if using_shakapacker_const? false end |
.public_output_uri_path ⇒ Object
93 94 95 |
# File 'lib/react_on_rails/packer_utils.rb', line 93 def self.public_output_uri_path "#{packer.config.public_output_path.relative_path_from(packer.config.public_path)}/" end |
.raise_nested_entries_disabled ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/react_on_rails/packer_utils.rb', line 163 def self.raise_nested_entries_disabled msg = <<~MSG **ERROR** ReactOnRails: `nested_entries` is configured to be disabled in shakapacker. Please update \ config/#{packer_type}.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_not_installed ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/react_on_rails/packer_utils.rb', line 183 def self.raise_shakapacker_not_installed msg = <<~MSG **ERROR** ReactOnRails: Missing Shakapacker gem. Please upgrade to use Shakapacker \ #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or above to use the \ automated bundle generation feature. MSG raise ReactOnRails::Error, msg end |
.raise_shakapacker_version_incompatible_for_autobundling ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/react_on_rails/packer_utils.rb', line 173 def self.raise_shakapacker_version_incompatible_for_autobundling msg = <<~MSG **ERROR** ReactOnRails: Please upgrade Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or \ above to use the automated bundle generation feature. The currently installed version is \ #{ReactOnRails::PackerUtils.shakapacker_version}. MSG raise ReactOnRails::Error, msg end |
.shakapacker_version ⇒ Object
52 53 54 55 56 57 |
# File 'lib/react_on_rails/packer_utils.rb', line 52 def self.shakapacker_version return @shakapacker_version if defined?(@shakapacker_version) return nil unless ReactOnRails::Utils.gem_available?("shakapacker") @shakapacker_version = Gem.loaded_specs["shakapacker"].version.to_s end |
.shakapacker_version_as_array ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/react_on_rails/packer_utils.rb', line 59 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
68 69 70 |
# File 'lib/react_on_rails/packer_utils.rb', line 68 def self.shakapacker_version_requirement_met?(required_version) Gem::Version.new(shakapacker_version) >= Gem::Version.new(required_version) end |
.using_packer? ⇒ Boolean
5 6 7 |
# File 'lib/react_on_rails/packer_utils.rb', line 5 def self.using_packer? using_shakapacker_const? || using_webpacker_const? end |
.using_shakapacker_const? ⇒ Boolean
9 10 11 12 13 14 |
# File 'lib/react_on_rails/packer_utils.rb', line 9 def self.using_shakapacker_const? return @using_shakapacker_const if defined?(@using_shakapacker_const) @using_shakapacker_const = ReactOnRails::Utils.gem_available?("shakapacker") && shakapacker_version_requirement_met?("7.0.0") end |
.using_webpacker_const? ⇒ Boolean
16 17 18 19 20 21 22 |
# File 'lib/react_on_rails/packer_utils.rb', line 16 def self.using_webpacker_const? return @using_webpacker_const if defined?(@using_webpacker_const) @using_webpacker_const = (ReactOnRails::Utils.gem_available?("shakapacker") && shakapacker_version_as_array[0] <= 6) || ReactOnRails::Utils.gem_available?("webpacker") end |
.webpack_assets_status_checker ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/react_on_rails/packer_utils.rb', line 151 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 |