2013-02-22 14:41:12 -06:00
|
|
|
class DiscourseIIFE < Sprockets::Processor
|
|
|
|
|
|
|
|
# Add a IIFE around our javascript
|
|
|
|
def evaluate(context, locals)
|
|
|
|
|
|
|
|
path = context.pathname.to_s
|
2013-02-28 13:31:39 -06:00
|
|
|
|
2013-02-22 14:41:12 -06:00
|
|
|
# Only discourse or admin paths
|
2013-06-18 12:44:20 -05:00
|
|
|
return data unless (path =~ /\/javascripts\/discourse/ || path =~ /\/javascripts\/admin/ || path =~ /\/test\/javascripts/)
|
2013-02-22 14:41:12 -06:00
|
|
|
|
2013-06-20 12:58:54 -05:00
|
|
|
# Ignore the js helpers
|
2013-06-18 12:44:20 -05:00
|
|
|
return data if (path =~ /test\_helper\.js/)
|
2013-06-20 12:58:54 -05:00
|
|
|
return data if (path =~ /javascripts\/helpers\//)
|
2013-06-18 12:44:20 -05:00
|
|
|
|
2014-04-29 19:59:57 -05:00
|
|
|
# Ignore ES6 files
|
|
|
|
return data if (path =~ /\.es6/)
|
|
|
|
|
2013-06-18 12:44:20 -05:00
|
|
|
# Ignore translations
|
2013-02-22 14:41:12 -06:00
|
|
|
return data if (path =~ /\/translations/)
|
|
|
|
|
|
|
|
# We don't add IIFEs to handlebars
|
|
|
|
return data if path =~ /\.handlebars/
|
2013-02-28 13:31:39 -06:00
|
|
|
return data if path =~ /\.shbrs/
|
|
|
|
return data if path =~ /\.hbrs/
|
2014-09-26 14:23:15 -05:00
|
|
|
return data if path =~ /\.hbs/
|
2013-02-22 14:41:12 -06:00
|
|
|
|
2014-07-22 13:13:13 -05:00
|
|
|
res = "(function () {\n\nvar $ = window.jQuery;\n// IIFE Wrapped Content Begins:\n\n#{data}\n\n// IIFE Wrapped Content Ends\n\n })(this);"
|
|
|
|
|
|
|
|
# Include JS code for JSHint
|
|
|
|
unless Rails.env.production?
|
|
|
|
req_path = path.sub(Rails.root.to_s, '')
|
|
|
|
.sub("/app/assets/javascripts", "")
|
|
|
|
.sub("/test/javascripts", "")
|
2015-08-11 16:34:02 -05:00
|
|
|
res << "\nwindow.__eslintSrc = window.__eslintSrc || {}; window.__eslintSrc['/assets#{req_path}'] = #{data.to_json};\n"
|
2014-07-22 13:13:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
res
|
2013-02-22 14:41:12 -06:00
|
|
|
end
|
|
|
|
|
2014-04-29 19:59:57 -05:00
|
|
|
end
|