gpt4 book ai didi

javascript - 在没有 Assets 管道的情况下使用 Turbolinks?

转载 作者:行者123 更新时间:2023-11-28 06:16:03 26 4
gpt4 key购买 nike

我正在围绕预制 HTML5 模板构建 Rails 5 Web 应用程序。我想使用 Turbolinks,因为它将极大地补充应用程序的用途。但是,我选择不使用 Rails 资源管道,因为模板的 HTML/CSS 文件中的所有内容都与绝对路径静态链接。

turbolinks 文档指定了要在 Javascript list 中创建的条目。但是,如果 Assets 管道没有处理任何其他内容,它仍然可以工作吗?

最佳答案

您应该努力减少您的 Assets 。它的好处很容易超过它所带来的障碍。我遇到过类似的情况两次(使用大模板构建rails 4应用程序,即metronic)。在第一个项目中我没有打扰,两年后我仍然为那个错误的决定付出代价。在第二个项目中,我编写了一个 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",]

祝你好运。

关于javascript - 在没有 Assets 管道的情况下使用 Turbolinks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35956035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com