File tree 8 files changed +64
-31
lines changed
8 files changed +64
-31
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ namespace :webpacker do
8
8
puts "Compiling webpacker assets 🎉"
9
9
asset_host = Rails . application . config . action_controller . asset_host
10
10
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`
12
12
13
13
unless $?. success?
14
14
puts JSON . parse ( result ) [ "errors" ]
@@ -18,17 +18,6 @@ namespace :webpacker do
18
18
puts "Compiled digests for all packs in #{ Webpacker ::Configuration . packs_path } : "
19
19
puts JSON . parse ( File . read ( Webpacker ::Configuration . manifest_path ) )
20
20
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" ] )
32
21
end
33
22
34
23
# Compile packs after we've compiled all other assets during precompilation
Original file line number Diff line number Diff line change 1
1
module Webpacker
2
- def self . bootstrap
2
+ extend self
3
+
4
+ def bootstrap
3
5
Webpacker ::Env . load
4
6
Webpacker ::Configuration . load
5
7
Webpacker ::Manifest . load
6
8
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
7
18
end
8
19
20
+ require "webpacker/env"
21
+ require "webpacker/configuration"
22
+ require "webpacker/manifest"
23
+ require "webpacker/compiler"
9
24
require "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 1
1
# Loads webpacker configuration from config/webpack/paths.yml
2
+
2
3
require "webpacker/file_loader"
3
- require "webpacker/env"
4
4
5
5
class Webpacker ::Configuration < Webpacker ::FileLoader
6
6
class << self
@@ -25,7 +25,7 @@ def packs_path
25
25
end
26
26
27
27
def paths
28
- load if Webpacker :: Env . development?
28
+ load if Webpacker . env . development?
29
29
raise Webpacker ::FileLoader ::FileLoaderError . new ( "Webpacker::Configuration.load must be called first" ) unless instance
30
30
instance . data
31
31
end
@@ -46,6 +46,6 @@ def source_path
46
46
private
47
47
def load
48
48
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 ] )
50
50
end
51
51
end
Original file line number Diff line number Diff line change @@ -8,10 +8,6 @@ def current
8
8
instance . data
9
9
end
10
10
11
- def development?
12
- current == "development"
13
- end
14
-
15
11
def file_path
16
12
Rails . root . join ( "config" , "webpack" , "paths.yml" )
17
13
end
Original file line number Diff line number Diff line change 1
- require "webpacker/manifest"
2
-
3
1
module Webpacker ::Helper
4
2
# Computes the full path for a given webpacker asset.
5
3
# Return relative path using manifest.json and passes it to asset_url helper
Original file line number Diff line number Diff line change 6
6
# "/packs/calendar-1016838bab065ae1e314.css" for long-term caching
7
7
8
8
require "webpacker/file_loader"
9
- require "webpacker/env"
10
- require "webpacker/configuration"
11
9
12
10
class Webpacker ::Manifest < Webpacker ::FileLoader
13
11
class << self
@@ -16,14 +14,33 @@ def file_path
16
14
end
17
15
18
16
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
22
24
end
23
25
24
26
def lookup_path ( name )
25
27
Rails . root . join ( File . join ( Webpacker ::Configuration . output_path , lookup ( name ) ) )
26
28
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
27
44
end
28
45
29
46
private
Original file line number Diff line number Diff line change 3
3
class EnvTest < Minitest ::Test
4
4
def test_current_env
5
5
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?
10
8
end
11
9
12
10
def test_file_path
You can’t perform that action at this time.
0 commit comments