Skip to content

Commit 116e038

Browse files
committed
Use a separate env file to define NODE_ENV
Remove redundant assign Fix comment Fix sentence
1 parent 90d2e97 commit 116e038

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

lib/tasks/webpacker/compile.rake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
require "webpacker/env"
12
require "webpacker/configuration"
23
REGEX_MAP = /\A.*\.map\z/
34

45
namespace :webpacker do
56
desc "Compile javascript packs using webpack for production with digests"
67
task compile: ["webpacker:verify_install", :environment] do
78
puts "Compiling webpacker assets 🎉"
8-
result = `NODE_ENV=#{ENV["NODE_ENV"] || "production"} ./bin/webpack`
9+
result = `NODE_ENV=#{Webpacker::Env.current} ./bin/webpack`
910

1011
unless $?.success?
1112
puts JSON.parse(result)["errors"]

lib/webpacker/configuration.rb

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

45
class Webpacker::Configuration < Webpacker::FileLoader
56
class << self
@@ -24,7 +25,7 @@ def output_path
2425
end
2526

2627
def paths
27-
load if ENV["NODE_ENV"] == "development"
28+
load if Webpacker::Env.development?
2829
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance
2930
instance.data
3031
end
@@ -37,6 +38,6 @@ def source_path
3738
private
3839
def load
3940
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])
4142
end
4243
end

lib/webpacker/env.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

lib/webpacker/manifest.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# "/packs/calendar-1016838bab065ae1e314.css" for long-term caching
77

88
require "webpacker/file_loader"
9+
require "webpacker/env"
910
require "webpacker/configuration"
1011

1112
class Webpacker::Manifest < Webpacker::FileLoader
@@ -15,7 +16,7 @@ def file_path
1516
end
1617

1718
def lookup(name)
18-
load if ENV["NODE_ENV"] == "development"
19+
load if Webpacker::Env.development?
1920
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance
2021
instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?"))
2122
end

lib/webpacker/railtie.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
require "rails/railtie"
22

33
require "webpacker/helper"
4+
require "webpacker/env"
45

56
class Webpacker::Engine < ::Rails::Engine
67
initializer :webpacker do |app|
78
ActiveSupport.on_load :action_controller do
89
ActionController::Base.helper Webpacker::Helper
910
end
1011

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
1414
# Loads webpacker config data from config/webpack/paths.yml
1515
Webpacker::Configuration.load
1616
# Loads manifest data from public/packs/manifest.json

0 commit comments

Comments
 (0)