コンポーネントヘルパーファイルで外部ライブラリを要求する方法は?

テーマコンポーネントを作成しており、ヘルパーファイルから2つの外部ライブラリにアクセスする必要があります。現在のアプローチは機能しますが、少し不自然に感じられます。

イニシャライザー内:

import { apiInitializer } from 'discourse/lib/api';
import loadScript from 'discourse/lib/load-script';
import myHelper from '../lib/helper';

export default apiInitializer('0.11.1', (api) => {
  api.decorateCookedElement(
    (elem) => {
      loadScript(D3_URL).then(() => {
        loadScript(LUXON_URL).then(() => {
          ...
          myHelper(whatever, d3, luxon)
          ...
        })
      })
    },
    {id: 'discourse-xyz', onlyStream: true}
  )
});

次に myHelper.js 内:

export default function myHelper(whatever, d3, luxon) {
  d3();
  luxon();
  ...
}

ヘルパーファイルから直接ライブラリを要求しようとしたところ、プロミスを返す必要があり、イニシャライザー内で myHelper(whatever, d3, luxon).then(...) のように処理する必要が出てきました。他の理由から、これは避けたいと考えています。

より良いアプローチはありますか?

GitHub - merefield/discourse-tc-tag-cloud: A Discourse Theme Component that displays a tag cloud above the tag lists on the tags page · GitHub で私が使用しているアプローチを利用できます。

アセットはここで登録されている点にご注意ください。

そして、assets フォルダに配置されています。

「いいね!」 4

ロバート、ありがとうございます。試してみましたが、「theme_uploads」が見つかりません。これは settings.yml で設定すべきことでしょうか?

いいえ、その作業は不要です。

構文は仕様書に記載されたままです:discourse/spec/models/theme_spec.rb at c8f4f7c235ca38b39d4b66bf9e1305410e36815e · discourse/discourse · GitHub

「いいね!」 1

ああ、私のミスでした。assets に間違ったファイル名を指定していました。修正は済ませたのですが、今度は CSP エラーが発生しています:

load-script.js:35 スクリプトの読み込みが拒否されました
'http://localhost:4200/uploads/default/original/1X/c4a31754250cf6a40f7cbed182cfe2456d9be9fe.js'
以下のコンテンツセキュリティポリシー指令に違反しているためです:
"script-src http://localhost:4200/logs/ http://localhost:4200/sidekiq/ http://localhost:4200/mini-profiler-resources/ http://localhost:4200/assets/ http://localhost:4200/brotli_asset/ http://localhost:4200/extra-locales/ http://localhost:4200/highlight-js/ http://localhost:4200/javascripts/ http://localhost:4200/plugins/ http://localhost:4200/theme-javascripts/ http://localhost:4200/svg-sprite/ 
'unsafe-eval' http://localhost:4200/ember-cli-live-reload.js http://localhost:4200/_lr/ /uploads"。
なお、'script-src-elem' は明示的に設定されていないため、フォールバックとして 'script-src' が使用されています。

settings.yml で extend_content_security_policy が使用されていないようでしたので、何が不足しているのかわかりません。

「いいね!」 1

いずれにせよ、CSP の src 設定にアップロードディレクトリの URL を追加しました。ただし、よりターゲットを絞るために追加するかもしれません👍🏻

ああ、ここのことですね?こうすれば動きます:

settings.yml でこんな風に設定できると思っていたのですが(localhost では動作しないようですが):

extend_content_security_policy:
  type: list
  default: "script_src: http://localhost:4200/"
「いいね!」 1