Class: ReactOnRails::TestHelper::EnsureAssetsCompiled

Inherits:
Object
  • Object
show all
Defined in:
lib/react_on_rails/test_helper/ensure_assets_compiled.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webpack_assets_status_checker: nil, webpack_assets_compiler: nil) ⇒ EnsureAssetsCompiled

Returns a new instance of EnsureAssetsCompiled.



17
18
19
20
21
# File 'lib/react_on_rails/test_helper/ensure_assets_compiled.rb', line 17

def initialize(webpack_assets_status_checker: nil,
               webpack_assets_compiler: nil)
  @webpack_assets_status_checker = webpack_assets_status_checker
  @webpack_assets_compiler = webpack_assets_compiler
end

Class Attribute Details

.has_been_runObject

Returns the value of attribute has_been_run.



10
11
12
# File 'lib/react_on_rails/test_helper/ensure_assets_compiled.rb', line 10

def has_been_run
  @has_been_run
end

.mutexObject (readonly)

Returns the value of attribute mutex.



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

def mutex
  @mutex
end

Instance Attribute Details

#webpack_assets_compilerObject (readonly)

Returns the value of attribute webpack_assets_compiler.



14
15
16
# File 'lib/react_on_rails/test_helper/ensure_assets_compiled.rb', line 14

def webpack_assets_compiler
  @webpack_assets_compiler
end

#webpack_assets_status_checkerObject (readonly)

Returns the value of attribute webpack_assets_status_checker.



14
15
16
# File 'lib/react_on_rails/test_helper/ensure_assets_compiled.rb', line 14

def webpack_assets_status_checker
  @webpack_assets_status_checker
end

Instance Method Details

#callObject

Several Scenarios:

  1. No webpack watch processes for static assets and files are missing or out of date.

  2. Only webpack watch process for server bundle as we’re the hot reloading setup.

  3. For whatever reason, the watch processes are running, but some clean script removed the generated bundles.

  4. bin/dev static is running with fresh assets → reuse dev output (no compilation).



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/react_on_rails/test_helper/ensure_assets_compiled.rb', line 29

def call
  self.class.mutex.synchronize do
    # Only check this ONCE during a test run, even with threaded test runners.
    return if self.class.has_been_run

    self.class.has_been_run = true
  end

  ReactOnRails::Locales.compile

  stale_gen_files = webpack_assets_status_checker.stale_generated_webpack_files

  # All done if no stale files!
  return if stale_gen_files.empty?

  # If only the manifest is stale, check if development assets can be reused.
  # This handles the common case where bin/dev static is running and has
  # already compiled fresh assets. In that case, we can safely point
  # Shakapacker's test config at the dev output and skip test compilation.
  return if stale_gen_files.all? { |path| File.basename(path.to_s) == "manifest.json" } &&
            DevAssetsDetector.try_activate_dev_assets!

  ReactOnRails::PacksGenerator.instance.generate_packs_if_stale if ReactOnRails.configuration.auto_load_bundle

  # Inform the developer that we're ensuring gen assets are ready.
  puts_start_compile_check_message(stale_gen_files)

  webpack_assets_compiler.compile_assets
end

#puts_start_compile_check_message(stale_files) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/react_on_rails/test_helper/ensure_assets_compiled.rb', line 59

def puts_start_compile_check_message(stale_files)
  puts <<~MSG

    React on Rails: Stale test assets detected:
      #{stale_files.join("\n  ")}

    Compiling with: `#{ReactOnRails::Utils.prepend_cd_node_modules_directory(ReactOnRails.configuration.build_test_command)}`

    Tip: To skip this wait, run 'bin/dev static' or 'bin/dev test-watch' in another terminal.
    See: #{WebpackAssetsCompiler::TESTING_DOCS_URL}

  MSG
end