REFACTOR: Remove `Discourse.__widget_helpers`

REFACTOR: Remove Discourse.__widget_helpers

It’s now a variable in the context where the templates are created.

diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 40f1d27..8adedfd 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 7c9b2a4..29a7601 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 765d030..9693598 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 21d4d9d..e73345d 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 ae87f5c..ea30d11 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 ef7bfb3..03d085e 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 0000000..b1f0d05
--- /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 5316bd3..d822820 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 f3c5be3..9936eda 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)

GitHub sha: 7df57b35