Skip to content

Commit b966229

Browse files
authored
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
1 parent 6a9c101 commit b966229

File tree

7 files changed

+55
-26
lines changed

7 files changed

+55
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac
1818
### [Unreleased]
1919
Changes since the last non-beta release.
2020

21+
22+
#### Improved
23+
- Improved dependency management by integrating package_json. [PR 1639](https://github.com/shakacode/react_on_rails/pull/1639) by [vaukalak](https://github.com/vaukalak).
24+
2125
#### Changed
2226
- 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).
2327

28+
2429
### [14.0.3] - 2024-06-28
2530

2631
#### Fixed

Gemfile.development_dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ gem "sprockets", "~> 4.0"
2323
gem "amazing_print"
2424

2525
group :development, :test do
26+
gem "package_json"
2627
gem "listen"
2728
gem "debug"
2829
gem "pry"

Gemfile.lock

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
react_on_rails (14.0.2)
4+
react_on_rails (14.0.3)
55
addressable
66
connection_pool
77
execjs (~> 2.5)
@@ -93,7 +93,7 @@ GEM
9393
bigdecimal (3.1.8)
9494
bootsnap (1.18.3)
9595
msgpack (~> 1.2)
96-
builder (3.2.4)
96+
builder (3.3.0)
9797
byebug (11.1.3)
9898
capybara (3.40.0)
9999
addressable
@@ -109,7 +109,7 @@ GEM
109109
launchy
110110
childprocess (5.0.0)
111111
coderay (1.1.3)
112-
concurrent-ruby (1.2.3)
112+
concurrent-ruby (1.3.3)
113113
connection_pool (2.4.1)
114114
coveralls (0.8.23)
115115
json (>= 1.8, < 3)
@@ -127,7 +127,7 @@ GEM
127127
drb (2.2.1)
128128
equivalent-xml (0.6.0)
129129
nokogiri (>= 1.4.3)
130-
erubi (1.12.0)
130+
erubi (1.13.0)
131131
execjs (2.9.1)
132132
ffi (1.16.3)
133133
gem-release (2.2.2)
@@ -140,7 +140,7 @@ GEM
140140
concurrent-ruby (~> 1.0)
141141
interception (0.5)
142142
io-console (0.7.2)
143-
irb (1.13.1)
143+
irb (1.13.2)
144144
rdoc (>= 4.0.0)
145145
reline (>= 0.4.2)
146146
jbuilder (2.12.0)
@@ -170,8 +170,8 @@ GEM
170170
matrix (0.4.2)
171171
method_source (1.1.0)
172172
mini_mime (1.1.5)
173-
mini_portile2 (2.8.6)
174-
minitest (5.23.0)
173+
mini_portile2 (2.8.7)
174+
minitest (5.24.1)
175175
msgpack (1.7.2)
176176
mutex_m (0.2.0)
177177
net-imap (0.4.11)
@@ -184,7 +184,7 @@ GEM
184184
net-smtp (0.5.0)
185185
net-protocol
186186
nio4r (2.7.3)
187-
nokogiri (1.16.5)
187+
nokogiri (1.16.6)
188188
mini_portile2 (~> 2.8.2)
189189
racc (~> 1.4)
190190
package_json (0.1.0)
@@ -211,8 +211,8 @@ GEM
211211
public_suffix (5.0.5)
212212
puma (6.4.2)
213213
nio4r (~> 2.0)
214-
racc (1.7.3)
215-
rack (3.0.11)
214+
racc (1.8.0)
215+
rack (3.1.4)
216216
rack-proxy (0.7.7)
217217
rack
218218
rack-session (2.0.0)
@@ -256,22 +256,22 @@ GEM
256256
rb-fsevent (0.11.2)
257257
rb-inotify (0.10.1)
258258
ffi (~> 1.0)
259-
rdoc (6.6.3.1)
259+
rdoc (6.7.0)
260260
psych (>= 4.0.0)
261261
regexp_parser (2.9.2)
262-
reline (0.5.7)
262+
reline (0.5.9)
263263
io-console (~> 0.5)
264264
rexml (3.2.7)
265265
strscan (>= 3.0.9)
266266
rspec-core (3.13.0)
267267
rspec-support (~> 3.13.0)
268-
rspec-expectations (3.13.0)
268+
rspec-expectations (3.13.1)
269269
diff-lcs (>= 1.2.0, < 2.0)
270270
rspec-support (~> 3.13.0)
271271
rspec-mocks (3.13.1)
272272
diff-lcs (>= 1.2.0, < 2.0)
273273
rspec-support (~> 3.13.0)
274-
rspec-rails (6.1.2)
274+
rspec-rails (6.1.3)
275275
actionpack (>= 6.1)
276276
activesupport (>= 6.1)
277277
railties (>= 6.1)
@@ -358,7 +358,7 @@ GEM
358358
sprockets (>= 3.0.0)
359359
sqlite3 (1.7.3)
360360
mini_portile2 (~> 2.8.0)
361-
stringio (3.1.0)
361+
stringio (3.1.1)
362362
strscan (3.1.0)
363363
sync (0.5.0)
364364
term-ansicolor (1.8.0)
@@ -389,7 +389,7 @@ GEM
389389
xpath (3.2.0)
390390
nokogiri (~> 1.8)
391391
yard (0.9.36)
392-
zeitwerk (2.6.14)
392+
zeitwerk (2.6.16)
393393

394394
PLATFORMS
395395
ruby
@@ -408,6 +408,7 @@ DEPENDENCIES
408408
jquery-rails
409409
launchy
410410
listen
411+
package_json
411412
pry
412413
pry-byebug
413414
pry-doc

docs/getting-started.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ You need a Rails application with Shakapacker installed and configured on it. Ch
1717
rails new PROJECT_NAME --skip-javascript
1818
cd PROJECT_NAME
1919
bundle add shakapacker --strict
20-
# currently react_on_rails uses yarn classic by default
21-
PACKAGE_JSON_FALLBACK_MANAGER=yarn_classic rails shakapacker:install
20+
rails shakapacker:install
2221
```
2322

2423
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.

lib/generators/react_on_rails/base_generator.rb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,40 @@ def add_base_gems_to_gemfile
7676
run "bundle"
7777
end
7878

79-
def add_yarn_dependencies
79+
def add_js_dependencies
8080
major_minor_patch_only = /\A\d+\.\d+\.\d+\z/
8181
if ReactOnRails::VERSION.match?(major_minor_patch_only)
82-
run "yarn add react-on-rails@#{ReactOnRails::VERSION} --exact"
82+
package_json.manager.add(["react-on-rails@#{ReactOnRails::VERSION}"])
8383
else
8484
# otherwise add latest
8585
puts "Adding the latest react-on-rails NPM module. Double check this is correct in package.json"
86-
run "yarn add react-on-rails --exact"
86+
package_json.manager.add(["react-on-rails"])
8787
end
8888

8989
puts "Adding React dependencies"
90-
run "yarn add react react-dom @babel/preset-react prop-types babel-plugin-transform-react-remove-prop-types \
91-
babel-plugin-macros"
90+
package_json.manager.add([
91+
"react",
92+
"react-dom",
93+
"@babel/preset-react",
94+
"prop-types",
95+
"babel-plugin-transform-react-remove-prop-types",
96+
"babel-plugin-macros"
97+
])
9298

9399
puts "Adding CSS handlers"
94100

95-
run "yarn add -D css-loader css-minimizer-webpack-plugin mini-css-extract-plugin style-loader"
101+
package_json.manager.add(%w[
102+
css-loader
103+
css-minimizer-webpack-plugin
104+
mini-css-extract-plugin
105+
style-loader
106+
])
96107

97108
puts "Adding dev dependencies"
98-
run "yarn add -D @pmmmwh/react-refresh-webpack-plugin react-refresh"
109+
package_json.manager.add([
110+
"@pmmmwh/react-refresh-webpack-plugin",
111+
"react-refresh"
112+
], type: :dev)
99113
end
100114

101115
def append_to_spec_rails_helper

lib/generators/react_on_rails/generator_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# frozen_string_literal: true
22

3+
require "package_json"
34
require "rainbow"
45

56
module GeneratorHelper
7+
def package_json
8+
@package_json ||= PackageJson.read
9+
end
10+
611
# Takes a relative path from the destination root, such as `.gitignore` or `app/assets/javascripts/application.js`
712
def dest_file_exists?(file)
813
dest_file = File.join(destination_root, file)

rakelib/webpacker_examples.rake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ namespace :webpacker_examples do # rubocop:disable Metrics/BlockLength
3737
sh_in_dir(example_type.dir, "echo \"gem 'shakapacker', '~> 6.6.0'\" >> #{example_type.gemfile}")
3838
bundle_install_in(example_type.dir)
3939
sh_in_dir(example_type.dir, "rake webpacker:install")
40-
sh_in_dir(example_type.dir, example_type.generator_shell_commands)
40+
shell_commands = []
41+
env = "PACKAGE_JSON_FALLBACK_MANAGER=yarn_classic"
42+
options = example_type.generator_options
43+
shell_commands << "#{env} rails generate react_on_rails:install #{options} --ignore-warnings --force"
44+
shell_commands << "#{env} rails generate react_on_rails:dev_tests #{options}"
4145
sh_in_dir(example_type.dir, "yarn")
4246
end
4347
end

0 commit comments

Comments
 (0)