2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-18 10:47:52 +08:00
|
|
|
class DiscourseIIFE
|
|
|
|
def initialize(options = {}, &block)
|
|
|
|
end
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2016-04-18 10:47:52 +08:00
|
|
|
def self.instance
|
|
|
|
@instance ||= new
|
|
|
|
end
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2016-04-18 10:47:52 +08:00
|
|
|
def self.call(input)
|
|
|
|
instance.call(input)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add a IIFE around our javascript
|
|
|
|
def call(input)
|
|
|
|
path = input[:environment].context_class.new(input).pathname.to_s
|
|
|
|
data = input[:data]
|
2013-03-01 03:31:39 +08:00
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
# Only discourse or admin paths
|
2013-06-19 01:44:20 +08:00
|
|
|
return data unless (path =~ /\/javascripts\/discourse/ || path =~ /\/javascripts\/admin/ || path =~ /\/test\/javascripts/)
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2013-06-21 01:58:54 +08:00
|
|
|
# Ignore the js helpers
|
2013-06-19 01:44:20 +08:00
|
|
|
return data if (path =~ /test\_helper\.js/)
|
2013-06-21 01:58:54 +08:00
|
|
|
return data if (path =~ /javascripts\/helpers\//)
|
2013-06-19 01:44:20 +08:00
|
|
|
|
2014-04-30 08:59:57 +08:00
|
|
|
# Ignore ES6 files
|
|
|
|
return data if (path =~ /\.es6/)
|
|
|
|
|
2013-06-19 01:44:20 +08:00
|
|
|
# Ignore translations
|
2013-02-23 04:41:12 +08:00
|
|
|
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/
|
2014-09-27 03:23:15 +08:00
|
|
|
return data if path =~ /\.hbs/
|
2020-02-12 03:38:12 +08:00
|
|
|
return data if path =~ /\.hbr/
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2016-11-16 00:41:16 +08:00
|
|
|
return data if path =~ /discourse-loader/
|
|
|
|
|
2015-08-14 03:19:27 +08:00
|
|
|
"(function () {\n\nvar $ = window.jQuery;\n// IIFE Wrapped Content Begins:\n\n#{data}\n\n// IIFE Wrapped Content Ends\n\n })(this);"
|
2013-02-23 04:41:12 +08:00
|
|
|
end
|
|
|
|
|
2014-04-30 08:59:57 +08:00
|
|
|
end
|