DEV: Remove `Discourse.RAW_TEMPLATES` (#9630)

DEV: Remove Discourse.RAW_TEMPLATES (#9630)

We were sharing Discourse both as an application object and a namespace which complicated things for Ember CLI. This patch moves raw templates into __DISCOURSE_RAW_TEMPLATES and adds a couple helper methods to create/remove them.

diff --git a/app/assets/javascripts/app-boot.js b/app/assets/javascripts/app-boot.js
index d744e7f..0fd7a5e 100644
--- a/app/assets/javascripts/app-boot.js
+++ b/app/assets/javascripts/app-boot.js
@@ -6,6 +6,11 @@
   }
   let Discourse = requirejs("discourse/app").default;
 
+  // required for our template compiler
+  window.__DISCOURSE_RAW_TEMPLATES = requirejs(
+    "discourse-common/lib/raw-templates"
+  ).__DISCOURSE_RAW_TEMPLATES;
+
   // ensure Discourse is added as a global
   window.Discourse = Discourse;
 })();
diff --git a/app/assets/javascripts/discourse-common/addon/lib/raw-templates.js b/app/assets/javascripts/discourse-common/addon/lib/raw-templates.js
new file mode 100644
index 0000000..2aa14db
--- /dev/null
+++ b/app/assets/javascripts/discourse-common/addon/lib/raw-templates.js
@@ -0,0 +1,38 @@
+import { getResolverOption } from "discourse-common/resolver";
+
+export const __DISCOURSE_RAW_TEMPLATES = {};
+
+export function addRawTemplate(name, template) {
+  __DISCOURSE_RAW_TEMPLATES[name] = template;
+}
+
+export function removeRawTemplate(name) {
+  delete __DISCOURSE_RAW_TEMPLATES[name];
+}
+
+export function findRawTemplate(name) {
+  if (getResolverOption("mobileView")) {
+    return (
+      __DISCOURSE_RAW_TEMPLATES[`javascripts/mobile/${name}`] ||
+      __DISCOURSE_RAW_TEMPLATES[`javascripts/${name}`] ||
+      __DISCOURSE_RAW_TEMPLATES[`mobile/${name}`] ||
+      __DISCOURSE_RAW_TEMPLATES[name]
+    );
+  }
+
+  return (
+    __DISCOURSE_RAW_TEMPLATES[`javascripts/${name}`] ||
+    __DISCOURSE_RAW_TEMPLATES[name]
+  );
+}
+
+export function buildRawConnectorCache(findOutlets) {
+  let result = {};
+  findOutlets(__DISCOURSE_RAW_TEMPLATES, (outletName, resource) => {
+    result[outletName] = result[outletName] || [];
+    result[outletName].push({
+      template: __DISCOURSE_RAW_TEMPLATES[resource]
+    });
+  });
+  return result;
+}
diff --git a/app/assets/javascripts/discourse/app/app.js b/app/assets/javascripts/discourse/app/app.js
index 8080da1..62c4cfd 100644
--- a/app/assets/javascripts/discourse/app/app.js
+++ b/app/assets/javascripts/discourse/app/app.js
@@ -10,7 +10,6 @@ const _pluginCallbacks = [];
 const Discourse = Application.extend(FocusEvent, {
   rootElement: "#main",
   _docTitle: document.title,
-  RAW_TEMPLATES: {},
   __widget_helpers: {},
   customEvents: {
     paste: "paste"
diff --git a/app/assets/javascripts/discourse/app/components/badge-selector.js b/app/assets/javascripts/discourse/app/components/badge-selector.js
index 0f5c229..47cf4db 100644
--- a/app/assets/javascripts/discourse/app/components/badge-selector.js
+++ b/app/assets/javascripts/discourse/app/components/badge-selector.js
@@ -3,7 +3,7 @@ import discourseComputed, {
   on,
   observes
 } from "discourse-common/utils/decorators";
-import { findRawTemplate } from "discourse/lib/raw-templates";
+import { findRawTemplate } from "discourse-common/lib/raw-templates";
 const { makeArray } = Ember;
 
 export default Component.extend({
diff --git a/app/assets/javascripts/discourse/app/components/composer-editor.js b/app/assets/javascripts/discourse/app/components/composer-editor.js
index a71902e..7d89caa 100644
--- a/app/assets/javascripts/discourse/app/components/composer-editor.js
+++ b/app/assets/javascripts/discourse/app/components/composer-editor.js
@@ -22,7 +22,7 @@ import { load, LOADING_ONEBOX_CSS_CLASS } from "pretty-text/oneboxer";
 import { applyInlineOneboxes } from "pretty-text/inline-oneboxer";
 import { ajax } from "discourse/lib/ajax";
 import EmberObject from "@ember/object";
-import { findRawTemplate } from "discourse/lib/raw-templates";
+import { findRawTemplate } from "discourse-common/lib/raw-templates";
 import { iconHTML } from "discourse-common/lib/icon-library";
 import {
   tinyAvatar,
diff --git a/app/assets/javascripts/discourse/app/components/d-editor.js b/app/assets/javascripts/discourse/app/components/d-editor.js
index faeeb11..d1a64b4 100644
--- a/app/assets/javascripts/discourse/app/components/d-editor.js
+++ b/app/assets/javascripts/discourse/app/components/d-editor.js
@@ -10,7 +10,7 @@ import { categoryHashtagTriggerRule } from "discourse/lib/category-hashtags";
 import { search as searchCategoryTag } from "discourse/lib/category-tag-search";
 import { cookAsync } from "discourse/lib/text";
 import { getRegister } from "discourse-common/lib/get-owner";
-import { findRawTemplate } from "discourse/lib/raw-templates";
+import { findRawTemplate } from "discourse-common/lib/raw-templates";
 import { siteDir } from "discourse/lib/text-direction";
 import {
   determinePostReplaceSelection,
diff --git a/app/assets/javascripts/discourse/app/components/emoji-picker.js b/app/assets/javascripts/discourse/app/components/emoji-picker.js
index e088f9c..86fa8fc 100644
--- a/app/assets/javascripts/discourse/app/components/emoji-picker.js
+++ b/app/assets/javascripts/discourse/app/components/emoji-picker.js
@@ -2,7 +2,7 @@ import { inject as service } from "@ember/service";
 import { throttle, debounce, schedule } from "@ember/runloop";
 import Component from "@ember/component";
 import { on, observes } from "discourse-common/utils/decorators";
-import { findRawTemplate } from "discourse/lib/raw-templates";
+import { findRawTemplate } from "discourse-common/lib/raw-templates";
 import { emojiUrlFor } from "discourse/lib/text";
 import {
   extendedEmojiList,
diff --git a/app/assets/javascripts/discourse/app/components/group-selector.js b/app/assets/javascripts/discourse/app/components/group-selector.js
index 3fe2e50..a743cde 100644
--- a/app/assets/javascripts/discourse/app/components/group-selector.js
+++ b/app/assets/javascripts/discourse/app/components/group-selector.js
@@ -4,7 +4,7 @@ import discourseComputed, {
   on,
   observes
 } from "discourse-common/utils/decorators";
-import { findRawTemplate } from "discourse/lib/raw-templates";
+import { findRawTemplate } from "discourse-common/lib/raw-templates";
 
 export default Component.extend({
   @discourseComputed("placeholderKey")
diff --git a/app/assets/javascripts/discourse/app/components/topic-list-item.js b/app/assets/javascripts/discourse/app/components/topic-list-item.js
index 0746ed9..2460dc8 100644
--- a/app/assets/javascripts/discourse/app/components/topic-list-item.js
+++ b/app/assets/javascripts/discourse/app/components/topic-list-item.js
@@ -3,7 +3,7 @@ import { alias } from "@ember/object/computed";
 import Component from "@ember/component";
 import { schedule } from "@ember/runloop";
 import DiscourseURL from "discourse/lib/url";
-import { findRawTemplate } from "discourse/lib/raw-templates";
+import { findRawTemplate } from "discourse-common/lib/raw-templates";
 import { wantsNewWindow } from "discourse/lib/intercept-click";
 import { on } from "@ember/object/evented";
 
diff --git a/app/assets/javascripts/discourse/app/components/user-selector.js b/app/assets/javascripts/discourse/app/components/user-selector.js
index d362c60..a340b27 100644
--- a/app/assets/javascripts/discourse/app/components/user-selector.js
+++ b/app/assets/javascripts/discourse/app/components/user-selector.js
@@ -2,7 +2,7 @@ import { isEmpty } from "@ember/utils";
 import { on, observes } from "discourse-common/utils/decorators";
 import TextField from "discourse/components/text-field";
 import userSearch from "discourse/lib/user-search";
-import { findRawTemplate } from "discourse/lib/raw-templates";
+import { findRawTemplate } from "discourse-common/lib/raw-templates";
 
 export default TextField.extend({
   autocorrect: false,
diff --git a/app/assets/javascripts/discourse/app/helpers/raw.js b/app/assets/javascripts/discourse/app/helpers/raw.js
index 0a764a1..fe0d43c 100644
--- a/app/assets/javascripts/discourse/app/helpers/raw.js
+++ b/app/assets/javascripts/discourse/app/helpers/raw.js
@@ -1,5 +1,5 @@
 import { registerUnbound } from "discourse-common/lib/helpers";
-import { findRawTemplate } from "discourse/lib/raw-templates";

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

GitHub sha: 612284ce

This commit appears in #9630 which was approved by davidtaylorhq and jjaffeux. It was merged by eviltrout.