DEV: Support for `import Handlebars from 'handlebars'`; (#9600)

DEV: Support for import Handlebars from 'handlebars'; (#9600)

  • Remove Handlebars.SafeString usage

  • DEV: Support for import Handlebars from 'handlebars';

  • FIX: Sprockets was broken when node_modules was present

By default the old version of sprockets looks for application.js anywhere, including in a node_modules folder if this exists (which it will when we move to Ember CLI.)

diff --git a/app/assets/javascripts/admin/helpers/check-icon.js b/app/assets/javascripts/admin/helpers/check-icon.js
index 88cddc3..4641a15 100644
--- a/app/assets/javascripts/admin/helpers/check-icon.js
+++ b/app/assets/javascripts/admin/helpers/check-icon.js
@@ -1,7 +1,8 @@
 import { registerUnbound } from "discourse-common/lib/helpers";
 import { renderIcon } from "discourse-common/lib/icon-library";
+import { htmlSafe } from "@ember/template";
 
 registerUnbound("check-icon", function(value) {
   let icon = value ? "check" : "times";
-  return new Handlebars.SafeString(renderIcon("string", icon));
+  return htmlSafe(renderIcon("string", icon));
 });
diff --git a/app/assets/javascripts/admin/mixins/setting-component.js b/app/assets/javascripts/admin/mixins/setting-component.js
index 200ec00..e7afc11 100644
--- a/app/assets/javascripts/admin/mixins/setting-component.js
+++ b/app/assets/javascripts/admin/mixins/setting-component.js
@@ -7,6 +7,7 @@ import Mixin from "@ember/object/mixin";
 import showModal from "discourse/lib/show-modal";
 import { Promise } from "rsvp";
 import { ajax } from "discourse/lib/ajax";
+import { htmlSafe } from "@ember/template";
 
 const CUSTOM_TYPES = [
   "bool",
@@ -63,7 +64,7 @@ export default Mixin.create({
     }
     let preview = setting.get("preview");
     if (preview) {
-      return new Handlebars.SafeString(
+      return htmlSafe(
         "<div class='preview'>" +
           preview.replace(/\{\{value\}\}/g, value) +
           "</div>"
diff --git a/app/assets/javascripts/discourse-common/addon/helpers/d-icon.js b/app/assets/javascripts/discourse-common/addon/helpers/d-icon.js
index 454bb22..97f9327 100644
--- a/app/assets/javascripts/discourse-common/addon/helpers/d-icon.js
+++ b/app/assets/javascripts/discourse-common/addon/helpers/d-icon.js
@@ -1,6 +1,7 @@
 import { registerUnbound } from "discourse-common/lib/helpers";
 import { renderIcon } from "discourse-common/lib/icon-library";
+import { htmlSafe } from "@ember/template";
 
 registerUnbound("d-icon", function(id, params) {
-  return new Handlebars.SafeString(renderIcon("string", id, params));
+  return htmlSafe(renderIcon("string", id, params));
 });
diff --git a/app/assets/javascripts/discourse-common/addon/helpers/fa-icon.js b/app/assets/javascripts/discourse-common/addon/helpers/fa-icon.js
index 1d657d0..12b1823 100644
--- a/app/assets/javascripts/discourse-common/addon/helpers/fa-icon.js
+++ b/app/assets/javascripts/discourse-common/addon/helpers/fa-icon.js
@@ -1,6 +1,7 @@
 import { registerUnbound } from "discourse-common/lib/helpers";
 import { renderIcon } from "discourse-common/lib/icon-library";
 import deprecated from "discourse-common/lib/deprecated";
+import { htmlSafe } from "@ember/template";
 
 export function iconHTML(id, params) {
   return renderIcon("string", id, params);
@@ -8,5 +9,5 @@ export function iconHTML(id, params) {
 
 registerUnbound("fa-icon", function(icon, params) {
   deprecated("Use `{{d-icon}}` instead of `{{fa-icon}}");
-  return new Handlebars.SafeString(iconHTML(icon, params));
+  return htmlSafe(iconHTML(icon, params));
 });
diff --git a/app/assets/javascripts/discourse-common/addon/lib/helpers.js b/app/assets/javascripts/discourse-common/addon/lib/helpers.js
index 0f87040..759cc89 100644
--- a/app/assets/javascripts/discourse-common/addon/lib/helpers.js
+++ b/app/assets/javascripts/discourse-common/addon/lib/helpers.js
@@ -1,6 +1,7 @@
 import { get } from "@ember/object";
 import Helper from "@ember/component/helper";
 import RawHandlebars from "discourse-common/lib/raw-handlebars";
+import { htmlSafe } from "@ember/template";
 
 export function makeArray(obj) {
   if (obj === null || obj === undefined) {
@@ -13,7 +14,7 @@ export function htmlHelper(fn) {
   return Helper.helper(function(...args) {
     args =
       args.length > 1 ? args[0].concat({ hash: args[args.length - 1] }) : args;
-    return new Handlebars.SafeString(fn.apply(this, args) || "");
+    return htmlSafe(fn.apply(this, args) || "");
   });
 }
 
diff --git a/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js b/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js
index ca08cfd..6fe7591 100644
--- a/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js
+++ b/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js
@@ -1,7 +1,8 @@
+import Handlebars from "handlebars";
+
 // This is a mechanism for quickly rendering templates which is Ember aware
 // templates are highly compatible with Ember so you don't need to worry about calling "get"
 // and discourseComputed properties function, additionally it uses stringParams like Ember does
-
 const RawHandlebars = Handlebars.create();
 
 function buildPath(blk, args) {
diff --git a/app/assets/javascripts/discourse/app/components/create-topics-notice.js b/app/assets/javascripts/discourse/app/components/create-topics-notice.js
index 7789ccd..7db4e65 100644
--- a/app/assets/javascripts/discourse/app/components/create-topics-notice.js
+++ b/app/assets/javascripts/discourse/app/components/create-topics-notice.js
@@ -3,6 +3,7 @@ import { alias } from "@ember/object/computed";
 import Component from "@ember/component";
 import { observes } from "discourse-common/utils/decorators";
 import LivePostCounts from "discourse/models/live-post-counts";
+import { htmlSafe } from "@ember/template";
 
 export default Component.extend({
   classNameBindings: ["hidden:hidden", ":create-topics-notice"],
@@ -81,7 +82,7 @@ export default Component.extend({
       msg = "too_few_posts_notice_MF";
     }
 
-    return new Handlebars.SafeString(
+    return htmlSafe(
       I18n.messageFormat(msg, {
         requiredTopics: this.requiredTopics,
         requiredPosts: this.requiredPosts,
diff --git a/app/assets/javascripts/discourse/app/helpers/category-link.js b/app/assets/javascripts/discourse/app/helpers/category-link.js
index d214112..7f566b5 100644
--- a/app/assets/javascripts/discourse/app/helpers/category-link.js
+++ b/app/assets/javascripts/discourse/app/helpers/category-link.js
@@ -5,6 +5,7 @@ import { iconHTML } from "discourse-common/lib/icon-library";
 import Category from "discourse/models/category";
 import Site from "discourse/models/site";
 import { escapeExpression } from "discourse/lib/utilities";
+import { htmlSafe } from "@ember/template";
 
 let _renderer = defaultCategoryLinkRenderer;
 
@@ -79,9 +80,7 @@ export function categoryLinkHTML(category, options) {
       categoryOptions.recursive = true;
     }
   }
-  return new Handlebars.SafeString(
-    categoryBadgeHTML(category, categoryOptions)
-  );
+  return htmlSafe(categoryBadgeHTML(category, categoryOptions));
 }
 
 registerUnbound("category-link", categoryLinkHTML);
diff --git a/app/assets/javascripts/discourse/app/helpers/custom-html.js b/app/assets/javascripts/discourse/app/helpers/custom-html.js
index fc055ee..b9d424a 100644
--- a/app/assets/javascripts/discourse/app/helpers/custom-html.js
+++ b/app/assets/javascripts/discourse/app/helpers/custom-html.js
@@ -1,16 +1,17 @@
 import PreloadStore from "preload-store";
+import { htmlSafe } from "@ember/template";
 
 let _customizations = {};
 
 export function getCustomHTML(key) {
   const c = _customizations[key];
   if (c) {
-    return new Handlebars.SafeString(c);
+    return htmlSafe(c);
   }
 
   const html = PreloadStore.get("customHTML");
   if (html && html[key] && html[key].length) {
-    return new Handlebars.SafeString(html[key]);
+    return htmlSafe(html[key]);
   }
 }
 
diff --git a/app/assets/javascripts/discourse/app/helpers/discourse-tag.js b/app/assets/javascripts/discourse/app/helpers/discourse-tag.js
index 1477cf9..9a56007 100644
--- a/app/assets/javascripts/discourse/app/helpers/discourse-tag.js
+++ b/app/assets/javascripts/discourse/app/helpers/discourse-tag.js
@@ -1,6 +1,7 @@
 import { registerUnbound } from "discourse-common/lib/helpers";
 import renderTag from "discourse/lib/render-tag";

[... diff too long, it was truncated ...]

GitHub sha: d615de91

This commit appears in #9600 which was approved by jjaffeux. It was merged by eviltrout.