Setting up rake-pipeline for use with handlebars alongside Google App Engine

时光怂恿深爱的人放手 提交于 2019-11-27 13:21:35
dudleyf

Rake::Pipeline.build is the method that evaluates an Assetfile. You can imagine that your entire Assetfile is wrapped inside a Rake::Pipeline.build {} block; you shouldn't ever need to write one inside an Assetfile.

Some of the filters in the docs are hypothetical, most of those docs were written before there were any filters at all. A CoffeeScript compiler has been recently added, though.

As to your main question, I'm not sure there's a clean way to do it with the current rakep implementation. An Assetfile is just Ruby, though, so it's possible to hack something together that should work. Here's how I would write yours:

require "json"
require "rake-pipeline-web-filters"
require "rake-pipeline-web-filters/helpers"

class HandlebarsFilter < Rake::Pipeline::Filter
  def initialize(&block)
    block ||= proc { |input| input.sub(/\.handlebars$/, '.js') }
    super(&block)
  end

  def generate_output(inputs, output)
    inputs.each do |input|
      output.write "return Ember.Handlebars.compile(#{input.read.to_json})"
    end
  end
end

# process all js, css and html files in app/assets
input "assets"

# processed files should be outputted to public
output "public"

# process all coffee files
match "**/*.coffee" do
  # compile all CoffeeScript files. the output file
  # for the compilation should be the input name
  # with the .coffee extension replaced with .js
  coffee_script

  # The coffee_script helper is exactly equivalent to:
  # filter Rake::Pipeline::Web::Filters::CoffeeScriptCompiler
end

match "**/*.js" do
  minispade
  if ENV['RAKEP_ENV'] == "production"
    concat "application.js"
  else
    concat
  end
end

match "**/*.handlebars" do
  filter HandlebarsFilter
  minispade
  concat "templates.js"
end

The if ENV['RAKEP_ENV'] bit reads an environment variable to decide whether to concatenate your JS to a single file.

So now you can run RAKEP_ENV="production" rakep build for a concatenated build, or just rakep build for a development build.

Since you're not a Ruby person, here are the most reliable steps for getting a stock OSX environment set up with rake pipeline:

Step 1: Install bundler

# on OSX, using built-in Ruby
$ sudo gem install bundler --pre

Step 2: Create a Gemfile

# inside your app directory
$ bundle init

# will create a file named Gemfile in the root

Step 3: Add rake-pipeline to the Gemfile

# inside the Gemfile
gem "rake-pipeline-web-filters"

Step 4: Install your gems

$ bundle install --binstubs

Step 5: Set up Assetfile

However you were already doing it...

Step 6: Run Rake::Pipeline

# to run the preview server
$ bin/rakep

# to build your assets
$ bin/rakep build
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!