templateWithLoader.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { overwriteLoadLocales } from './utils';
  2. export default function templateWithLoader(rawCode, _a) {
  3. 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;
  4. var tokenToReplace = "__CODE_TOKEN_".concat(Date.now().toString(16), "__");
  5. var modifiedCode = rawCode;
  6. if (hasLoader) {
  7. modifiedCode = modifiedCode
  8. .replace(new RegExp("(const|var|let|async +function|function|import|import {.* as) +".concat(loader, "\\W")), function (v) {
  9. return v.replace(new RegExp("\\W".concat(loader, "\\W")), function (r) {
  10. return r.replace(loader, '_' + loader);
  11. });
  12. })
  13. .replace(new RegExp("export +(const|var|let|async +function|function) +_".concat(loader)), function (v) { return v.replace('export', ''); })
  14. .replace(/export +\{ *(getStaticProps|getServerSideProps)( |,)*\}/, '')
  15. .replace(new RegExp("^ *export {(.|\n)*".concat(loader, "(.|\n)*}"), 'gm'), function (v) {
  16. return v
  17. .replace(new RegExp("(\\w+ +as +)?".concat(loader, "\\W"), 'gm'), function (v) {
  18. return v.endsWith(loader) ? '' : v[v.length - 1];
  19. })
  20. .replace(/,( |\n)*,/gm, ',')
  21. .replace(/{( |\n)*,/gm, '{')
  22. .replace(/{,( \n)*}/gm, '}')
  23. .replace(/^ *export +{( |\n)*}\W*$/gm, '');
  24. })
  25. .replace(/^ *import +{( |\n)*[^}]*/gm, function (v) {
  26. if (v.match(new RegExp("\\W+".concat(loader, " +as "))))
  27. return v;
  28. return v.replace(new RegExp("\\W+".concat(loader, "(\\W|$)")), function (r) {
  29. return r.replace(loader, "".concat(loader, " as _").concat(loader));
  30. });
  31. });
  32. }
  33. 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(overwriteLoadLocales(hasLoadLocaleFrom), "\n }))\n }\n }\n }\n ");
  34. if (typescript)
  35. template = template.replace(/\n/g, '\n// @ts-ignore\n');
  36. return template.replace(tokenToReplace, function () {
  37. return "\n".concat(modifiedCode, "\n");
  38. });
  39. }