mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 07:53:43 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
45 lines
1.1 KiB
Ruby
45 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class DiscourseIIFE
|
|
def initialize(options = {}, &block)
|
|
end
|
|
|
|
def self.instance
|
|
@instance ||= new
|
|
end
|
|
|
|
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]
|
|
|
|
# Only discourse or admin paths
|
|
return data unless (path =~ /\/javascripts\/discourse/ || path =~ /\/javascripts\/admin/ || path =~ /\/test\/javascripts/)
|
|
|
|
# Ignore the js helpers
|
|
return data if (path =~ /test\_helper\.js/)
|
|
return data if (path =~ /javascripts\/helpers\//)
|
|
|
|
# Ignore ES6 files
|
|
return data if (path =~ /\.es6/)
|
|
|
|
# Ignore translations
|
|
return data if (path =~ /\/translations/)
|
|
|
|
# We don't add IIFEs to handlebars
|
|
return data if path =~ /\.handlebars/
|
|
return data if path =~ /\.shbrs/
|
|
return data if path =~ /\.hbrs/
|
|
return data if path =~ /\.hbs/
|
|
|
|
return data if path =~ /discourse-loader/
|
|
|
|
"(function () {\n\nvar $ = window.jQuery;\n// IIFE Wrapped Content Begins:\n\n#{data}\n\n// IIFE Wrapped Content Ends\n\n })(this);"
|
|
end
|
|
|
|
end
|