discourse/lib/discourse_iife.rb

26 lines
818 B
Ruby
Raw Normal View History

class DiscourseIIFE < Sprockets::Processor
# Add a IIFE around our javascript
def evaluate(context, locals)
path = context.pathname.to_s
2013-03-01 03:31:39 +08:00
# Only discourse or admin paths
return data unless (path =~ /\/javascripts\/discourse/ || path =~ /\/javascripts\/admin/ || path =~ /\/test\/javascripts/)
2013-06-21 01:58:54 +08:00
# Ignore the js helpers
return data if (path =~ /test\_helper\.js/)
2013-06-21 01:58:54 +08:00
return data if (path =~ /javascripts\/helpers\//)
# Ignore translations
return data if (path =~ /\/translations/)
# We don't add IIFEs to handlebars
return data if path =~ /\.handlebars/
2013-03-01 03:31:39 +08:00
return data if path =~ /\.shbrs/
return data if path =~ /\.hbrs/
"(function () {\n\nvar $ = window.jQuery;\n// IIFE Wrapped Content Begins:\n\n#{data}\n\n// IIFE Wrapped Content Ends\n\n })(this);"
end
end