templateWithHoc.js 1.6 KB

1234567891011121314151617181920
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var utils_1 = require("./utils");
  4. function templateWithHoc(code, _a) {
  5. 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;
  6. var tokenToReplace = "__CODE_TOKEN_".concat(Date.now().toString(16), "__");
  7. var codeWithoutComments = code.replace(utils_1.clearCommentsRgx, '');
  8. var modifiedCode = code.replace(/export +default/g, "const ".concat(pageName, " ="));
  9. var _g = codeWithoutComments.match(/export +default +(function|class) +([A-Z]\w*)/) || [], componentName = _g[2];
  10. if (componentName) {
  11. modifiedCode = modifiedCode.replace(new RegExp("\\W".concat(componentName, "\\.getInitialProps"), 'g'), "".concat(pageName, ".getInitialProps"));
  12. }
  13. 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((0, utils_1.overwriteLoadLocales)(hasLoadLocaleFrom), "\n });\n ");
  14. if (typescript)
  15. template = template.replace(/\n/g, '\n// @ts-ignore\n');
  16. return template.replace(tokenToReplace, function () {
  17. return "\n".concat(modifiedCode, "\n");
  18. });
  19. }
  20. exports.default = templateWithHoc;