Skip to content

A platform for community discussion. Free, open, simple.

REFACTOR: Remove `Discourse.__widget_helpers`

Robin Ward7df57b35dae8
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 40f1d275bb4..8adedfd9c91 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -99,3 +99,4 @@
//= require_tree ./discourse/app/services
//= require_tree ./discourse/app/widgets
+//= require ./widget-runtime
diff --git a/app/assets/javascripts/discourse-common/addon/lib/icon-library.js b/app/assets/javascripts/discourse-common/addon/lib/icon-library.js
index 7c9b2a47aba..29a7601ae60 100644
--- a/app/assets/javascripts/discourse-common/addon/lib/icon-library.js
+++ b/app/assets/javascripts/discourse-common/addon/lib/icon-library.js
@@ -89,11 +89,6 @@ export function convertIconClass(icon) {
.trim();
}
-// TODO: Improve how helpers are registered for vdom compliation
-if (typeof Discourse !== "undefined") {
- Discourse.__widget_helpers.iconNode = iconNode;
-}
-
export function registerIconRenderer(renderer) {
_renderers.unshift(renderer);
}
diff --git a/app/assets/javascripts/discourse/app/app.js b/app/assets/javascripts/discourse/app/app.js
index 765d0300087..9693598a636 100644
--- a/app/assets/javascripts/discourse/app/app.js
+++ b/app/assets/javascripts/discourse/app/app.js
@@ -8,7 +8,6 @@ const _pluginCallbacks = [];
const Discourse = Application.extend({
rootElement: "#main",
- __widget_helpers: {},
customEvents: {
paste: "paste"
diff --git a/app/assets/javascripts/discourse/app/helpers/node.js b/app/assets/javascripts/discourse/app/helpers/node.js
index 21d4d9d2c8b..e73345dab4a 100644
--- a/app/assets/javascripts/discourse/app/helpers/node.js
+++ b/app/assets/javascripts/discourse/app/helpers/node.js
@@ -17,11 +17,6 @@ export function dateNode(dt) {
}
}
-// TODO: Improve how helpers are registered for vdom compliation
-if (typeof Discourse !== "undefined") {
- Discourse.__widget_helpers.dateNode = dateNode;
-}
-
export function numberNode(num, opts) {
opts = opts || {};
num = parseInt(num, 10);
diff --git a/app/assets/javascripts/discourse/app/widgets/post.js b/app/assets/javascripts/discourse/app/widgets/post.js
index ae87f5cbc19..ea30d110e4a 100644
--- a/app/assets/javascripts/discourse/app/widgets/post.js
+++ b/app/assets/javascripts/discourse/app/widgets/post.js
@@ -64,11 +64,6 @@ export function avatarFor(wanted, attrs) {
);
}
-// TODO: Improve how helpers are registered for vdom compliation
-if (typeof Discourse !== "undefined") {
- Discourse.__widget_helpers.avatar = avatarFor;
-}
-
createWidget("select-post", {
tagName: "div.select-posts",
diff --git a/app/assets/javascripts/discourse/app/widgets/raw-html.js b/app/assets/javascripts/discourse/app/widgets/raw-html.js
index ef7bfb363f1..03d085e3710 100644
--- a/app/assets/javascripts/discourse/app/widgets/raw-html.js
+++ b/app/assets/javascripts/discourse/app/widgets/raw-html.js
@@ -22,8 +22,3 @@ export default class RawHtml {
}
RawHtml.prototype.type = "Widget";
-
-// TODO: Improve how helpers are registered for vdom compliation
-if (typeof Discourse !== "undefined") {
- Discourse.__widget_helpers.rawHtml = RawHtml;
-}
diff --git a/app/assets/javascripts/widget-runtime.js b/app/assets/javascripts/widget-runtime.js
new file mode 100644
index 00000000000..b1f0d054ffd
--- /dev/null
+++ b/app/assets/javascripts/widget-runtime.js
@@ -0,0 +1,11 @@
+// discourse-skip-module
+
+(function(context) {
+ // register widget helpers for compiled `hbs`
+ context.__widget_helpers = {
+ avatar: require("discourse/widgets/post").avatarFor,
+ dateNode: require("discourse/helpers/node").dateNode,
+ iconNode: require("discourse-common/lib/icon-library").iconNode,
+ rawHtml: require("discourse/widgets/raw-html").default
+ };
+})(this);
diff --git a/lib/javascripts/widget-hbs-compiler.js b/lib/javascripts/widget-hbs-compiler.js
index 5316bd355f5..d8228205f80 100644
--- a/lib/javascripts/widget-hbs-compiler.js
+++ b/lib/javascripts/widget-hbs-compiler.js
@@ -283,7 +283,7 @@ function compile(template) {
Object.keys(compiler.state.helpersUsed).forEach(h => {
let id = compiler.state.helpersUsed[h];
- imports += `var __h${id} = Discourse.__widget_helpers.${h}; `;
+ imports += `var __h${id} = __widget_helpers.${h}; `;
});
return `function(attrs, state) { ${imports}var _r = [];\n${code}\nreturn _r; }`;
diff --git a/script/test_hbs_compiler.rb b/script/test_hbs_compiler.rb
index f3c5be35c1b..9936edae647 100644
--- a/script/test_hbs_compiler.rb
+++ b/script/test_hbs_compiler.rb
@@ -6,6 +6,7 @@ template = <<~HBS
{{actions-summary-item attrs=as}}
{{attach widget="actions-summary-item" attrs=as}}
{{testing value="hello"}}
+ {{date "today"}}
HBS
ctx = MiniRacer::Context.new(timeout: 15000)