我正在围绕预制的HTML5模板构建Rails 5 webapp。我想使用Turbolinks,因为它会大大补充应用程序的目的。但是,我选择不使用Rails资产管道,因为模板的HTML / CSS文件中的所有内容都与绝对路径静态链接。
turbolinks文档指定了在Javascript清单中创建的条目。但是,如果资产管道没有其他任何处理,它仍然可以工作吗?
答案 0 :(得分:0)
您应该努力缩小资产。它的好处很容易超越它所带来的障碍。我一直处于类似情况2次(用大模板构建rails 4 app,即metronic)。在第一个项目中我没有打扰,2年后我仍然为这个错误的决定买单。在第二个项目中,我写了一个rake任务,它梳理了css文件并将它们转换为scss文件并修复了图像路径。
namespace :metronic do
desc "Metronic template css yollarını rails asset pipeline'a uydur"
task import: :environment do |t|
assets_path= Pathname.new("#{Rails.root}/vendor/assets")
puts "Pass 1: rename css files to scss"
renamed_count = 0
Dir["#{assets_path}/**/*.css"].each do |f|
css_path= Pathname.new(f)
scss_path= css_path.sub_ext(".scss")
if scss_path.exist?
puts "deleting #{scss_path.relative_path_from(assets_path)} to avoid conflict"
scss_path.unlink
end
puts "#{css_path.relative_path_from(assets_path)} -> #{scss_path.relative_path_from(assets_path)}"
css_path.rename(scss_path)
renamed_count += 1
end
puts "Pass 1 complete: renamed #{renamed_count} files"
processed_scss_count = 0
total_fixed_lines_count=0
puts "Pass 2: fix url() references"
Dir["#{assets_path}/**/*.scss"].each do |path|
puts path
scss_path= Pathname.new(path)
dir= scss_path.relative_path_from(assets_path).dirname
dir_without_first_part= Pathname.new(dir.to_s[dir.to_s.index("/")+1..-1])
lines=[]
fixed_lines_count = 0
scss_path.each_line do |line|
if line.include?(" url(") or line.include?(":url(")
# background-image: url(data:...)
# background-image: url("data:...")
# background-image: url("../img/a.png")
# background-image: url(../img/a.png)
puts line
parts= line.split "url("
new_parts= [parts.delete_at(0)]
parts.each do |part|
url_subpart, rest_subpart = part.split(")", 2)
url_subpart.gsub! /["']/, ""
if url_subpart.start_with? "data:"
new_parts << "url(\"#{url_subpart}\")"
else
url_subpart= dir_without_first_part.join(url_subpart).to_s
url_subpart_delimited = url_subpart.split("/")
# "../../assets/admin/layout7/img/02.jpg" -> "layout7/img/02.jpg"
iki_noktalar = url_subpart_delimited.count("..")
iki_noktalar *= 2
url_subpart_delimited = url_subpart_delimited[iki_noktalar..-1]
url_subpart = url_subpart_delimited.join("/")
new_parts << "asset-url(\"#{url_subpart}\")"
end
new_parts << rest_subpart
end
new_line = new_parts.join
puts new_line
fixed_lines_count += 1
else
new_line= line
end
lines << new_line
end
scss_path.open(File::CREAT|File::TRUNC|File::RDWR) do |file|
file.puts lines
processed_scss_count += 1
end if fixed_lines_count>0
total_fixed_lines_count += fixed_lines_count
end
puts "Pass 2 complete."
puts "renamed #{renamed_count} files. Fixed #{total_fixed_lines_count} lines in #{processed_scss_count} scss files"
end
end
您还应该在config/initializers/assets.rb
中修改预编译选项,如下所示:
Rails.application.config.assets.precompile += ["signup.js", "signup.css", "login.js", "login.css", "modal.js", "modal.css", "home.js", "*.png", "*.gif", "*.jpg", "*.eot", "*.woff", "*.woff2", "*.ttf", "*.svg",]
祝你好运。