Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ramaze+;CoffeeScript:是否可以使用Ramaze自动生成Javascript?_Javascript_Coffeescript_Sinatra_Ramaze - Fatal编程技术网

Ramaze+;CoffeeScript:是否可以使用Ramaze自动生成Javascript?

Ramaze+;CoffeeScript:是否可以使用Ramaze自动生成Javascript?,javascript,coffeescript,sinatra,ramaze,Javascript,Coffeescript,Sinatra,Ramaze,我正试图找到一种从CoffeeScript文件自动生成Javascript的方法,就像您在Sinatra中很容易做到的那样: require 'sinatra' require 'coffee-script' get '/*.js' do name = params[:splat][0] file_name = File.join("#{name}.coffee") pass unless File.exists?(file_name) content_type(:js, :ch

我正试图找到一种从CoffeeScript文件自动生成Javascript的方法,就像您在Sinatra中很容易做到的那样:

require 'sinatra'
require 'coffee-script'
get '/*.js' do
  name = params[:splat][0]
  file_name = File.join("#{name}.coffee")
  pass unless File.exists?(file_name)
  content_type(:js, :charset => 'utf-8')
  coffee(IO.read(file_name))
end

这样,在我的代码中,即使我只得到了
.coffee
文件,我也可以表现得像
.js
文件一样。与使用客户端CoffeeScript编译器相比,它没有那么特别,也没有那么麻烦…

处理这一问题的最佳方法可能是编写自己的机架中间件,并像下面那样使用它。您可能需要使用某种缓存,这样就不会在每次调用时从
.coffee
文件重建
.js
文件

require 'ramaze'

class BrewedCoffee
  def initialize(app)
    @app = app
  end

  def call(env)
    name = env['PATH_INFO']

    # Continue processing if requested file is not js
    return @app.call(env) if name !~ /\.js$/

    # Caching & freshness checks here...
    # ...

    # Brewing coffee
    name = name.split('.')[0..-2].join('.') + ".coffee"

    Ramaze.options.roots.each do |p|
      file_name = File.join("#{name}.coffee")
      next unless File.exists?(file_name)

      response_body = coffee(IO.read(file_name))
      headers["Content-Type"] = 'application/javascript'
      headers["Content-Length"] = response_body.length.to_s


      [status, headers, response_body]
    end

    @app.call(env)
  end
end


class MyController < Ramaze::Controller
  map '/'

  ...
end

Ramaze.middleware! :dev do |m|
  m.use(BrewedCoffee)
  m.run(Ramaze::AppMap)
end

Ramaze.start
require'ramaze'
班级煮咖啡
def初始化(应用程序)
@app=app
结束
def呼叫(环境)
name=env['PATH\u INFO']
#如果请求的文件不是js,则继续处理
如果名称为!~/\,则返回@app.call(env)。js$/
#在此处缓存和新鲜度检查。。。
# ...
#煮咖啡
name=name.split('.')[0..-2]。加入('.')+“.coffee”
Ramaze.options.root.each do|p|
file_name=file.join(“#{name}.coffee”)
下一个,除非文件.exists?(文件名)
响应\正文=咖啡(IO.read(文件名))
标题[“内容类型”]=“应用程序/javascript”
标题[“内容长度”]=响应\u body.Length.to\u s
[状态、标题、响应\u正文]
结束
@应用程序调用(环境)
结束
结束
类MyController
这是很快黑客和需要更多的爱,但你得到的想法。您可能不想在生产中这样做,只需预先制作咖啡,否则您的系统操作会遇到麻烦:D

谢谢leucos

我也曾考虑过机架中间件,但不知道是否有更“ramaze”的解决方案(如Ingental)

不管怎么说,这很有效,很好

以下是您的代码的修改版本:

require 'coffee-script'

# An attempt to allow Ramaze to generate Js file from coffee files
module Rack
  class BrewedCoffee
    def initialize(app, options = {})
      @app = app
      @lookup_path = options[:lookup_path].nil? ? [__DIR__] : options[:lookup_path]
    end

    def call(env)
      name = env['PATH_INFO']

      # Continue processing if requested file is not js
      return @app.call(env) if File.extname(name) != '.js'

      coffee_name = "#{name[0...-3]}.coffee"

      @lookup_path.each do |p|
        coffee_file = File.join(p, coffee_name)
        next unless File.file?(coffee_file)

        response_body = CoffeeScript.compile(File.read(coffee_file))
        headers = {}
        headers["Content-Type"] = 'application/javascript'
        headers["Content-Length"] = response_body.length.to_s

        return [200, headers, [response_body]]
      end

      @app.call(env)
    end
  end
end
我稍微清理了一下,删除了Ramaze依赖项,这样它就是一个纯机架中间件:)

您可以使用它调用:

m.use Rack::BrewedCoffee, :lookup_path => Ramaze.options.roots
再见,
安德烈亚斯

事实上,我们可能都在重新创造生活: