Skip to content

Commit

Permalink
Support to Yarn v2/berry for webpacker 5.x (#2889)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrofurtado authored Jan 21, 2021
1 parent 9ec5daa commit 2f0f955
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

**Please note that Webpacker 4.1.0 has an installer bug. Please use 4.2.0 or above**

## [[5.3.0]](https://github.com/rails/webpacker/compare/v5.3.0...5.2.1) - 2021-TBD

- Adds experimental Yarn 2 support. Note you must manually set `nodeLinker: node-modules` in your `.yarnrc.yml`.

## [[5.2.1]](https://github.com/rails/webpacker/compare/v5.2.0...5.2.1) - 2020-08-17

- Revert [#1311](https://github.com/rails/webpacker/pull/1311).
Expand Down
8 changes: 8 additions & 0 deletions lib/install/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
run "yarn add @rails/webpacker@next"
end

package_json = File.read("#{__dir__}/../../package.json")
webpack_version = package_json.match(/"webpack": "(.*)"/)[1]
webpack_cli_version = package_json.match(/"webpack-cli": "(.*)"/)[1]

# needed for experimental Yarn 2 support and should not harm Yarn 1
say "Installing webpack and webpack-cli as direct dependencies"
run "yarn add webpack@#{webpack_version} webpack-cli@#{webpack_cli_version}"

say "Installing dev server for live reloading"
run "yarn add --dev webpack-dev-server"

Expand Down
3 changes: 1 addition & 2 deletions lib/tasks/webpacker/check_yarn.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ namespace :webpacker do
pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
yarn_range = JSON.parse(pkg_path.read)["engines"]["yarn"]
is_valid = SemanticRange.satisfies?(yarn_version, yarn_range) rescue false
is_unsupported = SemanticRange.satisfies?(yarn_version, ">=2.0.0") rescue false
is_unsupported = SemanticRange.satisfies?(yarn_version, ">=3.0.0") rescue false

unless is_valid
$stderr.puts "Webpacker requires Yarn \"#{yarn_range}\" and you are using #{yarn_version}"
if is_unsupported
$stderr.puts "This version of Webpacker does not support Yarn #{yarn_version}. Please downgrade to a supported version of Yarn https://yarnpkg.com/lang/en/docs/install/"
$stderr.puts "For information on using Webpacker with Yarn 2.0, see https://github.com/rails/webpacker/issues/2112"
else
$stderr.puts "Please upgrade Yarn https://yarnpkg.com/lang/en/docs/install/"
end
Expand Down
8 changes: 7 additions & 1 deletion lib/tasks/webpacker/yarn_install.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ namespace :webpacker do
node_env = ENV.fetch("NODE_ENV") do
valid_node_envs.include?(Rails.env) ? Rails.env : "production"
end
system({ "NODE_ENV" => node_env }, "yarn install --no-progress --frozen-lockfile")
yarn_flags =
if `yarn --version`.start_with?("1")
"--no-progress --frozen-lockfile"
else
"--immutable"
end
system({ "NODE_ENV" => node_env }, "yarn install #{yarn_flags}")
end
end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"engines": {
"node": ">=10.17.0",
"yarn": ">=1 <2"
"yarn": ">=1 <3"
},
"dependencies": {
"@babel/core": "^7.11.1",
Expand Down

0 comments on commit 2f0f955

Please sign in to comment.