Skip to content

Instantly share code, notes, and snippets.

@rjurado01
Created January 7, 2016 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjurado01/8bb326969f4ece54b03c to your computer and use it in GitHub Desktop.
Save rjurado01/8bb326969f4ece54b03c to your computer and use it in GitHub Desktop.
rails vue templates preprocesor
# config/initializers/vue_template.rb
module Sprockets
class Vue
def self.instance
@instance ||= new
end
def self.call(input)
instance.call(input)
end
def initialize(options = {})
end
def call(input)
code = input[:data]
return "" unless code
code.scan(/templatePath: (.*)\n/).each do |match|
relative_path = match[0].gsub(' ', '').gsub('\'', '').gsub('"', '')
absolute_path = Pathname.new(input[:load_path]).join(relative_path)
template = File.read(absolute_path)
template.gsub!("\n", '').gsub!('"', '\"')
code.gsub!(match[0], "\"#{template}\"")
end
code.gsub('templatePath:', 'template:')
end
end
#register_mime_type 'text/ecmascript-6', extensions: ['.vue'], charset: :unicode
#register_transformer 'text/ecmascript-6', 'application/javascript', ES6
#register_preprocessor 'text/ecmascript-6', DirectiveProcessor
register_engine '.vue', Vue
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment