Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getTextAssetをbackendに実装せず、直接importする #2536

Open
Hiroshiba opened this issue Feb 16, 2025 · 3 comments
Open

getTextAssetをbackendに実装せず、直接importする #2536

Hiroshiba opened this issue Feb 16, 2025 · 3 comments
Labels
初心者歓迎タスク 初心者にも優しい簡単めなタスク 機能向上

Comments

@Hiroshiba
Copy link
Member

内容

プライバシーポリシーの取得などは、現状electronのメインプロセスで読み込んだものをフロントエンドに渡しています。

window.backendからメインプロセスに問い合わせてるのがここで、

getTextAsset: (textType) => {
return ipcRendererInvokeProxy.GET_TEXT_ASSET(textType) as Promise<
TextAsset[typeof textType]
>;
},

フロントからwindow.backendにリクエスト投げてるのがここです。

voicevox/src/store/index.ts

Lines 154 to 200 in 41d6666

GET_HOW_TO_USE_TEXT: {
async action() {
return await window.backend.getTextAsset("HowToUse");
},
},
GET_CONTACT_TEXT: {
async action() {
return await window.backend.getTextAsset("Contact");
},
},
GET_Q_AND_A_TEXT: {
async action() {
return await window.backend.getTextAsset("QAndA");
},
},
GET_POLICY_TEXT: {
async action() {
return await window.backend.getTextAsset("PrivacyPolicy");
},
},
GET_OSS_LICENSES: {
async action() {
return await window.backend.getTextAsset("OssLicenses");
},
},
GET_UPDATE_INFOS: {
async action() {
return await window.backend.getTextAsset("UpdateInfos");
},
},
GET_OSS_COMMUNITY_INFOS: {
async action() {
return await window.backend.getTextAsset("OssCommunityInfos");
},
},
GET_PRIVACY_POLICY_TEXT: {
async action() {
return await window.backend.getTextAsset("PrivacyPolicy");
},
},

最近viteに詳しくなってわかったのですが、こんなことせず普通にファイルをimportすれば手っ取り早いはずです。
window.backendからgetTextAssetを、ipc通信からGET_TEXT_ASSETを削除できるはず。

Pros 良くなる点

コードが減る

実現方法

importすればOKです。
ブラウザ版がちょうどそういう実装をしているので、これをstore/index.ts内に直接書けば良いはず!

const fileName = AssetTextFileNames[textType];
const v = await fetch(toStaticPath(fileName));
if (textType === "OssLicenses" || textType === "UpdateInfos") {
return v.json();
}
return v.text();

その他

結構簡単にできつつ、かつviteの使い方の勉強にもなるので初学者におすすめです。

@Hiroshiba Hiroshiba added 初心者歓迎タスク 初心者にも優しい簡単めなタスク 機能向上 labels Feb 16, 2025
@Hiroshiba
Copy link
Member Author

@sabonerune さん辺りご興味ありませんか?
viteのちょっとした勉強になるのと、以前たしかこのあたりのリファクタリングに取り組んでくださったと思うので地の利があるかなーと思いメンションしてみました!

@sevenc-nanashi
Copy link
Member

sevenc-nanashi commented Feb 16, 2025

先読み:import.meta.globをeagerモードで動かしたほうが楽そうな気はちょっとします。
特定のフォルダ内におけばgetTextAssetsできる、みたいにできるはず?

@Hiroshiba
Copy link
Member Author

Hiroshiba commented Feb 16, 2025

たぶんこんな感じでimportできるはず!!(これはmock用のアセットを取得する例です)
https://github.com/VOICEVOX/voicevox/pull/2539/files?w=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
初心者歓迎タスク 初心者にも優しい簡単めなタスク 機能向上
Projects
None yet
Development

No branches or pull requests

2 participants