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 }, 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) } if: Proc.new { |field| ThemeField.theme_var_type_ids.include?(field.type_id) }
COMPILER_VERSION = 8 COMPILER_VERSION = 9
belongs_to :theme belongs_to :theme

View File

@@ -205,15 +205,17 @@ class ThemeJavascriptCompiler
def transpile(es6_source, version) def transpile(es6_source, version)
template = Tilt::ES6ModuleTranspilerTemplate.new {} template = Tilt::ES6ModuleTranspilerTemplate.new {}
wrapped = <<~PLUGIN_API_JS wrapped = <<~PLUGIN_API_JS
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') { (function() {
const settings = Discourse.__container__ if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
.lookup("service:theme-settings") const settings = Discourse.__container__
.getObjectForTheme(#{@theme_id}); .lookup("service:theme-settings")
const themePrefix = (key) => `theme_translations.#{@theme_id}.${key}`; .getObjectForTheme(#{@theme_id});
Discourse._registerPluginCode('#{version}', api => { const themePrefix = (key) => `theme_translations.#{@theme_id}.${key}`;
#{es6_source} Discourse._registerPluginCode('#{version}', api => {
}); #{es6_source}
} });
}
})();
PLUGIN_API_JS PLUGIN_API_JS
template.babel_transpile(wrapped) template.babel_transpile(wrapped)

View File

@@ -325,15 +325,17 @@ HTML
.registerSettings(#{theme.id}, {"name":"bob"}); .registerSettings(#{theme.id}, {"name":"bob"});
} }
})(); })();
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') { (function () {
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id}); if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
var themePrefix = function themePrefix(key) { var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
return 'theme_translations.#{theme.id}.' + key; var themePrefix = function themePrefix(key) {
}; return 'theme_translations.#{theme.id}.' + key;
Discourse._registerPluginCode('1.0', function (api) { };
alert(settings.name);var a = function a() {}; Discourse._registerPluginCode('1.0', function (api) {
}); alert(settings.name);var a = function a() {};
} });
}
})();
HTML HTML
theme_field.reload theme_field.reload
@@ -351,15 +353,17 @@ HTML
.registerSettings(#{theme.id}, {"name":"bill"}); .registerSettings(#{theme.id}, {"name":"bill"});
} }
})(); })();
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') { (function () {
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id}); if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
var themePrefix = function themePrefix(key) { var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
return 'theme_translations.#{theme.id}.' + key; var themePrefix = function themePrefix(key) {
}; return 'theme_translations.#{theme.id}.' + key;
Discourse._registerPluginCode('1.0', function (api) { };
alert(settings.name);var a = function a() {}; Discourse._registerPluginCode('1.0', function (api) {
}); alert(settings.name);var a = function a() {};
} });
}
})();
HTML HTML
theme_field.reload theme_field.reload