Skip to content

Commit 549988b

Browse files
authored
Lazy compile in test env (#360)
* Add Webpacker.compile method * Compile (once) on failed manifest lookups in test env * Reword load order / dependencies * Add Webpacker.env inquiry helper * Refactor compile before lookup
1 parent 9b40a29 commit 549988b

File tree

8 files changed

+64
-31
lines changed

8 files changed

+64
-31
lines changed

lib/tasks/webpacker/compile.rake

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace :webpacker do
88
puts "Compiling webpacker assets 🎉"
99
asset_host = Rails.application.config.action_controller.asset_host
1010
asset_env = asset_host ? "ASSET_HOST=#{asset_host}" : ""
11-
result = `#{asset_env} NODE_ENV=#{Webpacker::Env.current} ./bin/webpack --json`
11+
result = `#{asset_env} NODE_ENV=#{Webpacker.env} ./bin/webpack --json`
1212

1313
unless $?.success?
1414
puts JSON.parse(result)["errors"]
@@ -18,17 +18,6 @@ namespace :webpacker do
1818
puts "Compiled digests for all packs in #{Webpacker::Configuration.packs_path}: "
1919
puts JSON.parse(File.read(Webpacker::Configuration.manifest_path))
2020
end
21-
22-
desc "Compile javascript packs using webpack for test with digests"
23-
task compile_before_test: ["webpacker:compile"] do
24-
Webpacker::Manifest.load(Webpacker::Manifest.file_path)
25-
end
26-
end
27-
28-
# Compile packs prior to system and controller tests running
29-
if Rake::Task.task_defined?("test:system")
30-
Rake::Task["test:system"].enhance(["webpacker:compile_before_test"])
31-
Rake::Task["test:controllers"].enhance(["webpacker:compile_before_test"])
3221
end
3322

3423
# Compile packs after we've compiled all other assets during precompilation

lib/webpacker.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
module Webpacker
2-
def self.bootstrap
2+
extend self
3+
4+
def bootstrap
35
Webpacker::Env.load
46
Webpacker::Configuration.load
57
Webpacker::Manifest.load
68
end
9+
10+
def compile
11+
Webpacker::Compiler.compile
12+
Webpacker::Manifest.load
13+
end
14+
15+
def env
16+
Webpacker::Env.current.inquiry
17+
end
718
end
819

20+
require "webpacker/env"
21+
require "webpacker/configuration"
22+
require "webpacker/manifest"
23+
require "webpacker/compiler"
924
require "webpacker/railtie" if defined?(Rails)

lib/webpacker/compiler.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "rake"
2+
3+
module Webpacker::Compiler
4+
extend self
5+
6+
def compile
7+
compile_task.invoke
8+
compile_task.reenable
9+
end
10+
11+
private
12+
def compile_task
13+
@compile_task ||= load_rake_task("webpacker:compile")
14+
end
15+
16+
def load_rake_task(name)
17+
@load_rakefile ||= Rake.load_rakefile(Rails.root.join("Rakefile"))
18+
Rake::Task[name]
19+
end
20+
end

lib/webpacker/configuration.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Loads webpacker configuration from config/webpack/paths.yml
2+
23
require "webpacker/file_loader"
3-
require "webpacker/env"
44

55
class Webpacker::Configuration < Webpacker::FileLoader
66
class << self
@@ -25,7 +25,7 @@ def packs_path
2525
end
2626

2727
def paths
28-
load if Webpacker::Env.development?
28+
load if Webpacker.env.development?
2929
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance
3030
instance.data
3131
end
@@ -46,6 +46,6 @@ def source_path
4646
private
4747
def load
4848
return super unless File.exist?(@path)
49-
HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker::Env.current])
49+
HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker.env])
5050
end
5151
end

lib/webpacker/env.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ def current
88
instance.data
99
end
1010

11-
def development?
12-
current == "development"
13-
end
14-
1511
def file_path
1612
Rails.root.join("config", "webpack", "paths.yml")
1713
end

lib/webpacker/helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require "webpacker/manifest"
2-
31
module Webpacker::Helper
42
# Computes the full path for a given webpacker asset.
53
# Return relative path using manifest.json and passes it to asset_url helper

lib/webpacker/manifest.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
# "/packs/calendar-1016838bab065ae1e314.css" for long-term caching
77

88
require "webpacker/file_loader"
9-
require "webpacker/env"
10-
require "webpacker/configuration"
119

1210
class Webpacker::Manifest < Webpacker::FileLoader
1311
class << self
@@ -16,14 +14,33 @@ def file_path
1614
end
1715

1816
def lookup(name)
19-
load if Webpacker::Env.development?
20-
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance
21-
instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?"))
17+
load if Webpacker.env.development?
18+
19+
if Webpacker.env.test?
20+
find(name) || compile_and_find!(name)
21+
else
22+
find!(name)
23+
end
2224
end
2325

2426
def lookup_path(name)
2527
Rails.root.join(File.join(Webpacker::Configuration.output_path, lookup(name)))
2628
end
29+
30+
private
31+
def find(name)
32+
instance.data[name.to_s] if instance
33+
end
34+
35+
def find!(name)
36+
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance
37+
instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?"))
38+
end
39+
40+
def compile_and_find!(name)
41+
Webpacker.compile
42+
find!(name)
43+
end
2744
end
2845

2946
private

test/env_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
class EnvTest < Minitest::Test
44
def test_current_env
55
assert_equal Webpacker::Env.current, "production"
6-
end
7-
8-
def test_env_is_development?
9-
refute_predicate Webpacker::Env, :development?
6+
assert_equal Webpacker.env, "production"
7+
assert Webpacker.env.production?
108
end
119

1210
def test_file_path

0 commit comments

Comments
 (0)