Skip to content

Instantly share code, notes, and snippets.

@perlun
Last active December 15, 2015 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perlun/5286391 to your computer and use it in GitHub Desktop.
Save perlun/5286391 to your computer and use it in GitHub Desktop.
Guard helper to be able to compile Handlebars files for Ember.js
require 'guard/guard'
#
# Based on the guard-handlebars gem, which uses the following license:
#
# Copyright (C) 2012 by Chris Homer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
module Guard
class EmberHandlebars < Guard
def initialize(watchers = [], options = {})
super(watchers, options)
@options = options
run_all
end
def run_all
run_on_change(Watcher.match_files(self, Dir.glob(File.join('**', '*.*'))))
end
def run_on_change(paths)
paths.each do |file|
output_file = get_output(file)
FileUtils.mkdir_p File.dirname(output_file)
File.open(output_file, 'w') { |f| f.write(compile_handlebars(file)) }
::Guard::UI.info "Compiled handlebars in '#{file}' to js in '#{output_file}'"
::Guard::Notifier.notify("Compiled handlebars in #{file}", :title => "Guard::EmberHandlebars", :image => :success) if @options[:notifications]
end
notify paths
end
def notify(changed_files)
::Guard.guards.reject{ |guard| guard == self }.each do |guard|
paths = Watcher.match_files(guard, changed_files)
guard.run_on_change paths unless paths.empty?
end
end
private
# Get the file path to output the js based on the file being
# built. The output path is relative to where guard is being run.
#
# @param file [String] path to file being built
# @return [String] path to file where output should be written
#
def get_output(file)
file_dir = File.dirname(file)
file_name = File.basename(file).split('.')[0..-2].join('.')
unless file_name =~ /\.js$/
file_name << ".js"
end
file_dir = file_dir.gsub(Regexp.new("#{@options[:input]}(\/){0,1}"), '') if @options[:input]
file_dir = File.join(@options[:output], file_dir) if @options[:output]
if file_dir == ''
file_name
else
File.join(file_dir, file_name)
end
end
def compile_handlebars file
content = IO.read(file)
name = file.gsub('.handlebars', '')
name = name.gsub(/^#{@options[:remove_prefix]}/, '')
begin
content = content.gsub("\n", '')
content = content.gsub("'", "\\\\'")
result = "Ember.TEMPLATES['#{name}'] = Ember.Handlebars.compile('#{content}');"
result
rescue StandardError => error
puts "ERROR COMPILING #{file}: #{error}"
end
end
end
end
@mikegrassotti
Copy link

Agreed writing a guard makes sense, surprised nobody has done this yet. At first glance it seems like this guarddoes not actually precompile, but instead is generating the javascript that would be required to compile them client-side.

result = "Ember.TEMPLATES['#{name}'] = Ember.Handlebars.compile('#{content}');"

To precompile, you would need something like:

compiled_template = Barber::Ember::FilePrecompiler.call(IO.read(file))
# now ruby variable compiled_template is a string like: "Ember.Handlebars.template(function(...));"

result = "Ember.TEMPLATES['#{name}'] = '#{compiled_template}';"
# now result is a JS string that sets Ember.TEMPLATES[name] to precompiled handlebars

http://stackoverflow.com/questions/9860512/emberjs-handlebars-precompiling?rq=1

@perlun
Copy link
Author

perlun commented Apr 3, 2013

You're right - it's "false marketing". :)

Your changes look cool, will have to look into them further, since actually precompiling is better performance-wise. Thanks Mike!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment