utils.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.hasHOC = exports.isPageToIgnore = exports.hasExportName = exports.overwriteLoadLocales = exports.getDefaultAppJs = exports.defaultLoader = exports.clearCommentsRgx = void 0;
  4. var specFileOrFolderRgx = /(__mocks__|__tests__)|(\.(spec|test)\.(tsx|ts|js|jsx)$)/;
  5. exports.clearCommentsRgx = /\/\*[\s\S]*?\*\/|\/\/.*/g;
  6. exports.defaultLoader = '(l, n) => import(`@next-translate-root/locales/${l}/${n}`).then(m => m.default)';
  7. function getDefaultAppJs(hasLoadLocaleFrom) {
  8. return "\n import i18nConfig from '@next-translate-root/i18n'\n import appWithI18n from 'next-translate/appWithI18n'\n\n function MyApp({ Component, pageProps }) {\n return <Component {...pageProps} />\n }\n\n export default appWithI18n(MyApp, {\n ...i18nConfig,\n skipInitialProps: true,\n isLoader: true,\n ".concat(overwriteLoadLocales(hasLoadLocaleFrom), "\n })\n");
  9. }
  10. exports.getDefaultAppJs = getDefaultAppJs;
  11. function overwriteLoadLocales(exist) {
  12. if (exist)
  13. return '';
  14. return "loadLocaleFrom: ".concat(exports.defaultLoader, ",");
  15. }
  16. exports.overwriteLoadLocales = overwriteLoadLocales;
  17. function hasExportName(data, name) {
  18. return Boolean(data.match(new RegExp("export +(const|var|let|async +function|function) +".concat(name))) ||
  19. data.match(new RegExp("export\\s*\\{[^}]*(?<!\\w)".concat(name, "(?!\\w)[^}]*\\}"), 'm')));
  20. }
  21. exports.hasExportName = hasExportName;
  22. function isPageToIgnore(pageFilePath) {
  23. var fileName = pageFilePath.substring(pageFilePath.lastIndexOf('/') + 1);
  24. return (pageFilePath.startsWith('/api/') ||
  25. pageFilePath.startsWith('/_document.') ||
  26. pageFilePath.startsWith('/middleware.') ||
  27. fileName.startsWith('_middleware.') ||
  28. specFileOrFolderRgx.test(pageFilePath));
  29. }
  30. exports.isPageToIgnore = isPageToIgnore;
  31. function hasHOC(rawData) {
  32. var hasWithTranslationHOC = new RegExp('import *(\\w*) *.*from *.*next-translate\\/withTranslation.*');
  33. if (!rawData.includes('export default'))
  34. return false;
  35. if (hasExportName(rawData, 'getStaticProps') ||
  36. hasExportName(rawData, 'getServerSideProps') ||
  37. hasExportName(rawData, 'getStaticPaths')) {
  38. return false;
  39. }
  40. var _a = rawData.match(hasWithTranslationHOC) || [], withTranslationName = _a[1];
  41. var data = rawData
  42. .replace(new RegExp("".concat(withTranslationName, "\\(.*\\)")), function (d) {
  43. return d.replace(new RegExp("(".concat(withTranslationName, "|\\(|\\))"), 'g'), '');
  44. })
  45. .replace(exports.clearCommentsRgx, '');
  46. var exportedNormally = new RegExp("export default (\\(.*\\) *=>|function|class)").test(data);
  47. if (exportedNormally)
  48. return false;
  49. var ref = getRef(data);
  50. if (ref.includes('('))
  51. return true;
  52. return (data.split('\n').filter(function (line) {
  53. var isRefLine = line.includes(ref) && !/export +default/.test(line);
  54. var isComp = new RegExp("(function|class) +".concat(ref, "\\W")).test(line);
  55. var isCompInVar = new RegExp(" *".concat(ref, " += +(function|class) +")).test(line);
  56. var isArrowFunc = new RegExp(" *".concat(ref, "(: *\\w+ *)? += +\\(.*=>")).test(line);
  57. var isPotentialHOC = /=.*\(/.test(line);
  58. return (isRefLine && !isComp && !isCompInVar && !isArrowFunc && isPotentialHOC);
  59. }).length > 0);
  60. }
  61. exports.hasHOC = hasHOC;
  62. function getRef(data) {
  63. var escapeRegex = function (str) {
  64. return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  65. };
  66. var ref = (data.replace(/ /g, '').match("exportdefault*([^\\n|;]*)") ||
  67. [])[1];
  68. var prevRef = (data.match(new RegExp("".concat(escapeRegex(ref), " += +(\\w+)($| |;|\\n)"))) || [])[1];
  69. return prevRef || ref;
  70. }