From b96622903104811bb8272d5f177a2a08680712e1 Mon Sep 17 00:00:00 2001 From: Mikhail Boutylin Date: Wed, 3 Jul 2024 10:56:27 +0400 Subject: [PATCH] Use package_json gem (#1639) * feat: use `package_json` * fixed shakapacker detection during the installation generator work * fixed tests, updated changelog * added package_json to dev dependencies * fixed webpacker example * linted webpacker spec * removed usless import --- CHANGELOG.md | 5 +++ Gemfile.development_dependencies | 1 + Gemfile.lock | 33 ++++++++++--------- docs/getting-started.md | 3 +- .../react_on_rails/base_generator.rb | 28 ++++++++++++---- .../react_on_rails/generator_helper.rb | 5 +++ rakelib/webpacker_examples.rake | 6 +++- 7 files changed, 55 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db6b374a0..05fcde1ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,9 +18,14 @@ 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 1639](https://github.com/shakacode/react_on_rails/pull/1639) by [vaukalak](https://github.com/vaukalak). + #### Changed - Update outdated GitHub Actions to use Node.js 20.0 versions instead [PR 1623](https://github.com/shakacode/react_on_rails/pull/1623) by [adriangohjw](https://github.com/adriangohjw). + ### [14.0.3] - 2024-06-28 #### Fixed 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 6d766c6cd..af4b3f945 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 @@ -408,6 +408,7 @@ DEPENDENCIES jquery-rails launchy listen + package_json pry pry-byebug pry-doc 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 2527d91fa..cf7e851d4 100644 --- a/lib/generators/react_on_rails/base_generator.rb +++ b/lib/generators/react_on_rails/base_generator.rb @@ -76,26 +76,40 @@ 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) - 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 latest 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 -D 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" - 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..38414c17c 100644 --- a/lib/generators/react_on_rails/generator_helper.rb +++ b/lib/generators/react_on_rails/generator_helper.rb @@ -1,8 +1,13 @@ # frozen_string_literal: true +require "package_json" require "rainbow" module GeneratorHelper + def package_json + @package_json ||= PackageJson.read + 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/rakelib/webpacker_examples.rake b/rakelib/webpacker_examples.rake index 2925bd5e9..ddc40affc 100644 --- a/rakelib/webpacker_examples.rake +++ b/rakelib/webpacker_examples.rake @@ -37,7 +37,11 @@ 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 = [] + 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