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.

index.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ɵcodegenFunctionsDirectory = exports.ɵcodegenPublicDirectory = exports.build = exports.discover = exports.type = exports.support = exports.name = void 0;
  4. const child_process_1 = require("child_process");
  5. const fs_extra_1 = require("fs-extra");
  6. const promises_1 = require("fs/promises");
  7. const path_1 = require("path");
  8. const { dynamicImport } = require(true && "../../dynamicImport");
  9. exports.name = "Express.js";
  10. exports.support = "experimental";
  11. exports.type = 0;
  12. async function getConfig(root) {
  13. var _a, _b;
  14. const packageJsonBuffer = await (0, promises_1.readFile)((0, path_1.join)(root, "package.json"));
  15. const packageJson = JSON.parse(packageJsonBuffer.toString());
  16. const serve = (_a = packageJson.directories) === null || _a === void 0 ? void 0 : _a.serve;
  17. const serveDir = serve && (0, path_1.join)(root, (_b = packageJson.directories) === null || _b === void 0 ? void 0 : _b.serve);
  18. return { serveDir, packageJson };
  19. }
  20. async function discover(dir) {
  21. if (!(await (0, fs_extra_1.pathExists)((0, path_1.join)(dir, "package.json"))))
  22. return;
  23. const { serveDir } = await getConfig(dir);
  24. if (!serveDir)
  25. return;
  26. return { mayWantBackend: true };
  27. }
  28. exports.discover = discover;
  29. async function build(cwd) {
  30. (0, child_process_1.execSync)(`npm run build`, { stdio: "inherit", cwd });
  31. const wantsBackend = !!(await getBootstrapScript(cwd));
  32. return { wantsBackend };
  33. }
  34. exports.build = build;
  35. async function ɵcodegenPublicDirectory(root, dest) {
  36. const { serveDir } = await getConfig(root);
  37. await (0, fs_extra_1.copy)(serveDir, dest);
  38. }
  39. exports.ɵcodegenPublicDirectory = ɵcodegenPublicDirectory;
  40. async function getBootstrapScript(root, _bootstrapScript = "", _entry) {
  41. let entry = _entry;
  42. let bootstrapScript = _bootstrapScript;
  43. const allowRecursion = !entry;
  44. if (!entry) {
  45. const { packageJson: { name }, } = await getConfig(root);
  46. try {
  47. entry = require(root);
  48. bootstrapScript = `const bootstrap = Promise.resolve(require('${name}'))`;
  49. }
  50. catch (e) {
  51. entry = await dynamicImport(root).catch(() => undefined);
  52. bootstrapScript = `const bootstrap = import('${name}')`;
  53. }
  54. }
  55. if (!entry)
  56. return undefined;
  57. const { default: defaultExport, app, handle } = entry;
  58. if (typeof handle === "function") {
  59. return (bootstrapScript +
  60. ";\nexports.handle = async (req, res) => (await bootstrap).handle(req, res);");
  61. }
  62. if (typeof app === "function") {
  63. try {
  64. const express = app();
  65. if (typeof express.render === "function") {
  66. return (bootstrapScript +
  67. ";\nexports.handle = async (req, res) => (await bootstrap).app(req, res);");
  68. }
  69. }
  70. catch (e) {
  71. }
  72. }
  73. if (!allowRecursion)
  74. return undefined;
  75. if (typeof defaultExport === "object") {
  76. bootstrapScript += ".then(({ default }) => default)";
  77. if (typeof defaultExport.then === "function") {
  78. const awaitedDefaultExport = await defaultExport;
  79. return getBootstrapScript(root, bootstrapScript, awaitedDefaultExport);
  80. }
  81. else {
  82. return getBootstrapScript(root, bootstrapScript, defaultExport);
  83. }
  84. }
  85. return undefined;
  86. }
  87. async function ɵcodegenFunctionsDirectory(root, dest) {
  88. const bootstrapScript = await getBootstrapScript(root);
  89. if (!bootstrapScript)
  90. return;
  91. await (0, promises_1.mkdir)(dest, { recursive: true });
  92. const { packageJson } = await getConfig(root);
  93. const packResults = (0, child_process_1.execSync)(`npm pack ${root} --json`, { cwd: dest });
  94. const npmPackResults = JSON.parse(packResults.toString());
  95. const matchingPackResult = npmPackResults.find((it) => it.name === packageJson.name);
  96. const { filename } = matchingPackResult;
  97. packageJson.dependencies || (packageJson.dependencies = {});
  98. packageJson.dependencies[packageJson.name] = `file:${filename}`;
  99. return { bootstrapScript, packageJson };
  100. }
  101. exports.ɵcodegenFunctionsDirectory = ɵcodegenFunctionsDirectory;