No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 4.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.allDependencyNames = exports.isUsingAppDirectory = exports.isUsingImageOptimization = exports.isUsingMiddleware = exports.hasUnoptimizedImage = exports.usesNextImage = exports.usesAppDirRouter = exports.getNextjsRewritesToUse = exports.isHeaderSupportedByHosting = exports.isRedirectSupportedByHosting = exports.isRewriteSupportedByHosting = exports.cleanEscapedChars = exports.pathHasRegex = void 0;
  4. const fs_1 = require("fs");
  5. const fs_extra_1 = require("fs-extra");
  6. const path_1 = require("path");
  7. const utils_1 = require("../utils");
  8. const constants_1 = require("./constants");
  9. const fsutils_1 = require("../../fsutils");
  10. function pathHasRegex(path) {
  11. return /(?<!\\)\(/.test(path);
  12. }
  13. exports.pathHasRegex = pathHasRegex;
  14. function cleanEscapedChars(path) {
  15. return path.replace(/\\([(){}:+?*])/g, (a, b) => b);
  16. }
  17. exports.cleanEscapedChars = cleanEscapedChars;
  18. function isRewriteSupportedByHosting(rewrite) {
  19. return !("has" in rewrite || pathHasRegex(rewrite.source) || (0, utils_1.isUrl)(rewrite.destination));
  20. }
  21. exports.isRewriteSupportedByHosting = isRewriteSupportedByHosting;
  22. function isRedirectSupportedByHosting(redirect) {
  23. return !("has" in redirect || pathHasRegex(redirect.source) || "internal" in redirect);
  24. }
  25. exports.isRedirectSupportedByHosting = isRedirectSupportedByHosting;
  26. function isHeaderSupportedByHosting(header) {
  27. return !("has" in header || pathHasRegex(header.source));
  28. }
  29. exports.isHeaderSupportedByHosting = isHeaderSupportedByHosting;
  30. function getNextjsRewritesToUse(nextJsRewrites) {
  31. if (Array.isArray(nextJsRewrites)) {
  32. return nextJsRewrites;
  33. }
  34. if (nextJsRewrites === null || nextJsRewrites === void 0 ? void 0 : nextJsRewrites.beforeFiles) {
  35. return nextJsRewrites.beforeFiles;
  36. }
  37. return [];
  38. }
  39. exports.getNextjsRewritesToUse = getNextjsRewritesToUse;
  40. function usesAppDirRouter(sourceDir) {
  41. const appPathRoutesManifestPath = (0, path_1.join)(sourceDir, constants_1.APP_PATH_ROUTES_MANIFEST);
  42. return (0, fs_1.existsSync)(appPathRoutesManifestPath);
  43. }
  44. exports.usesAppDirRouter = usesAppDirRouter;
  45. async function usesNextImage(sourceDir, distDir) {
  46. const exportMarker = await (0, utils_1.readJSON)((0, path_1.join)(sourceDir, distDir, constants_1.EXPORT_MARKER));
  47. return exportMarker.isNextImageImported;
  48. }
  49. exports.usesNextImage = usesNextImage;
  50. async function hasUnoptimizedImage(sourceDir, distDir) {
  51. const imagesManifest = await (0, utils_1.readJSON)((0, path_1.join)(sourceDir, distDir, constants_1.IMAGES_MANIFEST));
  52. return imagesManifest.images.unoptimized;
  53. }
  54. exports.hasUnoptimizedImage = hasUnoptimizedImage;
  55. async function isUsingMiddleware(dir, isDevMode) {
  56. if (isDevMode) {
  57. const [middlewareJs, middlewareTs] = await Promise.all([
  58. (0, fs_extra_1.pathExists)((0, path_1.join)(dir, "middleware.js")),
  59. (0, fs_extra_1.pathExists)((0, path_1.join)(dir, "middleware.ts")),
  60. ]);
  61. return middlewareJs || middlewareTs;
  62. }
  63. else {
  64. const middlewareManifest = await (0, utils_1.readJSON)((0, path_1.join)(dir, "server", constants_1.MIDDLEWARE_MANIFEST));
  65. return Object.keys(middlewareManifest.middleware).length > 0;
  66. }
  67. }
  68. exports.isUsingMiddleware = isUsingMiddleware;
  69. async function isUsingImageOptimization(dir) {
  70. const { isNextImageImported } = await (0, utils_1.readJSON)((0, path_1.join)(dir, constants_1.EXPORT_MARKER));
  71. if (isNextImageImported) {
  72. const imagesManifest = await (0, utils_1.readJSON)((0, path_1.join)(dir, constants_1.IMAGES_MANIFEST));
  73. const usingImageOptimization = imagesManifest.images.unoptimized === false;
  74. if (usingImageOptimization) {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. exports.isUsingImageOptimization = isUsingImageOptimization;
  81. function isUsingAppDirectory(dir) {
  82. const appPathRoutesManifestPath = (0, path_1.join)(dir, constants_1.APP_PATH_ROUTES_MANIFEST);
  83. return (0, fsutils_1.fileExistsSync)(appPathRoutesManifestPath);
  84. }
  85. exports.isUsingAppDirectory = isUsingAppDirectory;
  86. function allDependencyNames(mod) {
  87. if (!mod.dependencies)
  88. return [];
  89. const dependencyNames = Object.keys(mod.dependencies).reduce((acc, it) => [...acc, it, ...allDependencyNames(mod.dependencies[it])], []);
  90. return [...new Set(dependencyNames)];
  91. }
  92. exports.allDependencyNames = allDependencyNames;