2015-12-07 34 views
0

我使用Rails 4.2,Ruby的2.2如何猴子補丁Rails使用Gulp-rev-all Manifest.json?

我使用生成一個新的應用:軌道新應用 --skip-鏈輪

我所有咕嘟咕嘟的任務都成功運行(他們中的很多:從咕嚕咕嚕咕嚕咕嚕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕,c c c c c c,g g)

使用gulp-rev-all我可以使用MD5 Fingerprint命名約定生成所有資產。

他們正確地保存到REV-的manifest.json

應用程序/資產/ REV-manifest.json的:

{ 
 
    "rev-manifest.json": "rev-manifest.9680cee8.json", 
 
    "images/measurement.png": "images/measurement.cedb4145.png", 
 
    "images/measurement2.png": "images/measurement2.cedb4145.png", 
 
    "scripts/chachin.js": "scripts/chachin.5f30b461.js", 
 
    "stylesheets/chachin.scss": "stylesheets/chachin.4c7c499d.scss" 
 
}

應用程序/傭工/ application_helper.rb

module ApplicationHelper 

    def stylesheet_link_tag(url, options={}) 
    url = AssetManifest.stylesheet_path(url) 

    super(url, options) 
    end 

    def crossorigin_javascript_include_tag(url, options={}) 
    url = AssetManifest.javascript_path(url) 

    super(url, options) 
    end 

    def image_tag(url, options={}) 
    url = AssetManifest.asset_path(url) 

    super(url, options) 
    end 

    def image_path(url, options={}) 
    url = AssetManifest.asset_path(url) 

    super(url, options) 
    end 

    def image_url(url, options={}) 
    url = AssetManifest.asset_path(url) 

    super((ActionController::Base.asset_host || "") + url, options) 
    end 

end 

配置/初始化/ asset_manifest.rb

class AssetManifest 

    def self.manifest 
    if File.exists?("app/assets/rev-manifest.json") 
     @manifest ||= JSON.parse(File.read("app/assets/rev-manifest.json")) 
    end 
    end 

    def self.stylesheet_path(url) 
    if AssetManifest.manifest 
     url += ".css" unless url.end_with?(".css") 
     AssetManifest.manifest[url] || url 
    else 
     url 
    end 
    end 

    def self.javascript_path(url) 
    if AssetManifest.manifest 
     url += ".js" unless url.end_with?(".js") 
     AssetManifest.manifest[url] || url 
    else 
     url 
    end 
    end 

    def self.asset_path(url) 
    if AssetManifest.manifest 
     AssetManifest.manifest[url] || url 
    else 
     url 
    end 
    end 
end 

我缺少的東西?

回答

1

Rails.root合併到資產清單文件的路徑中可能是一個好主意。

這裏是我做過什麼:

配置/初始化/ asset_manifest.rb

class AssetManifest 

    MANIFEST_FILE = "rev-manifest.json" 

    class << self 

    def manifest 
     @manifest ||= read_manifest(manifest_file_path) 
    end 

    def stylesheet_path(url) 
     url += ".css" unless url.end_with?(".css") 
     AssetManifest.manifest[url] || url 
    end 

    def javascript_path(url) 
     url += ".js" unless url.end_with?(".js") 
     AssetManifest.manifest[url] || url 
    end 

    def asset_path(url) 
     AssetManifest.manifest[url] || url 
    end 

    private 

    def manifest_file_path 
     File.join(Rails.root, "app", "assets", MANIFEST_FILE) 
    end 

    def read_manifest(path) 
     if File.exists?(path) 
     JSON.parse(File.read(manifest_file_path)) 
     else 
     {} 
     end 
    end 

    end 
end 

帽尖到https://bugsnag.com/blog/replacing-the-rails-asset-pipeline-with-gulp關於如何使用一飲而盡,而不是鏈輪資產管道偉大的介紹。

+0

我會嘗試,但是,然後,我需要將Rails.root合併到Gulp生成的文件中,對嗎? Rails.root是否可用於Gulp? – jaysoifer

+1

我不這麼認爲,只要你在那個目錄下有一個'gulpfile.js'並且從那裏運行它,你就可以配置它相對於本地目錄('「。」'')。請注意,將所有資產生成到「public/assets」可能是一個更好的主意,因爲rails應用程序無需進一步配置就無法提供app/assets。 – defsprite