From 429ae7d79b0d4aee203d4ce43ea3233f0bfb8153 Mon Sep 17 00:00:00 2001 From: monlu Date: Thu, 2 Jul 2020 21:23:29 -0400 Subject: [PATCH] webpacker backport --- Gemfile | 5 ++++ Gemfile.lock | 11 +++++++-- lib/extensions/hash.rb | 25 +++++++++++++++++++ lib/extensions/string.rb | 45 ++++++++++++++++++++++++++++++++++ lib/install/react.rb | 1 + lib/webpacker.rb | 5 ++-- lib/webpacker/compiler.rb | 2 +- lib/webpacker/configuration.rb | 2 +- lib/webpacker/env.rb | 2 +- lib/webpacker/instance.rb | 2 +- webpacker.gemspec | 6 ++--- 11 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 lib/extensions/hash.rb create mode 100644 lib/extensions/string.rb diff --git a/Gemfile b/Gemfile index 8760baf61..c27012cbb 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,8 @@ group :test do gem "minitest", "~> 5.0" gem "byebug" end + +group :development, :test do + gem 'pry' + gem 'rb-readline' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index fc55baeca..93ff87835 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 diff --git a/lib/extensions/hash.rb b/lib/extensions/hash.rb new file mode 100644 index 000000000..f41750ef7 --- /dev/null +++ b/lib/extensions/hash.rb @@ -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 \ No newline at end of file diff --git a/lib/extensions/string.rb b/lib/extensions/string.rb new file mode 100644 index 000000000..be894290c --- /dev/null +++ b/lib/extensions/string.rb @@ -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: + # + # < + # 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 \ No newline at end of file diff --git a/lib/install/react.rb b/lib/install/react.rb index c08e39717..e3b30f55a 100644 --- a/lib/install/react.rb +++ b/lib/install/react.rb @@ -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") diff --git a/lib/webpacker.rb b/lib/webpacker.rb index 8962f20ff..dcca68bd7 100644 --- a/lib/webpacker.rb +++ b/lib/webpacker.rb @@ -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 @@ -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 diff --git a/lib/webpacker/compiler.rb b/lib/webpacker/compiler.rb index 8c1ce6e35..f6ccf416b 100644 --- a/lib/webpacker/compiler.rb +++ b/lib/webpacker/compiler.rb @@ -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 diff --git a/lib/webpacker/configuration.rb b/lib/webpacker/configuration.rb index d0a696efd..0986b1f7c 100644 --- a/lib/webpacker/configuration.rb +++ b/lib/webpacker/configuration.rb @@ -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 diff --git a/lib/webpacker/env.rb b/lib/webpacker/env.rb index 8e5482545..4726c1b4b 100644 --- a/lib/webpacker/env.rb +++ b/lib/webpacker/env.rb @@ -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 diff --git a/lib/webpacker/instance.rb b/lib/webpacker/instance.rb index 70deb00d0..c26861de2 100644 --- a/lib/webpacker/instance.rb +++ b/lib/webpacker/instance.rb @@ -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 diff --git a/webpacker.gemspec b/webpacker.gemspec index 2b071c220..0585d581c 100644 --- a/webpacker.gemspec +++ b/webpacker.gemspec @@ -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"