# 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
1条答案
按热度按时间hpxqektj1#
这不是可选的(除非你有 importmap.rb):
两种解决方案:
制作生成器
你可以制作自己的生成器并使用它来代替
bin/rails g stimulus
:跳过“刺激:清单:更新”任务
这是一个更简单的选项,可以让默认的
bin/rails g stimulus
跳过更新:的字符串