Rollup 后帖子事件小组件中的聊天链接丢失

你好 :waving_hand:

我们在 discourse-calendar 插件的聊天功能中发现了一个小 bug,帖子事件小部件中的 :speech_balloon: 聊天频道链接突然消失了。

这很可能是由于 Discourse 核心最近将构建系统迁移到 Rollup 所导致的:

  • af3385baba — 引入了 Rollup 编译器(默认禁用)
  • 74d532c559 — 将 Rollup 编译器设为默认启用

我可以修复这个问题并提供一个 PR :technologist:

预期行为

当向主题添加事件并启用“聊天集成”(“创建和管理特定于事件的聊天频道”)时,帖子上的事件小部件应包含一个指向与该事件关联的聊天频道的 :speech_balloon: 图标链接:

当前行为

目前,在当前的 main 分支上,帖子事件小部件的这一部分根本不会被渲染:

修复/解决方案

问题似乎在于,懒加载的 optionalRequire 辅助函数在使用 Rollup 后不再按最初设想的方式工作。据我理解,Rollup 调用 optionalRequire 的时间过早,此时其他模块尚未注册,因此它总是返回 false

解决问题的方法是将 optionalRequire 调用移到组件的构造函数中,使其真正进行懒加载评估。

对于模型文件,例如可以这样写:

diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js b/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js
index 742cc35a84..f687b956aa 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js
@@ -7,10 +7,6 @@ import User from "discourse/models/user";
 import DiscoursePostEventEventStats from "./discourse-post-event-event-stats";
 import DiscoursePostEventInvitee from "./discourse-post-event-invitee";
 
-const ChatChannel = optionalRequire(
-  "discourse/plugins/chat/discourse/models/chat-channel"
-);
-
 const DEFAULT_REMINDER = {
   type: "notification",
   value: 15,
@@ -62,6 +58,8 @@ export default class DiscoursePostEventEvent {
   @tracked _reminders;
 
   constructor(args = {}) {
+    const ChatChannel = optionalRequire( "discourse/plugins/chat/discourse/models/chat-channel");
+
     this.id = args.id;
     this.rrule = args.rrule;
     this.name = args.name;

对于渲染实际链接的组件,相应修改如下:

diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/chat-channel.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/chat-channel.gjs
index f673ee8a9f..a988b341e6 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/chat-channel.gjs
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/chat-channel.gjs
@@ -1,24 +1,27 @@
+import Component from "@glimmer/component";
 import { LinkTo } from "@ember/routing";
 import { optionalRequire } from "discourse/lib/utilities";
 import { and } from "discourse/truth-helpers";
 
-const ChannelTitle = optionalRequire(
-  "discourse/plugins/chat/discourse/components/channel-title"
-);
+export default class DiscoursePostEventChatChannel extends Component {
+  get channelTitle() {
+    return optionalRequire(
+      "discourse/plugins/chat/discourse/components/channel-title"
+    );
+  }
 
-const DiscoursePostEventChatChannel = <template>
-  {{#if (and @event.channel ChannelTitle)}}
-    <section class="event__section event-chat-channel">
-      <span></span>
-      <LinkTo
-        @route="chat.channel"
-        @models={{@event.channel.routeModels}}
-        class="chat-channel-link"
-      >
-        <ChannelTitle @channel={{@event.channel}} />
-      </LinkTo>
-    </section>
-  {{/if}}
-</template>;
-
-export default DiscoursePostEventChatChannel;
+  <template>
+    {{#if (and @event.channel this.channelTitle)}}
+      <section class="event__section event-chat-channel">
+        <span></span>
+        <LinkTo
+          @route="chat.channel"
+          @models={{@event.channel.routeModels}}
+          class="chat-channel-link"
+        >
+          <this.channelTitle @channel={{@event.channel}} />
+        </LinkTo>
+      </section>
+    {{/if}}
+  </template>
+}

(注意:我需要将其转换为类组件,因此 diff 看起来更大)

我将在 GitHub 上提供一个包含修复方案的 PR,以便大家测试并发表意见。

2 个赞

感谢 @klappradla。您提出的修复方案听起来不错——准备好后请发布 PR 的链接 :rocket:

这就对了 → https://github.com/discourse/discourse/pull/39287

1 个赞