Skip to content

Commit 2f1d694

Browse files
committed
Swap default package manager from yarn to NPM
1 parent 7d5afa9 commit 2f1d694

File tree

10 files changed

+29
-29
lines changed

10 files changed

+29
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Use [Bun](https://bun.sh), [esbuild](https://esbuild.github.io), [rollup.js](https://rollupjs.org), or [Webpack](https://webpack.js.org) to bundle your JavaScript, then deliver it via the asset pipeline in Rails. This gem provides installers to get you going with the bundler of your choice in a new Rails application, and a convention to use `app/assets/builds` to hold your bundled output as artifacts that are not checked into source control (the installer adds this directory to `.gitignore` by default).
44

5-
You develop using this approach by running the bundler in watch mode in a terminal with `yarn build --watch` (and your Rails server in another, if you're not using something like [puma-dev](https://github.com/puma/puma-dev)). You can also use `./bin/dev`, which will start both the Rails server and the JS build watcher (along with a CSS build watcher, if you're also using `cssbundling-rails`).
5+
You develop using this approach by running the bundler in watch mode in a terminal with `npm run build -- --watch` (and your Rails server in another, if you're not using something like [puma-dev](https://github.com/puma/puma-dev)). You can also use `./bin/dev`, which will start both the Rails server and the JS build watcher (along with a CSS build watcher, if you're also using `cssbundling-rails`).
66

77
Whenever the bundler detects changes to any of the JavaScript files in your project, it'll bundle `app/javascript/application.js` into `app/assets/builds/application.js` (and all other entry points configured). You can refer to the build output in your layout using the standard asset pipeline approach with `<%= javascript_include_tag "application", defer: true %>`.
88

lib/install/Procfile.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
web: env RUBY_DEBUG_OPEN=true bin/rails server
2-
js: yarn build --watch
2+
js: npm run build -- --watch

lib/install/esbuild/install.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
apply "#{__dir__}/../install_procfile.rb"
33

44
say "Install esbuild"
5-
run "yarn add --dev esbuild"
5+
run "npm install -D esbuild"
66

77
say "Add build script"
88
build_script = "esbuild app/javascript/*.* --bundle --sourcemap --format=esm --outdir=app/assets/builds --public-path=/assets"
99

1010
case `npx -v`.to_f
1111
when 7.1...8.0
1212
run %(npm set-script build "#{build_script}")
13-
run %(yarn build)
13+
run %(npm run build)
1414
when (8.0..)
1515
run %(npm pkg set scripts.build="#{build_script}")
16-
run %(yarn build)
16+
run %(npm run build)
1717
else
1818
say %(Add "scripts": { "build": "#{build_script}" } to your package.json), :green
1919
end

lib/install/install_procfile.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
if Rails.root.join("Procfile.dev").exist?
2-
append_to_file "Procfile.dev", "js: yarn build --watch\n"
2+
append_to_file "Procfile.dev", "js: npm run build -- --watch\n"
33
else
44
say "Add default Procfile.dev"
55
copy_file "#{__dir__}/Procfile.dev", "Procfile.dev"

lib/install/rollup/install.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44
say "Install rollup with config"
55
copy_file "#{__dir__}/rollup.config.js", "rollup.config.js"
6-
run "yarn add --dev rollup @rollup/plugin-node-resolve"
6+
run "npm install -D rollup @rollup/plugin-node-resolve"
77

88
say "Add build script"
99
build_script = "rollup -c --bundleConfigAsCjs rollup.config.js"
1010

1111
case `npx -v`.to_f
1212
when 7.1...8.0
1313
run %(npm set-script build "#{build_script}")
14-
run %(yarn build)
14+
run %(npm run build)
1515
when (8.0..)
1616
run %(npm pkg set scripts.build="#{build_script}")
17-
run %(yarn build)
17+
run %(npm run build)
1818
else
1919
say %(Add "scripts": { "build": "#{build_script}" } to your package.json), :green
2020
end

lib/install/webpack/install.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44
say "Install Webpack with config"
55
copy_file "#{__dir__}/webpack.config.js", "webpack.config.js"
6-
run "yarn add --dev webpack webpack-cli"
6+
run "npm install -D webpack webpack-cli"
77

88
say "Add build script"
99
build_script = "webpack --config webpack.config.js"
1010

1111
case `npx -v`.to_f
1212
when 7.1...8.0
1313
run %(npm set-script build "#{build_script}")
14-
run %(yarn build)
14+
run %(npm run build)
1515
when (8.0..)
1616
run %(npm pkg set scripts.build="#{build_script}")
17-
run %(yarn build)
17+
run %(npm run build)
1818
else
1919
say %(Add "scripts": { "build": "#{build_script}" } to your package.json), :green
2020
end

lib/tasks/jsbundling/build.rake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace :javascript do
1515
end
1616
end
1717

18-
build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"] || ENV["SKIP_BUN_INSTALL"]
18+
build_task.prereqs << :install unless ENV["SKIP_BUN_INSTALL"] || ENV["SKIP_NPM_INSTALL"] || ENV["SKIP_YARN_INSTALL"]
1919
end
2020

2121
module Jsbundling
@@ -25,19 +25,19 @@ module Jsbundling
2525
def install_command
2626
case tool
2727
when :bun then "bun install"
28-
when :yarn then "yarn install"
2928
when :pnpm then "pnpm install"
3029
when :npm then "npm install"
30+
when :yarn then "yarn install"
3131
else raise "jsbundling-rails: No suitable tool found for installing JavaScript dependencies"
3232
end
3333
end
3434

3535
def build_command
3636
case tool
3737
when :bun then "bun run build"
38-
when :yarn then "yarn build"
3938
when :pnpm then "pnpm build"
4039
when :npm then "npm run build"
40+
when :yarn then "yarn build"
4141
else raise "jsbundling-rails: No suitable tool found for building JavaScript"
4242
end
4343
end
@@ -50,14 +50,14 @@ module Jsbundling
5050
case
5151
when File.exist?("bun.lockb") then :bun
5252
when File.exist?("bun.lock") then :bun
53-
when File.exist?("yarn.lock") then :yarn
5453
when File.exist?("pnpm-lock.yaml") then :pnpm
5554
when File.exist?("package-lock.json") then :npm
55+
when File.exist?("yarn.lock") then :yarn
5656
end
5757
end
5858

5959
def tool_determined_by_executable
60-
%i[ bun yarn pnpm npm ].each do |exe|
60+
%i[ bun pnpm npm yarn ].each do |exe|
6161
return exe if system "command -v #{exe} > /dev/null"
6262
end
6363
end

test/esbuild_installer_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ class EsbuildInstallerTest < ActiveSupport::TestCase
1010
out, _err = run_installer
1111

1212
File.read("Procfile.dev").tap do |procfile|
13-
assert_match "js: yarn build --watch", procfile
13+
assert_match "js: npm run build -- --watch", procfile
1414
end
1515

1616
assert_match "STUBBED gem install foreman", out
17-
assert_match %r{STUBBED yarn add.* esbuild}, out
17+
assert_match %r{STUBBED npm install .* esbuild}, out
1818
assert_match %r{STUBBED npm (?:set-script build |pkg set scripts.build=)esbuild app/javascript/\*\.\*}, out
19-
assert_match "STUBBED yarn build", out
19+
assert_match "STUBBED npm run build", out
2020
end
2121
end
2222

2323
private
2424
def run_installer
25-
stub_bins("gem", "yarn", "npm")
25+
stub_bins("gem","npm")
2626
run_command("bin/rails", "javascript:install:esbuild")
2727
end
2828
end

test/rollup_installer_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ class RollupInstallerTest < ActiveSupport::TestCase
1010
out, _err = run_installer
1111

1212
File.read("Procfile.dev").tap do |procfile|
13-
assert_match "js: yarn build --watch", procfile
13+
assert_match "js: npm run build -- --watch", procfile
1414
end
1515

1616
assert_match "STUBBED gem install foreman", out
1717

1818
assert File.exist?("rollup.config.js")
1919

20-
assert_match %r{STUBBED yarn add.* rollup}, out
20+
assert_match %r{STUBBED npm install .* rollup}, out
2121
assert_match %r{STUBBED npm (?:set-script build |pkg set scripts.build=)rollup .*rollup.config.js}, out
22-
assert_match "STUBBED yarn build", out
22+
assert_match "STUBBED npm run build", out
2323
end
2424
end
2525

2626
private
2727
def run_installer
28-
stub_bins("gem", "yarn", "npm")
28+
stub_bins("gem", "npm")
2929
run_command("bin/rails", "javascript:install:rollup")
3030
end
3131
end

test/webpack_installer_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ class WebpackInstallerTest < ActiveSupport::TestCase
1010
out, _err = run_installer
1111

1212
File.read("Procfile.dev").tap do |procfile|
13-
assert_match "js: yarn build --watch", procfile
13+
assert_match "js: npm run build -- --watch", procfile
1414
end
1515

1616
assert_match "STUBBED gem install foreman", out
1717

1818
assert File.exist?("webpack.config.js")
1919

20-
assert_match %r{STUBBED yarn add.* webpack}, out
20+
assert_match %r{STUBBED npm install .* webpack}, out
2121
assert_match %r{STUBBED npm (?:set-script build |pkg set scripts.build=)webpack --config webpack.config.js}, out
22-
assert_match "STUBBED yarn build", out
22+
assert_match "STUBBED npm run build", out
2323
end
2424
end
2525

2626
private
2727
def run_installer
28-
stub_bins("gem", "yarn", "npm")
28+
stub_bins("gem", "npm")
2929
run_command("bin/rails", "javascript:install:webpack")
3030
end
3131
end

0 commit comments

Comments
 (0)