如何从 Rails 应用程序中完全删除 Yarn?
How to remove Yarn from a Rails app completely?
我正在使用 jQuery
和其他一些库构建 Rails 5.1.x rails 应用程序。 Rails 坚持使用 Yarn,这在开发机器上很好,但我不能在生产机器上使用它。
有没有办法不让 rails 默认使用 Yarn?删除 yarn.lock
和 node_modules
以及它附带的所有其他内容。
创建rails项目时,可以将--skip-yarn
添加为rails new app_path --skip-yarn
。
从文件中删除以下行
bin/setup.rb 和 bin/update.rb
- # Install JavaScript dependencies if using Yarn
- system('bin/yarn')
config/initializers/assets.rb
# Add Yarn node_modules folder to the asset load path.
Rails.application.config.assets.paths << Rails.root.join('node_modules')
在 Rails 5.2.x 和 6.0.x 中,如果您使用 --skip-yarn 标志创建新的 Rails 应用程序,它仍会添加检查是否纱线已安装。
所以当你 运行 rails webpacker:install
时,结果可能是
Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/
解决方法是在Rakefile的末尾添加这四行,紧跟在Rails.application.load_tasks
之后:
# Replace yarn with npm
Rake::Task['webpacker:yarn_install'].clear
Rake::Task['webpacker:check_yarn'].clear
Rake::Task.define_task('webpacker:verify_install' => ['webpacker:check_npm'])
Rake::Task.define_task('webpacker:compile' => ['webpacker:npm_install'])
这将取消对 yarn 的检查,您只能使用 webpack。
编辑:
如果你使用资产编译,或者你想管理客户端库,比如 React,Angular 或 Vue,我实际上 推荐 使用 yarn 而不是 npm,因为 Rails 6 似乎与 yarn 深度结合。它会在服务器启动时调用 yarn,也会在资产编译时调用。与其努力用 npm 替换 yarn,不如接受 Rails repo 开发人员的选择。
请注意以后阅读此问题的任何人: only works if you define the new rake tasks referenced in their Rakefile first. There's a little more information on this blog post但本质上,您需要添加如下文件:
# lib/tasks/webpacker.rake
namespace :webpacker do
task :check_npm do
begin
npm_version = `npm --version`
raise Errno::ENOENT if npm_version.blank?
version = Gem::Version.new(npm_version)
package_json_path = Pathname.new("#{Rails.root}/package.json").realpath
npm_requirement = JSON.parse(package_json_path.read).dig('engines', 'npm')
requirement = Gem::Requirement.new(npm_requirement)
unless requirement.satisfied_by?(version)
$stderr.puts "Webpacker requires npm #{requirement} and you are using #{version}" && exit!
end
rescue Errno::ENOENT
$stderr.puts 'npm not installed'
$stderr.puts 'Install NPM https://www.npmjs.com/get-npm' && exit!
end
end
task :npm_install do
system 'npm install'
end
end
我正在使用 jQuery
和其他一些库构建 Rails 5.1.x rails 应用程序。 Rails 坚持使用 Yarn,这在开发机器上很好,但我不能在生产机器上使用它。
有没有办法不让 rails 默认使用 Yarn?删除 yarn.lock
和 node_modules
以及它附带的所有其他内容。
创建rails项目时,可以将--skip-yarn
添加为rails new app_path --skip-yarn
。
从文件中删除以下行
bin/setup.rb 和 bin/update.rb
- # Install JavaScript dependencies if using Yarn
- system('bin/yarn')
config/initializers/assets.rb
# Add Yarn node_modules folder to the asset load path.
Rails.application.config.assets.paths << Rails.root.join('node_modules')
在 Rails 5.2.x 和 6.0.x 中,如果您使用 --skip-yarn 标志创建新的 Rails 应用程序,它仍会添加检查是否纱线已安装。
所以当你 运行 rails webpacker:install
时,结果可能是
Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/
解决方法是在Rakefile的末尾添加这四行,紧跟在Rails.application.load_tasks
之后:
# Replace yarn with npm
Rake::Task['webpacker:yarn_install'].clear
Rake::Task['webpacker:check_yarn'].clear
Rake::Task.define_task('webpacker:verify_install' => ['webpacker:check_npm'])
Rake::Task.define_task('webpacker:compile' => ['webpacker:npm_install'])
这将取消对 yarn 的检查,您只能使用 webpack。
编辑: 如果你使用资产编译,或者你想管理客户端库,比如 React,Angular 或 Vue,我实际上 推荐 使用 yarn 而不是 npm,因为 Rails 6 似乎与 yarn 深度结合。它会在服务器启动时调用 yarn,也会在资产编译时调用。与其努力用 npm 替换 yarn,不如接受 Rails repo 开发人员的选择。
请注意以后阅读此问题的任何人:
# lib/tasks/webpacker.rake
namespace :webpacker do
task :check_npm do
begin
npm_version = `npm --version`
raise Errno::ENOENT if npm_version.blank?
version = Gem::Version.new(npm_version)
package_json_path = Pathname.new("#{Rails.root}/package.json").realpath
npm_requirement = JSON.parse(package_json_path.read).dig('engines', 'npm')
requirement = Gem::Requirement.new(npm_requirement)
unless requirement.satisfied_by?(version)
$stderr.puts "Webpacker requires npm #{requirement} and you are using #{version}" && exit!
end
rescue Errno::ENOENT
$stderr.puts 'npm not installed'
$stderr.puts 'Install NPM https://www.npmjs.com/get-npm' && exit!
end
end
task :npm_install do
system 'npm install'
end
end