From 6c0a29ebff3b584b6446f1065dec23b6bf7ffb50 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 9 Sep 2023 13:14:27 +1200 Subject: [PATCH 1/7] feat: use `package_json` --- .../react_on_rails/base_generator.rb | 26 ++++++++++++++----- .../react_on_rails/generator_helper.rb | 18 +++++++++++++ .../react_with_redux_generator.rb | 1 + 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/generators/react_on_rails/base_generator.rb b/lib/generators/react_on_rails/base_generator.rb index 14148dfc9..af98508d6 100644 --- a/lib/generators/react_on_rails/base_generator.rb +++ b/lib/generators/react_on_rails/base_generator.rb @@ -78,23 +78,37 @@ def add_base_gems_to_gemfile def add_yarn_dependencies major_minor_patch_only = /\A\d+\.\d+\.\d+\z/ if ReactOnRails::VERSION.match?(major_minor_patch_only) - run "yarn add react-on-rails@#{ReactOnRails::VERSION} --exact" + package_json.manager.add(["react-on-rails@#{ReactOnRails::VERSION}"]) else # otherwise add latest puts "Adding the lastest react-on-rails NPM module. Double check this is correct in package.json" - run "yarn add react-on-rails --exact" + package_json.manager.add(["react-on-rails"]) end puts "Adding React dependencies" - run "yarn add react react-dom @babel/preset-react prop-types babel-plugin-transform-react-remove-prop-types \ - babel-plugin-macros" + package_json.manager.add([ + "react", + "react-dom", + "@babel/preset-react", + "prop-types", + "babel-plugin-transform-react-remove-prop-types", + "babel-plugin-macros" + ]) puts "Adding CSS handlers" - run "yarn add css-loader css-minimizer-webpack-plugin mini-css-extract-plugin style-loader@" + package_json.manager.add([ + "css-loader", + "css-minimizer-webpack-plugin", + "mini-css-extract-plugin", + "style-loader@" + ]) puts "Adding dev dependencies" - run "yarn add -D @pmmmwh/react-refresh-webpack-plugin react-refresh" + package_json.manager.add([ + "@pmmmwh/react-refresh-webpack-plugin", + "react-refresh" + ], type: :dev) end def append_to_spec_rails_helper diff --git a/lib/generators/react_on_rails/generator_helper.rb b/lib/generators/react_on_rails/generator_helper.rb index 281df870e..440ba6ba0 100644 --- a/lib/generators/react_on_rails/generator_helper.rb +++ b/lib/generators/react_on_rails/generator_helper.rb @@ -3,6 +3,24 @@ require "rainbow" module GeneratorHelper + def require_package_json_gem + require "bundler/inline" + + gemfile { gem "package_json", github: "G-Rath/package_json" } + + puts "using package_json v#{PackageJson::VERSION}" + end + + def package_json + if @package_json.nil? + require_package_json_gem + + @package_json = PackageJson.read(@app_path) + end + + @package_json + end + # Takes a relative path from the destination root, such as `.gitignore` or `app/assets/javascripts/application.js` def dest_file_exists?(file) dest_file = File.join(destination_root, file) diff --git a/lib/generators/react_on_rails/react_with_redux_generator.rb b/lib/generators/react_on_rails/react_with_redux_generator.rb index 2b68c9520..4b25bf5fc 100644 --- a/lib/generators/react_on_rails/react_with_redux_generator.rb +++ b/lib/generators/react_on_rails/react_with_redux_generator.rb @@ -5,6 +5,7 @@ module ReactOnRails module Generators class ReactWithReduxGenerator < Rails::Generators::Base + include GeneratorHelper Rails::Generators.hide_namespace(namespace) source_root(File.expand_path("templates", __dir__)) From c281479bfdb46f1011ef21bf81ed05a49959eafc Mon Sep 17 00:00:00 2001 From: Mikhail Boutylin Date: Wed, 26 Jun 2024 17:31:59 +0400 Subject: [PATCH 2/7] fixed shakapacker detection during the installation generator work --- Gemfile.lock | 6 +++- .../react_on_rails/base_generator.rb | 32 +++++++++---------- .../react_on_rails/generator_helper.rb | 17 ++-------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 728c05d38..d041068f2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - react_on_rails (14.0.0) + react_on_rails (14.0.2) addressable connection_pool execjs (~> 2.5) @@ -119,6 +119,9 @@ GEM tins (~> 1.6) crass (1.0.6) date (3.3.4) + debug (1.9.2) + irb (~> 1.10) + reline (>= 0.3.8) diff-lcs (1.5.1) docile (1.4.0) drb (2.2.1) @@ -397,6 +400,7 @@ DEPENDENCIES capybara capybara-screenshot coveralls + debug equivalent-xml gem-release generator_spec diff --git a/lib/generators/react_on_rails/base_generator.rb b/lib/generators/react_on_rails/base_generator.rb index 8c127cd01..9c1644e9d 100644 --- a/lib/generators/react_on_rails/base_generator.rb +++ b/lib/generators/react_on_rails/base_generator.rb @@ -88,28 +88,28 @@ def add_yarn_dependencies puts "Adding React dependencies" package_json.manager.add([ - "react", - "react-dom", - "@babel/preset-react", - "prop-types", - "babel-plugin-transform-react-remove-prop-types", - "babel-plugin-macros" - ]) + "react", + "react-dom", + "@babel/preset-react", + "prop-types", + "babel-plugin-transform-react-remove-prop-types", + "babel-plugin-macros" + ]) puts "Adding CSS handlers" - package_json.manager.add([ - "css-loader", - "css-minimizer-webpack-plugin", - "mini-css-extract-plugin", - "style-loader" - ]) + package_json.manager.add(%w[ + css-loader + css-minimizer-webpack-plugin + mini-css-extract-plugin + style-loader + ]) puts "Adding dev dependencies" package_json.manager.add([ - "@pmmmwh/react-refresh-webpack-plugin", - "react-refresh" - ], type: :dev) + "@pmmmwh/react-refresh-webpack-plugin", + "react-refresh" + ], type: :dev) end def append_to_spec_rails_helper diff --git a/lib/generators/react_on_rails/generator_helper.rb b/lib/generators/react_on_rails/generator_helper.rb index 440ba6ba0..bb363911c 100644 --- a/lib/generators/react_on_rails/generator_helper.rb +++ b/lib/generators/react_on_rails/generator_helper.rb @@ -1,24 +1,11 @@ # frozen_string_literal: true +require "package_json" require "rainbow" module GeneratorHelper - def require_package_json_gem - require "bundler/inline" - - gemfile { gem "package_json", github: "G-Rath/package_json" } - - puts "using package_json v#{PackageJson::VERSION}" - end - def package_json - if @package_json.nil? - require_package_json_gem - - @package_json = PackageJson.read(@app_path) - end - - @package_json + @package_json ||= PackageJson.read(Rails.root.to_s) end # Takes a relative path from the destination root, such as `.gitignore` or `app/assets/javascripts/application.js` From 8bbc55dd16385a6d296bb2f0e5f4b84f85691181 Mon Sep 17 00:00:00 2001 From: Mikhail Boutylin Date: Sun, 30 Jun 2024 22:04:41 +0400 Subject: [PATCH 3/7] fixed tests, updated changelog --- CHANGELOG.md | 3 ++ Gemfile.lock | 32 +++++++++---------- docs/getting-started.md | 3 +- .../react_on_rails/base_generator.rb | 2 +- .../react_on_rails/generator_helper.rb | 2 +- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69c4136aa..6304e9bf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac ### [Unreleased] Changes since the last non-beta release. +#### Improved +- Improved dependency management by integrating package_json. [PR 1544](https://github.com/shakacode/react_on_rails/pull/1639) by [vaukalak](https://github.com/vaukalak). + ### [14.0.3] - 2024-06-28 #### Fixed diff --git a/Gemfile.lock b/Gemfile.lock index 6d766c6cd..380a6f2bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - react_on_rails (14.0.2) + react_on_rails (14.0.3) addressable connection_pool execjs (~> 2.5) @@ -93,7 +93,7 @@ GEM bigdecimal (3.1.8) bootsnap (1.18.3) msgpack (~> 1.2) - builder (3.2.4) + builder (3.3.0) byebug (11.1.3) capybara (3.40.0) addressable @@ -109,7 +109,7 @@ GEM launchy childprocess (5.0.0) coderay (1.1.3) - concurrent-ruby (1.2.3) + concurrent-ruby (1.3.3) connection_pool (2.4.1) coveralls (0.8.23) json (>= 1.8, < 3) @@ -127,7 +127,7 @@ GEM drb (2.2.1) equivalent-xml (0.6.0) nokogiri (>= 1.4.3) - erubi (1.12.0) + erubi (1.13.0) execjs (2.9.1) ffi (1.16.3) gem-release (2.2.2) @@ -140,7 +140,7 @@ GEM concurrent-ruby (~> 1.0) interception (0.5) io-console (0.7.2) - irb (1.13.1) + irb (1.13.2) rdoc (>= 4.0.0) reline (>= 0.4.2) jbuilder (2.12.0) @@ -170,8 +170,8 @@ GEM matrix (0.4.2) method_source (1.1.0) mini_mime (1.1.5) - mini_portile2 (2.8.6) - minitest (5.23.0) + mini_portile2 (2.8.7) + minitest (5.24.1) msgpack (1.7.2) mutex_m (0.2.0) net-imap (0.4.11) @@ -184,7 +184,7 @@ GEM net-smtp (0.5.0) net-protocol nio4r (2.7.3) - nokogiri (1.16.5) + nokogiri (1.16.6) mini_portile2 (~> 2.8.2) racc (~> 1.4) package_json (0.1.0) @@ -211,8 +211,8 @@ GEM public_suffix (5.0.5) puma (6.4.2) nio4r (~> 2.0) - racc (1.7.3) - rack (3.0.11) + racc (1.8.0) + rack (3.1.4) rack-proxy (0.7.7) rack rack-session (2.0.0) @@ -256,22 +256,22 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) - rdoc (6.6.3.1) + rdoc (6.7.0) psych (>= 4.0.0) regexp_parser (2.9.2) - reline (0.5.7) + reline (0.5.9) io-console (~> 0.5) rexml (3.2.7) strscan (>= 3.0.9) rspec-core (3.13.0) rspec-support (~> 3.13.0) - rspec-expectations (3.13.0) + rspec-expectations (3.13.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) rspec-mocks (3.13.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) - rspec-rails (6.1.2) + rspec-rails (6.1.3) actionpack (>= 6.1) activesupport (>= 6.1) railties (>= 6.1) @@ -358,7 +358,7 @@ GEM sprockets (>= 3.0.0) sqlite3 (1.7.3) mini_portile2 (~> 2.8.0) - stringio (3.1.0) + stringio (3.1.1) strscan (3.1.0) sync (0.5.0) term-ansicolor (1.8.0) @@ -389,7 +389,7 @@ GEM xpath (3.2.0) nokogiri (~> 1.8) yard (0.9.36) - zeitwerk (2.6.14) + zeitwerk (2.6.16) PLATFORMS ruby diff --git a/docs/getting-started.md b/docs/getting-started.md index 05500a53d..e26f490a0 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -17,8 +17,7 @@ You need a Rails application with Shakapacker installed and configured on it. Ch rails new PROJECT_NAME --skip-javascript cd PROJECT_NAME bundle add shakapacker --strict -# currently react_on_rails uses yarn classic by default -PACKAGE_JSON_FALLBACK_MANAGER=yarn_classic rails shakapacker:install +rails shakapacker:install ``` You may need to check [the instructions for installing into an existing Rails app](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/) if you have an already working Rails application. diff --git a/lib/generators/react_on_rails/base_generator.rb b/lib/generators/react_on_rails/base_generator.rb index c05a49375..cf7e851d4 100644 --- a/lib/generators/react_on_rails/base_generator.rb +++ b/lib/generators/react_on_rails/base_generator.rb @@ -76,7 +76,7 @@ def add_base_gems_to_gemfile run "bundle" end - def add_yarn_dependencies + def add_js_dependencies major_minor_patch_only = /\A\d+\.\d+\.\d+\z/ if ReactOnRails::VERSION.match?(major_minor_patch_only) package_json.manager.add(["react-on-rails@#{ReactOnRails::VERSION}"]) diff --git a/lib/generators/react_on_rails/generator_helper.rb b/lib/generators/react_on_rails/generator_helper.rb index bb363911c..38414c17c 100644 --- a/lib/generators/react_on_rails/generator_helper.rb +++ b/lib/generators/react_on_rails/generator_helper.rb @@ -5,7 +5,7 @@ module GeneratorHelper def package_json - @package_json ||= PackageJson.read(Rails.root.to_s) + @package_json ||= PackageJson.read end # Takes a relative path from the destination root, such as `.gitignore` or `app/assets/javascripts/application.js` From 0d0f63ef95109e41fc080ee95a7eb2a7e5518190 Mon Sep 17 00:00:00 2001 From: Mikhail Boutylin Date: Sun, 30 Jun 2024 22:22:02 +0400 Subject: [PATCH 4/7] added package_json to dev dependencies --- Gemfile.development_dependencies | 1 + Gemfile.lock | 1 + 2 files changed, 2 insertions(+) diff --git a/Gemfile.development_dependencies b/Gemfile.development_dependencies index f972b719f..ea9496173 100644 --- a/Gemfile.development_dependencies +++ b/Gemfile.development_dependencies @@ -23,6 +23,7 @@ gem "sprockets", "~> 4.0" gem "amazing_print" group :development, :test do + gem "package_json" gem "listen" gem "debug" gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index 380a6f2bc..af4b3f945 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -408,6 +408,7 @@ DEPENDENCIES jquery-rails launchy listen + package_json pry pry-byebug pry-doc From 9d5e8c2e1cc651f7871e354ce0e4c5fd20839a84 Mon Sep 17 00:00:00 2001 From: Mikhail Boutylin Date: Sun, 30 Jun 2024 22:39:27 +0400 Subject: [PATCH 5/7] fixed webpacker example --- rakelib/webpacker_examples.rake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rakelib/webpacker_examples.rake b/rakelib/webpacker_examples.rake index 2925bd5e9..206f2d6cb 100644 --- a/rakelib/webpacker_examples.rake +++ b/rakelib/webpacker_examples.rake @@ -37,7 +37,9 @@ namespace :webpacker_examples do # rubocop:disable Metrics/BlockLength sh_in_dir(example_type.dir, "echo \"gem 'shakapacker', '~> 6.6.0'\" >> #{example_type.gemfile}") bundle_install_in(example_type.dir) sh_in_dir(example_type.dir, "rake webpacker:install") - sh_in_dir(example_type.dir, example_type.generator_shell_commands) + shell_commands = [] + shell_commands << "PACKAGE_JSON_FALLBACK_MANAGER=yarn_classic rails generate react_on_rails:install #{example_type.generator_options} --ignore-warnings --force" + shell_commands << "PACKAGE_JSON_FALLBACK_MANAGER=yarn_classic rails generate react_on_rails:dev_tests #{example_type.generator_options}" sh_in_dir(example_type.dir, "yarn") end end From fa701859b09eaadde521c684d99d46e523c25157 Mon Sep 17 00:00:00 2001 From: Mikhail Boutylin Date: Sun, 30 Jun 2024 22:47:59 +0400 Subject: [PATCH 6/7] linted webpacker spec --- CHANGELOG.md | 2 +- rakelib/webpacker_examples.rake | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6304e9bf3..01d0b867d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac Changes since the last non-beta release. #### Improved -- Improved dependency management by integrating package_json. [PR 1544](https://github.com/shakacode/react_on_rails/pull/1639) by [vaukalak](https://github.com/vaukalak). +- Improved dependency management by integrating package_json. [PR 1639](https://github.com/shakacode/react_on_rails/pull/1639) by [vaukalak](https://github.com/vaukalak). ### [14.0.3] - 2024-06-28 diff --git a/rakelib/webpacker_examples.rake b/rakelib/webpacker_examples.rake index 206f2d6cb..ddc40affc 100644 --- a/rakelib/webpacker_examples.rake +++ b/rakelib/webpacker_examples.rake @@ -38,8 +38,10 @@ namespace :webpacker_examples do # rubocop:disable Metrics/BlockLength bundle_install_in(example_type.dir) sh_in_dir(example_type.dir, "rake webpacker:install") shell_commands = [] - shell_commands << "PACKAGE_JSON_FALLBACK_MANAGER=yarn_classic rails generate react_on_rails:install #{example_type.generator_options} --ignore-warnings --force" - shell_commands << "PACKAGE_JSON_FALLBACK_MANAGER=yarn_classic rails generate react_on_rails:dev_tests #{example_type.generator_options}" + env = "PACKAGE_JSON_FALLBACK_MANAGER=yarn_classic" + options = example_type.generator_options + shell_commands << "#{env} rails generate react_on_rails:install #{options} --ignore-warnings --force" + shell_commands << "#{env} rails generate react_on_rails:dev_tests #{options}" sh_in_dir(example_type.dir, "yarn") end end From af73a83058da4ca10eb8a84d185c1eb4f5996fa2 Mon Sep 17 00:00:00 2001 From: Mikhail Boutylin Date: Wed, 3 Jul 2024 08:22:21 +0400 Subject: [PATCH 7/7] removed usless import --- lib/generators/react_on_rails/react_with_redux_generator.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/generators/react_on_rails/react_with_redux_generator.rb b/lib/generators/react_on_rails/react_with_redux_generator.rb index 4b25bf5fc..2b68c9520 100644 --- a/lib/generators/react_on_rails/react_with_redux_generator.rb +++ b/lib/generators/react_on_rails/react_with_redux_generator.rb @@ -5,7 +5,6 @@ module ReactOnRails module Generators class ReactWithReduxGenerator < Rails::Generators::Base - include GeneratorHelper Rails::Generators.hide_namespace(namespace) source_root(File.expand_path("templates", __dir__))