加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在Rails中预编译任何资源之前运行自定义代

发布时间:2020-12-16 22:20:25 所属栏目:百科 来源:网络整理
导读:像在一个初始化器中的东西: Sprockets.before_precompile do # some custom stuff that preps or autogenerates some asset files # that should then be considered in the asset pipeline as if they were checked inend 具体来说,我想运行一个gulp任务来
像在一个初始化器中的东西:
Sprockets.before_precompile do
  # some custom stuff that preps or autogenerates some asset files
  # that should then be considered in the asset pipeline as if they were checked in
end

具体来说,我想运行一个gulp任务来捆绑一些Javascript与一些特殊的预处理器,我宁愿不重写我的gulpfile来获得资产管道来处理所有的东西…我也希望这可以在Heroku上工作,没有需要一个定制的buildpack,如果可能的话.有什么想法吗?推测链轮有这些类型的钩子.

解决方法

从源头可以看出,Sprockets没有这样的钩子,但是你可以使用耙子任务钩子.例如,您将创建一个启动所有预处理程序,gulp等的耙子任务,因此可以将此任务放在预编译之前.
# lib/tasks/before_assets_precompile.rake

task :before_assets_precompile do
  # run a command which starts your packaging
  system('gulp production')
end

# every time you execute 'rake assets:precompile'
# run 'before_assets_precompile' first    
Rake::Task['assets:precompile'].enhance ['before_assets_precompile']

然后,您只需运行rake资源:预编译,因此在之前执行的任务before_assets_precompile将被执行.

还要确保使用系统而不是exec,因为exec将在运行此前任务的阶段退出进程,并且不会运行资源:在预期之后自己编译.

资料来源:

> Rake before task hook
> http://www.dan-manges.com/blog/modifying-rake-tasks

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读