assemble-prod.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. # Assemble production artifacts for GROWI app.
  3. # Run from the workspace root.
  4. set -euo pipefail
  5. echo "[1/4] Deploying production dependencies..."
  6. rm -rf out
  7. pnpm deploy out --prod --legacy --filter @growi/app
  8. rm -rf apps/app/node_modules
  9. mv out/node_modules apps/app/node_modules
  10. echo "[1/4] Done."
  11. # Rewrite apps/app/node_modules/ top-level symlinks from workspace root to the local .pnpm/ store.
  12. # pnpm deploy creates symlinks that point to ../../../../node_modules/.pnpm/ (workspace root),
  13. # which will not exist in production. Rewrite to point within apps/app/node_modules/.pnpm/ instead.
  14. echo "[1b/4] Rewriting apps/app/node_modules symlinks..."
  15. find apps/app/node_modules -maxdepth 2 -type l | while read -r link; do
  16. target=$(readlink "$link")
  17. # Scoped packages (@scope/pkg, maxdepth 2): ../../../../node_modules/.pnpm/ → ../.pnpm/
  18. # Non-scoped packages (maxdepth 1): ../../../node_modules/.pnpm/ → .pnpm/
  19. new_target=$(echo "$target" | sed 's|../../../../node_modules/\.pnpm/|../.pnpm/|; s|../../../node_modules/\.pnpm/|.pnpm/|')
  20. if [ "$target" != "$new_target" ]; then ln -sfn "$new_target" "$link"; fi
  21. done
  22. echo "[1b/4] Done."
  23. # Redirect .next/node_modules/ symlinks from workspace root to deployed apps/app/node_modules/.pnpm/.
  24. # Turbopack generates symlinks pointing to ../../../../node_modules/.pnpm/ (workspace root),
  25. # which will not exist in production environments.
  26. # Rewriting to ../../node_modules/.pnpm/ (apps/app/) uses the pnpm deploy output instead,
  27. # preserving pnpm's isolated structure so transitive deps remain resolvable.
  28. echo "[2/4] Rewriting .next/node_modules symlinks..."
  29. if [ -d apps/app/.next/node_modules ]; then
  30. find apps/app/.next/node_modules -maxdepth 2 -type l | while read -r link; do
  31. target=$(readlink "$link")
  32. new_target=$(echo "$target" | sed 's|../../../../node_modules/\.pnpm/|../../node_modules/.pnpm/|')
  33. if [ "$target" != "$new_target" ]; then ln -sfn "$new_target" "$link"; fi
  34. done
  35. else
  36. echo "[2/4] Skipped (no .next/node_modules directory)."
  37. fi
  38. echo "[2/4] Done."
  39. echo "[3/4] Removing build cache..."
  40. rm -rf apps/app/.next/cache
  41. echo "[3/4] Done."
  42. # Remove next.config.ts to prevent Next.js from attempting to install TypeScript at server startup,
  43. # which would corrupt node_modules (e.g. @growi/core).
  44. echo "[4/4] Removing next.config.ts..."
  45. rm -f apps/app/next.config.ts
  46. echo "[4/4] Done."
  47. echo "Assembly complete."