Skip to content

Commit

Permalink
Merge pull request #197 from gauravtiwari/fix/node_env
Browse files Browse the repository at this point in the history
Use NODE_ENV as default env
  • Loading branch information
rafaelfranca authored Mar 28, 2017
2 parents c4d912d + b70e396 commit 3564a1c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
webpacker (1.0)
webpacker (1.1)
activesupport (>= 4.2)
multi_json (~> 1.2)
railties (>= 4.2)
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/webpacker/compile.rake
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require "webpacker/env"
require "webpacker/configuration"
REGEX_MAP = /\A.*\.map\z/

namespace :webpacker do
desc "Compile javascript packs using webpack for production with digests"
task compile: ["webpacker:verify_install", :environment] do
puts "Compiling webpacker assets 🎉"
result = `NODE_ENV=production ./bin/webpack --json`
result = `NODE_ENV=#{Webpacker::Env.current} ./bin/webpack --json`

unless $?.success?
puts JSON.parse(result)["errors"]
Expand Down
5 changes: 3 additions & 2 deletions lib/webpacker/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Loads webpacker configuration from config/webpack/paths.yml
require "webpacker/file_loader"
require "webpacker/env"

class Webpacker::Configuration < Webpacker::FileLoader
class << self
Expand All @@ -24,7 +25,7 @@ def output_path
end

def paths
load if Rails.env.development?
load if Webpacker::Env.development?
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance
instance.data
end
Expand All @@ -37,6 +38,6 @@ def source_path
private
def load
return super unless File.exist?(@path)
HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Rails.env])
HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker::Env.current])
end
end
27 changes: 27 additions & 0 deletions lib/webpacker/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Singleton registry for determining NODE_ENV from config/webpack/paths.yml
require "webpacker/file_loader"

class Webpacker::Env < Webpacker::FileLoader
class << self
def current
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Env.load must be called first") unless instance
instance.data
end

def development?
current == "development"
end

def file_path
Rails.root.join("config", "webpack", "paths.yml")
end
end

private
def load
environments = File.exist?(@path) ? YAML.load(File.read(@path)).keys : [].freeze
return ENV["NODE_ENV"] if environments.include?(ENV["NODE_ENV"])
return Rails.env if environments.include?(Rails.env)
"production"
end
end
3 changes: 2 additions & 1 deletion lib/webpacker/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# "/packs/calendar-1016838bab065ae1e314.css" for long-term caching

require "webpacker/file_loader"
require "webpacker/env"
require "webpacker/configuration"

class Webpacker::Manifest < Webpacker::FileLoader
Expand All @@ -15,7 +16,7 @@ def file_path
end

def lookup(name)
load if Rails.env.development?
load if Webpacker::Env.development?
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance
instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?"))
end
Expand Down
3 changes: 3 additions & 0 deletions lib/webpacker/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
require "rails/railtie"

require "webpacker/helper"
require "webpacker/env"

class Webpacker::Engine < ::Rails::Engine
initializer :webpacker do |app|
ActiveSupport.on_load :action_controller do
ActionController::Base.helper Webpacker::Helper
end

# Determine NODE_ENV environment based on config/webpack/paths.yml
Webpacker::Env.load
# Loads webpacker config data from config/webpack/paths.yml
Webpacker::Configuration.load
# Loads manifest data from public/packs/manifest.json
Expand Down

0 comments on commit 3564a1c

Please sign in to comment.