rake assets:precompile:nodigest in Rails 4

前端 未结 6 1480
耶瑟儿~
耶瑟儿~ 2021-01-11 17:03

In Rails 3 there was a rake assets:precompile:nodigest task which was compiling assets and writing them without the digest part in /public/assets directory. In Rails 4 this

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-11 17:33

    My enormous analysis of all the available options is here:

    https://bibwild.wordpress.com/2014/10/02/non-digested-asset-names-in-rails-4-your-options/

    I decided to add a custom rake task, using custom configuration to determine certain assets to produce as non-digested versions, and using the Sprockets manifest to find the digested assets and copy them with non-digested names.

    https://github.com/team-umlaut/umlaut/blob/5edcc609389edf833a79caa6f3ef92982312f0c5/lib/tasks/umlaut_asset_compile.rake

    # Rails4 doesn't create un-fingerprinted assets anymore, but we
    # need a couple for umlaut's API. Let's try to hook in and make
    # symlinks. 
    #
    # list of what file(globs) need non-digest-named copies is kept in
    #     Umlaut::Engine.config.non_digest_named_assets
    # defined in lib/umlaut.rb, but app can modify it if desired. 
    
    require 'umlaut'
    require 'pathname'
    
    
    # Every time assets:precompile is called, trigger umlaut:create_non_digest_assets afterwards. 
    Rake::Task["assets:precompile"].enhance do
      Rake::Task["umlaut:create_non_digest_assets"].invoke
    end
    
    namespace :umlaut do 
    
      # This seems to be basically how ordinary asset precompile
      # is logging, ugh. 
      logger = Logger.new($stderr)  
    
      # Based on suggestion at https://github.com/rails/sprockets-rails/issues/49#issuecomment-20535134
      # but limited to files in umlaut's namespaced asset directories. 
      task :create_non_digest_assets => :"assets:environment"  do    
        manifest_path = Dir.glob(File.join(Rails.root, 'public/assets/manifest-*.json')).first
        manifest_data = JSON.load(File.new(manifest_path))
    
        manifest_data["assets"].each do |logical_path, digested_path|
          logical_pathname = Pathname.new logical_path
    
          if Umlaut::Engine.config.non_digest_named_assets.any? {|testpath| logical_pathname.fnmatch?(testpath, File::FNM_PATHNAME) }
            full_digested_path    = File.join(Rails.root, 'public/assets', digested_path)
            full_nondigested_path = File.join(Rails.root, 'public/assets', logical_path)
    
            logger.info "(Umlaut) Copying to #{full_nondigested_path}"
    
            # Use FileUtils.copy_file with true third argument to copy
            # file attributes (eg mtime) too, as opposed to FileUtils.cp
            # Making symlnks with FileUtils.ln_s would be another option, not
            # sure if it would have unexpected issues. 
            FileUtils.copy_file full_digested_path, full_nondigested_path, true      
          end
        end
    
      end
    end
    

提交回复
热议问题