templateWithLoader.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var utils_1 = require("./utils");
  4. function templateWithLoader(rawCode, _a) {
  5. var _b = _a === void 0 ? {} : _a, _c = _b.page, page = _c === void 0 ? '' : _c, _d = _b.typescript, typescript = _d === void 0 ? false : _d, _e = _b.loader, loader = _e === void 0 ? 'getStaticProps' : _e, _f = _b.hasLoader, hasLoader = _f === void 0 ? false : _f, _g = _b.hasLoadLocaleFrom, hasLoadLocaleFrom = _g === void 0 ? false : _g, _h = _b.revalidate, revalidate = _h === void 0 ? 0 : _h;
  6. var tokenToReplace = "__CODE_TOKEN_".concat(Date.now().toString(16), "__");
  7. var modifiedCode = rawCode;
  8. if (hasLoader) {
  9. modifiedCode = modifiedCode
  10. .replace(new RegExp("(const|var|let|async +function|function|import|import {.* as) +".concat(loader, "\\W")), function (v) {
  11. return v.replace(new RegExp("\\W".concat(loader, "\\W")), function (r) {
  12. return r.replace(loader, '_' + loader);
  13. });
  14. })
  15. .replace(new RegExp("export +(const|var|let|async +function|function) +_".concat(loader)), function (v) { return v.replace('export', ''); })
  16. .replace(/export +\{ *(getStaticProps|getServerSideProps)( |,)*\}/, '')
  17. .replace(new RegExp("^ *export {(.|\n)*".concat(loader, "(.|\n)*}"), 'gm'), function (v) {
  18. return v
  19. .replace(new RegExp("(\\w+ +as +)?".concat(loader, "\\W"), 'gm'), function (v) {
  20. return v.endsWith(loader) ? '' : v[v.length - 1];
  21. })
  22. .replace(/,( |\n)*,/gm, ',')
  23. .replace(/{( |\n)*,/gm, '{')
  24. .replace(/{,( \n)*}/gm, '}')
  25. .replace(/^ *export +{( |\n)*}\W*$/gm, '');
  26. })
  27. .replace(/^ *import +{( |\n)*[^}]*/gm, function (v) {
  28. if (v.match(new RegExp("\\W+".concat(loader, " +as "))))
  29. return v;
  30. return v.replace(new RegExp("\\W+".concat(loader, "(\\W|$)")), function (r) {
  31. return r.replace(loader, "".concat(loader, " as _").concat(loader));
  32. });
  33. });
  34. }
  35. var template = "\n import __i18nConfig from '@next-translate-root/i18n'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n ".concat(tokenToReplace, "\n export async function ").concat(loader, "(ctx) {\n ").concat(hasLoader ? "let res = _".concat(loader, "(ctx)") : '', "\n ").concat(hasLoader ? "if(typeof res.then === 'function') res = await res" : '', "\n return {\n ").concat(hasLoader && revalidate > 0 ? "revalidate: ".concat(revalidate, ",") : '', "\n ").concat(hasLoader ? '...res,' : '', "\n props: {\n ").concat(hasLoader ? '...(res.props || {}),' : '', "\n ...(await __loadNamespaces({\n ...ctx,\n pathname: '").concat(page, "',\n loaderName: '").concat(loader, "',\n ...__i18nConfig,\n ").concat((0, utils_1.overwriteLoadLocales)(hasLoadLocaleFrom), "\n }))\n }\n }\n }\n ");
  36. if (typescript)
  37. template = template.replace(/\n/g, '\n// @ts-ignore\n');
  38. return template.replace(tokenToReplace, function () {
  39. return "\n".concat(modifiedCode, "\n");
  40. });
  41. }
  42. exports.default = templateWithLoader;