next-translate-plugin 2.0.0-canary.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/LICENSE +23 -0
- package/README.md +57 -0
- package/index.d.ts +2 -0
- package/lib/cjs/index.js +112 -0
- package/lib/cjs/loader.js +62 -0
- package/lib/cjs/templateWithHoc.js +13 -0
- package/lib/cjs/templateWithLoader.js +12 -0
- package/lib/cjs/types.js +2 -0
- package/lib/cjs/utils.js +352 -0
- package/lib/esm/index.js +106 -0
- package/lib/esm/loader.js +56 -0
- package/lib/esm/templateWithHoc.js +10 -0
- package/lib/esm/templateWithLoader.js +9 -0
- package/lib/esm/types.js +1 -0
- package/lib/esm/utils.js +327 -0
- package/package.json +101 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2023 Aral Roca Gomez
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
|
|
2
|
+
<h2 align="center">
|
|
3
|
+
<b>Next-translate-plugin</b>
|
|
4
|
+
</h2>
|
|
5
|
+
<p align="center">
|
|
6
|
+
<b>Webpack plugin</b>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
(Maybe TurboPack in the future)
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center">
|
|
14
|
+
<img src="images/logo.svg" width="200" alt="next-translate" />
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
<div align="center">
|
|
18
|
+
|
|
19
|
+
[](https://badge.fury.io/js/next-translate-plugin)
|
|
20
|
+
[](https://github.com/aralroca/next-translate-plugin/blob/main/CONTRIBUTING.md)
|
|
21
|
+
<a href="https://github.com/aralroca/next-translate/actions?query=workflow%3ACI" alt="Tests status">
|
|
22
|
+
<img src="https://github.com/aralroca/next-translate/workflows/CI/badge.svg" /></a>
|
|
23
|
+
<a href="https://twitter.com/intent/follow?screen_name=aralroca">
|
|
24
|
+
<img src="https://img.shields.io/twitter/follow/aralroca?style=social&logo=twitter"
|
|
25
|
+
alt="follow on Twitter"></a>
|
|
26
|
+
|
|
27
|
+
</div>
|
|
28
|
+
|
|
29
|
+
<hr />
|
|
30
|
+
|
|
31
|
+
Two of the goals of **[next-translate](https://github.com/aralroca/next-translate)** is to be a **tiny library** (~2kb) and to have **no external dependencies**.
|
|
32
|
+
|
|
33
|
+
Since **this plugin uses TypeScript compiler** dependency, and we don't want you to have to bring extra kb into the pipeline, we have chosen to separate **next-translate** and **next-translate-plugin** with two distinct packages.
|
|
34
|
+
|
|
35
|
+
This way you can save this as `devDependencies` instead of dependencies.
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
yarn add next-translate-plugin -D
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
or
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
npm install next-translate-plugin --save-dev
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Documentation
|
|
48
|
+
|
|
49
|
+
All the documentation is in Next-translate repository:
|
|
50
|
+
|
|
51
|
+
- [Next-translate GitHub repository](https://github.com/aralroca/next-translate)
|
|
52
|
+
|
|
53
|
+
## Contributors ✨
|
|
54
|
+
|
|
55
|
+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
56
|
+
|
|
57
|
+
- [Contributors list is in Next-translate GitHub repository](https://github.com/aralroca/next-translate#contributors-)
|
package/index.d.ts
ADDED
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
14
|
+
var t = {};
|
|
15
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
16
|
+
t[p] = s[p];
|
|
17
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
18
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
19
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
20
|
+
t[p[i]] = s[p[i]];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
24
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
25
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
var fs_1 = __importDefault(require("fs"));
|
|
29
|
+
var path_1 = __importDefault(require("path"));
|
|
30
|
+
var utils_1 = require("./utils");
|
|
31
|
+
function nextTranslate(nextConfig) {
|
|
32
|
+
if (nextConfig === void 0) { nextConfig = {}; }
|
|
33
|
+
var test = /\.(tsx|ts|js|mjs|jsx)$/;
|
|
34
|
+
var basePath = pkgDir();
|
|
35
|
+
var possiblePageDirs = [
|
|
36
|
+
'pages',
|
|
37
|
+
'src/pages',
|
|
38
|
+
'app/pages',
|
|
39
|
+
'integrations/pages',
|
|
40
|
+
];
|
|
41
|
+
var translationDir = path_1.default.resolve(path_1.default.relative(basePath, process.env.NEXT_TRANSLATE_PATH || '.'));
|
|
42
|
+
var nextConfigI18n = nextConfig.i18n || {};
|
|
43
|
+
var _a = require(path_1.default.join(translationDir, 'i18n')), _b = _a.locales, locales = _b === void 0 ? nextConfigI18n.locales || [] : _b, _c = _a.defaultLocale, defaultLocale = _c === void 0 ? nextConfigI18n.defaultLocale || 'en' : _c, _d = _a.domains, domains = _d === void 0 ? nextConfigI18n.domains : _d, _e = _a.localeDetection, localeDetection = _e === void 0 ? nextConfigI18n.localeDetection : _e, _f = _a.loader, loader = _f === void 0 ? true : _f, pagesInDir = _a.pagesInDir, restI18n = __rest(_a, ["locales", "defaultLocale", "domains", "localeDetection", "loader", "pagesInDir"]);
|
|
44
|
+
var nextConfigWithI18n = __assign(__assign({}, nextConfig), { i18n: {
|
|
45
|
+
locales: locales,
|
|
46
|
+
defaultLocale: defaultLocale,
|
|
47
|
+
domains: domains,
|
|
48
|
+
localeDetection: localeDetection,
|
|
49
|
+
} });
|
|
50
|
+
var hasGetInitialPropsOnAppJs = false;
|
|
51
|
+
if (!pagesInDir) {
|
|
52
|
+
for (var _i = 0, possiblePageDirs_1 = possiblePageDirs; _i < possiblePageDirs_1.length; _i++) {
|
|
53
|
+
var possiblePageDir = possiblePageDirs_1[_i];
|
|
54
|
+
if (fs_1.default.existsSync(path_1.default.join(basePath, possiblePageDir))) {
|
|
55
|
+
pagesInDir = possiblePageDir;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!pagesInDir || !fs_1.default.existsSync(path_1.default.join(basePath, pagesInDir))) {
|
|
61
|
+
return nextConfigWithI18n;
|
|
62
|
+
}
|
|
63
|
+
var pagesPath = path_1.default.join(basePath, pagesInDir);
|
|
64
|
+
var app = fs_1.default.readdirSync(pagesPath).find(function (page) { return page.startsWith('_app.'); });
|
|
65
|
+
if (app) {
|
|
66
|
+
var appPkg = (0, utils_1.parseFile)(basePath, path_1.default.join(pagesPath, app));
|
|
67
|
+
var defaultExport = (0, utils_1.getDefaultExport)(appPkg);
|
|
68
|
+
if (defaultExport) {
|
|
69
|
+
var isGetInitialProps = (0, utils_1.hasStaticName)(appPkg, defaultExport, 'getInitialProps');
|
|
70
|
+
hasGetInitialPropsOnAppJs = isGetInitialProps || (0, utils_1.hasHOC)(appPkg);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return __assign(__assign({}, nextConfigWithI18n), { webpack: function (conf, options) {
|
|
74
|
+
var config = typeof nextConfig.webpack === 'function'
|
|
75
|
+
? nextConfig.webpack(conf, options)
|
|
76
|
+
: conf;
|
|
77
|
+
if (!config.resolve)
|
|
78
|
+
config.resolve = {};
|
|
79
|
+
if (!config.module)
|
|
80
|
+
config.module = {};
|
|
81
|
+
if (!config.module.rules)
|
|
82
|
+
config.module.rules = [];
|
|
83
|
+
config.resolve.alias = __assign(__assign({}, (config.resolve.alias || {})), { '@next-translate-root': path_1.default.resolve(translationDir) });
|
|
84
|
+
if (!loader)
|
|
85
|
+
return config;
|
|
86
|
+
config.module.rules.push({
|
|
87
|
+
test: test,
|
|
88
|
+
use: {
|
|
89
|
+
loader: 'next-translate/plugin/loader',
|
|
90
|
+
options: {
|
|
91
|
+
basePath: basePath,
|
|
92
|
+
pagesPath: path_1.default.join(pagesPath, '/'),
|
|
93
|
+
hasAppJs: Boolean(app),
|
|
94
|
+
hasGetInitialPropsOnAppJs: hasGetInitialPropsOnAppJs,
|
|
95
|
+
hasLoadLocaleFrom: typeof restI18n.loadLocaleFrom === 'function',
|
|
96
|
+
extensionsRgx: restI18n.extensionsRgx || test,
|
|
97
|
+
revalidate: restI18n.revalidate || 0,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
return config;
|
|
102
|
+
} });
|
|
103
|
+
}
|
|
104
|
+
exports.default = nextTranslate;
|
|
105
|
+
function pkgDir() {
|
|
106
|
+
try {
|
|
107
|
+
return require('pkg-dir').sync() || process.cwd();
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
return process.cwd();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var templateWithHoc_1 = __importDefault(require("./templateWithHoc"));
|
|
7
|
+
var templateWithLoader_1 = __importDefault(require("./templateWithLoader"));
|
|
8
|
+
var utils_1 = require("./utils");
|
|
9
|
+
function loader(rawCode) {
|
|
10
|
+
var _a = this.getOptions(), basePath = _a.basePath, pagesPath = _a.pagesPath, hasAppJs = _a.hasAppJs, hasGetInitialPropsOnAppJs = _a.hasGetInitialPropsOnAppJs, hasLoadLocaleFrom = _a.hasLoadLocaleFrom, extensionsRgx = _a.extensionsRgx, revalidate = _a.revalidate;
|
|
11
|
+
var normalizedPagesPath = pagesPath.replace(/\\/g, '/');
|
|
12
|
+
var normalizedResourcePath = this.resourcePath.replace(/\\/g, '/');
|
|
13
|
+
if (normalizedResourcePath.includes('node_modules/next/dist/pages/_app')) {
|
|
14
|
+
if (hasAppJs)
|
|
15
|
+
return rawCode;
|
|
16
|
+
return (0, utils_1.getDefaultAppJs)(hasLoadLocaleFrom);
|
|
17
|
+
}
|
|
18
|
+
if (!normalizedResourcePath.startsWith(normalizedPagesPath))
|
|
19
|
+
return rawCode;
|
|
20
|
+
var page = normalizedResourcePath.replace(normalizedPagesPath, '/');
|
|
21
|
+
var pageNoExt = page.replace(extensionsRgx, '');
|
|
22
|
+
var pagePkg = (0, utils_1.parseFile)(basePath, normalizedResourcePath);
|
|
23
|
+
var defaultExport = (0, utils_1.getDefaultExport)(pagePkg);
|
|
24
|
+
if (!defaultExport)
|
|
25
|
+
return rawCode;
|
|
26
|
+
if ((0, utils_1.hasExportName)(pagePkg, '__N_SSP') || (0, utils_1.hasExportName)(pagePkg, '__N_SSG')) {
|
|
27
|
+
return rawCode;
|
|
28
|
+
}
|
|
29
|
+
if (hasGetInitialPropsOnAppJs) {
|
|
30
|
+
return pageNoExt === '/_app'
|
|
31
|
+
? (0, templateWithHoc_1.default)(pagePkg, { hasLoadLocaleFrom: hasLoadLocaleFrom })
|
|
32
|
+
: rawCode;
|
|
33
|
+
}
|
|
34
|
+
if (pageNoExt === '/_app') {
|
|
35
|
+
return (0, templateWithHoc_1.default)(pagePkg, {
|
|
36
|
+
skipInitialProps: true,
|
|
37
|
+
hasLoadLocaleFrom: hasLoadLocaleFrom,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if ((0, utils_1.isPageToIgnore)(page))
|
|
41
|
+
return rawCode;
|
|
42
|
+
var isWrapperWithExternalHOC = (0, utils_1.hasHOC)(pagePkg);
|
|
43
|
+
var isDynamicPage = page.includes('[');
|
|
44
|
+
var isGetStaticProps = (0, utils_1.hasExportName)(pagePkg, 'getStaticProps');
|
|
45
|
+
var isGetStaticPaths = (0, utils_1.hasExportName)(pagePkg, 'getStaticPaths');
|
|
46
|
+
var isGetServerSideProps = (0, utils_1.hasExportName)(pagePkg, 'getServerSideProps');
|
|
47
|
+
var isGetInitialProps = (0, utils_1.hasStaticName)(pagePkg, defaultExport, 'getInitialProps');
|
|
48
|
+
var hasLoader = isGetStaticProps || isGetServerSideProps || isGetInitialProps;
|
|
49
|
+
if (isGetInitialProps || (!hasLoader && isWrapperWithExternalHOC)) {
|
|
50
|
+
return (0, templateWithHoc_1.default)(pagePkg, { hasLoadLocaleFrom: hasLoadLocaleFrom });
|
|
51
|
+
}
|
|
52
|
+
var loader = isGetServerSideProps || (!hasLoader && isDynamicPage && !isGetStaticPaths)
|
|
53
|
+
? 'getServerSideProps'
|
|
54
|
+
: 'getStaticProps';
|
|
55
|
+
return (0, templateWithLoader_1.default)(pagePkg, {
|
|
56
|
+
page: pageNoExt,
|
|
57
|
+
loader: loader,
|
|
58
|
+
hasLoadLocaleFrom: hasLoadLocaleFrom,
|
|
59
|
+
revalidate: revalidate,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
exports.default = loader;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var utils_1 = require("./utils");
|
|
4
|
+
function templateWithHoc(pagePkg, _a) {
|
|
5
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.skipInitialProps, skipInitialProps = _c === void 0 ? false : _c, _d = _b.hasLoadLocaleFrom, hasLoadLocaleFrom = _d === void 0 ? false : _d;
|
|
6
|
+
var hash = Date.now().toString(16);
|
|
7
|
+
var pageVariableName = (0, utils_1.interceptExport)(pagePkg, 'default', "__Next_Translate__Page__".concat(hash, "__"));
|
|
8
|
+
var hasDefaultExport = Boolean(pageVariableName);
|
|
9
|
+
if (!hasDefaultExport)
|
|
10
|
+
return pagePkg.getCode();
|
|
11
|
+
return "\n import __i18nConfig from '@next-translate-root/i18n'\n import __appWithI18n from 'next-translate/appWithI18n'\n ".concat(pagePkg.getCode(), "\n export default __appWithI18n(").concat(pageVariableName, ", {\n ...__i18nConfig,\n isLoader: true,\n skipInitialProps: ").concat(skipInitialProps, ",\n ").concat((0, utils_1.overwriteLoadLocales)(hasLoadLocaleFrom), "\n });\n ");
|
|
12
|
+
}
|
|
13
|
+
exports.default = templateWithHoc;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var utils_1 = require("./utils");
|
|
4
|
+
function templateWithLoader(pagePkg, _a) {
|
|
5
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.page, page = _c === void 0 ? '' : _c, _d = _b.loader, loader = _d === void 0 ? 'getStaticProps' : _d, _e = _b.revalidate, revalidate = _e === void 0 ? 0 : _e, _f = _b.hasLoadLocaleFrom, hasLoadLocaleFrom = _f === void 0 ? false : _f;
|
|
6
|
+
var hash = Date.now().toString(16);
|
|
7
|
+
var oldLoaderName = (0, utils_1.interceptExport)(pagePkg, loader, "__Next_Translate_old_".concat(loader, "__").concat(hash, "__"));
|
|
8
|
+
var newLoaderName = "__Next_Translate__".concat(loader, "__").concat(hash, "__");
|
|
9
|
+
var hasLoader = Boolean(oldLoaderName);
|
|
10
|
+
return "\n import __i18nConfig from '@next-translate-root/i18n'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n ".concat(pagePkg.getCode(), "\n async function ").concat(newLoaderName, "(ctx) {\n ").concat(hasLoader ? "const res = await ".concat(oldLoaderName, "(ctx)") : '', "\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 ...__i18nConfig,\n pathname: '").concat(page, "',\n loaderName: '").concat(loader, "',\n ").concat((0, utils_1.overwriteLoadLocales)(hasLoadLocaleFrom), "\n }))\n }\n }\n }\n export { ").concat(newLoaderName, " as ").concat(loader, " }\n ");
|
|
11
|
+
}
|
|
12
|
+
exports.default = templateWithLoader;
|
package/lib/cjs/types.js
ADDED
package/lib/cjs/utils.js
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
|
+
if (ar || !(i in from)) {
|
|
16
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
17
|
+
ar[i] = from[i];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
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.overwriteLoadLocales = exports.getDefaultAppJs = exports.defaultLoader = void 0;
|
|
27
|
+
var typescript_1 = __importDefault(require("typescript"));
|
|
28
|
+
var specFileOrFolderRgx = /(__mocks__|__tests__)|(\.(spec|test)\.(tsx|ts|js|jsx)$)/;
|
|
29
|
+
exports.defaultLoader = '(l, n) => import(`@next-translate-root/locales/${l}/${n}`).then(m => m.default)';
|
|
30
|
+
function getDefaultAppJs(hasLoadLocaleFrom) {
|
|
31
|
+
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 ");
|
|
32
|
+
}
|
|
33
|
+
exports.getDefaultAppJs = getDefaultAppJs;
|
|
34
|
+
function overwriteLoadLocales(hasLoadLocaleFrom) {
|
|
35
|
+
if (hasLoadLocaleFrom)
|
|
36
|
+
return '';
|
|
37
|
+
return "loadLocaleFrom: ".concat(exports.defaultLoader, ",");
|
|
38
|
+
}
|
|
39
|
+
exports.overwriteLoadLocales = overwriteLoadLocales;
|
|
40
|
+
function getTsCompilerOptions(basePath, cutDependencies) {
|
|
41
|
+
if (cutDependencies === void 0) { cutDependencies = false; }
|
|
42
|
+
var options;
|
|
43
|
+
var configPath = typescript_1.default.findConfigFile(basePath, typescript_1.default.sys.fileExists, 'tsconfig.json');
|
|
44
|
+
if (!configPath) {
|
|
45
|
+
options = { allowJs: true };
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
var readConfigFileResult = typescript_1.default.readConfigFile(configPath, typescript_1.default.sys.readFile);
|
|
49
|
+
var jsonConfig = readConfigFileResult.config;
|
|
50
|
+
var convertResult = typescript_1.default.convertCompilerOptionsFromJson(jsonConfig.compilerOptions, basePath);
|
|
51
|
+
options = convertResult.options;
|
|
52
|
+
}
|
|
53
|
+
if (cutDependencies) {
|
|
54
|
+
options = __assign(__assign({}, options), { types: [], noResolve: true, noLib: true });
|
|
55
|
+
}
|
|
56
|
+
return options;
|
|
57
|
+
}
|
|
58
|
+
exports.getTsCompilerOptions = getTsCompilerOptions;
|
|
59
|
+
function getFilePkg(program, filename) {
|
|
60
|
+
var checker = program.getTypeChecker();
|
|
61
|
+
var sourceFile = program.getSourceFile(filename);
|
|
62
|
+
var printer = typescript_1.default.createPrinter({ newLine: typescript_1.default.NewLineKind.LineFeed });
|
|
63
|
+
var fileSymbol = checker.getSymbolAtLocation(sourceFile);
|
|
64
|
+
var filePkg;
|
|
65
|
+
function transform(transformer) {
|
|
66
|
+
var sourceFile = filePkg.sourceFile;
|
|
67
|
+
var transformationResult = typescript_1.default.transform(sourceFile, [
|
|
68
|
+
function (context) { return function (sourceFile) { return transformer(sourceFile, context); }; },
|
|
69
|
+
]);
|
|
70
|
+
filePkg.sourceFile = transformationResult.transformed[0];
|
|
71
|
+
filePkg.fileSymbol = checker.getSymbolAtLocation(filePkg.sourceFile);
|
|
72
|
+
}
|
|
73
|
+
function getCode() {
|
|
74
|
+
var sourceFile = filePkg.sourceFile;
|
|
75
|
+
return printer.printNode(typescript_1.default.EmitHint.Unspecified, sourceFile, sourceFile);
|
|
76
|
+
}
|
|
77
|
+
filePkg = { program: program, checker: checker, sourceFile: sourceFile, fileSymbol: fileSymbol, transform: transform, getCode: getCode };
|
|
78
|
+
return filePkg;
|
|
79
|
+
}
|
|
80
|
+
exports.getFilePkg = getFilePkg;
|
|
81
|
+
function parseFile(basePath, filename) {
|
|
82
|
+
var options = getTsCompilerOptions(basePath, true);
|
|
83
|
+
var program = typescript_1.default.createProgram([filename], options);
|
|
84
|
+
return getFilePkg(program, filename);
|
|
85
|
+
}
|
|
86
|
+
exports.parseFile = parseFile;
|
|
87
|
+
function parseCode(format, code) {
|
|
88
|
+
var options = getTsCompilerOptions('/', true);
|
|
89
|
+
var host = typescript_1.default.createCompilerHost(options);
|
|
90
|
+
var filename = "source.".concat(format);
|
|
91
|
+
host.getSourceFile = function (fileName) {
|
|
92
|
+
return typescript_1.default.createSourceFile(fileName, code, typescript_1.default.ScriptTarget.Latest);
|
|
93
|
+
};
|
|
94
|
+
var program = typescript_1.default.createProgram([filename], options, host);
|
|
95
|
+
return getFilePkg(program, filename);
|
|
96
|
+
}
|
|
97
|
+
exports.parseCode = parseCode;
|
|
98
|
+
function getSymbol(filePkg, node) {
|
|
99
|
+
var location = typescript_1.default.isVariableDeclaration(node) ? node.name : node;
|
|
100
|
+
return filePkg.checker.getSymbolAtLocation(location);
|
|
101
|
+
}
|
|
102
|
+
exports.getSymbol = getSymbol;
|
|
103
|
+
function getImportedNames(filePkg, moduleName) {
|
|
104
|
+
var importClause = filePkg.sourceFile.forEachChild(function (node) {
|
|
105
|
+
if (typescript_1.default.isImportDeclaration(node)) {
|
|
106
|
+
if (node.moduleSpecifier.getText().slice(1, -1) === moduleName) {
|
|
107
|
+
return node.importClause;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
});
|
|
112
|
+
if (importClause) {
|
|
113
|
+
var exportedNamesToImported_1 = new Map();
|
|
114
|
+
if (importClause.name) {
|
|
115
|
+
exportedNamesToImported_1.set('default', importClause.name);
|
|
116
|
+
}
|
|
117
|
+
if (importClause.namedBindings) {
|
|
118
|
+
importClause.namedBindings.forEachChild(function (node) {
|
|
119
|
+
if (typescript_1.default.isImportSpecifier(node)) {
|
|
120
|
+
if (node.propertyName) {
|
|
121
|
+
exportedNamesToImported_1.set(node.propertyName.getText(), node.name);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
exportedNamesToImported_1.set(node.name.getText(), node.name);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return exportedNamesToImported_1;
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
exports.getImportedNames = getImportedNames;
|
|
134
|
+
function resolveParenthesis(filePkg, parenthesizedExpression) {
|
|
135
|
+
var content = parenthesizedExpression.expression;
|
|
136
|
+
if (typescript_1.default.isParenthesizedExpression(content)) {
|
|
137
|
+
return resolveParenthesis(filePkg, content);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
return content;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.resolveParenthesis = resolveParenthesis;
|
|
144
|
+
function resolveIdentifier(filePkg, identifier) {
|
|
145
|
+
var identifierSymbol = getSymbol(filePkg, identifier);
|
|
146
|
+
if (identifierSymbol && Array.isArray(identifierSymbol.declarations)) {
|
|
147
|
+
var identifierDeclaration = identifierSymbol.declarations[0];
|
|
148
|
+
if (typescript_1.default.isVariableDeclaration(identifierDeclaration)) {
|
|
149
|
+
var initializer = identifierDeclaration.initializer;
|
|
150
|
+
if (initializer && typescript_1.default.isParenthesizedExpression(initializer)) {
|
|
151
|
+
initializer = resolveParenthesis(filePkg, initializer);
|
|
152
|
+
}
|
|
153
|
+
if (initializer && typescript_1.default.isIdentifier(initializer)) {
|
|
154
|
+
return resolveIdentifier(filePkg, initializer);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return identifierDeclaration;
|
|
158
|
+
}
|
|
159
|
+
return identifier;
|
|
160
|
+
}
|
|
161
|
+
exports.resolveIdentifier = resolveIdentifier;
|
|
162
|
+
function getNamedExport(filePkg, name, resolveExport) {
|
|
163
|
+
if (resolveExport === void 0) { resolveExport = true; }
|
|
164
|
+
var checker = filePkg.checker, fileSymbol = filePkg.fileSymbol;
|
|
165
|
+
var exportContent;
|
|
166
|
+
if (fileSymbol) {
|
|
167
|
+
var exportSymbol = checker.tryGetMemberInModuleExports(name, fileSymbol);
|
|
168
|
+
if (exportSymbol && Array.isArray(exportSymbol.declarations)) {
|
|
169
|
+
var exportDeclaration = exportSymbol.declarations[0];
|
|
170
|
+
if (resolveExport && typescript_1.default.isExportAssignment(exportDeclaration)) {
|
|
171
|
+
exportContent = exportDeclaration.expression;
|
|
172
|
+
if (typescript_1.default.isParenthesizedExpression(exportContent)) {
|
|
173
|
+
exportContent = resolveParenthesis(filePkg, exportContent);
|
|
174
|
+
}
|
|
175
|
+
if (typescript_1.default.isIdentifier(exportContent)) {
|
|
176
|
+
exportContent = resolveIdentifier(filePkg, exportContent);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
exportContent = exportDeclaration;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return exportContent;
|
|
185
|
+
}
|
|
186
|
+
exports.getNamedExport = getNamedExport;
|
|
187
|
+
function getDefaultExport(filePkg, resolveExport) {
|
|
188
|
+
if (resolveExport === void 0) { resolveExport = true; }
|
|
189
|
+
return getNamedExport(filePkg, 'default', resolveExport);
|
|
190
|
+
}
|
|
191
|
+
exports.getDefaultExport = getDefaultExport;
|
|
192
|
+
function hasExportName(filePkg, name) {
|
|
193
|
+
return Boolean(getNamedExport(filePkg, name, false));
|
|
194
|
+
}
|
|
195
|
+
exports.hasExportName = hasExportName;
|
|
196
|
+
function getStaticName(filePkg, target, name) {
|
|
197
|
+
var symbol = getSymbol(filePkg, target);
|
|
198
|
+
if (symbol) {
|
|
199
|
+
return filePkg.checker.tryGetMemberInModuleExports(name, symbol);
|
|
200
|
+
}
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
exports.getStaticName = getStaticName;
|
|
204
|
+
function hasStaticName(filePkg, target, name) {
|
|
205
|
+
return Boolean(getStaticName(filePkg, target, name));
|
|
206
|
+
}
|
|
207
|
+
exports.hasStaticName = hasStaticName;
|
|
208
|
+
function isPageToIgnore(pageFilePath) {
|
|
209
|
+
var fileName = pageFilePath.substring(pageFilePath.lastIndexOf('/') + 1);
|
|
210
|
+
return (pageFilePath.startsWith('/api/') ||
|
|
211
|
+
pageFilePath.startsWith('/_document.') ||
|
|
212
|
+
pageFilePath.startsWith('/middleware.') ||
|
|
213
|
+
fileName.startsWith('_middleware.') ||
|
|
214
|
+
specFileOrFolderRgx.test(pageFilePath));
|
|
215
|
+
}
|
|
216
|
+
exports.isPageToIgnore = isPageToIgnore;
|
|
217
|
+
function hasHOC(filePkg) {
|
|
218
|
+
var defaultExport = getDefaultExport(filePkg);
|
|
219
|
+
if (!defaultExport ||
|
|
220
|
+
hasExportName(filePkg, 'getStaticProps') ||
|
|
221
|
+
hasExportName(filePkg, 'getServerSideProps') ||
|
|
222
|
+
hasExportName(filePkg, 'getStaticPaths')) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
if (typescript_1.default.isVariableDeclaration(defaultExport) && defaultExport.initializer) {
|
|
226
|
+
defaultExport = defaultExport.initializer;
|
|
227
|
+
if (typescript_1.default.isParenthesizedExpression(defaultExport)) {
|
|
228
|
+
defaultExport = resolveParenthesis(filePkg, defaultExport);
|
|
229
|
+
}
|
|
230
|
+
if (typescript_1.default.isIdentifier(defaultExport)) {
|
|
231
|
+
defaultExport = resolveIdentifier(filePkg, defaultExport);
|
|
232
|
+
}
|
|
233
|
+
if (!typescript_1.default.isCallExpression(defaultExport)) {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (typescript_1.default.isFunctionDeclaration(defaultExport) ||
|
|
238
|
+
typescript_1.default.isClassDeclaration(defaultExport)) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
var importedNames = getImportedNames(filePkg, 'next-translate/withTranslation');
|
|
242
|
+
var withTranslationId = importedNames === null || importedNames === void 0 ? void 0 : importedNames.get('default');
|
|
243
|
+
function isCallExpressionWithHOC(callExpression) {
|
|
244
|
+
var _a;
|
|
245
|
+
var callable = callExpression.expression;
|
|
246
|
+
var expressionsToVisit = __spreadArray([callable], callExpression.arguments, true);
|
|
247
|
+
for (var _i = 0, expressionsToVisit_1 = expressionsToVisit; _i < expressionsToVisit_1.length; _i++) {
|
|
248
|
+
var expression = expressionsToVisit_1[_i];
|
|
249
|
+
if (typescript_1.default.isCallExpression(expression)) {
|
|
250
|
+
return isCallExpressionWithHOC(expression);
|
|
251
|
+
}
|
|
252
|
+
if (typescript_1.default.isIdentifier(expression)) {
|
|
253
|
+
var resolved = resolveIdentifier(filePkg, expression);
|
|
254
|
+
if (typescript_1.default.isVariableDeclaration(resolved)) {
|
|
255
|
+
var initializer = resolved.initializer;
|
|
256
|
+
if (initializer && typescript_1.default.isParenthesizedExpression(initializer)) {
|
|
257
|
+
initializer = resolveParenthesis(filePkg, initializer);
|
|
258
|
+
}
|
|
259
|
+
if (initializer && typescript_1.default.isCallExpression(initializer)) {
|
|
260
|
+
return isCallExpressionWithHOC(initializer);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (typescript_1.default.isImportClause(resolved)) {
|
|
264
|
+
if (((_a = resolved.name) === null || _a === void 0 ? void 0 : _a.getText()) !== (withTranslationId === null || withTranslationId === void 0 ? void 0 : withTranslationId.getText())) {
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
if (typescript_1.default.isCallExpression(defaultExport)) {
|
|
273
|
+
if (withTranslationId) {
|
|
274
|
+
return isCallExpressionWithHOC(defaultExport);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
exports.hasHOC = hasHOC;
|
|
283
|
+
function isNotExportModifier(modifier) {
|
|
284
|
+
var exportModifiers = [
|
|
285
|
+
typescript_1.default.SyntaxKind.DefaultKeyword,
|
|
286
|
+
typescript_1.default.SyntaxKind.ExportKeyword,
|
|
287
|
+
];
|
|
288
|
+
return !exportModifiers.includes(modifier.kind);
|
|
289
|
+
}
|
|
290
|
+
exports.isNotExportModifier = isNotExportModifier;
|
|
291
|
+
function interceptExport(filePkg, exportName, defaultLocalName) {
|
|
292
|
+
var exportContent = getNamedExport(filePkg, exportName, false);
|
|
293
|
+
var finalLocalName = '';
|
|
294
|
+
var extraImport;
|
|
295
|
+
if (!exportContent)
|
|
296
|
+
return finalLocalName;
|
|
297
|
+
filePkg.transform(function (sourceFile, context) {
|
|
298
|
+
function visitor(node) {
|
|
299
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
300
|
+
if (typescript_1.default.isClassDeclaration(node) && node === exportContent) {
|
|
301
|
+
if (node.name)
|
|
302
|
+
finalLocalName = node.name.getText();
|
|
303
|
+
return typescript_1.default.factory.updateClassDeclaration(node, node.decorators, (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.filter(isNotExportModifier), (_b = node.name) !== null && _b !== void 0 ? _b : typescript_1.default.factory.createIdentifier(defaultLocalName), node.typeParameters, node.heritageClauses, node.members);
|
|
304
|
+
}
|
|
305
|
+
if (typescript_1.default.isFunctionDeclaration(node) && node === exportContent) {
|
|
306
|
+
if (node.name)
|
|
307
|
+
finalLocalName = node.name.getText();
|
|
308
|
+
return typescript_1.default.factory.updateFunctionDeclaration(node, node.decorators, (_c = node.modifiers) === null || _c === void 0 ? void 0 : _c.filter(isNotExportModifier), node.asteriskToken, (_d = node.name) !== null && _d !== void 0 ? _d : typescript_1.default.factory.createIdentifier(defaultLocalName), node.typeParameters, node.parameters, node.type, node.body);
|
|
309
|
+
}
|
|
310
|
+
if (typescript_1.default.isVariableStatement(node) &&
|
|
311
|
+
typescript_1.default.isVariableDeclaration(exportContent) &&
|
|
312
|
+
node.declarationList.declarations.includes(exportContent)) {
|
|
313
|
+
finalLocalName = exportContent.name.getText();
|
|
314
|
+
return typescript_1.default.factory.updateVariableStatement(node, (_e = node.modifiers) === null || _e === void 0 ? void 0 : _e.filter(isNotExportModifier), node.declarationList);
|
|
315
|
+
}
|
|
316
|
+
if (typescript_1.default.isExportDeclaration(node) &&
|
|
317
|
+
typescript_1.default.isExportSpecifier(exportContent) &&
|
|
318
|
+
node.exportClause &&
|
|
319
|
+
typescript_1.default.isNamedExports(node.exportClause)) {
|
|
320
|
+
var filteredSpecifiers = node.exportClause.elements.filter(function (specifier) { return specifier !== exportContent; });
|
|
321
|
+
if (node.moduleSpecifier) {
|
|
322
|
+
finalLocalName = defaultLocalName;
|
|
323
|
+
extraImport = typescript_1.default.factory.createImportDeclaration(undefined, undefined, typescript_1.default.factory.createImportClause(node.isTypeOnly, undefined, typescript_1.default.factory.createNamedImports([
|
|
324
|
+
typescript_1.default.factory.createImportSpecifier(exportContent.isTypeOnly, (_f = exportContent.propertyName) !== null && _f !== void 0 ? _f : exportContent.name, typescript_1.default.factory.createIdentifier(defaultLocalName)),
|
|
325
|
+
])), node.moduleSpecifier);
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
var localId = (_g = exportContent.propertyName) !== null && _g !== void 0 ? _g : exportContent.name;
|
|
329
|
+
finalLocalName = localId.getText();
|
|
330
|
+
}
|
|
331
|
+
return typescript_1.default.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, typescript_1.default.factory.updateNamedExports(node.exportClause, filteredSpecifiers), node.moduleSpecifier, node.assertClause);
|
|
332
|
+
}
|
|
333
|
+
if (typescript_1.default.isExportAssignment(node) && node === exportContent) {
|
|
334
|
+
finalLocalName = defaultLocalName;
|
|
335
|
+
return typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([
|
|
336
|
+
typescript_1.default.factory.createVariableDeclaration(defaultLocalName, undefined, undefined, node.expression),
|
|
337
|
+
], typescript_1.default.NodeFlags.Const));
|
|
338
|
+
}
|
|
339
|
+
return typescript_1.default.visitEachChild(node, visitor, context);
|
|
340
|
+
}
|
|
341
|
+
return typescript_1.default.visitNode(sourceFile, visitor);
|
|
342
|
+
});
|
|
343
|
+
if (extraImport) {
|
|
344
|
+
filePkg.transform(function (sourceFile) {
|
|
345
|
+
return typescript_1.default.factory.updateSourceFile(sourceFile, __spreadArray([
|
|
346
|
+
extraImport
|
|
347
|
+
], sourceFile.statements, true));
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
return finalLocalName;
|
|
351
|
+
}
|
|
352
|
+
exports.interceptExport = interceptExport;
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
|
+
var t = {};
|
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
15
|
+
t[p] = s[p];
|
|
16
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
17
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
18
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
19
|
+
t[p[i]] = s[p[i]];
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
23
|
+
import fs from 'fs';
|
|
24
|
+
import path from 'path';
|
|
25
|
+
import { parseFile, getDefaultExport, hasStaticName, hasHOC } from './utils';
|
|
26
|
+
export default function nextTranslate(nextConfig) {
|
|
27
|
+
if (nextConfig === void 0) { nextConfig = {}; }
|
|
28
|
+
var test = /\.(tsx|ts|js|mjs|jsx)$/;
|
|
29
|
+
var basePath = pkgDir();
|
|
30
|
+
var possiblePageDirs = [
|
|
31
|
+
'pages',
|
|
32
|
+
'src/pages',
|
|
33
|
+
'app/pages',
|
|
34
|
+
'integrations/pages',
|
|
35
|
+
];
|
|
36
|
+
var translationDir = path.resolve(path.relative(basePath, process.env.NEXT_TRANSLATE_PATH || '.'));
|
|
37
|
+
var nextConfigI18n = nextConfig.i18n || {};
|
|
38
|
+
var _a = require(path.join(translationDir, 'i18n')), _b = _a.locales, locales = _b === void 0 ? nextConfigI18n.locales || [] : _b, _c = _a.defaultLocale, defaultLocale = _c === void 0 ? nextConfigI18n.defaultLocale || 'en' : _c, _d = _a.domains, domains = _d === void 0 ? nextConfigI18n.domains : _d, _e = _a.localeDetection, localeDetection = _e === void 0 ? nextConfigI18n.localeDetection : _e, _f = _a.loader, loader = _f === void 0 ? true : _f, pagesInDir = _a.pagesInDir, restI18n = __rest(_a, ["locales", "defaultLocale", "domains", "localeDetection", "loader", "pagesInDir"]);
|
|
39
|
+
var nextConfigWithI18n = __assign(__assign({}, nextConfig), { i18n: {
|
|
40
|
+
locales: locales,
|
|
41
|
+
defaultLocale: defaultLocale,
|
|
42
|
+
domains: domains,
|
|
43
|
+
localeDetection: localeDetection,
|
|
44
|
+
} });
|
|
45
|
+
var hasGetInitialPropsOnAppJs = false;
|
|
46
|
+
if (!pagesInDir) {
|
|
47
|
+
for (var _i = 0, possiblePageDirs_1 = possiblePageDirs; _i < possiblePageDirs_1.length; _i++) {
|
|
48
|
+
var possiblePageDir = possiblePageDirs_1[_i];
|
|
49
|
+
if (fs.existsSync(path.join(basePath, possiblePageDir))) {
|
|
50
|
+
pagesInDir = possiblePageDir;
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!pagesInDir || !fs.existsSync(path.join(basePath, pagesInDir))) {
|
|
56
|
+
return nextConfigWithI18n;
|
|
57
|
+
}
|
|
58
|
+
var pagesPath = path.join(basePath, pagesInDir);
|
|
59
|
+
var app = fs.readdirSync(pagesPath).find(function (page) { return page.startsWith('_app.'); });
|
|
60
|
+
if (app) {
|
|
61
|
+
var appPkg = parseFile(basePath, path.join(pagesPath, app));
|
|
62
|
+
var defaultExport = getDefaultExport(appPkg);
|
|
63
|
+
if (defaultExport) {
|
|
64
|
+
var isGetInitialProps = hasStaticName(appPkg, defaultExport, 'getInitialProps');
|
|
65
|
+
hasGetInitialPropsOnAppJs = isGetInitialProps || hasHOC(appPkg);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return __assign(__assign({}, nextConfigWithI18n), { webpack: function (conf, options) {
|
|
69
|
+
var config = typeof nextConfig.webpack === 'function'
|
|
70
|
+
? nextConfig.webpack(conf, options)
|
|
71
|
+
: conf;
|
|
72
|
+
if (!config.resolve)
|
|
73
|
+
config.resolve = {};
|
|
74
|
+
if (!config.module)
|
|
75
|
+
config.module = {};
|
|
76
|
+
if (!config.module.rules)
|
|
77
|
+
config.module.rules = [];
|
|
78
|
+
config.resolve.alias = __assign(__assign({}, (config.resolve.alias || {})), { '@next-translate-root': path.resolve(translationDir) });
|
|
79
|
+
if (!loader)
|
|
80
|
+
return config;
|
|
81
|
+
config.module.rules.push({
|
|
82
|
+
test: test,
|
|
83
|
+
use: {
|
|
84
|
+
loader: 'next-translate/plugin/loader',
|
|
85
|
+
options: {
|
|
86
|
+
basePath: basePath,
|
|
87
|
+
pagesPath: path.join(pagesPath, '/'),
|
|
88
|
+
hasAppJs: Boolean(app),
|
|
89
|
+
hasGetInitialPropsOnAppJs: hasGetInitialPropsOnAppJs,
|
|
90
|
+
hasLoadLocaleFrom: typeof restI18n.loadLocaleFrom === 'function',
|
|
91
|
+
extensionsRgx: restI18n.extensionsRgx || test,
|
|
92
|
+
revalidate: restI18n.revalidate || 0,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
return config;
|
|
97
|
+
} });
|
|
98
|
+
}
|
|
99
|
+
function pkgDir() {
|
|
100
|
+
try {
|
|
101
|
+
return require('pkg-dir').sync() || process.cwd();
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
return process.cwd();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import templateWithHoc from './templateWithHoc';
|
|
2
|
+
import templateWithLoader from './templateWithLoader';
|
|
3
|
+
import { parseFile, getDefaultAppJs, getDefaultExport, hasExportName, hasStaticName, isPageToIgnore, hasHOC, } from './utils';
|
|
4
|
+
export default function loader(rawCode) {
|
|
5
|
+
var _a = this.getOptions(), basePath = _a.basePath, pagesPath = _a.pagesPath, hasAppJs = _a.hasAppJs, hasGetInitialPropsOnAppJs = _a.hasGetInitialPropsOnAppJs, hasLoadLocaleFrom = _a.hasLoadLocaleFrom, extensionsRgx = _a.extensionsRgx, revalidate = _a.revalidate;
|
|
6
|
+
var normalizedPagesPath = pagesPath.replace(/\\/g, '/');
|
|
7
|
+
var normalizedResourcePath = this.resourcePath.replace(/\\/g, '/');
|
|
8
|
+
if (normalizedResourcePath.includes('node_modules/next/dist/pages/_app')) {
|
|
9
|
+
if (hasAppJs)
|
|
10
|
+
return rawCode;
|
|
11
|
+
return getDefaultAppJs(hasLoadLocaleFrom);
|
|
12
|
+
}
|
|
13
|
+
if (!normalizedResourcePath.startsWith(normalizedPagesPath))
|
|
14
|
+
return rawCode;
|
|
15
|
+
var page = normalizedResourcePath.replace(normalizedPagesPath, '/');
|
|
16
|
+
var pageNoExt = page.replace(extensionsRgx, '');
|
|
17
|
+
var pagePkg = parseFile(basePath, normalizedResourcePath);
|
|
18
|
+
var defaultExport = getDefaultExport(pagePkg);
|
|
19
|
+
if (!defaultExport)
|
|
20
|
+
return rawCode;
|
|
21
|
+
if (hasExportName(pagePkg, '__N_SSP') || hasExportName(pagePkg, '__N_SSG')) {
|
|
22
|
+
return rawCode;
|
|
23
|
+
}
|
|
24
|
+
if (hasGetInitialPropsOnAppJs) {
|
|
25
|
+
return pageNoExt === '/_app'
|
|
26
|
+
? templateWithHoc(pagePkg, { hasLoadLocaleFrom: hasLoadLocaleFrom })
|
|
27
|
+
: rawCode;
|
|
28
|
+
}
|
|
29
|
+
if (pageNoExt === '/_app') {
|
|
30
|
+
return templateWithHoc(pagePkg, {
|
|
31
|
+
skipInitialProps: true,
|
|
32
|
+
hasLoadLocaleFrom: hasLoadLocaleFrom,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
if (isPageToIgnore(page))
|
|
36
|
+
return rawCode;
|
|
37
|
+
var isWrapperWithExternalHOC = hasHOC(pagePkg);
|
|
38
|
+
var isDynamicPage = page.includes('[');
|
|
39
|
+
var isGetStaticProps = hasExportName(pagePkg, 'getStaticProps');
|
|
40
|
+
var isGetStaticPaths = hasExportName(pagePkg, 'getStaticPaths');
|
|
41
|
+
var isGetServerSideProps = hasExportName(pagePkg, 'getServerSideProps');
|
|
42
|
+
var isGetInitialProps = hasStaticName(pagePkg, defaultExport, 'getInitialProps');
|
|
43
|
+
var hasLoader = isGetStaticProps || isGetServerSideProps || isGetInitialProps;
|
|
44
|
+
if (isGetInitialProps || (!hasLoader && isWrapperWithExternalHOC)) {
|
|
45
|
+
return templateWithHoc(pagePkg, { hasLoadLocaleFrom: hasLoadLocaleFrom });
|
|
46
|
+
}
|
|
47
|
+
var loader = isGetServerSideProps || (!hasLoader && isDynamicPage && !isGetStaticPaths)
|
|
48
|
+
? 'getServerSideProps'
|
|
49
|
+
: 'getStaticProps';
|
|
50
|
+
return templateWithLoader(pagePkg, {
|
|
51
|
+
page: pageNoExt,
|
|
52
|
+
loader: loader,
|
|
53
|
+
hasLoadLocaleFrom: hasLoadLocaleFrom,
|
|
54
|
+
revalidate: revalidate,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { interceptExport, overwriteLoadLocales } from './utils';
|
|
2
|
+
export default function templateWithHoc(pagePkg, _a) {
|
|
3
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.skipInitialProps, skipInitialProps = _c === void 0 ? false : _c, _d = _b.hasLoadLocaleFrom, hasLoadLocaleFrom = _d === void 0 ? false : _d;
|
|
4
|
+
var hash = Date.now().toString(16);
|
|
5
|
+
var pageVariableName = interceptExport(pagePkg, 'default', "__Next_Translate__Page__".concat(hash, "__"));
|
|
6
|
+
var hasDefaultExport = Boolean(pageVariableName);
|
|
7
|
+
if (!hasDefaultExport)
|
|
8
|
+
return pagePkg.getCode();
|
|
9
|
+
return "\n import __i18nConfig from '@next-translate-root/i18n'\n import __appWithI18n from 'next-translate/appWithI18n'\n ".concat(pagePkg.getCode(), "\n export default __appWithI18n(").concat(pageVariableName, ", {\n ...__i18nConfig,\n isLoader: true,\n skipInitialProps: ").concat(skipInitialProps, ",\n ").concat(overwriteLoadLocales(hasLoadLocaleFrom), "\n });\n ");
|
|
10
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { interceptExport, overwriteLoadLocales } from './utils';
|
|
2
|
+
export default function templateWithLoader(pagePkg, _a) {
|
|
3
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.page, page = _c === void 0 ? '' : _c, _d = _b.loader, loader = _d === void 0 ? 'getStaticProps' : _d, _e = _b.revalidate, revalidate = _e === void 0 ? 0 : _e, _f = _b.hasLoadLocaleFrom, hasLoadLocaleFrom = _f === void 0 ? false : _f;
|
|
4
|
+
var hash = Date.now().toString(16);
|
|
5
|
+
var oldLoaderName = interceptExport(pagePkg, loader, "__Next_Translate_old_".concat(loader, "__").concat(hash, "__"));
|
|
6
|
+
var newLoaderName = "__Next_Translate__".concat(loader, "__").concat(hash, "__");
|
|
7
|
+
var hasLoader = Boolean(oldLoaderName);
|
|
8
|
+
return "\n import __i18nConfig from '@next-translate-root/i18n'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n ".concat(pagePkg.getCode(), "\n async function ").concat(newLoaderName, "(ctx) {\n ").concat(hasLoader ? "const res = await ".concat(oldLoaderName, "(ctx)") : '', "\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 ...__i18nConfig,\n pathname: '").concat(page, "',\n loaderName: '").concat(loader, "',\n ").concat(overwriteLoadLocales(hasLoadLocaleFrom), "\n }))\n }\n }\n }\n export { ").concat(newLoaderName, " as ").concat(loader, " }\n ");
|
|
9
|
+
}
|
package/lib/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/esm/utils.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
13
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
14
|
+
if (ar || !(i in from)) {
|
|
15
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
16
|
+
ar[i] = from[i];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
|
+
};
|
|
21
|
+
import ts from 'typescript';
|
|
22
|
+
var specFileOrFolderRgx = /(__mocks__|__tests__)|(\.(spec|test)\.(tsx|ts|js|jsx)$)/;
|
|
23
|
+
export var defaultLoader = '(l, n) => import(`@next-translate-root/locales/${l}/${n}`).then(m => m.default)';
|
|
24
|
+
export function getDefaultAppJs(hasLoadLocaleFrom) {
|
|
25
|
+
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 ");
|
|
26
|
+
}
|
|
27
|
+
export function overwriteLoadLocales(hasLoadLocaleFrom) {
|
|
28
|
+
if (hasLoadLocaleFrom)
|
|
29
|
+
return '';
|
|
30
|
+
return "loadLocaleFrom: ".concat(defaultLoader, ",");
|
|
31
|
+
}
|
|
32
|
+
export function getTsCompilerOptions(basePath, cutDependencies) {
|
|
33
|
+
if (cutDependencies === void 0) { cutDependencies = false; }
|
|
34
|
+
var options;
|
|
35
|
+
var configPath = ts.findConfigFile(basePath, ts.sys.fileExists, 'tsconfig.json');
|
|
36
|
+
if (!configPath) {
|
|
37
|
+
options = { allowJs: true };
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
var readConfigFileResult = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
41
|
+
var jsonConfig = readConfigFileResult.config;
|
|
42
|
+
var convertResult = ts.convertCompilerOptionsFromJson(jsonConfig.compilerOptions, basePath);
|
|
43
|
+
options = convertResult.options;
|
|
44
|
+
}
|
|
45
|
+
if (cutDependencies) {
|
|
46
|
+
options = __assign(__assign({}, options), { types: [], noResolve: true, noLib: true });
|
|
47
|
+
}
|
|
48
|
+
return options;
|
|
49
|
+
}
|
|
50
|
+
export function getFilePkg(program, filename) {
|
|
51
|
+
var checker = program.getTypeChecker();
|
|
52
|
+
var sourceFile = program.getSourceFile(filename);
|
|
53
|
+
var printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
|
|
54
|
+
var fileSymbol = checker.getSymbolAtLocation(sourceFile);
|
|
55
|
+
var filePkg;
|
|
56
|
+
function transform(transformer) {
|
|
57
|
+
var sourceFile = filePkg.sourceFile;
|
|
58
|
+
var transformationResult = ts.transform(sourceFile, [
|
|
59
|
+
function (context) { return function (sourceFile) { return transformer(sourceFile, context); }; },
|
|
60
|
+
]);
|
|
61
|
+
filePkg.sourceFile = transformationResult.transformed[0];
|
|
62
|
+
filePkg.fileSymbol = checker.getSymbolAtLocation(filePkg.sourceFile);
|
|
63
|
+
}
|
|
64
|
+
function getCode() {
|
|
65
|
+
var sourceFile = filePkg.sourceFile;
|
|
66
|
+
return printer.printNode(ts.EmitHint.Unspecified, sourceFile, sourceFile);
|
|
67
|
+
}
|
|
68
|
+
filePkg = { program: program, checker: checker, sourceFile: sourceFile, fileSymbol: fileSymbol, transform: transform, getCode: getCode };
|
|
69
|
+
return filePkg;
|
|
70
|
+
}
|
|
71
|
+
export function parseFile(basePath, filename) {
|
|
72
|
+
var options = getTsCompilerOptions(basePath, true);
|
|
73
|
+
var program = ts.createProgram([filename], options);
|
|
74
|
+
return getFilePkg(program, filename);
|
|
75
|
+
}
|
|
76
|
+
export function parseCode(format, code) {
|
|
77
|
+
var options = getTsCompilerOptions('/', true);
|
|
78
|
+
var host = ts.createCompilerHost(options);
|
|
79
|
+
var filename = "source.".concat(format);
|
|
80
|
+
host.getSourceFile = function (fileName) {
|
|
81
|
+
return ts.createSourceFile(fileName, code, ts.ScriptTarget.Latest);
|
|
82
|
+
};
|
|
83
|
+
var program = ts.createProgram([filename], options, host);
|
|
84
|
+
return getFilePkg(program, filename);
|
|
85
|
+
}
|
|
86
|
+
export function getSymbol(filePkg, node) {
|
|
87
|
+
var location = ts.isVariableDeclaration(node) ? node.name : node;
|
|
88
|
+
return filePkg.checker.getSymbolAtLocation(location);
|
|
89
|
+
}
|
|
90
|
+
export function getImportedNames(filePkg, moduleName) {
|
|
91
|
+
var importClause = filePkg.sourceFile.forEachChild(function (node) {
|
|
92
|
+
if (ts.isImportDeclaration(node)) {
|
|
93
|
+
if (node.moduleSpecifier.getText().slice(1, -1) === moduleName) {
|
|
94
|
+
return node.importClause;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
});
|
|
99
|
+
if (importClause) {
|
|
100
|
+
var exportedNamesToImported_1 = new Map();
|
|
101
|
+
if (importClause.name) {
|
|
102
|
+
exportedNamesToImported_1.set('default', importClause.name);
|
|
103
|
+
}
|
|
104
|
+
if (importClause.namedBindings) {
|
|
105
|
+
importClause.namedBindings.forEachChild(function (node) {
|
|
106
|
+
if (ts.isImportSpecifier(node)) {
|
|
107
|
+
if (node.propertyName) {
|
|
108
|
+
exportedNamesToImported_1.set(node.propertyName.getText(), node.name);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
exportedNamesToImported_1.set(node.name.getText(), node.name);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return exportedNamesToImported_1;
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
export function resolveParenthesis(filePkg, parenthesizedExpression) {
|
|
121
|
+
var content = parenthesizedExpression.expression;
|
|
122
|
+
if (ts.isParenthesizedExpression(content)) {
|
|
123
|
+
return resolveParenthesis(filePkg, content);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
return content;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export function resolveIdentifier(filePkg, identifier) {
|
|
130
|
+
var identifierSymbol = getSymbol(filePkg, identifier);
|
|
131
|
+
if (identifierSymbol && Array.isArray(identifierSymbol.declarations)) {
|
|
132
|
+
var identifierDeclaration = identifierSymbol.declarations[0];
|
|
133
|
+
if (ts.isVariableDeclaration(identifierDeclaration)) {
|
|
134
|
+
var initializer = identifierDeclaration.initializer;
|
|
135
|
+
if (initializer && ts.isParenthesizedExpression(initializer)) {
|
|
136
|
+
initializer = resolveParenthesis(filePkg, initializer);
|
|
137
|
+
}
|
|
138
|
+
if (initializer && ts.isIdentifier(initializer)) {
|
|
139
|
+
return resolveIdentifier(filePkg, initializer);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return identifierDeclaration;
|
|
143
|
+
}
|
|
144
|
+
return identifier;
|
|
145
|
+
}
|
|
146
|
+
export function getNamedExport(filePkg, name, resolveExport) {
|
|
147
|
+
if (resolveExport === void 0) { resolveExport = true; }
|
|
148
|
+
var checker = filePkg.checker, fileSymbol = filePkg.fileSymbol;
|
|
149
|
+
var exportContent;
|
|
150
|
+
if (fileSymbol) {
|
|
151
|
+
var exportSymbol = checker.tryGetMemberInModuleExports(name, fileSymbol);
|
|
152
|
+
if (exportSymbol && Array.isArray(exportSymbol.declarations)) {
|
|
153
|
+
var exportDeclaration = exportSymbol.declarations[0];
|
|
154
|
+
if (resolveExport && ts.isExportAssignment(exportDeclaration)) {
|
|
155
|
+
exportContent = exportDeclaration.expression;
|
|
156
|
+
if (ts.isParenthesizedExpression(exportContent)) {
|
|
157
|
+
exportContent = resolveParenthesis(filePkg, exportContent);
|
|
158
|
+
}
|
|
159
|
+
if (ts.isIdentifier(exportContent)) {
|
|
160
|
+
exportContent = resolveIdentifier(filePkg, exportContent);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
exportContent = exportDeclaration;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return exportContent;
|
|
169
|
+
}
|
|
170
|
+
export function getDefaultExport(filePkg, resolveExport) {
|
|
171
|
+
if (resolveExport === void 0) { resolveExport = true; }
|
|
172
|
+
return getNamedExport(filePkg, 'default', resolveExport);
|
|
173
|
+
}
|
|
174
|
+
export function hasExportName(filePkg, name) {
|
|
175
|
+
return Boolean(getNamedExport(filePkg, name, false));
|
|
176
|
+
}
|
|
177
|
+
export function getStaticName(filePkg, target, name) {
|
|
178
|
+
var symbol = getSymbol(filePkg, target);
|
|
179
|
+
if (symbol) {
|
|
180
|
+
return filePkg.checker.tryGetMemberInModuleExports(name, symbol);
|
|
181
|
+
}
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
export function hasStaticName(filePkg, target, name) {
|
|
185
|
+
return Boolean(getStaticName(filePkg, target, name));
|
|
186
|
+
}
|
|
187
|
+
export function isPageToIgnore(pageFilePath) {
|
|
188
|
+
var fileName = pageFilePath.substring(pageFilePath.lastIndexOf('/') + 1);
|
|
189
|
+
return (pageFilePath.startsWith('/api/') ||
|
|
190
|
+
pageFilePath.startsWith('/_document.') ||
|
|
191
|
+
pageFilePath.startsWith('/middleware.') ||
|
|
192
|
+
fileName.startsWith('_middleware.') ||
|
|
193
|
+
specFileOrFolderRgx.test(pageFilePath));
|
|
194
|
+
}
|
|
195
|
+
export function hasHOC(filePkg) {
|
|
196
|
+
var defaultExport = getDefaultExport(filePkg);
|
|
197
|
+
if (!defaultExport ||
|
|
198
|
+
hasExportName(filePkg, 'getStaticProps') ||
|
|
199
|
+
hasExportName(filePkg, 'getServerSideProps') ||
|
|
200
|
+
hasExportName(filePkg, 'getStaticPaths')) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
if (ts.isVariableDeclaration(defaultExport) && defaultExport.initializer) {
|
|
204
|
+
defaultExport = defaultExport.initializer;
|
|
205
|
+
if (ts.isParenthesizedExpression(defaultExport)) {
|
|
206
|
+
defaultExport = resolveParenthesis(filePkg, defaultExport);
|
|
207
|
+
}
|
|
208
|
+
if (ts.isIdentifier(defaultExport)) {
|
|
209
|
+
defaultExport = resolveIdentifier(filePkg, defaultExport);
|
|
210
|
+
}
|
|
211
|
+
if (!ts.isCallExpression(defaultExport)) {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (ts.isFunctionDeclaration(defaultExport) ||
|
|
216
|
+
ts.isClassDeclaration(defaultExport)) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
var importedNames = getImportedNames(filePkg, 'next-translate/withTranslation');
|
|
220
|
+
var withTranslationId = importedNames === null || importedNames === void 0 ? void 0 : importedNames.get('default');
|
|
221
|
+
function isCallExpressionWithHOC(callExpression) {
|
|
222
|
+
var _a;
|
|
223
|
+
var callable = callExpression.expression;
|
|
224
|
+
var expressionsToVisit = __spreadArray([callable], callExpression.arguments, true);
|
|
225
|
+
for (var _i = 0, expressionsToVisit_1 = expressionsToVisit; _i < expressionsToVisit_1.length; _i++) {
|
|
226
|
+
var expression = expressionsToVisit_1[_i];
|
|
227
|
+
if (ts.isCallExpression(expression)) {
|
|
228
|
+
return isCallExpressionWithHOC(expression);
|
|
229
|
+
}
|
|
230
|
+
if (ts.isIdentifier(expression)) {
|
|
231
|
+
var resolved = resolveIdentifier(filePkg, expression);
|
|
232
|
+
if (ts.isVariableDeclaration(resolved)) {
|
|
233
|
+
var initializer = resolved.initializer;
|
|
234
|
+
if (initializer && ts.isParenthesizedExpression(initializer)) {
|
|
235
|
+
initializer = resolveParenthesis(filePkg, initializer);
|
|
236
|
+
}
|
|
237
|
+
if (initializer && ts.isCallExpression(initializer)) {
|
|
238
|
+
return isCallExpressionWithHOC(initializer);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (ts.isImportClause(resolved)) {
|
|
242
|
+
if (((_a = resolved.name) === null || _a === void 0 ? void 0 : _a.getText()) !== (withTranslationId === null || withTranslationId === void 0 ? void 0 : withTranslationId.getText())) {
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
if (ts.isCallExpression(defaultExport)) {
|
|
251
|
+
if (withTranslationId) {
|
|
252
|
+
return isCallExpressionWithHOC(defaultExport);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
export function isNotExportModifier(modifier) {
|
|
261
|
+
var exportModifiers = [
|
|
262
|
+
ts.SyntaxKind.DefaultKeyword,
|
|
263
|
+
ts.SyntaxKind.ExportKeyword,
|
|
264
|
+
];
|
|
265
|
+
return !exportModifiers.includes(modifier.kind);
|
|
266
|
+
}
|
|
267
|
+
export function interceptExport(filePkg, exportName, defaultLocalName) {
|
|
268
|
+
var exportContent = getNamedExport(filePkg, exportName, false);
|
|
269
|
+
var finalLocalName = '';
|
|
270
|
+
var extraImport;
|
|
271
|
+
if (!exportContent)
|
|
272
|
+
return finalLocalName;
|
|
273
|
+
filePkg.transform(function (sourceFile, context) {
|
|
274
|
+
function visitor(node) {
|
|
275
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
276
|
+
if (ts.isClassDeclaration(node) && node === exportContent) {
|
|
277
|
+
if (node.name)
|
|
278
|
+
finalLocalName = node.name.getText();
|
|
279
|
+
return ts.factory.updateClassDeclaration(node, node.decorators, (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.filter(isNotExportModifier), (_b = node.name) !== null && _b !== void 0 ? _b : ts.factory.createIdentifier(defaultLocalName), node.typeParameters, node.heritageClauses, node.members);
|
|
280
|
+
}
|
|
281
|
+
if (ts.isFunctionDeclaration(node) && node === exportContent) {
|
|
282
|
+
if (node.name)
|
|
283
|
+
finalLocalName = node.name.getText();
|
|
284
|
+
return ts.factory.updateFunctionDeclaration(node, node.decorators, (_c = node.modifiers) === null || _c === void 0 ? void 0 : _c.filter(isNotExportModifier), node.asteriskToken, (_d = node.name) !== null && _d !== void 0 ? _d : ts.factory.createIdentifier(defaultLocalName), node.typeParameters, node.parameters, node.type, node.body);
|
|
285
|
+
}
|
|
286
|
+
if (ts.isVariableStatement(node) &&
|
|
287
|
+
ts.isVariableDeclaration(exportContent) &&
|
|
288
|
+
node.declarationList.declarations.includes(exportContent)) {
|
|
289
|
+
finalLocalName = exportContent.name.getText();
|
|
290
|
+
return ts.factory.updateVariableStatement(node, (_e = node.modifiers) === null || _e === void 0 ? void 0 : _e.filter(isNotExportModifier), node.declarationList);
|
|
291
|
+
}
|
|
292
|
+
if (ts.isExportDeclaration(node) &&
|
|
293
|
+
ts.isExportSpecifier(exportContent) &&
|
|
294
|
+
node.exportClause &&
|
|
295
|
+
ts.isNamedExports(node.exportClause)) {
|
|
296
|
+
var filteredSpecifiers = node.exportClause.elements.filter(function (specifier) { return specifier !== exportContent; });
|
|
297
|
+
if (node.moduleSpecifier) {
|
|
298
|
+
finalLocalName = defaultLocalName;
|
|
299
|
+
extraImport = ts.factory.createImportDeclaration(undefined, undefined, ts.factory.createImportClause(node.isTypeOnly, undefined, ts.factory.createNamedImports([
|
|
300
|
+
ts.factory.createImportSpecifier(exportContent.isTypeOnly, (_f = exportContent.propertyName) !== null && _f !== void 0 ? _f : exportContent.name, ts.factory.createIdentifier(defaultLocalName)),
|
|
301
|
+
])), node.moduleSpecifier);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
var localId = (_g = exportContent.propertyName) !== null && _g !== void 0 ? _g : exportContent.name;
|
|
305
|
+
finalLocalName = localId.getText();
|
|
306
|
+
}
|
|
307
|
+
return ts.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, ts.factory.updateNamedExports(node.exportClause, filteredSpecifiers), node.moduleSpecifier, node.assertClause);
|
|
308
|
+
}
|
|
309
|
+
if (ts.isExportAssignment(node) && node === exportContent) {
|
|
310
|
+
finalLocalName = defaultLocalName;
|
|
311
|
+
return ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
312
|
+
ts.factory.createVariableDeclaration(defaultLocalName, undefined, undefined, node.expression),
|
|
313
|
+
], ts.NodeFlags.Const));
|
|
314
|
+
}
|
|
315
|
+
return ts.visitEachChild(node, visitor, context);
|
|
316
|
+
}
|
|
317
|
+
return ts.visitNode(sourceFile, visitor);
|
|
318
|
+
});
|
|
319
|
+
if (extraImport) {
|
|
320
|
+
filePkg.transform(function (sourceFile) {
|
|
321
|
+
return ts.factory.updateSourceFile(sourceFile, __spreadArray([
|
|
322
|
+
extraImport
|
|
323
|
+
], sourceFile.statements, true));
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
return finalLocalName;
|
|
327
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "next-translate-plugin",
|
|
3
|
+
"version": "2.0.0-canary.1",
|
|
4
|
+
"description": "Tiny and powerful i18n plugin to translate your Next.js pages.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"react",
|
|
8
|
+
"preact",
|
|
9
|
+
"i18n",
|
|
10
|
+
"nextjs",
|
|
11
|
+
"next.js",
|
|
12
|
+
"next",
|
|
13
|
+
"plugin",
|
|
14
|
+
"translate",
|
|
15
|
+
"translation",
|
|
16
|
+
"internationalization",
|
|
17
|
+
"locale",
|
|
18
|
+
"locales",
|
|
19
|
+
"globalization"
|
|
20
|
+
],
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/aralroca/next-translate.git"
|
|
24
|
+
},
|
|
25
|
+
"author": {
|
|
26
|
+
"name": "Aral Roca Gòmez",
|
|
27
|
+
"email": "contact@aralroca.com"
|
|
28
|
+
},
|
|
29
|
+
"main": "./lib/cjs/index.js",
|
|
30
|
+
"module": "./lib/esm/index.js",
|
|
31
|
+
"types": "./index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"lib",
|
|
34
|
+
"index*"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "yarn clean && cross-env NODE_ENV=production && yarn tsc",
|
|
38
|
+
"clean": "yarn clean:build",
|
|
39
|
+
"clean:build": "rm -rf lib index *.d.ts",
|
|
40
|
+
"format": "pretty-quick",
|
|
41
|
+
"husky": "pretty-quick --staged && yarn test",
|
|
42
|
+
"prepare": "husky install",
|
|
43
|
+
"prepublish": "yarn test && yarn build",
|
|
44
|
+
"test": "cross-env NODE_ENV=test jest --env=jsdom",
|
|
45
|
+
"test:coverage": "cross-env NODE_ENV=test jest --env=jsdom --coverage",
|
|
46
|
+
"test:watch": "cross-env NODE_ENV=test jest --env=jsdom --watch",
|
|
47
|
+
"tsc": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"typescript": "4.5.2"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@babel/cli": "7.20.7",
|
|
54
|
+
"@babel/core": "7.20.12",
|
|
55
|
+
"@babel/preset-env": "7.20.2",
|
|
56
|
+
"@babel/preset-typescript": "7.18.6",
|
|
57
|
+
"@types/jest": "29.2.6",
|
|
58
|
+
"@types/node": "18.11.18",
|
|
59
|
+
"@types/react": "18.0.27",
|
|
60
|
+
"@types/react-dom": "^18.0.10",
|
|
61
|
+
"@types/webpack": "5.28.0",
|
|
62
|
+
"babel-jest": "29.3.1",
|
|
63
|
+
"babel-plugin-transform-es2015-modules-commonjs": "6.26.2",
|
|
64
|
+
"babel-preset-minify": "0.5.2",
|
|
65
|
+
"cross-env": "7.0.3",
|
|
66
|
+
"husky": "7.0.4",
|
|
67
|
+
"jest": "27.3.1",
|
|
68
|
+
"next": "13.1.2",
|
|
69
|
+
"next-translate": "2.0.0-canary.1",
|
|
70
|
+
"prettier": "2.8.3",
|
|
71
|
+
"pretty-quick": "3.1.3",
|
|
72
|
+
"react": "18.2.0",
|
|
73
|
+
"react-dom": "^18.2.0",
|
|
74
|
+
"supertest": "6.3.3"
|
|
75
|
+
},
|
|
76
|
+
"peerDependencies": {
|
|
77
|
+
"next-translate": ">= 2.0.0-canary.1"
|
|
78
|
+
},
|
|
79
|
+
"prettier": {
|
|
80
|
+
"trailingComma": "es5",
|
|
81
|
+
"tabWidth": 2,
|
|
82
|
+
"semi": false,
|
|
83
|
+
"singleQuote": true
|
|
84
|
+
},
|
|
85
|
+
"engines": {
|
|
86
|
+
"node": ">=14.15.0"
|
|
87
|
+
},
|
|
88
|
+
"jest": {
|
|
89
|
+
"roots": [
|
|
90
|
+
"<rootDir>/__tests__",
|
|
91
|
+
"<rootDir>/src"
|
|
92
|
+
],
|
|
93
|
+
"testPathIgnorePatterns": [
|
|
94
|
+
"/node_modules/",
|
|
95
|
+
".utils.js"
|
|
96
|
+
],
|
|
97
|
+
"transform": {
|
|
98
|
+
"^.+\\.(j|t)sx?$": "babel-jest"
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|