如何通过 Monkey-Patch Rails 来使用 Gulp-rev-all Manifest.json?
How to Monkey-Patch Rails to use Gulp-rev-all Manifest.json?
我正在使用 Rails 4.2,Ruby 2.2
我使用以下方法生成了一个新应用:rails new app --skip-sprockets
我所有的 Gulp 任务都成功 运行(其中很多:从 gulp-changed 和 gulp-livereload 到 gulp-minify -css, gulp-uncss 等).
使用 gulp-rev-all 我能够使用 MD5 指纹命名约定生成所有资产。
它们已正确保存到 rev-manifest.json
app/assets/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"
}
app/helpers/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
config/initializers/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
我是不是漏掉了什么?
将 Rails.root
合并到资产清单文件的路径中可能是个好主意。
这是我所做的:
config/initializers/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 致敬,了解如何使用 gulp 而不是链轮作为资产管道的精彩介绍。
我正在使用 Rails 4.2,Ruby 2.2
我使用以下方法生成了一个新应用:rails new app --skip-sprockets
我所有的 Gulp 任务都成功 运行(其中很多:从 gulp-changed 和 gulp-livereload 到 gulp-minify -css, gulp-uncss 等).
使用 gulp-rev-all 我能够使用 MD5 指纹命名约定生成所有资产。
它们已正确保存到 rev-manifest.json
app/assets/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"
}
app/helpers/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
config/initializers/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
我是不是漏掉了什么?
将 Rails.root
合并到资产清单文件的路径中可能是个好主意。
这是我所做的:
config/initializers/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 致敬,了解如何使用 gulp 而不是链轮作为资产管道的精彩介绍。