Skip to content

Commit

Permalink
changed asset compliation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jbhatab committed Apr 30, 2016
1 parent ddebe1b commit aa3d80f
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ ReactOnRails.configure do |config|
# or if you change your webpack bundle file name.
config.server_bundle_js_file = "webpack-bundle.js"

config.npm_build_command = "npm run build"

# increase if you're on JRuby
config.server_renderer_pool_size = 1
# seconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace :assets do

desc "Compile assets with webpack"
task :webpack do
sh "cd client && npm run build"
sh "cd client && #{ReactOnRails.configuration.npm_build_command}"
end

task :clobber do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"scripts": {
"postinstall": "cd client && npm install",
"rails-server": "echo 'visit http://localhost:3000/hello_world' && foreman start -f Procfile.dev",
"build": "(cd client && npm run build --silent)",
"build:dev": "(cd client && npm run build:dev --silent)",
"test": "rspec"
}
}
1 change: 0 additions & 1 deletion lib/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
require "react_on_rails/test_helper"
require "react_on_rails/test_helper/webpack_assets_compiler"
require "react_on_rails/test_helper/webpack_assets_status_checker"
require "react_on_rails/test_helper/webpack_process_checker"
require "react_on_rails/test_helper/ensure_assets_compiled"
8 changes: 5 additions & 3 deletions lib/react_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def self.configuration
server_renderer_timeout: 20,
skip_display_none: false,
webpack_generated_files: [],
rendering_extension: nil
rendering_extension: nil,
npm_build_command: "npm run build"
)
end

Expand All @@ -65,18 +66,19 @@ class Configuration
:logging_on_server, :server_renderer_pool_size,
:server_renderer_timeout, :raise_on_prerender_error,
:skip_display_none, :generated_assets_dirs, :generated_assets_dir,
:webpack_generated_files, :rendering_extension
:webpack_generated_files, :rendering_extension, :npm_build_command

def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil,
trace: nil, development_mode: nil,
logging_on_server: nil, server_renderer_pool_size: nil,
server_renderer_timeout: nil, raise_on_prerender_error: nil,
skip_display_none: nil, generated_assets_dirs: nil,
generated_assets_dir: nil, webpack_generated_files: nil,
rendering_extension: nil)
rendering_extension: nil, npm_build_command: nil)
self.server_bundle_js_file = server_bundle_js_file
self.generated_assets_dirs = generated_assets_dirs
self.generated_assets_dir = generated_assets_dir
self.npm_build_command = npm_build_command

self.prerender = prerender
self.replay_console = replay_console
Expand Down
7 changes: 1 addition & 6 deletions lib/react_on_rails/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def self.configure_rspec_to_compile_assets(config, *metatags)
# 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 generated_assets_dir are passed into the default webpack_assets_status_checker if you
Expand All @@ -54,7 +52,6 @@ def self.configure_rspec_to_compile_assets(config, *metatags)
# webpack_generated_files in your configuration
def self.ensure_assets_compiled(webpack_assets_status_checker: nil,
webpack_assets_compiler: nil,
webpack_process_checker: nil,
client_dir: nil,
generated_assets_dir: nil,
webpack_generated_files: nil)
Expand All @@ -80,12 +77,10 @@ def self.ensure_assets_compiled(webpack_assets_status_checker: nil,
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
webpack_assets_compiler: webpack_assets_compiler
).call
end
end
Expand Down
81 changes: 4 additions & 77 deletions lib/react_on_rails/test_helper/ensure_assets_compiled.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
module ReactOnRails
module TestHelper
class EnsureAssetsCompiled
SECONDS_TO_WAIT = 10

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
:webpack_assets_compiler

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

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity

# Several Scenarios:
# 1. No webpack watch processes for static assets and files are mising or out of date.
# 2. Only webpack watch process for server bundle as we're the hot reloading setup.
Expand All @@ -46,79 +35,17 @@ def call
# Inform the developer that we're ensuring gen assets are ready.
puts_start_compile_check_message(stale_gen_files)

hot_running = webpack_process_checker.hot_running?
client_running = webpack_process_checker.client_running?
server_running = webpack_process_checker.server_running?
already_compiled_client_file = false

# Check if running "hot" and not running a process to statically compile the client files.
if hot_running && !client_running
puts "Appears you're running hot reloading and are not rebuilding client files "\
"automatically. We'll try rebuilding only your client files first."
webpack_assets_compiler.compile_client(stale_gen_files)
already_compiled_client_file = true

stale_gen_files = webpack_assets_status_checker.stale_generated_webpack_files

# Return if we're all done!
return if stale_gen_files.empty?
end

loop_count = 0
if (already_compiled_client_file && server_running) ||
(!already_compiled_client_file && client_running)
puts "Waiting #{SECONDS_TO_WAIT} for webpack watch processes to compile files"
loop do
sleep 1
stale_gen_files = webpack_assets_status_checker.stale_generated_webpack_files
loop_count += 1
break if loop_count == SECONDS_TO_WAIT || stale_gen_files.empty?
end
end

final_compilation_check(already_compiled_client_file, client_running, server_running, stale_gen_files)
end

# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity

def final_compilation_check(already_compiled_client_file, client_running, server_running, stale_gen_files)
return unless stale_gen_files.present?
if client_running || server_running
puts <<-MSG
Arghhhhhh! We still have the following stale generated files after waiting for Webpack to compile:
#{stale_gen_files.join("\n")}
This can happen if you removed the generated files after they've been created by your webpack
watch processes, such by running a clean on your generated bundles before starting your tests.
MSG
end

puts <<-MSG
If you are frequently running tests, you can run webpack in watch mode for static assets 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

if already_compiled_client_file
# So only do serer file
webpack_assets_compiler.compile_server(stale_gen_files)
else
webpack_assets_compiler.compile_as_necessary(stale_gen_files)
end
webpack_assets_compiler.compile_assets
end

def puts_start_compile_check_message(stale_files)
server_msg = Utils.server_rendering_is_enabled? ? "and `build:server`" : ""
puts <<-MSG
Detected are the following stale generated files:
#{stale_files.join("\n")}
React on Rails will ensure your JavaScript generated files are up to date, using your
top level package.json `build:client` #{server_msg} commands.
client level package.json `build` command.
MSG
end
Expand Down
32 changes: 4 additions & 28 deletions lib/react_on_rails/test_helper/webpack_assets_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,14 @@
module ReactOnRails
module TestHelper
class WebpackAssetsCompiler
def compile_as_necessary(stale_files)
compile_client(stale_files)
compile_server(stale_files)
end

def compile_client(stale_files)
compile_type(:client) if needs_client_compile?(stale_files)
end

def compile_server(stale_files)
compile_type(:server) if needs_server_compile?(stale_files)
end

private
def compile_assets
puts "\nBuilding Webpack assets..."

def compile_type(type)
puts "\nBuilding Webpack #{type}-rendering assets..."

build_output = `cd client && npm run build:#{type}`
build_output = `cd client && #{ReactOnRails.configuration.npm_build_command}`

raise "Error in building assets!\n#{build_output}" unless Utils.last_process_completed_successfully?

puts "Completed building Webpack #{type}-rendering assets."
end

def needs_client_compile?(stale_files)
!stale_files.all? { |name| name.include?("server") }
end

def needs_server_compile?(stale_files)
return false unless Utils.server_rendering_is_enabled?
stale_files.any? { |name| name.include?("server") }
puts "Completed building Webpack assets."
end
end
end
Expand Down
54 changes: 0 additions & 54 deletions lib/react_on_rails/test_helper/webpack_process_checker.rb

This file was deleted.

1 change: 1 addition & 0 deletions spec/dummy/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"jscs:fix": "jscs --verbose -x . --silent",
"build:test": "webpack --config webpack.client.js && webpack --config webpack.server.js",
"test": "npm run build:test && rspec",
"build": "npm run build:client && npm run build:server",
"build:production:client": "NODE_ENV=production webpack --config webpack.client.rails.build.config.js",
"build:production:server": "NODE_ENV=production webpack --config webpack.server.rails.build.config.js",
"build:client": "webpack --config webpack.client.rails.build.config.js",
Expand Down
Loading

0 comments on commit aa3d80f

Please sign in to comment.