copy-version-to-standalone.cjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. const fs = require("node:fs");
  2. const path = require("node:path");
  3. function copyDirIfExists(srcDir, dstDir) {
  4. if (!fs.existsSync(srcDir)) {
  5. console.warn(`[copy-standalone] Skip missing dir: ${srcDir}`);
  6. return;
  7. }
  8. fs.mkdirSync(path.dirname(dstDir), { recursive: true });
  9. fs.cpSync(srcDir, dstDir, { recursive: true, force: true });
  10. console.log(`[copy-standalone] Copied ${srcDir} -> ${dstDir}`);
  11. }
  12. const src = path.resolve(process.cwd(), "VERSION");
  13. const dstDir = path.resolve(process.cwd(), ".next", "standalone");
  14. const dst = path.join(dstDir, "VERSION");
  15. if (!fs.existsSync(src)) {
  16. console.error(`[copy-version] VERSION not found at ${src}`);
  17. process.exit(1);
  18. }
  19. fs.mkdirSync(dstDir, { recursive: true });
  20. fs.copyFileSync(src, dst);
  21. console.log(`[copy-version] Copied VERSION -> ${dst}`);
  22. // Make standalone output self-contained for local `node .next/standalone/server.js` runs.
  23. // Next.js standalone requires `.next/static` and `public` to exist next to `server.js`.
  24. copyDirIfExists(
  25. path.resolve(process.cwd(), ".next", "static"),
  26. path.resolve(dstDir, ".next", "static")
  27. );
  28. copyDirIfExists(path.resolve(process.cwd(), "public"), path.resolve(dstDir, "public"));