Skip to content

Commit

Permalink
FIX: Wrap theme javascript inside IIFE to prevent using global namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaylorhq committed Jan 17, 2019
1 parent def6b2f commit 24b59af
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/models/theme_field.rb
Expand Up @@ -60,7 +60,7 @@ def self.force_recompilation!
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

Expand Down
20 changes: 11 additions & 9 deletions lib/theme_javascript_compiler.rb
Expand Up @@ -205,15 +205,17 @@ def append_js_error(message)
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)
Expand Down
40 changes: 22 additions & 18 deletions spec/models/theme_spec.rb
Expand Up @@ -325,15 +325,17 @@ def transpile(html)
.registerSettings(#{theme.id}, {"name":"bob"});
}
})();
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
var themePrefix = function themePrefix(key) {
return 'theme_translations.#{theme.id}.' + key;
};
Discourse._registerPluginCode('1.0', function (api) {
alert(settings.name);var a = function a() {};
});
}
(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) {
return 'theme_translations.#{theme.id}.' + key;
};
Discourse._registerPluginCode('1.0', function (api) {
alert(settings.name);var a = function a() {};
});
}
})();
HTML

theme_field.reload
Expand All @@ -351,15 +353,17 @@ def transpile(html)
.registerSettings(#{theme.id}, {"name":"bill"});
}
})();
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
var themePrefix = function themePrefix(key) {
return 'theme_translations.#{theme.id}.' + key;
};
Discourse._registerPluginCode('1.0', function (api) {
alert(settings.name);var a = function a() {};
});
}
(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) {
return 'theme_translations.#{theme.id}.' + key;
};
Discourse._registerPluginCode('1.0', function (api) {
alert(settings.name);var a = function a() {};
});
}
})();
HTML

theme_field.reload
Expand Down

1 comment on commit 24b59af

@discoursebot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/menu-disappears-after-update-2-2-0-beta8/106806/7

Please sign in to comment.