-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from shakacode/justin/improve-ensure-assets-c…
…ompiled Rework files for asset checking
- Loading branch information
Showing
18 changed files
with
480 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module ReactOnRails | ||
module TestHelper | ||
# Because you will probably want to run RSpec tests that rely on compiled webpack assets | ||
# (typically, your integration/feature specs where `js: true`), you will want to ensure you | ||
# don't accidentally run tests on missing or stale webpack assets. If you did use stale | ||
# Webpack assets, you will get invalid test results as your tests do not use the very latest | ||
# JavaScript code. | ||
# | ||
# Call this method from inside of the `RSpec.configure` block in your `spec/rails_helper.rb` | ||
# file, passing the config as an argument. You can customize this to your particular needs by | ||
# replacing any of the default components. | ||
# | ||
# RSpec.configure do |config| | ||
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) | ||
# | ||
# You can pass an RSpec metatag as an optional second parameter to this helper method | ||
# if you want this helper to run on examples other than where `js: true` (default). The helper | ||
# will compile webpack files at most once per test run. | ||
# | ||
# If you do not want to be slowed down by re-compiling webpack assets from scratch every test | ||
# run, you can call `npm run build:client` (and `npm run build:server` if doing server | ||
# rendering) to have webpack recompile these files in the background, which will be *much* | ||
# faster. The helper looks for these processes and will abort recompiling if it finds them | ||
# to be running. | ||
# | ||
# See docs/additional_reading/rspec_configuration.md for more info | ||
def self.configure_rspec_to_compile_assets(config, metatag = :js) | ||
config.before(:example, metatag) { ReactOnRails::TestHelper.ensure_assets_compiled } | ||
end | ||
|
||
# Main entry point to ensuring assets are compiled. See `configure_rspec_to_compile_assets` for | ||
# an example of usage. | ||
# | ||
# Typical usage passes all params as nil defaults. | ||
# webpack_assets_status_checker: provide: `up_to_date?`, `whats_not_up_to_date`, `client_dir` | ||
# defaults to ReactOnRails::TestHelper::WebpackAssetsStatusChecker | ||
# webpack_process_checker: provide one method: `def running?` | ||
# defaults to ReactOnRails::TestHelper::WebpackProcessChecker | ||
# webpack_assets_compiler: provide one method: `def compile` | ||
# defaults to ReactOnRails::TestHelper::WebpackAssetsCompiler | ||
# client_dir and compiled_dirs are passed into the default webpack_assets_status_checker if you | ||
# don't provide one. | ||
def self.ensure_assets_compiled(webpack_assets_status_checker: nil, | ||
webpack_assets_compiler: nil, | ||
webpack_process_checker: nil, | ||
client_dir: nil, | ||
compiled_dirs: nil) | ||
|
||
if webpack_assets_status_checker.nil? | ||
client_dir ||= Rails.root.join("client") | ||
compiled_dirs ||= ReactOnRails.configuration.generated_assets_dirs | ||
webpack_assets_status_checker ||= | ||
WebpackAssetsStatusChecker.new(client_dir: client_dir, | ||
compiled_dirs: compiled_dirs) | ||
end | ||
|
||
webpack_assets_compiler ||= WebpackAssetsCompiler.new | ||
webpack_process_checker ||= WebpackProcessChecker.new | ||
|
||
ReactOnRails::TestHelper::EnsureAssetsCompiled.new( | ||
webpack_assets_status_checker: webpack_assets_status_checker, | ||
webpack_assets_compiler: webpack_assets_compiler, | ||
webpack_process_checker: webpack_process_checker | ||
).call | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module ReactOnRails | ||
module TestHelper | ||
class EnsureAssetsCompiled | ||
class << self | ||
attr_accessor :has_been_run | ||
@has_been_run = false | ||
end | ||
|
||
attr_reader :webpack_assets_status_checker, | ||
:webpack_assets_compiler, | ||
:webpack_process_checker | ||
|
||
MAX_TIME_TO_WAIT = 5 | ||
|
||
def initialize(webpack_assets_status_checker: nil, | ||
webpack_assets_compiler: nil, | ||
webpack_process_checker: nil) | ||
@webpack_assets_status_checker = webpack_assets_status_checker | ||
@webpack_assets_compiler = webpack_assets_compiler | ||
@webpack_process_checker = webpack_process_checker | ||
end | ||
|
||
def call | ||
return if self.class.has_been_run | ||
|
||
loop_count = 0 | ||
loop do | ||
break if webpack_assets_status_checker.up_to_date? | ||
|
||
puts_first_iteration_message(loop_count) | ||
|
||
if webpack_process_checker.running? && loop_count < MAX_TIME_TO_WAIT | ||
loop_count += 1 | ||
sleep 1 | ||
else | ||
puts_max_iterations_message(loop_count) | ||
|
||
webpack_assets_compiler.compile | ||
puts | ||
break | ||
end | ||
end | ||
|
||
self.class.has_been_run = true | ||
end | ||
|
||
def puts_first_iteration_message(loop_count) | ||
return unless loop_count == 0 | ||
|
||
puts "\n\nReact on Rails is ensuring your JavaScript generated files are up to date!" | ||
end | ||
|
||
def puts_max_iterations_message(loop_count) | ||
if loop_count == MAX_TIME_TO_WAIT | ||
stale_files = webpack_assets_status_checker.whats_not_up_to_date.join("\n") | ||
|
||
puts <<-MSG | ||
Even though we detected the webpack watch processes are running, we found files modified that are | ||
not causing a rebuild of your generated files: | ||
#{stale_files} | ||
One possibility is that you modified a file in your directory that is not a dependency of | ||
your webpack files: #{webpack_assets_status_checker.client_dir} | ||
To be sure, we will now rebuild your generated files. | ||
MSG | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# You can replace this implementation with your own for use by the | ||
# ReactOnRails::TestHelper.ensure_assets_compiled helper | ||
module ReactOnRails | ||
module TestHelper | ||
class WebpackAssetsCompiler | ||
def compile | ||
compile_type(:client) | ||
compile_type(:server) if Utils.server_rendering_is_enabled? | ||
end | ||
|
||
private | ||
|
||
def compile_type(type) | ||
unless @printed_msg | ||
puts <<-MSG | ||
If you are frequently running tests, you can run webpack in watch mode to speed up this process. | ||
See the official documentation: | ||
https://github.com/shakacode/react_on_rails/blob/master/docs/additional_reading/rspec_configuration.md | ||
MSG | ||
@printed_msg = true | ||
end | ||
|
||
puts "\nBuilding Webpack #{type}-rendering assets..." | ||
|
||
build_output = `cd client && npm run build:#{type}` | ||
|
||
fail "Error in building assets!\n#{build_output}" unless Utils.last_process_completed_successfully? | ||
|
||
puts "Completed building Webpack #{type}-rendering assets." | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.