2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-10 21:01:33 +08:00

FIX: Wrap theme javascript inside IIFE to prevent using global namespace

This commit is contained in:
David Taylor 2019-01-17 14:39:39 +00:00
parent def6b2fca1
commit 24b59afe2a
3 changed files with 34 additions and 28 deletions

View file

@ -60,7 +60,7 @@ class ThemeField < ActiveRecord::Base
validates :name, format: { with: /\A[a-z_][a-z0-9_-]*\z/i },
if: Proc.new { |field| ThemeField.theme_var_type_ids.include?(field.type_id) }
COMPILER_VERSION = 8
COMPILER_VERSION = 9
belongs_to :theme

View file

@ -205,6 +205,7 @@ class ThemeJavascriptCompiler
def transpile(es6_source, version)
template = Tilt::ES6ModuleTranspilerTemplate.new {}
wrapped = <<~PLUGIN_API_JS
(function() {
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
const settings = Discourse.__container__
.lookup("service:theme-settings")
@ -214,6 +215,7 @@ class ThemeJavascriptCompiler
#{es6_source}
});
}
})();
PLUGIN_API_JS
template.babel_transpile(wrapped)

View file

@ -325,6 +325,7 @@ HTML
.registerSettings(#{theme.id}, {"name":"bob"});
}
})();
(function () {
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
var themePrefix = function themePrefix(key) {
@ -334,6 +335,7 @@ HTML
alert(settings.name);var a = function a() {};
});
}
})();
HTML
theme_field.reload
@ -351,6 +353,7 @@ HTML
.registerSettings(#{theme.id}, {"name":"bill"});
}
})();
(function () {
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
var themePrefix = function themePrefix(key) {
@ -360,6 +363,7 @@ HTML
alert(settings.name);var a = function a() {};
});
}
})();
HTML
theme_field.reload