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

chore: 開発モードでフロントエンドとバックエンドを独立して起動するようにする #12452

Closed

Conversation

fruitriin
Copy link
Contributor

@fruitriin fruitriin commented Nov 25, 2023

What

Misskeyの開発において、フロントエンドとバックエンドの単体での起動が可能になります。
これは pnpm dev で起動した場合のみで、 pnpm start の場合、今までと動作は変わらない想定です。

pnpm dev でバックエンドと疎通するには .config/default.yml で hostを https://localhost:3000/ にする必要があります
プロジェクトルートで pnpm dev をすると、フロントエンドとバックエンドのローカル開発環境が立ち上がります

http://127.0.0.1:5173/ ← viteのパス をブラウザで開いてください 注意:localhost:5173 ではいけません
また、バックエンドのポートである :3000 でもありません。

Note: フロントエンドとバックエンドを別々のシェルで起動したい

一つのシェルで frontendディレクトリに移動して pnpm dev、
もう一つのシェルでbackendディレクトリに移動してpnpm devでもよいです。

* プロジェクトルートのpnpm dev は、 すべてのworkspaceでpnpm devします
* backendには postgresqlとredisが起動している必要があります

Why

HMRが有効になり、開発体験が向上します。
(もはや、フロントエンドの変更を確認するのに都度ビルドする必要はありあません!)

BackendとFrontendを別々に起動/再起動した場合、 packages/backend, packages/frontend 配下でpnpm dev すればいつでもそれらのプロセスを個別にとめて再起動することができます

Additional info (optional)

PostgresqlとRedisを用意するのがめんどくさいのでこれらだけDockerでやりたい

postgresqlとredisだけ起動するdocker-compose.yml を同梱しました。

docker compose -f docker-compose.local-db.yml up

redisとpostgresqlだけ立ち上がります

テスト観点など

私の手元で何度か環境を作り直してうまく動いているように見えますが、少なからずビルドチェーンに手を加えているため多くの開発者によって検証するほうが良いでしょう。

Checklist

  • Read the contribution guide
  • Test working in a local environment
  • (If needed) Add story of storybook
  • (If needed) Update CHANGELOG.md
  • (If possible) Add tests

@github-actions github-actions bot added packages/frontend Client side specific issue/PR packages/backend Server side specific issue/PR labels Nov 25, 2023
Copy link

codecov bot commented Nov 25, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e42c91d) 78.93% compared to head (35d90db) 78.83%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #12452      +/-   ##
===========================================
- Coverage    78.93%   78.83%   -0.10%     
===========================================
  Files          957      958       +1     
  Lines       104165   104229      +64     
  Branches      8361     8331      -30     
===========================================
- Hits         82218    82171      -47     
- Misses       21947    22058     +111     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


# Final accessible URL seen by a user.
# dev-local では identiconのCORS問題の解決のため 127.0.0.1 である必要があります
url: http://127.0.0.1:5173/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このファイルの主な違いはここだけです

@@ -0,0 +1,42 @@
version: "3"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このファイルでは redisとpostgresのみを起動するために作りました。
webのサービスが消え、そのdependencyがなくなり、ホストマシンからアクセスできるように portsが加えられています。

@@ -26,7 +26,7 @@
"check:connect": "cd packages/backend && pnpm check:connect",
"migrateandstart": "pnpm migrate && pnpm start",
"watch": "pnpm dev",
"dev": "node ./scripts/dev.mjs",
"dev": "pnpm -r dev",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dev.mjsのかわりに pnpm -r dev をしています。
dev.mjsは削除してもよいかもしれません(このPRでは行っていません)

Comment on lines +190 to +195
} else {
if (defaultStore.state.darkMode) {
applyTheme(darkTheme.value);
} else {
applyTheme(lightTheme.value);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local-devモードでテーマがapplyされないことの修正です

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

↑の旨をコメントに残しておくと良さそう

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自由に付け足したりしておいていただけると

packages/frontend/src/index.html Show resolved Hide resolved
@@ -260,6 +260,7 @@ const useGroupedNotifications = computed(defaultStore.makeGetterSetter('useGroup
watch(lang, () => {
miLocalStorage.setItem('lang', lang.value as string);
miLocalStorage.removeItem('locale');
miLocalStorage.removeItem('localeVersion');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

言語を変更したときに local-devモードで色々おかしくなる問題の対応です。
fruitriin#3

@@ -0,0 +1,187 @@
import path from 'path';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local-devモードのvite.configです。 通常用のvite.config.tsの差分として作ってもよかったのですが、今回はスコープアウトしました

Comment on lines 51 to 75
host: '127.0.0.1',
port: 5173,
proxy: {
'/api': {
changeOrigin: true,
target: 'http://127.0.0.1:3000/',
},
'/assets': 'http://127.0.0.1:3000/',
'/files': 'http://127.0.0.1:3000/',
'/twemoji': 'http://127.0.0.1:3000/',
'/fluent-emoji': 'http://127.0.0.1:3000/',
'/sw.js': 'http://127.0.0.1:3000/',
'/streaming': {
target: 'ws://127.0.0.1:3000/',
ws: true,
},
'/favicon.ico': 'http://127.0.0.1:3000/',
'/identicon': {
target: 'http://127.0.0.1:3000/',
rewrite(path) {
return path.replace('@127.0.0.1:5173', '');
},

},
},
Copy link
Contributor Author

@fruitriin fruitriin Nov 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主な変更点です。
これにより、フロントエンドへのリクエストの一部をバックエンドにプロキシするなどを行っています。

'safari16',
],
manifest: 'manifest.json',
rollupOptions: {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rollupOptionsのinputを削除しています。
この場合、index.htmlが対象となります。

Copy link
Contributor

github-actions bot commented Nov 25, 2023

このPRによるapi.jsonの差分

差分はこちら

Get diff files from Workflow Page

@kakkokari-gtyih kakkokari-gtyih added the 🛠️Dev Development of Misskey itself label Nov 25, 2023
fruitriin and others added 7 commits December 6, 2023 10:56
…lone-frontend

# Conflicts:
#	packages/frontend/src/pages/welcome.entrance.a.vue
…lone-frontend

# Conflicts:
#	packages/frontend/src/pages/welcome.entrance.a.vue
…lone-frontend

# Conflicts:
#	packages/frontend/src/pages/welcome.entrance.a.vue
@fruitriin fruitriin changed the title Feat/launch standalone frontend chore: 開発モードでフロントエンドとバックエンドを独立して起動するようにする Dec 6, 2023
@@ -4,6 +4,7 @@
"type": "module",
"scripts": {
"watch": "vite",
"dev": "vite --config vite.config.local-dev.ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

devでは vite.config.ts のかわりに vite.config.local-dev.tsを使用します。

Copy link
Member

@samunohito samunohito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2点ほどコメントしました

docker-compose.local-db.yml Outdated Show resolved Hide resolved
packages/frontend/src/index.html Show resolved Hide resolved
packages/frontend/src/index.html Show resolved Hide resolved
docker-compose.local-db.yml Outdated Show resolved Hide resolved
@fruitriin
Copy link
Contributor Author

@syuilo これでどうだ!

@tamaina
Copy link
Contributor

tamaina commented Dec 6, 2023

最近私は開発に参加してないし割と根本的な話なので無視してもらって構わないのだけれど、個人的にviteでプロキシする挙動はあんまり好きではないです…
(実環境でもnginxが挟まっているけど)バックエンドに直接リクエストする場合とは内容が変わってしまう可能性があったりproxyのパスを書き加えたりする必要があるため(こういうところで仕事が増えると割と面倒事が起きるイメージがある)

(あとそもそも今HMRが動かなくなってるのは何でなのかしら)

これを解消する方向の方がいいと思う

@fruitriin
Copy link
Contributor Author

fruitriin commented Dec 6, 2023

プロクシする挙動になってるのはフロントエンドとバックエンドのドメイン(ホスト名+ポート名)が同じものとして認識してくれないとCORSによって各種リクエストが拒絶されてしまうからです
従って、プロクシすること自体はフロントエンドとバックエンドを分離するという目的に対する本PRでの変更点(コード上の差分)において重要なことですが目的に対する主題ではありません

  • 短期的にはHMRが有効になるので開発体験が大幅に向上します
  • 本番環境でフロントエンドのコードをバックエンドを介さずに返したいというIssueがあり、それに向けた一歩となると信じています

どちらが望ましいですか?

@tamaina
Copy link
Contributor

tamaina commented Dec 6, 2023

本番環境でフロントエンドのコードをバックエンドを介さずに返したいというIssue

番号を教えてください:pray:

@fruitriin
Copy link
Contributor Author

これ
#10894

@tamaina
Copy link
Contributor

tamaina commented Dec 6, 2023

#10894 の理解に相違がありそうというのは一旦置いておいて、フロント開発するのにわざわざバックエンドを動かしておきたくないよね(つまり既存のサーバーにつなげたい)というのはわかる

@fruitriin
Copy link
Contributor Author

たしかに、IssueではMisskey全体(ないし個別ページ)をSSRしたいように見えます

その議論とは別にこのPRの一つ目の効用である開発体験向上に繋がり、
この議論へ対応していく場合でもこのPRは有効だと考えます

@acid-chicken
Copy link
Member

#10894 の理解に相違がありそうというのは一旦置いておいて、フロント開発するのにわざわざバックエンドを動かしておきたくないよね(つまり既存のサーバーにつなげたい)というのはわかる

これについては HTTP API と密結合状態なのは解消できないと思っているので Storybook で実現した差がある(し一部しているつもり)

@tamaina
Copy link
Contributor

tamaina commented Dec 6, 2023

(本当に必要なものって、環境変数で既存の公開サーバーを指定してViteがそこにプロキシする…というのはhttps絡みで難しいかしらね

@fruitriin
Copy link
Contributor Author

default.yml の hostはlocalhostで、 dbの値に外部のサーバーの値を入れればなんとなく動くようです
ドライブの解決がうまくいってませんが

スクリーンショット 2023-12-07 0 28 28

@fruitriin
Copy link
Contributor Author

PRのオーナーが飽きたのでクローズします
望むならば誰か改めてPRを作ってください

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛠️Dev Development of Misskey itself packages/backend Server side specific issue/PR packages/frontend Client side specific issue/PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants