Skip to content

Commit

Permalink
Add support for Yarn v2 (#2799)
Browse files Browse the repository at this point in the history
* experimental yarn 2 support

This should make Webpacker support both Yarn 1 and 2. It comes down to
the two changes:

- Use correct `yarn install` flags
- Add `webpack` and `webpack-cli` as direct per project dependencies,
  otherwise Yarn 2 complains about not finding the binary `webpack`

In addition `nodeLinker: node-modules` must be added to `.yarnrc.yml`
for maximum compatibility, but this is left as an exercise for the user
who wants to experiment with Yarn 2 for now.

* add a changelog entry

* amend downgrade message to check for yarn 3
  • Loading branch information
doits authored Dec 19, 2020
1 parent b6c2180 commit 4210dd5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ environment.loaders.append('nodeModules', nodeModules)
- The install task will now set the `extract_css` default to `true` in all environments and generate a separate `application.css` file for the default `application` pack, as supported by multiple files per entry introduced in 5.0.0. [#2608](https://github.com/rails/webpacker/pull/2608)
- Webpacker's wrapper to the `splitChunks()` API will now default `runtimeChunk: 'single'` which will help prevent potential issues when using multiple entry points per page [#2708](https://github.com/rails/webpacker/pull/2708).
- Changes `@babel/preset-env` modules option to `'auto'` per recommendation in the Babel docs [#2709](https://github.com/rails/webpacker/pull/2709)
- 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

Expand Down
8 changes: 8 additions & 0 deletions lib/install/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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"
end
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 @@ -6,7 +6,13 @@ namespace :webpacker do
valid_node_envs.include?(Rails.env) ? Rails.env : "production"
end
Dir.chdir(Rails.root) do
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
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 || ^12 || >=14",
"yarn": ">=1 <2"
"yarn": ">=1 <3"
},
"dependencies": {
"@babel/core": "^7.12.9",
Expand Down

0 comments on commit 4210dd5

Please sign in to comment.