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

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