ruby-on-rails 如何禁用刺激:清单:生成刺激控制器时更新

6tdlim6h  于 2023-08-08  发布在  Ruby
关注(0)|答案(1)|浏览(140)

在Rails 7上,当我运行rails generate stimulus controller_name时,rails stimulus:manifest:update会自动触发。有什么办法可以解除这个钩子吗?

hpxqektj

hpxqektj1#

这不是可选的(除非你有 importmap.rb):

  • https:github.com/hotwired/stimulus-rails/blob/v1.2.1/lib/generators/stimulus/stimulus_generator.rb#L9*

两种解决方案:

制作生成器

你可以制作自己的生成器并使用它来代替bin/rails g stimulus

$ rails g generator stimulus/controller
# lib/generators/stimulus/controller_generator.rb

require "generators/stimulus/stimulus_generator"

class Stimulus::ControllerGenerator < StimulusGenerator
  # +++
  source_root superclass.source_root

  # -m to update manifest, otherwise no update
  class_option :manifest, type: :boolean, default: false, aliases: "-m"
  # -M to skip manifest, otherwise update
  # class_option :skip_manifest, type: :boolean, default: false, aliases: "-M"
  # ###

  def copy_view_files
    @attribute = stimulus_attribute_value(controller_name)
    template "controller.js", "app/javascript/controllers/#{controller_name}_controller.js"

    # +++
    return if !options[:manifest]         # no update by default
    # return if options[:skip_manifest]   # update by default
    # ###

    rails_command "stimulus:manifest:update" unless Rails.root.join("config/importmap.rb").exist?
  end
end
$ bin/rails g stimulus:controller test
      create  app/javascript/controllers/test_controller.js

$ bin/rails g stimulus:controller test -m
   identical  app/javascript/controllers/test_controller.js
       rails  stimulus:manifest:update

跳过“刺激:清单:更新”任务

这是一个更简单的选项,可以让默认的bin/rails g stimulus跳过更新:

# lib/tasks/skip_stimulus_manifest.rake

Rake::Task["stimulus:manifest:update"].clear

namespace :stimulus do
  namespace :manifest do
    task :update do
      puts "Skipping manifest update"
    end
  end
end
$ bin/rails g stimulus test
   identical  app/javascript/controllers/test_controller.js
       rails  stimulus:manifest:update
Skipping manifest update

的字符串

相关问题