next-translate-plugin 2.4.0 → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/templateAppDir.js +9 -48
- package/lib/cjs/utils.js +4 -36
- package/lib/esm/templateAppDir.js +10 -49
- package/lib/esm/utils.js +3 -34
- package/package.json +4 -4
- package/utils.d.ts +0 -6
|
@@ -10,35 +10,28 @@ var validPages = [
|
|
|
10
10
|
'/global-error',
|
|
11
11
|
];
|
|
12
12
|
var validPagesRegex = new RegExp("(".concat(validPages.join('|'), ")$"));
|
|
13
|
-
var lastPathname = '';
|
|
14
13
|
function templateAppDir(pagePkg, _a) {
|
|
15
14
|
var _b = _a === void 0 ? {} : _a, _c = _b.code, code = _c === void 0 ? '' : _c, _d = _b.pageNoExt, pageNoExt = _d === void 0 ? '/' : _d, _e = _b.normalizedResourcePath, normalizedResourcePath = _e === void 0 ? '' : _e, _f = _b.appFolder, appFolder = _f === void 0 ? '' : _f, _g = _b.isClientComponent, isClientComponent = _g === void 0 ? false : _g, _h = _b.existLocalesFolder, existLocalesFolder = _h === void 0 ? true : _h;
|
|
16
15
|
var routeType = validPages.find(function (pageName) { return pageNoExt.endsWith(pageName); }) || 'component';
|
|
17
16
|
var isPage = routeType !== 'component' && normalizedResourcePath.includes(appFolder);
|
|
18
|
-
if (!isPage
|
|
17
|
+
if (!isPage)
|
|
19
18
|
return code;
|
|
20
19
|
var hash = Date.now().toString(16);
|
|
21
|
-
var pathname =
|
|
22
|
-
? pageNoExt.replace(validPagesRegex, '/')
|
|
23
|
-
: lastPathname;
|
|
24
|
-
lastPathname = pathname;
|
|
20
|
+
var pathname = pageNoExt.replace(validPagesRegex, '/');
|
|
25
21
|
var pageVariableName = (0, utils_1.interceptExport)(pagePkg, 'default', "__Next_Translate__Page__".concat(hash, "__"));
|
|
26
22
|
if (isPage && !pageVariableName)
|
|
27
23
|
return code;
|
|
28
|
-
if (isClientComponent && !code.includes('next-translate'))
|
|
29
|
-
return code;
|
|
30
24
|
if (isClientComponent) {
|
|
31
|
-
return
|
|
25
|
+
return templateRCCPage({
|
|
32
26
|
pagePkg: pagePkg,
|
|
33
27
|
hash: hash,
|
|
34
28
|
pageVariableName: pageVariableName,
|
|
35
29
|
pathname: pathname,
|
|
36
|
-
isPage: isPage,
|
|
37
30
|
routeType: routeType,
|
|
38
31
|
existLocalesFolder: existLocalesFolder,
|
|
39
32
|
});
|
|
40
33
|
}
|
|
41
|
-
return
|
|
34
|
+
return templateRSCPage({
|
|
42
35
|
pagePkg: pagePkg,
|
|
43
36
|
hash: hash,
|
|
44
37
|
pageVariableName: pageVariableName,
|
|
@@ -48,49 +41,17 @@ function templateAppDir(pagePkg, _a) {
|
|
|
48
41
|
});
|
|
49
42
|
}
|
|
50
43
|
exports.default = templateAppDir;
|
|
51
|
-
function
|
|
44
|
+
function templateRSCPage(_a) {
|
|
52
45
|
var pagePkg = _a.pagePkg, hash = _a.hash, pageVariableName = _a.pageVariableName, pathname = _a.pathname, routeType = _a.routeType, existLocalesFolder = _a.existLocalesFolder;
|
|
53
46
|
var code = pagePkg.getCode();
|
|
54
|
-
return "\n import ".concat(utils_1.INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import
|
|
47
|
+
return "\n import ".concat(utils_1.INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import AppDirI18nProvider from 'next-translate/AppDirI18nProvider'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n\n ").concat(code, "\n\n export default async function __Next_Translate_new__").concat(hash, "__(props) {\n const config = { \n ...").concat(utils_1.INTERNAL_CONFIG_KEY, ",\n locale: props.params?.lang ?? props.searchParams?.lang ?? ").concat(utils_1.INTERNAL_CONFIG_KEY, ".defaultLocale,\n loaderName: 'server ").concat(routeType, "',\n pathname: '").concat(pathname, "'\n }\n\n const { __lang, __namespaces } = await __loadNamespaces({ ...config, ").concat((0, utils_1.addLoadLocalesFrom)(existLocalesFolder), " });\n globalThis.__NEXT_TRANSLATE__ = { lang: __lang, namespaces: __namespaces, config }\n\n return <AppDirI18nProvider lang={__lang} namespaces={__namespaces} config={config}><").concat(pageVariableName, " {...props} /></AppDirI18nProvider>\n }\n");
|
|
55
48
|
}
|
|
56
|
-
function
|
|
57
|
-
var pagePkg = _a.pagePkg, hash = _a.hash, pageVariableName = _a.pageVariableName, pathname = _a.pathname,
|
|
49
|
+
function templateRCCPage(_a) {
|
|
50
|
+
var pagePkg = _a.pagePkg, hash = _a.hash, pageVariableName = _a.pageVariableName, pathname = _a.pathname, routeType = _a.routeType, existLocalesFolder = _a.existLocalesFolder;
|
|
58
51
|
var topLine = utils_1.clientLine[0];
|
|
59
|
-
var namedExportsModified = modifyNamedExportsComponents(pagePkg, { hash: hash, existLocalesFolder: existLocalesFolder });
|
|
60
|
-
var exportDefault = pageVariableName
|
|
61
|
-
? 'export default ' +
|
|
62
|
-
wrapClientComponent({
|
|
63
|
-
name: "__Next_Translate_new__".concat(hash, "__"),
|
|
64
|
-
pathname: pathname,
|
|
65
|
-
isPage: isPage,
|
|
66
|
-
pageVariableName: pageVariableName,
|
|
67
|
-
routeType: routeType,
|
|
68
|
-
existLocalesFolder: existLocalesFolder,
|
|
69
|
-
})
|
|
70
|
-
: '';
|
|
71
52
|
var clientCode = pagePkg.getCode();
|
|
72
53
|
utils_1.clientLine.forEach(function (line) {
|
|
73
54
|
clientCode = clientCode.replace(line, '');
|
|
74
55
|
});
|
|
75
|
-
return "".concat(topLine, "\n import ").concat(utils_1.INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import
|
|
76
|
-
}
|
|
77
|
-
function wrapClientComponent(_a) {
|
|
78
|
-
var _b = _a.name, name = _b === void 0 ? '' : _b, _c = _a.pathname, pathname = _c === void 0 ? '' : _c, _d = _a.isPage, isPage = _d === void 0 ? false : _d, _e = _a.pageVariableName, pageVariableName = _e === void 0 ? '' : _e, _f = _a.routeType, routeType = _f === void 0 ? 'component' : _f, _g = _a.existLocalesFolder, existLocalesFolder = _g === void 0 ? true : _g;
|
|
79
|
-
return "function ".concat(name, "(props) {\n const searchParams = __useSearchParams()\n const params = __useParams()\n const lang = params.lang ?? searchParams.get('lang') ?? ").concat(utils_1.INTERNAL_CONFIG_KEY, ".defaultLocale\n const config = {\n ...").concat(utils_1.INTERNAL_CONFIG_KEY, ",\n locale: lang,\n loaderName: 'client ").concat(routeType, "',\n pathname: '").concat(pathname, "',\n logBuild: ").concat(isPage, ",\n ").concat((0, utils_1.addLoadLocalesFrom)(existLocalesFolder), "\n }\n const { __lang, __namespaces } = __use(__loadNamespaces(config));\n\n __nt_store.set({ lang, namespaces: __namespaces, config })\n\n return <").concat(pageVariableName, " {...props} />\n }");
|
|
80
|
-
}
|
|
81
|
-
function modifyNamedExportsComponents(pagePkg, _a) {
|
|
82
|
-
var hash = _a.hash, existLocalesFolder = _a.existLocalesFolder;
|
|
83
|
-
return (0, utils_1.interceptNamedExportsFromReactComponents)(pagePkg, hash)
|
|
84
|
-
.map(function (_a) {
|
|
85
|
-
var exportName = _a.exportName, defaultLocalName = _a.defaultLocalName;
|
|
86
|
-
return "\n ".concat(wrapClientComponent({
|
|
87
|
-
name: defaultLocalName,
|
|
88
|
-
pathname: lastPathname,
|
|
89
|
-
isPage: false,
|
|
90
|
-
pageVariableName: exportName,
|
|
91
|
-
existLocalesFolder: existLocalesFolder
|
|
92
|
-
}), "\n export { ").concat(defaultLocalName, " as ").concat(exportName, " }\n ");
|
|
93
|
-
})
|
|
94
|
-
.join('')
|
|
95
|
-
.trim();
|
|
56
|
+
return "".concat(topLine, "\n import ").concat(utils_1.INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import AppDirI18nProvider from 'next-translate/AppDirI18nProvider'\n import { useSearchParams as __useSearchParams, useParams as __useParams } from 'next/navigation'\n import { use as __use } from 'react'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n\n ").concat(clientCode, "\n\n export default function __Next_Translate_new__").concat(hash, "__(props) {\n const searchParams = __useSearchParams()\n const params = __useParams()\n const lang = params.lang ?? searchParams.get('lang') ?? ").concat(utils_1.INTERNAL_CONFIG_KEY, ".defaultLocale\n const config = {\n ...").concat(utils_1.INTERNAL_CONFIG_KEY, ",\n locale: lang,\n loaderName: 'client ").concat(routeType, "',\n pathname: '").concat(pathname, "',\n }\n\n const { __lang, __namespaces } = __use(__loadNamespaces({ ...config, ").concat((0, utils_1.addLoadLocalesFrom)(existLocalesFolder), " }));\n globalThis.__NEXT_TRANSLATE__ = { lang: __lang, namespaces: __namespaces, config }\n\n return <").concat(pageVariableName, " {...props} />\n }\n");
|
|
96
57
|
}
|
package/lib/cjs/utils.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
23
23
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.existLocalesFolderWithNamespaces = exports.existPages = exports.calculatePageDir = exports.removeCommentsFromCode = exports.interceptExport = exports.
|
|
26
|
+
exports.existLocalesFolderWithNamespaces = exports.existPages = exports.calculatePageDir = exports.removeCommentsFromCode = exports.interceptExport = exports.isNotExportModifier = exports.hasHOC = exports.isPageToIgnore = exports.hasStaticName = exports.getStaticName = exports.hasExportName = exports.getDefaultExport = exports.getNamedExport = exports.resolveIdentifier = exports.resolveParenthesis = exports.getImportedNames = exports.getSymbol = exports.parseCode = exports.parseFile = exports.getFilePkg = exports.getTsCompilerOptions = exports.addLoadLocalesFrom = exports.getDefaultAppJs = exports.defaultLoader = exports.clientLine = exports.INTERNAL_CONFIG_KEY = void 0;
|
|
27
27
|
var typescript_1 = __importDefault(require("typescript"));
|
|
28
28
|
var path_1 = __importDefault(require("path"));
|
|
29
29
|
var fs_1 = __importDefault(require("fs"));
|
|
@@ -36,7 +36,9 @@ function getDefaultAppJs(existLocalesFolder) {
|
|
|
36
36
|
}
|
|
37
37
|
exports.getDefaultAppJs = getDefaultAppJs;
|
|
38
38
|
function addLoadLocalesFrom(existLocalesFolder) {
|
|
39
|
-
var defaultFn = existLocalesFolder
|
|
39
|
+
var defaultFn = existLocalesFolder
|
|
40
|
+
? exports.defaultLoader
|
|
41
|
+
: "() => Promise.resolve({})";
|
|
40
42
|
return "loadLocaleFrom: ".concat(exports.INTERNAL_CONFIG_KEY, ".loadLocaleFrom || (").concat(defaultFn, "),");
|
|
41
43
|
}
|
|
42
44
|
exports.addLoadLocalesFrom = addLoadLocalesFrom;
|
|
@@ -291,40 +293,6 @@ function isNotExportModifier(modifier) {
|
|
|
291
293
|
return !exportModifiers.includes(modifier.kind);
|
|
292
294
|
}
|
|
293
295
|
exports.isNotExportModifier = isNotExportModifier;
|
|
294
|
-
function isPascalCase(str) {
|
|
295
|
-
return (str.length > 1 &&
|
|
296
|
-
str[0] === str[0].toUpperCase() &&
|
|
297
|
-
str[1] === str[1].toLowerCase());
|
|
298
|
-
}
|
|
299
|
-
function interceptNamedExportsFromReactComponents(filePkg, hash) {
|
|
300
|
-
var sourceFile = filePkg.sourceFile;
|
|
301
|
-
var interceptedExports = [];
|
|
302
|
-
sourceFile.statements
|
|
303
|
-
.filter(function (node) {
|
|
304
|
-
var _a;
|
|
305
|
-
return (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.some(function (modifier) { return (modifier === null || modifier === void 0 ? void 0 : modifier.kind) === typescript_1.default.SyntaxKind.ExportKeyword; });
|
|
306
|
-
})
|
|
307
|
-
.forEach(function (node) {
|
|
308
|
-
var _a, _b, _c;
|
|
309
|
-
var exportName = '';
|
|
310
|
-
if (typescript_1.default.isFunctionDeclaration(node) || typescript_1.default.isClassDeclaration(node)) {
|
|
311
|
-
exportName = (_b = (_a = node.name) === null || _a === void 0 ? void 0 : _a.getText()) !== null && _b !== void 0 ? _b : '';
|
|
312
|
-
}
|
|
313
|
-
else if (typescript_1.default.isVariableStatement(node)) {
|
|
314
|
-
var declaration = node.declarationList.declarations[0];
|
|
315
|
-
if (!typescript_1.default.isArrowFunction(declaration.initializer))
|
|
316
|
-
return;
|
|
317
|
-
exportName = (_c = declaration.name) === null || _c === void 0 ? void 0 : _c.getText();
|
|
318
|
-
}
|
|
319
|
-
if (isPascalCase(exportName)) {
|
|
320
|
-
var defaultLocalName = "__Next_Translate_Component_".concat(hash, "_").concat(interceptedExports.length);
|
|
321
|
-
interceptExport(filePkg, exportName, defaultLocalName);
|
|
322
|
-
interceptedExports.push({ exportName: exportName, defaultLocalName: defaultLocalName });
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
return interceptedExports;
|
|
326
|
-
}
|
|
327
|
-
exports.interceptNamedExportsFromReactComponents = interceptNamedExportsFromReactComponents;
|
|
328
296
|
function interceptExport(filePkg, exportName, defaultLocalName) {
|
|
329
297
|
var exportContent = getNamedExport(filePkg, exportName, false);
|
|
330
298
|
var finalLocalName = '';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { interceptExport, addLoadLocalesFrom, clientLine,
|
|
1
|
+
import { interceptExport, addLoadLocalesFrom, clientLine, INTERNAL_CONFIG_KEY, } from './utils';
|
|
2
2
|
var validPages = [
|
|
3
3
|
'/page',
|
|
4
4
|
'/layout',
|
|
@@ -8,35 +8,28 @@ var validPages = [
|
|
|
8
8
|
'/global-error',
|
|
9
9
|
];
|
|
10
10
|
var validPagesRegex = new RegExp("(".concat(validPages.join('|'), ")$"));
|
|
11
|
-
var lastPathname = '';
|
|
12
11
|
export default function templateAppDir(pagePkg, _a) {
|
|
13
12
|
var _b = _a === void 0 ? {} : _a, _c = _b.code, code = _c === void 0 ? '' : _c, _d = _b.pageNoExt, pageNoExt = _d === void 0 ? '/' : _d, _e = _b.normalizedResourcePath, normalizedResourcePath = _e === void 0 ? '' : _e, _f = _b.appFolder, appFolder = _f === void 0 ? '' : _f, _g = _b.isClientComponent, isClientComponent = _g === void 0 ? false : _g, _h = _b.existLocalesFolder, existLocalesFolder = _h === void 0 ? true : _h;
|
|
14
13
|
var routeType = validPages.find(function (pageName) { return pageNoExt.endsWith(pageName); }) || 'component';
|
|
15
14
|
var isPage = routeType !== 'component' && normalizedResourcePath.includes(appFolder);
|
|
16
|
-
if (!isPage
|
|
15
|
+
if (!isPage)
|
|
17
16
|
return code;
|
|
18
17
|
var hash = Date.now().toString(16);
|
|
19
|
-
var pathname =
|
|
20
|
-
? pageNoExt.replace(validPagesRegex, '/')
|
|
21
|
-
: lastPathname;
|
|
22
|
-
lastPathname = pathname;
|
|
18
|
+
var pathname = pageNoExt.replace(validPagesRegex, '/');
|
|
23
19
|
var pageVariableName = interceptExport(pagePkg, 'default', "__Next_Translate__Page__".concat(hash, "__"));
|
|
24
20
|
if (isPage && !pageVariableName)
|
|
25
21
|
return code;
|
|
26
|
-
if (isClientComponent && !code.includes('next-translate'))
|
|
27
|
-
return code;
|
|
28
22
|
if (isClientComponent) {
|
|
29
|
-
return
|
|
23
|
+
return templateRCCPage({
|
|
30
24
|
pagePkg: pagePkg,
|
|
31
25
|
hash: hash,
|
|
32
26
|
pageVariableName: pageVariableName,
|
|
33
27
|
pathname: pathname,
|
|
34
|
-
isPage: isPage,
|
|
35
28
|
routeType: routeType,
|
|
36
29
|
existLocalesFolder: existLocalesFolder,
|
|
37
30
|
});
|
|
38
31
|
}
|
|
39
|
-
return
|
|
32
|
+
return templateRSCPage({
|
|
40
33
|
pagePkg: pagePkg,
|
|
41
34
|
hash: hash,
|
|
42
35
|
pageVariableName: pageVariableName,
|
|
@@ -45,49 +38,17 @@ export default function templateAppDir(pagePkg, _a) {
|
|
|
45
38
|
existLocalesFolder: existLocalesFolder,
|
|
46
39
|
});
|
|
47
40
|
}
|
|
48
|
-
function
|
|
41
|
+
function templateRSCPage(_a) {
|
|
49
42
|
var pagePkg = _a.pagePkg, hash = _a.hash, pageVariableName = _a.pageVariableName, pathname = _a.pathname, routeType = _a.routeType, existLocalesFolder = _a.existLocalesFolder;
|
|
50
43
|
var code = pagePkg.getCode();
|
|
51
|
-
return "\n import ".concat(INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import
|
|
44
|
+
return "\n import ".concat(INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import AppDirI18nProvider from 'next-translate/AppDirI18nProvider'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n\n ").concat(code, "\n\n export default async function __Next_Translate_new__").concat(hash, "__(props) {\n const config = { \n ...").concat(INTERNAL_CONFIG_KEY, ",\n locale: props.params?.lang ?? props.searchParams?.lang ?? ").concat(INTERNAL_CONFIG_KEY, ".defaultLocale,\n loaderName: 'server ").concat(routeType, "',\n pathname: '").concat(pathname, "'\n }\n\n const { __lang, __namespaces } = await __loadNamespaces({ ...config, ").concat(addLoadLocalesFrom(existLocalesFolder), " });\n globalThis.__NEXT_TRANSLATE__ = { lang: __lang, namespaces: __namespaces, config }\n\n return <AppDirI18nProvider lang={__lang} namespaces={__namespaces} config={config}><").concat(pageVariableName, " {...props} /></AppDirI18nProvider>\n }\n");
|
|
52
45
|
}
|
|
53
|
-
function
|
|
54
|
-
var pagePkg = _a.pagePkg, hash = _a.hash, pageVariableName = _a.pageVariableName, pathname = _a.pathname,
|
|
46
|
+
function templateRCCPage(_a) {
|
|
47
|
+
var pagePkg = _a.pagePkg, hash = _a.hash, pageVariableName = _a.pageVariableName, pathname = _a.pathname, routeType = _a.routeType, existLocalesFolder = _a.existLocalesFolder;
|
|
55
48
|
var topLine = clientLine[0];
|
|
56
|
-
var namedExportsModified = modifyNamedExportsComponents(pagePkg, { hash: hash, existLocalesFolder: existLocalesFolder });
|
|
57
|
-
var exportDefault = pageVariableName
|
|
58
|
-
? 'export default ' +
|
|
59
|
-
wrapClientComponent({
|
|
60
|
-
name: "__Next_Translate_new__".concat(hash, "__"),
|
|
61
|
-
pathname: pathname,
|
|
62
|
-
isPage: isPage,
|
|
63
|
-
pageVariableName: pageVariableName,
|
|
64
|
-
routeType: routeType,
|
|
65
|
-
existLocalesFolder: existLocalesFolder,
|
|
66
|
-
})
|
|
67
|
-
: '';
|
|
68
49
|
var clientCode = pagePkg.getCode();
|
|
69
50
|
clientLine.forEach(function (line) {
|
|
70
51
|
clientCode = clientCode.replace(line, '');
|
|
71
52
|
});
|
|
72
|
-
return "".concat(topLine, "\n import ").concat(INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import
|
|
73
|
-
}
|
|
74
|
-
function wrapClientComponent(_a) {
|
|
75
|
-
var _b = _a.name, name = _b === void 0 ? '' : _b, _c = _a.pathname, pathname = _c === void 0 ? '' : _c, _d = _a.isPage, isPage = _d === void 0 ? false : _d, _e = _a.pageVariableName, pageVariableName = _e === void 0 ? '' : _e, _f = _a.routeType, routeType = _f === void 0 ? 'component' : _f, _g = _a.existLocalesFolder, existLocalesFolder = _g === void 0 ? true : _g;
|
|
76
|
-
return "function ".concat(name, "(props) {\n const searchParams = __useSearchParams()\n const params = __useParams()\n const lang = params.lang ?? searchParams.get('lang') ?? ").concat(INTERNAL_CONFIG_KEY, ".defaultLocale\n const config = {\n ...").concat(INTERNAL_CONFIG_KEY, ",\n locale: lang,\n loaderName: 'client ").concat(routeType, "',\n pathname: '").concat(pathname, "',\n logBuild: ").concat(isPage, ",\n ").concat(addLoadLocalesFrom(existLocalesFolder), "\n }\n const { __lang, __namespaces } = __use(__loadNamespaces(config));\n\n __nt_store.set({ lang, namespaces: __namespaces, config })\n\n return <").concat(pageVariableName, " {...props} />\n }");
|
|
77
|
-
}
|
|
78
|
-
function modifyNamedExportsComponents(pagePkg, _a) {
|
|
79
|
-
var hash = _a.hash, existLocalesFolder = _a.existLocalesFolder;
|
|
80
|
-
return interceptNamedExportsFromReactComponents(pagePkg, hash)
|
|
81
|
-
.map(function (_a) {
|
|
82
|
-
var exportName = _a.exportName, defaultLocalName = _a.defaultLocalName;
|
|
83
|
-
return "\n ".concat(wrapClientComponent({
|
|
84
|
-
name: defaultLocalName,
|
|
85
|
-
pathname: lastPathname,
|
|
86
|
-
isPage: false,
|
|
87
|
-
pageVariableName: exportName,
|
|
88
|
-
existLocalesFolder: existLocalesFolder
|
|
89
|
-
}), "\n export { ").concat(defaultLocalName, " as ").concat(exportName, " }\n ");
|
|
90
|
-
})
|
|
91
|
-
.join('')
|
|
92
|
-
.trim();
|
|
53
|
+
return "".concat(topLine, "\n import ").concat(INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import AppDirI18nProvider from 'next-translate/AppDirI18nProvider'\n import { useSearchParams as __useSearchParams, useParams as __useParams } from 'next/navigation'\n import { use as __use } from 'react'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n\n ").concat(clientCode, "\n\n export default function __Next_Translate_new__").concat(hash, "__(props) {\n const searchParams = __useSearchParams()\n const params = __useParams()\n const lang = params.lang ?? searchParams.get('lang') ?? ").concat(INTERNAL_CONFIG_KEY, ".defaultLocale\n const config = {\n ...").concat(INTERNAL_CONFIG_KEY, ",\n locale: lang,\n loaderName: 'client ").concat(routeType, "',\n pathname: '").concat(pathname, "',\n }\n\n const { __lang, __namespaces } = __use(__loadNamespaces({ ...config, ").concat(addLoadLocalesFrom(existLocalesFolder), " }));\n globalThis.__NEXT_TRANSLATE__ = { lang: __lang, namespaces: __namespaces, config }\n\n return <").concat(pageVariableName, " {...props} />\n }\n");
|
|
93
54
|
}
|
package/lib/esm/utils.js
CHANGED
|
@@ -29,7 +29,9 @@ export function getDefaultAppJs(existLocalesFolder) {
|
|
|
29
29
|
return "\n import ".concat(INTERNAL_CONFIG_KEY, " 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 ...").concat(INTERNAL_CONFIG_KEY, ",\n skipInitialProps: true,\n isLoader: true,\n ").concat(addLoadLocalesFrom(existLocalesFolder), "\n })\n ");
|
|
30
30
|
}
|
|
31
31
|
export function addLoadLocalesFrom(existLocalesFolder) {
|
|
32
|
-
var defaultFn = existLocalesFolder
|
|
32
|
+
var defaultFn = existLocalesFolder
|
|
33
|
+
? defaultLoader
|
|
34
|
+
: "() => Promise.resolve({})";
|
|
33
35
|
return "loadLocaleFrom: ".concat(INTERNAL_CONFIG_KEY, ".loadLocaleFrom || (").concat(defaultFn, "),");
|
|
34
36
|
}
|
|
35
37
|
export function getTsCompilerOptions(basePath, cutDependencies) {
|
|
@@ -267,39 +269,6 @@ export function isNotExportModifier(modifier) {
|
|
|
267
269
|
];
|
|
268
270
|
return !exportModifiers.includes(modifier.kind);
|
|
269
271
|
}
|
|
270
|
-
function isPascalCase(str) {
|
|
271
|
-
return (str.length > 1 &&
|
|
272
|
-
str[0] === str[0].toUpperCase() &&
|
|
273
|
-
str[1] === str[1].toLowerCase());
|
|
274
|
-
}
|
|
275
|
-
export function interceptNamedExportsFromReactComponents(filePkg, hash) {
|
|
276
|
-
var sourceFile = filePkg.sourceFile;
|
|
277
|
-
var interceptedExports = [];
|
|
278
|
-
sourceFile.statements
|
|
279
|
-
.filter(function (node) {
|
|
280
|
-
var _a;
|
|
281
|
-
return (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.some(function (modifier) { return (modifier === null || modifier === void 0 ? void 0 : modifier.kind) === ts.SyntaxKind.ExportKeyword; });
|
|
282
|
-
})
|
|
283
|
-
.forEach(function (node) {
|
|
284
|
-
var _a, _b, _c;
|
|
285
|
-
var exportName = '';
|
|
286
|
-
if (ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) {
|
|
287
|
-
exportName = (_b = (_a = node.name) === null || _a === void 0 ? void 0 : _a.getText()) !== null && _b !== void 0 ? _b : '';
|
|
288
|
-
}
|
|
289
|
-
else if (ts.isVariableStatement(node)) {
|
|
290
|
-
var declaration = node.declarationList.declarations[0];
|
|
291
|
-
if (!ts.isArrowFunction(declaration.initializer))
|
|
292
|
-
return;
|
|
293
|
-
exportName = (_c = declaration.name) === null || _c === void 0 ? void 0 : _c.getText();
|
|
294
|
-
}
|
|
295
|
-
if (isPascalCase(exportName)) {
|
|
296
|
-
var defaultLocalName = "__Next_Translate_Component_".concat(hash, "_").concat(interceptedExports.length);
|
|
297
|
-
interceptExport(filePkg, exportName, defaultLocalName);
|
|
298
|
-
interceptedExports.push({ exportName: exportName, defaultLocalName: defaultLocalName });
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
return interceptedExports;
|
|
302
|
-
}
|
|
303
272
|
export function interceptExport(filePkg, exportName, defaultLocalName) {
|
|
304
273
|
var exportContent = getNamedExport(filePkg, exportName, false);
|
|
305
274
|
var finalLocalName = '';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-translate-plugin",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"description": "Tiny and powerful i18n plugin to translate your Next.js pages.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"husky": "7.0.4",
|
|
73
73
|
"jest": "27.3.1",
|
|
74
74
|
"next": "13.1.2",
|
|
75
|
-
"next-translate": "2.
|
|
75
|
+
"next-translate": "2.4.0",
|
|
76
76
|
"prettier": "2.8.3",
|
|
77
77
|
"pretty-quick": "3.1.3",
|
|
78
78
|
"react": "18.2.0",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"supertest": "6.3.3"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
83
|
-
"next-translate": ">= 2.4.
|
|
83
|
+
"next-translate": ">= 2.4.1"
|
|
84
84
|
},
|
|
85
85
|
"prettier": {
|
|
86
86
|
"trailingComma": "es5",
|
|
@@ -104,4 +104,4 @@
|
|
|
104
104
|
"^.+\\.(j|t)sx?$": "babel-jest"
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
-
}
|
|
107
|
+
}
|
package/utils.d.ts
CHANGED
|
@@ -21,14 +21,8 @@ export declare function hasStaticName(filePkg: ParsedFilePkg, target: ts.Declara
|
|
|
21
21
|
export declare function isPageToIgnore(pageFilePath: string): boolean;
|
|
22
22
|
export declare function hasHOC(filePkg: ParsedFilePkg): boolean;
|
|
23
23
|
export declare function isNotExportModifier(modifier: ts.Modifier): boolean;
|
|
24
|
-
declare type InterceptedExport = {
|
|
25
|
-
exportName: string;
|
|
26
|
-
defaultLocalName: string;
|
|
27
|
-
};
|
|
28
|
-
export declare function interceptNamedExportsFromReactComponents(filePkg: ParsedFilePkg, hash: string): InterceptedExport[];
|
|
29
24
|
export declare function interceptExport(filePkg: ParsedFilePkg, exportName: string, defaultLocalName: string): string;
|
|
30
25
|
export declare function removeCommentsFromCode(code: string): string;
|
|
31
26
|
export declare function calculatePageDir(name: 'pages' | 'app', pagesInDir: string | undefined, dir: string): string;
|
|
32
27
|
export declare function existPages(dir: string, pages: string | undefined): boolean | "" | undefined;
|
|
33
28
|
export declare function existLocalesFolderWithNamespaces(dir: string): boolean;
|
|
34
|
-
export {};
|