Ruby on Rails 4.2 资产管道不会缩小/压缩 application-fingerprinted.js JS 文件

Mat*_*ieu 5 ruby-on-rails heroku asset-pipeline ruby-on-rails-4

我有一个 ruby​​ on Rails 4.2 应用程序,并且我的资产管道在长达 2 天的时间里都面临着“令人毛骨悚然”的问题。我的产品托管在 Heroku 上,我在这里直接提到这一点,因为我认为这可能是相关的,我有他们 gem 'rails_12factor', group::production

我阅读并尝试了许多关于 Rails 资产没有编译的(太多)SO 问题的建议,但没有一个工作,因为我将进一步描述。

观察到的问题导致我提出这个 SO 问题是我的 javascript application.js 文件在生产中没有被缩小

我怎么知道?空格仍然在这里,注释还没有被删除,applciation.js,与我的 application.css 相反,只是所有 js 文件的串联,但没有压缩/缩小已经完成。

关于 SO 的大多数问题都涉及图像、css 或 js 都没有被缩小/预编译的问题,但我的情况很特殊,因为图像、css 是预编译/缩小的,但只有 js 是一个问题并且没有被缩小

我的js有问题吗?(请参阅下面的一些实验,我试图找出错误的原因)似乎不是

我在下面的设置将向您展示我如何处理资产(在我初学者理解的范围内):我使用守卫不断监视任何更改并预编译内容并将生成的/生成的 application-tr56d7.css(指纹)和 application- 45dsugdsy67.js((指纹)在 public/assets 中,然后当我在 git 上部署时,它会推送所有更改,包括预编译/缩小的文件,然后当我推送到 Heroku 时,我的生产资产设置告诉 Rails 部署我已经预编译的资产。我是一个初学者,很难理解所有众多的开发/生产环境资产设置,但我认为这就是您将在下面找到的代码中定义的内容。

我知道所有这些过程都在工作,因为每次我更改文件时,我都能找到一个新的 application-tr56d7.css 和一个新的 application-45dsugdsy67.js(当然是示例)(以及一个新的 css.gz 和 .js.z这必须是二进制的东西)

例如,每次我更改一个 js 文件时,守卫都会执行他的工作,我可以阅读如下内容:

I, [2018-02-09T09:53:41.140165 #130534]  INFO -- : Writing /home/mathieu/rails_projects/my_app/public/assets/application-af0ab4a348e4f5545c844cfac02a0670.js
Run Code Online (Sandbox Code Playgroud)

然后可以在 public/assets 文件夹中找到新生成的 application.css 和 application.js 文件:例如

/public/assets/application-1021e7d2ea120fe40c67ec288f1c6525.js
/public/assets/application-1021e7d2ea120fe40c67ec288f1c6525.js.gz (binary: just a list of numbers...)
/public/assets/application-753e1d0958f76ae233a460ad47606790.css
/public/assets/application-753e1d0958f76ae233a460ad47606790.css.gz (binary: just a list of numbers...)
Run Code Online (Sandbox Code Playgroud)

因此,当我观察到在生产中 css 是 application-753e1d0958f76ae233a460ad47606790.css 缩小但不是 application-1021e7d2ea120fe40c67ec288f1c6525.js js 时,我也看到了公共/资产:

  • 由guard生成的css文件例如/public/assets/application-753e1d0958f76ae233a460ad47606790.css css被缩小

  • 但是由 gyard 生成的 js 文件没有被缩小

所以我认为,但我不确定,这不是 Heorku 特定的问题,只是即使在将其推送到 Heroku 之前,我托管在公共/资产上的 js 文件应该但没有缩小。

我试图调试这个(破坏悬念:全部失败):

  • 试图在 assets.rb 中明确说编译 application.js 不起作用 => 结果:js 仍然没有在本地缩小,也没有在 heroku 的生产网站上

    # 建议在config/application.rb config.assets.precompile = ['application.js'] 中明确列出资产

  • 尝试通过 rake 清理所有旧东西:assets clobber => 结果:js 仍然没有在本地缩小,也没有在 heroku 的生产网站上缩小

  • 尝试通过更改版本 assets.rb 来清理旧东西:已更改 => 结果:js 仍然没有在本地缩小,也没有在 heroku 的生产网站上

     config.assets.version = '1.0'
    
    Run Code Online (Sandbox Code Playgroud)

    进入

     config.assets.version = '1.1'
    
    Run Code Online (Sandbox Code Playgroud)
  • 试图更改所有各种开发和生产资产设置 => 结果:js 仍然没有在本地缩小,也没有在 heroku 的生产网站上

config.serve_static_assets = true 并尝试 false 也尝试了两个文件的许多不同设置,但没有一个工作。

  • 尝试在本地和 prod 中编译 => 结果:js 仍然没有在本地缩小,也没有在 heroku 的生产网站上

    耙子资产:预编译 RAILS_ENV=production --trace

也试过本地:

rake assets:precompile
Run Code Online (Sandbox Code Playgroud)
  • 2 次最奇怪的尝试:在我通过修改资产管道设置来完成这项工作的所有尝试之后都失败了,我想也许某处有一个棘手的 javascript 错误默默地打破由守卫所做的缩小/压缩(在页面加载方面也是沉默的,因为当我加载我的页面时,chrome 开发工具上没有出现错误,但谁知道...阅读一些 SO 问题上的一些奇怪的评论对资产管道)',所以我决定在 assets/javascripts/! 什么都没有了:甚至删除了最终在管道中但由 gem 注入的 js(因此在我的文件夹 app/assets/javascripts 中不可见),例如 jquery gem:并创建 2 个非常基本的 js 文件,这将是唯一剩下的文件...仍然很好:=> 结果:js 仍然没有在本地缩小,也没有在 heroku 的生产网站上

再次进行了与上面相同的测试,但这里更进一步:我清空了(删除了所有内容)我的资产/javascripts 中的所有 js 文件,并从 application.js 中删除了所有 gems,只留下 require 目录,然后创建了 2非常简单的 js 文件来检查它现在是否被缩小了......结果仍然相同:js 仍然没有在本地缩小,也没有在 heroku 的生产网站上

  • 一种有效的测试

做 rake assets:clobber 然后推送 (git add, git commit, git push, git push heroku pasteer) 确实编译了 js 但不幸的是它产生了其他问题:它向生产发送了一个旧版本的 js(我知道,因为我在 js 中放置一条警告消息,它不是最新的!)。它揭示了关于 rake assets:clobber 类型的调试的错误的什么?

编辑我让它工作但有一个相当苛刻的过程:利用一些人说如果你不改变 css 或 js 就没有编译(见/sf/answers/559208261/),我调整了以前的进程:(更改js文件中的某个东西,rake assets:clobber, git add, git commit, git push, git heroku master)并且它有效:它cpmpiles并发送最新的js文件!有没有办法不必记住每一次在js内部更改某事以确保编译的时间?

我没有想法了。也许我的设置是错误的,作为初学者,我错过了一些明显的东西。

最奇怪的是:我的 cs 被缩小了!为什么不是js????

我的代码库

/config/environments/development.rb

MyApp::Application.configure do
 # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = true

  config.serve_static_files = false
end
Run Code Online (Sandbox Code Playgroud)

/config/environments/production.rb

MyApp::Application.configure do

# Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_files = false

  # Compress JavaScripts and CSS
  # config.assets.compress = true removed when switch from Rails 3.2 to Rails 4
  config.assets.js_compressor  = :uglifier
  config.assets.js_compressor = Uglifier.new(
    # Remove all console.* functions
    :compress => { :drop_console => true }
  ) if defined? Uglifier
  config.assets.css_compressor = :sass

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true

  config.force_ssl = true

end
Run Code Online (Sandbox Code Playgroud)

/config/initializers/assets.rb

Rails.application.configure do
  # Precompile additional assets.
  # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.

  # related to deployment pb with active admin
  config.assets.precompile += %w[admin/active_admin.css admin/active_admin.js] 

  # for ckeditor: github.com/galetahub/ckeditor
  config.assets.precompile += %w( ckeditor/* )

  # Version of your assets, change this if you want to expire all your assets
  config.assets.version = '1.0'  

end
Run Code Online (Sandbox Code Playgroud)

/配置/应用程序.rb

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

require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
end

module MyApp
  class Application < Rails::Application

    config.autoload_paths += %W(#{config.root}/lib)
    # Enable the asset pipeline
    config.assets.enabled = true
  end
end
Run Code Online (Sandbox Code Playgroud)

/保护文件

# More info at https://github.com/guard/guard#readme
require 'active_support' # edded this due to a bug where guard did not load github.com/rails/rails/issues/14664
require 'active_support/core_ext'

require 'active_support/inflector'

# Make sure this guard is ABOVE any other guards using assets such as jasmine-headless-webkit
# It is recommended to make explicit list of assets in `config/application.rb`
# config.assets.precompile = ['application.js', 'application.css', 'all-ie.css']
# update dec 2014- added :runner => :cli because of a know bug on guard rail assets
# if bug is solved i can remove the part :runner=> cli
guard 'rails-assets', :run_on => [:start, :change], :runner => :cli do
  watch(%r{^app/assets/.+$})
  watch('config/application.rb')  
end
Run Code Online (Sandbox Code Playgroud)

资产/javascripts/application.js

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery.cookie
//= require cloudinary-jquery.min
//= require twitter/bootstrap
//= require paloma
//= require html5shiv-printshiv
//= require storageService
//= require turbolinks
//= require_directory .
Run Code Online (Sandbox Code Playgroud)

部署后的终端进程,例如更改一些 js 文件和等待守卫通知我它已完成其预编译工作:

git add --all
git commit -a -m "fix issue with asset pipeline"
git push
git push heroku master
Run Code Online (Sandbox Code Playgroud)

Erv*_*ouS 4

我知道我要说的不是传统的,但你的方法还没有那么明确,你应该尝试添加你在开发中设置为 false 的显式压缩调用

/config/环境/生产.rb

MyApp::Application.configure do
  # Compress assets please
  config.assets.compress = true
  # ... other stuff
end
Run Code Online (Sandbox Code Playgroud)

之后清理你的资产并重新生成它们

$ bundle exec rake assets:clobber
$ RAILS_ENV=production bundle exec rake assets:precompile
Run Code Online (Sandbox Code Playgroud)