From 3a4992633ee62d5edfbb484d9c6bcb3cf158489d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 31 Jul 2023 14:34:36 +0200 Subject: Migrate server to ESM Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports) --- scripts/dev/cli.sh | 16 ---------------- scripts/dev/peertube-cli.sh | 11 +++++++++++ scripts/dev/peertube-runner.sh | 6 +++--- scripts/dev/server.sh | 8 ++++---- 4 files changed, 18 insertions(+), 23 deletions(-) delete mode 100755 scripts/dev/cli.sh create mode 100755 scripts/dev/peertube-cli.sh (limited to 'scripts/dev') diff --git a/scripts/dev/cli.sh b/scripts/dev/cli.sh deleted file mode 100755 index 39ecaad94..000000000 --- a/scripts/dev/cli.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -eu - -rm -rf ./dist/server/tools/ - -( - cd ./server/tools - yarn install --pure-lockfile -) - -mkdir -p "./dist/server/tools" -cp -r "./server/tools/node_modules" "./dist/server/tools" - -cd ./server/tools -../../node_modules/.bin/tsc-watch --build --verbose --onSuccess 'sh -c "cd ../../ && npm run resolve-tspaths:server"' diff --git a/scripts/dev/peertube-cli.sh b/scripts/dev/peertube-cli.sh new file mode 100755 index 000000000..172bf038e --- /dev/null +++ b/scripts/dev/peertube-cli.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -eu + +rm -rf ./apps/peertube-cli/dist + +cd ./apps/peertube-cli + +../../node_modules/.bin/concurrently -k \ + "../../node_modules/.bin/tsc -w --noEmit" \ + "node ./scripts/watch.js" diff --git a/scripts/dev/peertube-runner.sh b/scripts/dev/peertube-runner.sh index e39259372..7bd756123 100755 --- a/scripts/dev/peertube-runner.sh +++ b/scripts/dev/peertube-runner.sh @@ -2,10 +2,10 @@ set -eu -rm -rf ./packages/peertube-runner/dist +rm -rf ./apps/peertube-runner/dist -cd ./packages/peertube-runner +cd ./apps/peertube-runner ../../node_modules/.bin/concurrently -k \ "../../node_modules/.bin/tsc -w --noEmit" \ - "./node_modules/.bin/esbuild ./peertube-runner.ts --bundle --sourcemap --platform=node --external:"./lib-cov/fluent-ffmpeg" --external:pg-hstore --watch --outfile=dist/peertube-runner.js" + "node ./scripts/watch.js" diff --git a/scripts/dev/server.sh b/scripts/dev/server.sh index c52c5124c..4112cb2f8 100755 --- a/scripts/dev/server.sh +++ b/scripts/dev/server.sh @@ -16,10 +16,10 @@ cp -r "./client/src/locale" "./client/dist/locale" mkdir -p "./dist/server/lib" -npm run tsc -- -b -v --incremental +npm run tsc -- -b -v --incremental server/tsconfig.json npm run resolve-tspaths:server -cp -r ./server/static ./server/assets ./dist/server -cp -r "./server/lib/emails" "./dist/server/lib" +cp -r ./server/server/static ./server/server/assets ./dist/server +cp -r "./server/server/lib/emails" "./dist/server/lib" -./node_modules/.bin/tsc-watch --build --preserveWatchOutput --verbose --onSuccess 'sh -c "npm run resolve-tspaths:server && NODE_ENV=dev node dist/server"' +./node_modules/.bin/tsc-watch --build --preserveWatchOutput --verbose --onSuccess 'sh -c "npm run resolve-tspaths:server && NODE_ENV=dev node dist/server"' server/tsconfig.json -- cgit v1.2.3