gpt4 book ai didi

css - 如何在 Sinatra 应用程序中使用 rails-autoprefixer?

转载 作者:太空宇宙 更新时间:2023-11-04 10:33:25 25 4
gpt4 key购买 nike

我无法使自动前缀工作。它被调用,但在我的 css 代码中没有结果。

这里有 Sinatra 应用程序的说明 - https://github.com/ai/autoprefixer-rails

应用.rb

class Application < Sinatra::Base
# Load path and gems/bundler
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
require "bundler"
Bundler.require
register Sinatra::AssetPipeline
assets = Sprockets::Environment.new
AutoprefixerRails.install(assets)
### other

# Actual Rails Assets integration, everything else is Sprockets
if defined?(RailsAssets)
RailsAssets.load_paths.each do |path|
settings.sprockets.append_path(path)
end
end
end

我查看了 gem 源并找到了这样的例子:

@assets = Sprockets::Environment.new
@assets.append_path(@dir.join('app/app/assets/stylesheets'))
AutoprefixerRails.install(@assets, browsers: ['chrome 25'])

@dir = Pathname(__FILE__).dirname
@css = @dir.join('app/app/assets/stylesheets/test.css').read
AutoprefixerRails.process(@css)

最佳答案

从外观上看,Sprockets 配置不正确。 Sprockets::Enviroment接受一个 block ,使用它到 Assets 需要配置。这是我使用的文件夹结构这个例子:

├── app.rb
├── assets
│   ├── some_more_styles.css
│   └── styles.css
└── views
└── index.erb

这是我配置 Sprockets 环境的方式:

# app.rb
require 'autoprefixer-rails'

assets = Sprockets::Environment.new do |env|
# This ensures sprockets can find the CSS files
env.append_path "assets"
end

AutoprefixerRails.install(assets)

还有一个步骤可以让 Sprockets 与 Sinatra 协同工作。每一个 Assets 需要手动路由。例如,如果 index.erb有一个<link>尝试在路径 /assets/styles.css 加载文件的标签,如果该路由未在中定义,则该路由将导致 404 Not Found 错误 app.rb .为避免这些 404,请像这样定义路由:

# app.rb
get '/assets/*' do
# The env["PATH_INFO"] results in the string '/assets/styles.css' in
# our example. We need to remove the '/assets' part since Sprockets
# will take care of appending it when invoked on the next line.

env["PATH_INFO"].sub!("/assets", "")
assets.call(env)
end

我已将完整代码上传至 https://gist.github.com/kgrz/5caf63f827e5a6181597cefae484a515为您引用。这又基于 Sinatra Recipes article onSprockets

关于css - 如何在 Sinatra 应用程序中使用 rails-autoprefixer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36354581/

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