From 7df57b35dae86007eb75b040ea76b7809f14733f Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 6 Aug 2020 14:35:46 -0400 Subject: [PATCH] REFACTOR: Remove `Discourse.__widget_helpers` It's now a variable in the context where the templates are created. --- app/assets/javascripts/application.js | 1 + .../discourse-common/addon/lib/icon-library.js | 5 ----- app/assets/javascripts/discourse/app/app.js | 1 - app/assets/javascripts/discourse/app/helpers/node.js | 5 ----- app/assets/javascripts/discourse/app/widgets/post.js | 5 ----- .../javascripts/discourse/app/widgets/raw-html.js | 5 ----- app/assets/javascripts/widget-runtime.js | 11 +++++++++++ lib/javascripts/widget-hbs-compiler.js | 2 +- script/test_hbs_compiler.rb | 1 + 9 files changed, 14 insertions(+), 22 deletions(-) create mode 100644 app/assets/javascripts/widget-runtime.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 40f1d275bb428..8adedfd9c9170 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 7c9b2a47aba79..29a7601ae6073 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 765d0300087ee..9693598a6368b 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 21d4d9d2c8bc1..e73345dab4a15 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 ae87f5cbc19ce..ea30d110e4a1d 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 ef7bfb363f1cb..03d085e37107d 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 0000000000000..b1f0d054ffd7a --- /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 5316bd355f548..d8228205f808e 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 f3c5be35c1b88..9936edae647e3 100644 --- a/script/test_hbs_compiler.rb +++ b/script/test_hbs_compiler.rb @@ -6,6 +6,7 @@ {{actions-summary-item attrs=as}} {{attach widget="actions-summary-item" attrs=as}} {{testing value="hello"}} + {{date "today"}} HBS ctx = MiniRacer::Context.new(timeout: 15000)