File tree Expand file tree Collapse file tree 8 files changed +64
-31
lines changed Expand file tree Collapse file tree 8 files changed +64
-31
lines changed Original file line number Diff line number Diff 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" ] )
3221end
3322
3423# Compile packs after we've compiled all other assets during precompilation
Original file line number Diff line number Diff line change 11module 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
718end
819
20+ require "webpacker/env"
21+ require "webpacker/configuration"
22+ require "webpacker/manifest"
23+ require "webpacker/compiler"
924require "webpacker/railtie" if defined? ( Rails )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 11# Loads webpacker configuration from config/webpack/paths.yml
2+
23require "webpacker/file_loader"
3- require "webpacker/env"
44
55class 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
5151end
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1- require "webpacker/manifest"
2-
31module 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
Original file line number Diff line number Diff line change 66# "/packs/calendar-1016838bab065ae1e314.css" for long-term caching
77
88require "webpacker/file_loader"
9- require "webpacker/env"
10- require "webpacker/configuration"
119
1210class 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
Original file line number Diff line number Diff line change 33class 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
You can’t perform that action at this time.
0 commit comments