Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use package_json gem #1639

Merged
merged 10 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac
### [Unreleased]
vaukalak marked this conversation as resolved.
Show resolved Hide resolved
Changes since the last non-beta release.

#### Improved
vaukalak marked this conversation as resolved.
Show resolved Hide resolved
- Improved dependency management by integrating package_json. [PR 1639](https://github.com/shakacode/react_on_rails/pull/1639) by [vaukalak](https://github.com/vaukalak).
vaukalak marked this conversation as resolved.
Show resolved Hide resolved

### [14.0.3] - 2024-06-28

#### Fixed
Expand Down
1 change: 1 addition & 0 deletions Gemfile.development_dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem "sprockets", "~> 4.0"
gem "amazing_print"

group :development, :test do
gem "package_json"
gem "listen"
gem "debug"
gem "pry"
Expand Down
33 changes: 17 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -408,6 +408,7 @@ DEPENDENCIES
jquery-rails
launchy
listen
package_json
pry
pry-byebug
pry-doc
Expand Down
3 changes: 1 addition & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 21 additions & 7 deletions lib/generators/react_on_rails/base_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/react_on_rails/generator_helper.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module ReactOnRails
module Generators
class ReactWithReduxGenerator < Rails::Generators::Base
include GeneratorHelper
vaukalak marked this conversation as resolved.
Show resolved Hide resolved
Rails::Generators.hide_namespace(namespace)
source_root(File.expand_path("templates", __dir__))

Expand Down
6 changes: 5 additions & 1 deletion rakelib/webpacker_examples.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading