templateWithHoc.js 1.6 KB

1234567891011121314151617
  1. import { clearCommentsRgx, overwriteLoadLocales } from './utils';
  2. export default function templateWithHoc(code, _a) {
  3. var _b = _a === void 0 ? {} : _a, _c = _b.skipInitialProps, skipInitialProps = _c === void 0 ? false : _c, _d = _b.typescript, typescript = _d === void 0 ? false : _d, _e = _b.pageName, pageName = _e === void 0 ? '__Page_Next_Translate__' : _e, _f = _b.hasLoadLocaleFrom, hasLoadLocaleFrom = _f === void 0 ? false : _f;
  4. var tokenToReplace = "__CODE_TOKEN_".concat(Date.now().toString(16), "__");
  5. var codeWithoutComments = code.replace(clearCommentsRgx, '');
  6. var modifiedCode = code.replace(/export +default/g, "const ".concat(pageName, " ="));
  7. var _g = codeWithoutComments.match(/export +default +(function|class) +([A-Z]\w*)/) || [], componentName = _g[2];
  8. if (componentName) {
  9. modifiedCode = modifiedCode.replace(new RegExp("\\W".concat(componentName, "\\.getInitialProps"), 'g'), "".concat(pageName, ".getInitialProps"));
  10. }
  11. var template = "\n import __i18nConfig from '@next-translate-root/i18n'\n import __appWithI18n from 'next-translate/appWithI18n'\n ".concat(tokenToReplace, "\n export default __appWithI18n(__Page_Next_Translate__, {\n ...__i18nConfig,\n isLoader: true,\n skipInitialProps: ").concat(skipInitialProps, ",\n ").concat(overwriteLoadLocales(hasLoadLocaleFrom), "\n });\n ");
  12. if (typescript)
  13. template = template.replace(/\n/g, '\n// @ts-ignore\n');
  14. return template.replace(tokenToReplace, function () {
  15. return "\n".concat(modifiedCode, "\n");
  16. });
  17. }