【问题标题】:How to remove Yarn from a Rails app completely?如何从 Rails 应用程序中完全删除 Yarn?
【发布时间】:2019-03-25 03:11:33
【问题描述】:

我正在使用 jQuery 和其他一些库构建 Rails 5.1.x rails 应用程序。 Rails 坚持使用 Yarn,这在开发机器上很好,但我不能在生产机器上使用它。

有没有办法让rails默认不使用Yarn?删除 yarn.locknode_modules 以及它附带的所有其他内容。

【问题讨论】:

    标签: ruby-on-rails configuration yarnpkg


    【解决方案1】:

    从文件中删除以下行

    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')
    

    【讨论】:

    • 请解释为什么要这样做
    【解决方案2】:

    在 Rails 5.2.x 和 6.0.x 中,如果您使用 --skip-yarn 标志创建新的 Rails 应用程序,它仍然会检查是否安装了 yarn。 所以当你运行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'])
    

    这将删除对纱线的检查,并且您只能使用 webpack。

    编辑: 如果您使用资产编译,或者您想管理客户端库,例如 React、Angular 或 Vue,我实际上建议使用 yarn 而不是 npm,因为Rails 6 似乎与 yarn 深度集成。它会在服务器启动时调用 yarn,也会在资产编译时调用。您可以接受 Rails repo 开发人员的选择,而不是费力地用 npm 替换 yarn。

    【讨论】:

      【解决方案3】:

      创建rails项目时,可以将--skip-yarn添加为rails new app_path --skip-yarn

      【讨论】:

        【解决方案4】:

        提醒以后阅读此问题的任何人:Max Popoff's answer 仅在您首先定义其 Rakefile 中引用的新 rake 任务时才有效。关于这个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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-07-07
          • 2018-08-12
          • 1970-01-01
          • 2020-09-04
          • 2016-11-02
          • 1970-01-01
          • 2015-12-27
          相关资源
          最近更新 更多