Skip to content

Commit 7f0c34d

Browse files
committed
Swap default package manager from yarn to NPM
See: #217
1 parent 7d5afa9 commit 7f0c34d

File tree

11 files changed

+40
-40
lines changed

11 files changed

+40
-40
lines changed

README.md

Lines changed: 4 additions & 4 deletions
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

@@ -19,6 +19,7 @@ If you're already using [`webpacker`](https://github.com/rails/webpacker) and yo
1919
If you want to use webpack features like [code splitting](https://webpack.js.org/guides/code-splitting/) and [hot module reloading](https://webpack.js.org/concepts/hot-module-replacement/), consider using the official fork of `webpacker`, [`shakapacker`](https://github.com/shakacode/shakapacker).
2020

2121
## Installation
22+
2223
If you are installing esbuild, rollup, or webpack, you must already have node installed on your system. You will also need npx version 7.1.0 or later.
2324

2425
If you are using Bun, then you must have the Bun runtime already installed on
@@ -36,7 +37,6 @@ To get started run:
3637

3738
Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with `rails new myapp -j [bun|esbuild|rollup|webpack]`.
3839

39-
4040
## FAQ
4141

4242
### Is there a work-around for lack of glob syntax on Windows?
@@ -53,8 +53,8 @@ Suppose you have an image `app/javascript/images/example.png` that you need to r
5353

5454
1. Create the image at `app/javascript/images/example.png`.
5555
1. In `package.json`, under `"scripts"` and `"build"`, add the additional arguments:
56-
* `--loader:.png=file` This instructs esbuild to copy png files to the build directory.
57-
* `--asset-names=[name]-[hash].digested` This tells esbuild to append `.digested` to the file name so that sprockets or propshaft will not append an additional digest hash to the file.
56+
- `--loader:.png=file` This instructs esbuild to copy png files to the build directory.
57+
- `--asset-names=[name]-[hash].digested` This tells esbuild to append `.digested` to the file name so that sprockets or propshaft will not append an additional digest hash to the file.
5858
1. When esbuild runs, it will copy the png file to something like `app/assets/builds/example-5SRKKTLZ.digested.png`.
5959
1. In frontend code, the image is available for import by its original name: `import Example from "../images/example.png"`.
6060
1. The image itself can now be referenced by its imported name, e.g. in React, `<img src={Example} />`.

docs/switch_from_webpacker.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Webpacker includes [many dependencies](https://github.com/rails/webpacker/blob/5
9393

9494
```sh
9595
# From the CLI, remove Webpacker packages
96-
yarn remove @rails/webpacker webpack-dev-server
96+
npm uninstall @rails/webpacker webpack-dev-server
9797
```
9898

9999
### Optional: Babel
@@ -191,32 +191,32 @@ yarn add css-loader sass sass-loader mini-css-extract-plugin webpack-remove-empt
191191
// In webpack.config.js
192192

193193
// Extracts CSS into .css file
194-
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
194+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
195195
// Removes exported JavaScript files from CSS-only entries
196196
// in this example, entry.custom will create a corresponding empty custom.js file
197-
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
197+
const RemoveEmptyScriptsPlugin = require("webpack-remove-empty-scripts");
198198

199199
module.exports = {
200200
entry: {
201201
// add your css or sass entries
202202
application: [
203-
'./app/assets/javascripts/application.js',
204-
'./app/assets/stylesheets/application.scss',
203+
"./app/assets/javascripts/application.js",
204+
"./app/assets/stylesheets/application.scss",
205205
],
206-
custom: './app/assets/stylesheets/custom.scss',
206+
custom: "./app/assets/stylesheets/custom.scss",
207207
},
208208
module: {
209209
rules: [
210210
// Add CSS/SASS/SCSS rule with loaders
211211
{
212212
test: /\.(?:sa|sc|c)ss$/i,
213-
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
213+
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
214214
},
215215
],
216216
},
217217
resolve: {
218218
// Add additional file types
219-
extensions: ['.js', '.jsx', '.scss', '.css'],
219+
extensions: [".js", ".jsx", ".scss", ".css"],
220220
},
221221
plugins: [
222222
// Include plugins

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

0 commit comments

Comments
 (0)