File tree 5 files changed +37
-7
lines changed
5 files changed +37
-7
lines changed Original file line number Diff line number Diff line change
1
+ require "webpacker/env"
1
2
require "webpacker/configuration"
2
3
REGEX_MAP = /\A .*\. map\z /
3
4
4
5
namespace :webpacker do
5
6
desc "Compile javascript packs using webpack for production with digests"
6
7
task compile : [ "webpacker:verify_install" , :environment ] do
7
8
puts "Compiling webpacker assets 🎉"
8
- result = `NODE_ENV=#{ ENV [ "NODE_ENV" ] || "production" } ./bin/webpack`
9
+ result = `NODE_ENV=#{ Webpacker :: Env . current } ./bin/webpack`
9
10
10
11
unless $?. success?
11
12
puts JSON . parse ( result ) [ "errors" ]
Original file line number Diff line number Diff line change 1
1
# Loads webpacker configuration from config/webpack/paths.yml
2
2
require "webpacker/file_loader"
3
+ require "webpacker/env"
3
4
4
5
class Webpacker ::Configuration < Webpacker ::FileLoader
5
6
class << self
@@ -24,7 +25,7 @@ def output_path
24
25
end
25
26
26
27
def paths
27
- load if ENV [ "NODE_ENV" ] == " development"
28
+ load if Webpacker :: Env . development?
28
29
raise Webpacker ::FileLoader ::FileLoaderError . new ( "Webpacker::Configuration.load must be called first" ) unless instance
29
30
instance . data
30
31
end
@@ -37,6 +38,6 @@ def source_path
37
38
private
38
39
def load
39
40
return super unless File . exist? ( @path )
40
- HashWithIndifferentAccess . new ( YAML . load ( File . read ( @path ) ) [ ENV [ "NODE_ENV" ] ] )
41
+ HashWithIndifferentAccess . new ( YAML . load ( File . read ( @path ) ) [ Webpacker :: Env . current ] )
41
42
end
42
43
end
Original file line number Diff line number Diff line change
1
+ # Singleton registry for determining NODE_ENV from config/webpack/paths.yml
2
+ require "webpacker/file_loader"
3
+
4
+ class Webpacker ::Env < Webpacker ::FileLoader
5
+ class << self
6
+ def current
7
+ raise Webpacker ::FileLoader ::FileLoaderError . new ( "Webpacker::Env.load must be called first" ) unless instance
8
+ instance . data
9
+ end
10
+
11
+ def development?
12
+ current == "development"
13
+ end
14
+
15
+ def file_path
16
+ Rails . root . join ( "config" , "webpack" , "paths.yml" )
17
+ end
18
+ end
19
+
20
+ private
21
+ def load
22
+ environments = File . exist? ( @path ) ? YAML . load ( File . read ( @path ) ) . keys : [ ] . freeze
23
+ return ENV [ "NODE_ENV" ] if environments . include? ( ENV [ "NODE_ENV" ] )
24
+ return Rails . env if environments . include? ( Rails . env )
25
+ "production"
26
+ end
27
+ end
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"
9
10
require "webpacker/configuration"
10
11
11
12
class Webpacker ::Manifest < Webpacker ::FileLoader
@@ -15,7 +16,7 @@ def file_path
15
16
end
16
17
17
18
def lookup ( name )
18
- load if ENV [ "NODE_ENV" ] == " development"
19
+ load if Webpacker :: Env . development?
19
20
raise Webpacker ::FileLoader ::FileLoaderError . new ( "Webpacker::Manifest.load must be called first" ) unless instance
20
21
instance . data [ name . to_s ] || raise ( Webpacker ::FileLoader ::NotFoundError . new ( "Can't find #{ name } in #{ file_path } . Is webpack still compiling?" ) )
21
22
end
Original file line number Diff line number Diff line change 1
1
require "rails/railtie"
2
2
3
3
require "webpacker/helper"
4
+ require "webpacker/env"
4
5
5
6
class Webpacker ::Engine < ::Rails ::Engine
6
7
initializer :webpacker do |app |
7
8
ActiveSupport . on_load :action_controller do
8
9
ActionController ::Base . helper Webpacker ::Helper
9
10
end
10
11
11
- # Use NODE_ENV if defined, else fallback to RAILS_ENV
12
- ENV [ "NODE_ENV" ] ||= ENV [ "RAILS_ENV" ]
13
-
12
+ # Setup NODE_ENV environment based on config/webpack/paths.yml
13
+ Webpacker ::Env . load
14
14
# Loads webpacker config data from config/webpack/paths.yml
15
15
Webpacker ::Configuration . load
16
16
# Loads manifest data from public/packs/manifest.json
You can’t perform that action at this time.
0 commit comments