exportable-builder.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __generator = (this && this.__generator) || function (thisArg, body) {
  12. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  13. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  14. function verb(n) { return function (v) { return step([n, v]); }; }
  15. function step(op) {
  16. if (f) throw new TypeError("Generator is already executing.");
  17. while (_) try {
  18. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  19. if (y = 0, t) op = [op[0] & 2, t.value];
  20. switch (op[0]) {
  21. case 0: case 1: t = op; break;
  22. case 4: _.label++; return { value: op[1], done: false };
  23. case 5: _.label++; y = op[1]; op = [0]; continue;
  24. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  25. default:
  26. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  27. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  28. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  29. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  30. if (t[2]) _.ops.pop();
  31. _.trys.pop(); continue;
  32. }
  33. op = body.call(thisArg, _);
  34. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  35. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  36. }
  37. };
  38. var __read = (this && this.__read) || function (o, n) {
  39. var m = typeof Symbol === "function" && o[Symbol.iterator];
  40. if (!m) return o;
  41. var i = m.call(o), r, ar = [], e;
  42. try {
  43. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  44. }
  45. catch (error) { e = { error: error }; }
  46. finally {
  47. try {
  48. if (r && !r.done && (m = i["return"])) m.call(i);
  49. }
  50. finally { if (e) throw e.error; }
  51. }
  52. return ar;
  53. };
  54. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  55. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  56. if (ar || !(i in from)) {
  57. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  58. ar[i] = from[i];
  59. }
  60. }
  61. return to.concat(ar || Array.prototype.slice.call(from));
  62. };
  63. var __importDefault = (this && this.__importDefault) || function (mod) {
  64. return (mod && mod.__esModule) ? mod : { "default": mod };
  65. };
  66. exports.__esModule = true;
  67. exports.ExportableBuilder = void 0;
  68. var sitemap_builder_js_1 = require("./sitemap-builder.js");
  69. var node_path_1 = __importDefault(require("node:path"));
  70. var url_js_1 = require("../utils/url.js");
  71. var merge_js_1 = require("../utils/merge.js");
  72. var robots_txt_builder_js_1 = require("./robots-txt-builder.js");
  73. var defaults_js_1 = require("../utils/defaults.js");
  74. var file_js_1 = require("../utils/file.js");
  75. var ExportableBuilder = /** @class */ (function () {
  76. function ExportableBuilder(config, runtimePaths) {
  77. this.exportableList = [];
  78. this.config = config;
  79. this.runtimePaths = runtimePaths;
  80. this.sitemapBuilder = new sitemap_builder_js_1.SitemapBuilder();
  81. this.robotsTxtBuilder = new robots_txt_builder_js_1.RobotsTxtBuilder();
  82. this.exportDir = node_path_1["default"].resolve(process.cwd(), this.config.outDir);
  83. }
  84. /**
  85. * Register sitemap index files
  86. */
  87. ExportableBuilder.prototype.registerIndexSitemap = function () {
  88. var _a, _b, _c;
  89. return __awaiter(this, void 0, void 0, function () {
  90. var sitemaps, content, item;
  91. return __generator(this, function (_d) {
  92. sitemaps = __spreadArray(__spreadArray([], __read(this.generatedSitemaps()), false), __read(((_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.robotsTxtOptions) === null || _b === void 0 ? void 0 : _b.additionalSitemaps) !== null && _c !== void 0 ? _c : [])), false);
  93. content = this.sitemapBuilder.buildSitemapIndexXml(sitemaps);
  94. item = {
  95. type: 'sitemap-index',
  96. filename: this.runtimePaths.SITEMAP_INDEX_FILE,
  97. url: this.runtimePaths.SITEMAP_INDEX_URL,
  98. content: content
  99. };
  100. // Add to exportable list
  101. this.exportableList.push(item);
  102. return [2 /*return*/];
  103. });
  104. });
  105. };
  106. /**
  107. * Resolve filename if index sitemap is generated
  108. * @param index
  109. * @returns
  110. */
  111. ExportableBuilder.prototype.resolveFilenameWithIndexSitemap = function (index) {
  112. return "".concat(this.config.sitemapBaseFileName, "-").concat(index, ".xml");
  113. };
  114. /**
  115. * Resolve filename if index sitemaps is not generated
  116. * @param index
  117. * @returns
  118. */
  119. ExportableBuilder.prototype.resolveFilenameWithoutIndexSitemap = function (index) {
  120. if (index === 0) {
  121. return "".concat(this.config.sitemapBaseFileName, ".xml");
  122. }
  123. return this.resolveFilenameWithIndexSitemap(index);
  124. };
  125. /**
  126. * Register sitemaps with exportable builder
  127. * @param chunks
  128. */
  129. ExportableBuilder.prototype.registerSitemaps = function (chunks) {
  130. return __awaiter(this, void 0, void 0, function () {
  131. var hasIndexSitemap, items;
  132. var _a;
  133. var _this = this;
  134. return __generator(this, function (_b) {
  135. hasIndexSitemap = this.config.generateIndexSitemap;
  136. items = chunks === null || chunks === void 0 ? void 0 : chunks.map(function (chunk, index) {
  137. // Get sitemap base filename
  138. var baseFilename = hasIndexSitemap
  139. ? _this.resolveFilenameWithIndexSitemap(index)
  140. : _this.resolveFilenameWithoutIndexSitemap(index);
  141. return {
  142. type: 'sitemap',
  143. url: (0, url_js_1.generateUrl)(_this.config.siteUrl, baseFilename),
  144. filename: node_path_1["default"].resolve(_this.exportDir, baseFilename),
  145. content: _this.sitemapBuilder.buildSitemapXml(chunk)
  146. };
  147. });
  148. // Add to exportable list
  149. (_a = this.exportableList).push.apply(_a, __spreadArray([], __read(items), false));
  150. return [2 /*return*/];
  151. });
  152. });
  153. };
  154. /**
  155. * Get robots.txt export config
  156. * @returns
  157. */
  158. ExportableBuilder.prototype.robotsTxtExportConfig = function () {
  159. var _a, _b;
  160. // Endpoints list
  161. var endpoints = [];
  162. // Include non-index sitemaps
  163. // Optionally allow user to include non-index sitemaps along with generated sitemap list
  164. // Set to true if index-sitemap is not generated
  165. var includeNonIndexSitemaps = this.config.generateIndexSitemap
  166. ? (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.robotsTxtOptions) === null || _b === void 0 ? void 0 : _b.includeNonIndexSitemaps
  167. : true;
  168. // Add all sitemap indices
  169. if (this.config.generateIndexSitemap) {
  170. endpoints.push.apply(endpoints, __spreadArray([], __read(this.generatedSitemapIndices()), false));
  171. }
  172. // Add all non-index sitemaps
  173. if (includeNonIndexSitemaps) {
  174. endpoints.push.apply(endpoints, __spreadArray([], __read(this.generatedSitemaps()), false));
  175. }
  176. // Combine merge with additional sitemaps
  177. return (0, merge_js_1.combineMerge)({
  178. robotsTxtOptions: {
  179. additionalSitemaps: endpoints
  180. }
  181. }, this.config);
  182. };
  183. /**
  184. * Register robots.txt export
  185. */
  186. ExportableBuilder.prototype.registerRobotsTxt = function () {
  187. var _a, _b;
  188. return __awaiter(this, void 0, void 0, function () {
  189. var baseFilename, robotsConfig, content, robotsTransformer, item;
  190. return __generator(this, function (_c) {
  191. switch (_c.label) {
  192. case 0:
  193. baseFilename = 'robots.txt';
  194. robotsConfig = this.robotsTxtExportConfig();
  195. content = this.robotsTxtBuilder.generateRobotsTxt(robotsConfig);
  196. robotsTransformer = (_b = (_a = robotsConfig === null || robotsConfig === void 0 ? void 0 : robotsConfig.robotsTxtOptions) === null || _a === void 0 ? void 0 : _a.transformRobotsTxt) !== null && _b !== void 0 ? _b : defaults_js_1.defaultRobotsTxtTransformer;
  197. return [4 /*yield*/, robotsTransformer(robotsConfig, content)
  198. // Generate exportable item
  199. ];
  200. case 1:
  201. // Transform generated robots txt
  202. content = _c.sent();
  203. item = {
  204. type: 'robots.txt',
  205. filename: node_path_1["default"].resolve(this.exportDir, baseFilename),
  206. url: (0, url_js_1.generateUrl)(robotsConfig === null || robotsConfig === void 0 ? void 0 : robotsConfig.siteUrl, baseFilename),
  207. content: content
  208. };
  209. // Add to exportableList
  210. this.exportableList.push(item);
  211. return [2 /*return*/];
  212. }
  213. });
  214. });
  215. };
  216. /**
  217. * Generic reducer to extract by type
  218. * @param condition
  219. * @returns
  220. */
  221. ExportableBuilder.prototype.exportableUrlReducer = function (condition) {
  222. return this.exportableList.reduce(function (prev, curr) {
  223. var matches = condition(curr);
  224. if (matches) {
  225. prev.push(curr.url);
  226. }
  227. return prev;
  228. }, []);
  229. };
  230. /**
  231. * Return a lit of sitemap urls
  232. * @returns
  233. */
  234. ExportableBuilder.prototype.generatedSitemaps = function () {
  235. return this.exportableUrlReducer(function (x) { return x.type == 'sitemap'; });
  236. };
  237. /**
  238. * Generate sitemap indices
  239. * @returns
  240. */
  241. ExportableBuilder.prototype.generatedSitemapIndices = function () {
  242. return this.exportableUrlReducer(function (x) { return x.type == 'sitemap-index'; });
  243. };
  244. /**
  245. * Export all registered files
  246. * @returns
  247. */
  248. ExportableBuilder.prototype.exportAll = function () {
  249. var _a;
  250. return __awaiter(this, void 0, void 0, function () {
  251. var _this = this;
  252. return __generator(this, function (_b) {
  253. switch (_b.label) {
  254. case 0: return [4 /*yield*/, Promise.all((_a = this.exportableList) === null || _a === void 0 ? void 0 : _a.map(function (item) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
  255. return [2 /*return*/, (0, file_js_1.exportFile)(item.filename, item.content)];
  256. }); }); }))
  257. // Create result object
  258. ];
  259. case 1:
  260. _b.sent();
  261. // Create result object
  262. return [2 /*return*/, {
  263. runtimePaths: this.runtimePaths,
  264. sitemaps: this.generatedSitemaps(),
  265. sitemapIndices: this.generatedSitemapIndices()
  266. }];
  267. }
  268. });
  269. });
  270. };
  271. return ExportableBuilder;
  272. }());
  273. exports.ExportableBuilder = ExportableBuilder;