build.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.build = void 0;
  7. const fs_extra_1 = __importDefault(require("fs-extra"));
  8. const bundle_1 = require("./bundle");
  9. const config_1 = require("../config");
  10. const render_1 = require("./render");
  11. async function build(buildOptions = {}) {
  12. process.env.NODE_ENV = 'production';
  13. const siteConfig = await config_1.resolveConfig(buildOptions.root);
  14. try {
  15. const [clientResult, , pageToHashMap] = await bundle_1.bundle(siteConfig, buildOptions);
  16. console.log('rendering pages...');
  17. const appChunk = clientResult.assets.find((chunk) => chunk.type === 'chunk' && chunk.fileName.match(/^app\.\w+\.js$/));
  18. const cssChunk = clientResult.assets.find((chunk) => chunk.type === 'asset' && chunk.fileName.endsWith('.css'));
  19. // We embed the hash map string into each page directly so that it doesn't
  20. // alter the main chunk's hash on every build. It's also embedded as a
  21. // string and JSON.parsed from the client because it's faster than embedding
  22. // as JS object literal.
  23. const hashMapStirng = JSON.stringify(JSON.stringify(pageToHashMap));
  24. for (const page of siteConfig.pages) {
  25. await render_1.renderPage(siteConfig, page, clientResult, appChunk, cssChunk, pageToHashMap, hashMapStirng);
  26. }
  27. }
  28. finally {
  29. await fs_extra_1.default.remove(siteConfig.tempDir);
  30. }
  31. console.log('done.');
  32. }
  33. exports.build = build;
  34. //# sourceMappingURL=build.js.map