Skip to content

Commit

Permalink
webpacker backport
Browse files Browse the repository at this point in the history
  • Loading branch information
monlu committed Jul 3, 2020
1 parent 5d27155 commit 429ae7d
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ group :test do
gem "minitest", "~> 5.0"
gem "byebug"
end

group :development, :test do
gem 'pry'
gem 'rb-readline'
end
11 changes: 9 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ PATH
remote: .
specs:
webpacker (5.1.1)
activesupport (>= 5.2)
activesupport (>= 3.2.22)
rack-proxy (>= 0.6.1)
railties (>= 5.2)
railties (>= 3.2.22)
semantic_range (>= 2.3.0)

GEM
Expand Down Expand Up @@ -68,6 +68,7 @@ GEM
ast (2.4.0)
builder (3.2.4)
byebug (11.1.3)
coderay (1.1.2)
concurrent-ruby (1.1.6)
crass (1.0.6)
erubi (1.9.0)
Expand All @@ -94,6 +95,9 @@ GEM
parallel (1.19.1)
parser (2.7.1.3)
ast (~> 2.4.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
rack (2.2.3)
rack-proxy (0.6.5)
rack
Expand Down Expand Up @@ -127,6 +131,7 @@ GEM
thor (>= 0.20.3, < 2.0)
rainbow (3.0.0)
rake (13.0.1)
rb-readline (0.5.5)
rubocop (0.68.1)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
Expand Down Expand Up @@ -162,9 +167,11 @@ DEPENDENCIES
bundler (>= 1.3.0)
byebug
minitest (~> 5.0)
pry
rack-proxy
rails
rake (>= 11.1)
rb-readline
rubocop (< 0.69)
rubocop-performance
semantic_range
Expand Down
25 changes: 25 additions & 0 deletions lib/extensions/hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "active_support/core_ext/enumerable"
require "active_support/core_ext/hash/keys"

class Hash
def deep_symbolize_keys
deep_transform_keys { |key| key.to_sym rescue key }
end

def deep_transform_keys(&block)
_deep_transform_keys_in_object(self, &block)
end

def _deep_transform_keys_in_object(object, &block)
case object
when Hash
object.each_with_object({}) do |(key, value), result|
result[yield(key)] = _deep_transform_keys_in_object(value, &block)
end
when Array
object.map { |e| _deep_transform_keys_in_object(e, &block) }
else
object
end
end
end
45 changes: 45 additions & 0 deletions lib/extensions/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class String
# Same as +indent+, except it indents the receiver in-place.
#
# Returns the indented string, or +nil+ if there was nothing to indent.
def indent!(amount, indent_string = nil, indent_empty_lines = false)
indent_string = indent_string || self[/^[ \t]/] || " "
re = indent_empty_lines ? /^/ : /^(?!$)/
gsub!(re, indent_string * amount)
end

# Indents the lines in the receiver:
#
# <<EOS.indent(2)
# def some_method
# some_code
# end
# EOS
# # =>
# def some_method
# some_code
# end
#
# The second argument, +indent_string+, specifies which indent string to
# use. The default is +nil+, which tells the method to make a guess by
# peeking at the first indented line, and fallback to a space if there is
# none.
#
# " foo".indent(2) # => " foo"
# "foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar"
# "foo".indent(2, "\t") # => "\t\tfoo"
#
# While +indent_string+ is typically one space or tab, it may be any string.
#
# The third argument, +indent_empty_lines+, is a flag that says whether
# empty lines should be indented. Default is false.
#
# "foo\n\nbar".indent(2) # => " foo\n\n bar"
# "foo\n\nbar".indent(2, nil, true) # => " foo\n \n bar"
#
def indent(amount, indent_string = nil, indent_empty_lines = false)
dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) }
end
end
1 change: 1 addition & 0 deletions lib/install/react.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "webpacker/configuration"
require "fileutils"
require_relative "../extensions/string"

replace_babel_config = FileUtils.compare_file(Rails.root.join("babel.config.js"), "#{__dir__}/config/babel.config.js")

Expand Down
5 changes: 3 additions & 2 deletions lib/webpacker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "active_support/core_ext/module/attribute_accessors"
require "active_support/core_ext/string/inquiry"
require "active_support/logger"
require "active_support/core_ext/logger"
require "active_support/core_ext/module/delegation"
require "active_support/tagged_logging"

module Webpacker
Expand All @@ -24,7 +25,7 @@ def with_node_env(env)

def ensure_log_goes_to_stdout
old_logger = Webpacker.logger
Webpacker.logger = ActiveSupport::Logger.new(STDOUT)
Webpacker.logger = Logger.new(STDOUT)
yield
ensure
Webpacker.logger = old_logger
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def compilation_digest_path
def webpack_env
return env unless defined?(ActionController::Base)

env.merge("WEBPACKER_ASSET_HOST" => ENV.fetch("WEBPACKER_ASSET_HOST", ActionController::Base.helpers.compute_asset_host),
env.merge("WEBPACKER_ASSET_HOST" => ENV.fetch("WEBPACKER_ASSET_HOST", nil),
"WEBPACKER_RELATIVE_URL_ROOT" => ENV.fetch("WEBPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root),
"WEBPACKER_CONFIG" => webpacker.config_path.to_s)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "yaml"
require "active_support/core_ext/hash/keys"
require "active_support/core_ext/hash/indifferent_access"
require_relative "../extensions/hash"

class Webpacker::Configuration
attr_reader :root_path, :config_path, :env
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def inquire

private
def current
Rails.env.presence_in(available_environments)
available_environments.find { |e| e == Rails.env }
end

def fallback_env_warning
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/instance.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Webpacker::Instance
cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)) }

attr_reader :root_path, :config_path

Expand Down
6 changes: 3 additions & 3 deletions webpacker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ Gem::Specification.new do |s|
"changelog_uri" => "https://github.com/rails/webpacker/blob/v#{Webpacker::VERSION}/CHANGELOG.md"
}

s.required_ruby_version = ">= 2.4.0"
s.required_ruby_version = ">= 2.3.0"

s.add_dependency "activesupport", ">= 5.2"
s.add_dependency "railties", ">= 5.2"
s.add_dependency "activesupport", ">= 3.2.22"
s.add_dependency "railties", ">= 3.2.22"
s.add_dependency "rack-proxy", ">= 0.6.1"
s.add_dependency "semantic_range", ">= 2.3.0"

Expand Down

0 comments on commit 429ae7d

Please sign in to comment.