しょーもないメモメモ。
module Rack
class LessCss
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
req = Rack::Request.new(env)
if req.fullpath == '/css/main.css' then
lessfile = 'less_template/main.less' # less file
if ::File.exists?(lessfile) then
cssstr = Less::Engine.new(::File.new(lessfile)).to_css
body = [cssstr]
headers['Content-Type'] = 'text/css'
headers.delete('Content-Length') # Rack::ContentLength に任せる
status = 200
end
end
[status, headers, body]
end
end
end
PATHが、ウルトラハードコーディングだけど今は気にしない(汗。
んで、main.less には、以下のように import を書いています。
@import 'html5-reset-1.4.css'; @import 'core.less'; @import 'header.less'; @import 'footer.less'; . . .
書くときは分割して、読み込むときは1つのファイルですむと。
キャッシュなどは全く考えてないので、制作のためのものです。
# 実際 main.css にまとめて css にしてから上げてるし。