Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy compile in test env #360

Merged
merged 5 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions lib/tasks/webpacker/compile.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace :webpacker do
puts "Compiling webpacker assets 🎉"
asset_host = Rails.application.config.action_controller.asset_host
asset_env = asset_host ? "ASSET_HOST=#{asset_host}" : ""
result = `#{asset_env} NODE_ENV=#{Webpacker::Env.current} ./bin/webpack --json`
result = `#{asset_env} NODE_ENV=#{Webpacker.env} ./bin/webpack --json`

unless $?.success?
puts JSON.parse(result)["errors"]
Expand All @@ -18,17 +18,6 @@ namespace :webpacker do
puts "Compiled digests for all packs in #{Webpacker::Configuration.packs_path}: "
puts JSON.parse(File.read(Webpacker::Configuration.manifest_path))
end

desc "Compile javascript packs using webpack for test with digests"
task compile_before_test: ["webpacker:compile"] do
Webpacker::Manifest.load(Webpacker::Manifest.file_path)
end
end

# Compile packs prior to system and controller tests running
if Rake::Task.task_defined?("test:system")
Rake::Task["test:system"].enhance(["webpacker:compile_before_test"])
Rake::Task["test:controllers"].enhance(["webpacker:compile_before_test"])
end

# Compile packs after we've compiled all other assets during precompilation
Expand Down
17 changes: 16 additions & 1 deletion lib/webpacker.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
module Webpacker
def self.bootstrap
extend self

def bootstrap
Webpacker::Env.load
Webpacker::Configuration.load
Webpacker::Manifest.load
end

def compile
Webpacker::Compiler.compile
Webpacker::Manifest.load
end

def env
Webpacker::Env.current.inquiry
end
end

require "webpacker/env"
require "webpacker/configuration"
require "webpacker/manifest"
require "webpacker/compiler"
require "webpacker/railtie" if defined?(Rails)
20 changes: 20 additions & 0 deletions lib/webpacker/compiler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "rake"

module Webpacker::Compiler
extend self

def compile
compile_task.invoke
compile_task.reenable
end

private
def compile_task
@compile_task ||= load_rake_task("webpacker:compile")
end

def load_rake_task(name)
@load_rakefile ||= Rake.load_rakefile(Rails.root.join("Rakefile"))
Rake::Task[name]
end
end
6 changes: 3 additions & 3 deletions lib/webpacker/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Loads webpacker configuration from config/webpack/paths.yml

require "webpacker/file_loader"
require "webpacker/env"

class Webpacker::Configuration < Webpacker::FileLoader
class << self
Expand All @@ -25,7 +25,7 @@ def packs_path
end

def paths
load if Webpacker::Env.development?
load if Webpacker.env.development?
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance
instance.data
end
Expand All @@ -46,6 +46,6 @@ def source_path
private
def load
return super unless File.exist?(@path)
HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker::Env.current])
HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker.env])
end
end
4 changes: 0 additions & 4 deletions lib/webpacker/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ def current
instance.data
end

def development?
current == "development"
end

def file_path
Rails.root.join("config", "webpack", "paths.yml")
end
Expand Down
2 changes: 0 additions & 2 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "webpacker/manifest"

module Webpacker::Helper
# Computes the full path for a given webpacker asset.
# Return relative path using manifest.json and passes it to asset_url helper
Expand Down
27 changes: 22 additions & 5 deletions lib/webpacker/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
# "/packs/calendar-1016838bab065ae1e314.css" for long-term caching

require "webpacker/file_loader"
require "webpacker/env"
require "webpacker/configuration"

class Webpacker::Manifest < Webpacker::FileLoader
class << self
Expand All @@ -16,14 +14,33 @@ def file_path
end

def lookup(name)
load if Webpacker::Env.development?
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance
instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?"))
load if Webpacker.env.development?

if Webpacker.env.test?
find(name) || compile_and_find!(name)
else
find!(name)
end
end

def lookup_path(name)
Rails.root.join(File.join(Webpacker::Configuration.output_path, lookup(name)))
end

private
def find(name)
instance.data[name.to_s] if instance
end

def find!(name)
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance
instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?"))
end

def compile_and_find!(name)
Webpacker.compile
find!(name)
end
end

private
Expand Down
6 changes: 2 additions & 4 deletions test/env_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
class EnvTest < Minitest::Test
def test_current_env
assert_equal Webpacker::Env.current, "production"
end

def test_env_is_development?
refute_predicate Webpacker::Env, :development?
assert_equal Webpacker.env, "production"
assert Webpacker.env.production?
end

def test_file_path
Expand Down