require.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPagePath = getPagePath;
  6. exports.requirePage = requirePage;
  7. exports.requireFontManifest = requireFontManifest;
  8. var _fs = require("fs");
  9. var _path = require("path");
  10. var _constants = require("../shared/lib/constants");
  11. var _normalizeLocalePath = require("../shared/lib/i18n/normalize-locale-path");
  12. var _normalizePagePath = require("../shared/lib/page-path/normalize-page-path");
  13. var _denormalizePagePath = require("../shared/lib/page-path/denormalize-page-path");
  14. var _utils = require("../shared/lib/utils");
  15. function getPagePath(page, distDir, serverless, dev, locales, appDirEnabled) {
  16. const serverBuildPath = (0, _path).join(distDir, serverless && !dev ? _constants.SERVERLESS_DIRECTORY : _constants.SERVER_DIRECTORY);
  17. let appPathsManifest;
  18. if (appDirEnabled) {
  19. appPathsManifest = require((0, _path).join(serverBuildPath, _constants.APP_PATHS_MANIFEST));
  20. }
  21. const pagesManifest = require((0, _path).join(serverBuildPath, _constants.PAGES_MANIFEST));
  22. try {
  23. page = (0, _denormalizePagePath).denormalizePagePath((0, _normalizePagePath).normalizePagePath(page));
  24. } catch (err) {
  25. console.error(err);
  26. throw new _utils.PageNotFoundError(page);
  27. }
  28. const checkManifest = (manifest)=>{
  29. let curPath = manifest[page];
  30. if (!manifest[curPath] && locales) {
  31. const manifestNoLocales = {};
  32. for (const key of Object.keys(manifest)){
  33. manifestNoLocales[(0, _normalizeLocalePath).normalizeLocalePath(key, locales).pathname] = pagesManifest[key];
  34. }
  35. curPath = manifestNoLocales[page];
  36. }
  37. return curPath;
  38. };
  39. let pagePath;
  40. if (appPathsManifest) {
  41. pagePath = checkManifest(appPathsManifest);
  42. }
  43. if (!pagePath) {
  44. pagePath = checkManifest(pagesManifest);
  45. }
  46. if (!pagePath) {
  47. throw new _utils.PageNotFoundError(page);
  48. }
  49. return (0, _path).join(serverBuildPath, pagePath);
  50. }
  51. function requirePage(page, distDir, serverless, appDirEnabled) {
  52. const pagePath = getPagePath(page, distDir, serverless, false, undefined, appDirEnabled);
  53. if (pagePath.endsWith(".html")) {
  54. return _fs.promises.readFile(pagePath, "utf8").catch((err)=>{
  55. throw new _utils.MissingStaticPage(page, err.message);
  56. });
  57. }
  58. return require(pagePath);
  59. }
  60. function requireFontManifest(distDir, serverless) {
  61. const serverBuildPath = (0, _path).join(distDir, serverless ? _constants.SERVERLESS_DIRECTORY : _constants.SERVER_DIRECTORY);
  62. const fontManifest = require((0, _path).join(serverBuildPath, _constants.FONT_MANIFEST));
  63. return fontManifest;
  64. }
  65. //# sourceMappingURL=require.js.map