【问题标题】:How disable assets compilation on heroku?如何在heroku上禁用资产编译?
【发布时间】:2014-07-04 11:24:25
【问题描述】:

我正在尝试使用此教程将我的 rails 应用程序部署到 heroku:

https://devcenter.heroku.com/articles/getting-started-with-rails4

所以,我使用 rails 4.1.1 和 ruby​​ 2.1.1

我的 Gemfile 里面有 gem 'rails_12factor', group: :production

我的应用程序.rb:


require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(*Rails.groups)

module Charticus
  class Application  Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
  end
end

我创建了文件public/assets/manifest.yml

但是当我将应用程序部署到 heroku 时 - 它会将我所有的 js 文件编译为 application.js 和所有 css 文件 application.css。而且我在 app.heroku.com 上使用 firebug 看不到它。

我需要如何处理我的配置才能在 app.heroku.com 上查看我的所有 js 和 css 文件? 如何在heroku上禁用资产预编译和缩小?

请帮帮我! 谢谢

【问题讨论】:

    标签: heroku ruby-on-rails-4 asset-pipeline assets precompile


    【解决方案1】:

    lib/tasks/assets.rake

    Rake::Task["assets:precompile"].clear
       namespace :assets do
         task 'precompile' do
         puts "Not pre-compiling assets..."
       end
    end
    

    你已经完成了。

    【讨论】:

    • 就我而言,我必须将其写在Rakefile
    【解决方案2】:

    我比较 config/environments/development.rbconfig/environments/production.rb

    并像在 development.rb 中一样制作 production.rb 资产配置:

    评论这行:

    • config.serve_static_assets = false
    • config.assets.js_compressor = :uglifier
    • config.assets.compile = false
    • config.assets.digest = true

    然后:

    1. 将我的更改推送到 git repo git push origin master
    2. 将更改推送到 heroku git push heroku master

    【讨论】:

    • 我还必须添加设置config.assets.debug = true
    • 这不会阻止 Heroku 的 Ruby buildpack 调用 rake assets:precompile rake 任务。它只是将生产环境配置为像开发环境一样。
    【解决方案3】:

    Rails 4 应用程序有一个manifest-*.json 文件,而不是manifest.yml 文件。该文件通常在您运行 rake assets:precompile 时生成,您是如何编译资产的?

    不管怎样,你需要一个文件public/assets/manifest-(fingerprint).json文件

    【讨论】:

    • 我创建了public/assets/manifest-(fingerprint).json,现在heroku app找不到application.css和application.js ...
    • 你想做什么?您是否在本地运行资产编译?然后为您创建清单文件。这不仅仅是一个空白文件...
    【解决方案4】:

    快进到 2018 年,您需要将以下内容添加到 config/initializers/production.rb

    config.assets.enabled = false
    

    然后您需要自定义Heroku's Ruby Buildpack 以不运行assets:precompile rake 任务。我不会提供这样一个 buildpack 的链接,因为我不支持或保证一个,但它很容易在 lib/language_pack/ruby.rb 中找到它并开始删除相关代码。

    然后,您必须配置 Heroku 应用以使用新的分叉 Buildpack 而不是默认的 Buildpack(例如,使用 heroku buildpacks)。

    这是在带有 Rails 的 Heroku 应用程序中禁用资产管道的最干净的方法,无需诉诸于覆盖 Rails 的内置 rake 任务。

    【讨论】:

      【解决方案5】:

      快进到 2021 年和 Rails 6.x,如果您完全删除了 Webpacker 和 Sprockets/Asset Pipeline,请将 bin/yarn 文件内容替换为:

      #!/usr/bin/env ruby
      puts 'Yarn not present, nothing to do.'
      

      @danielricecodes 的建议可能仍然有效,但更具侵略性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-26
        • 2018-03-22
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-28
        相关资源
        最近更新 更多