Skip to content

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

FIX: Restore backwards-compatibility of `templates/components/*.hbs` (#33131)

David Taylor8f52b81efa70
diff --git a/app/assets/javascripts/discourse/app/instance-initializers/component-templates.js b/app/assets/javascripts/discourse/app/instance-initializers/component-templates.js
new file mode 100644
index 00000000000..91b4e0ac459
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/instance-initializers/component-templates.js
@@ -0,0 +1,25 @@
+import ClassicComponent from "@ember/component";
+import DiscourseTemplateMap from "discourse/lib/discourse-template-map";
+
+// Looks for `**/templates/components/**` in themes and plugins, and registers
+// a classic component backing class for it, for backwards compatibility.
+// These templates will all be raising the `component-template-resolving` deprecation,
+// so there's no need to emit our own deprecation here.
+export default {
+ after: ["populate-template-map"],
+
+ initialize(owner) {
+ for (const templatePath of DiscourseTemplateMap.templates.keys()) {
+ if (!templatePath.startsWith("components/")) {
+ continue;
+ }
+
+ const componentName = templatePath.slice("components/".length);
+ const component = owner.resolveRegistration(`component:${componentName}`);
+
+ if (!component) {
+ owner.register(`component:${componentName}`, ClassicComponent);
+ }
+ }
+ },
+};
diff --git a/app/assets/javascripts/discourse/app/services/deprecation-warning-handler.js b/app/assets/javascripts/discourse/app/services/deprecation-warning-handler.js
index e00a3e87c07..6b12285a340 100644
--- a/app/assets/javascripts/discourse/app/services/deprecation-warning-handler.js
+++ b/app/assets/javascripts/discourse/app/services/deprecation-warning-handler.js
@@ -29,6 +29,7 @@ export const CRITICAL_DEPRECATIONS = [
"discourse.post-stream.trigger-new-post",
"discourse.plugin-outlet-classic-args-clash",
"discourse.decorate-plugin-outlet",
+ "component-template-resolving",
];
if (DEBUG) {
diff --git a/app/assets/javascripts/discourse/tests/integration/component-templates-test.gjs b/app/assets/javascripts/discourse/tests/integration/component-templates-test.gjs
new file mode 100644
index 00000000000..b64e8c2bc7b
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/component-templates-test.gjs
@@ -0,0 +1,31 @@
+import { render } from "@ember/test-helpers";
+import { hbs } from "ember-cli-htmlbars";
+import { module, test } from "qunit";
+import { withSilencedDeprecationsAsync } from "discourse/lib/deprecated";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { registerTemporaryModule } from "../helpers/temporary-module-helper";
+
+module("Integration | Initializers | plugin-component-templates", function () {
+ module("template-only component definition behaviour", function (hooks) {
+ hooks.beforeEach(() => {
+ registerTemporaryModule(
+ `discourse/plugins/some-plugin-name/discourse/templates/components/plugin-template-only-definition`,
+ hbs`classic component`
+ );
+ });
+
+ setupRenderingTest(hooks);
+
+ test("treats plugin template-only definition as classic component", async function (assert) {
+ await withSilencedDeprecationsAsync(
+ "component-template-resolving",
+ async () => {
+ await render(hbs`<PluginTemplateOnlyDefinition class='test-class'/>`);
+ assert
+ .dom("div.test-class")
+ .hasText("classic component", "renders as classic component");
+ }
+ );
+ });
+ });
+});