@modern-js/plugin-i18n 3.11.2 → 3.12.0

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.
Files changed (40) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/dist/cjs/cli/index.js +10 -0
  3. package/dist/cjs/runtime/i18n/detection/index.js +4 -4
  4. package/dist/cjs/runtime/i18n/detection/middleware.js +2 -2
  5. package/dist/cjs/runtime/i18n/detection/middleware.node.js +1 -1
  6. package/dist/cjs/runtime/i18n/instance.js +2 -24
  7. package/dist/cjs/runtime/i18n/react-i18next.js +58 -0
  8. package/dist/cjs/runtime/index.js +8 -3
  9. package/dist/cjs/runtime/utils.js +1 -1
  10. package/dist/cjs/shared/detection.js +1 -1
  11. package/dist/esm/cli/index.mjs +10 -0
  12. package/dist/esm/runtime/i18n/detection/index.mjs +4 -4
  13. package/dist/esm/runtime/i18n/detection/middleware.mjs +2 -2
  14. package/dist/esm/runtime/i18n/detection/middleware.node.mjs +1 -1
  15. package/dist/esm/runtime/i18n/instance.mjs +3 -19
  16. package/dist/esm/runtime/i18n/react-i18next.mjs +17 -0
  17. package/dist/esm/runtime/index.mjs +7 -2
  18. package/dist/esm/runtime/utils.mjs +1 -1
  19. package/dist/esm/shared/detection.mjs +1 -1
  20. package/dist/esm-node/cli/index.mjs +10 -0
  21. package/dist/esm-node/runtime/i18n/detection/index.mjs +4 -4
  22. package/dist/esm-node/runtime/i18n/detection/middleware.mjs +2 -2
  23. package/dist/esm-node/runtime/i18n/detection/middleware.node.mjs +1 -1
  24. package/dist/esm-node/runtime/i18n/instance.mjs +3 -19
  25. package/dist/esm-node/runtime/i18n/react-i18next.mjs +18 -0
  26. package/dist/esm-node/runtime/index.mjs +7 -2
  27. package/dist/esm-node/runtime/utils.mjs +1 -1
  28. package/dist/esm-node/shared/detection.mjs +1 -1
  29. package/dist/types/runtime/i18n/instance.d.ts +0 -2
  30. package/dist/types/runtime/i18n/react-i18next.d.ts +2 -0
  31. package/package.json +13 -13
  32. package/src/cli/index.ts +19 -1
  33. package/src/runtime/i18n/detection/index.ts +4 -4
  34. package/src/runtime/i18n/detection/middleware.node.ts +1 -1
  35. package/src/runtime/i18n/detection/middleware.ts +2 -2
  36. package/src/runtime/i18n/instance.ts +2 -20
  37. package/src/runtime/i18n/react-i18next.ts +20 -0
  38. package/src/runtime/index.tsx +8 -6
  39. package/src/runtime/utils.ts +1 -1
  40. package/src/shared/detection.ts +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @modern-js/plugin-i18n
2
2
 
3
+ ## 3.12.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [b35f295]
8
+ - Updated dependencies [67fc367]
9
+ - Updated dependencies [d378801]
10
+ - Updated dependencies [8bb2f02]
11
+ - @modern-js/utils@3.12.0
12
+ - @modern-js/types@3.12.0
13
+ - @modern-js/server-core@3.12.0
14
+ - @modern-js/plugin@3.12.0
15
+ - @modern-js/runtime-utils@3.12.0
16
+ - @modern-js/server-runtime@3.12.0
17
+
18
+ ## 3.11.3
19
+
20
+ ### Patch Changes
21
+
22
+ - 75631e4: fix(plugin-i18n): bundle the runtime's react-i18next import eagerly so it no longer adds an async chunk request before the first render.
23
+
24
+ fix(plugin-i18n): 运行时对 react-i18next 的动态引入改为打进当前 chunk,首屏渲染前不再多请求一个异步 chunk。
25
+
26
+ - 6e86aac: fix(plugin-i18n): register react-i18next once per i18n instance so server requests no longer grow its plugin list.
27
+
28
+ fix(plugin-i18n): 每个 i18n 实例只注册一次 react-i18next,避免服务端每个请求都让插件列表变长。
29
+
30
+ - Updated dependencies [4d498bb]
31
+ - @modern-js/plugin@3.11.3
32
+ - @modern-js/server-core@3.11.3
33
+ - @modern-js/server-runtime@3.11.3
34
+ - @modern-js/runtime-utils@3.11.3
35
+ - @modern-js/types@3.11.3
36
+ - @modern-js/utils@3.11.3
37
+
3
38
  ## 3.11.2
4
39
 
5
40
  ### Patch Changes
@@ -35,10 +35,20 @@ const server_core_namespaceObject = require("@modern-js/server-core");
35
35
  const utils_js_namespaceObject = require("../shared/utils.js");
36
36
  const external_locales_js_namespaceObject = require("./locales.js");
37
37
  require("../runtime/types.js");
38
+ const eagerReactI18next = (chain)=>{
39
+ chain.module.rule('plugin-i18n-react-i18next').test(/[\\/]plugin-i18n[\\/]dist[\\/][^\\/]+[\\/]runtime[\\/]i18n[\\/]react-i18next\.m?js$/).parser({
40
+ dynamicImportMode: 'eager'
41
+ });
42
+ };
38
43
  const i18nPlugin = (options = {})=>({
39
44
  name: '@modern-js/plugin-i18n',
40
45
  setup: (api)=>{
41
46
  const { localeDetection, backend, transformRuntimeConfig, customPlugin, ...restOptions } = options;
47
+ api.config(()=>({
48
+ tools: {
49
+ bundlerChain: eagerReactI18next
50
+ }
51
+ }));
42
52
  api._internalRuntimePlugins(({ entrypoint, plugins })=>{
43
53
  const localeDetectionOptions = localeDetection ? (0, utils_js_namespaceObject.getLocaleDetectionOptions)(entrypoint.entryName, localeDetection) : void 0;
44
54
  let backendOptions;
@@ -114,7 +114,7 @@ const getLanguageFromSSRData = (window1)=>{
114
114
  if (!ssrData || !ssrData.data || !ssrData.data.i18nData) return;
115
115
  const lng = ssrData.data.i18nData.lng;
116
116
  return 'string' == typeof lng && '' !== lng.trim() ? lng : void 0;
117
- } catch (error) {
117
+ } catch {
118
118
  return;
119
119
  }
120
120
  };
@@ -143,7 +143,7 @@ const detectLanguageFromSSR = (languages)=>{
143
143
  try {
144
144
  const ssrLanguage = getLanguageFromSSRData(window);
145
145
  if (ssrLanguage && isLanguageSupported(ssrLanguage, languages)) return ssrLanguage;
146
- } catch (error) {}
146
+ } catch {}
147
147
  };
148
148
  const detectLanguageFromPathPriority = (pathname, languages, localePathRedirect)=>{
149
149
  if (!localePathRedirect) return;
@@ -152,7 +152,7 @@ const detectLanguageFromPathPriority = (pathname, languages, localePathRedirect)
152
152
  try {
153
153
  const pathDetection = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(pathname, languages, localePathRedirect);
154
154
  if (true === pathDetection.detected && pathDetection.language) return pathDetection.language;
155
- } catch (error) {}
155
+ } catch {}
156
156
  };
157
157
  const createDetectorInstance = (baseInstance, configKey)=>{
158
158
  const cached = detectorInstanceCache.get(baseInstance);
@@ -236,7 +236,7 @@ const detectLanguageFromI18nextDetector = async (i18nInstance, options)=>{
236
236
  const currentLang = detectorInstance.language;
237
237
  if (isLanguageSupported(currentLang, options.languages)) return currentLang;
238
238
  }
239
- } catch (error) {} finally{
239
+ } catch {} finally{
240
240
  if (isTemporary && detectorInstance !== i18nInstance) detectorInstanceCache.set(i18nInstance, {
241
241
  instance: detectorInstance,
242
242
  isTemporary: true,
@@ -111,7 +111,7 @@ const readLanguageFromStorage = (detectionOptions, _request, _languages)=>{
111
111
  break;
112
112
  }
113
113
  }
114
- } catch (error) {}
114
+ } catch {}
115
115
  };
116
116
  const detectLanguage = (i18nInstance, _request, detectionOptions)=>{
117
117
  try {
@@ -142,7 +142,7 @@ const detectLanguage = (i18nInstance, _request, detectionOptions)=>{
142
142
  if (Array.isArray(result) && result.length > 0) return result[0];
143
143
  }
144
144
  }
145
- } catch (error) {}
145
+ } catch {}
146
146
  };
147
147
  const cacheUserLanguage = (i18nInstance, language, detectionOptions)=>{
148
148
  if ("u" < typeof window) return;
@@ -66,7 +66,7 @@ const detectLanguage = (i18nInstance, request, detectionOptions)=>{
66
66
  if ('string' == typeof result) return result;
67
67
  if (Array.isArray(result) && result.length > 0) return result[0];
68
68
  }
69
- } catch (error) {}
69
+ } catch {}
70
70
  };
71
71
  exports.cacheUserLanguage = __webpack_exports__.cacheUserLanguage;
72
72
  exports.detectLanguage = __webpack_exports__.detectLanguage;
@@ -54,7 +54,7 @@ async function tryImportI18next() {
54
54
  try {
55
55
  const i18next = await import("i18next");
56
56
  return i18next.default;
57
- } catch (error) {
57
+ } catch {
58
58
  return null;
59
59
  }
60
60
  }
@@ -65,7 +65,7 @@ async function createI18nextInstance() {
65
65
  return i18next.createInstance?.({
66
66
  initImmediate: false
67
67
  });
68
- } catch (error) {
68
+ } catch {
69
69
  return null;
70
70
  }
71
71
  }
@@ -85,29 +85,11 @@ async function getI18nInstance(userInstance) {
85
85
  if (i18nextInstance) return i18nextInstance;
86
86
  throw new Error('No i18n instance found');
87
87
  }
88
- async function getInitReactI18next() {
89
- try {
90
- const { initReactI18next } = await import("react-i18next");
91
- return initReactI18next;
92
- } catch (error) {
93
- return null;
94
- }
95
- }
96
- async function getI18nextProvider() {
97
- try {
98
- const { I18nextProvider } = await import("react-i18next");
99
- return I18nextProvider;
100
- } catch (error) {
101
- return null;
102
- }
103
- }
104
88
  __webpack_require__.d(__webpack_exports__, {}, {
105
89
  getActualI18nextInstance: getActualI18nextInstance,
106
90
  getI18nInstance: getI18nInstance,
107
91
  getI18nWrapperI18nextInstance: getI18nWrapperI18nextInstance,
108
92
  getI18nextInstanceForProvider: getI18nextInstanceForProvider,
109
- getI18nextProvider: getI18nextProvider,
110
- getInitReactI18next: getInitReactI18next,
111
93
  isI18nInstance: isI18nInstance,
112
94
  isI18nWrapperInstance: isI18nWrapperInstance
113
95
  });
@@ -115,8 +97,6 @@ exports.getActualI18nextInstance = __webpack_exports__.getActualI18nextInstance;
115
97
  exports.getI18nInstance = __webpack_exports__.getI18nInstance;
116
98
  exports.getI18nWrapperI18nextInstance = __webpack_exports__.getI18nWrapperI18nextInstance;
117
99
  exports.getI18nextInstanceForProvider = __webpack_exports__.getI18nextInstanceForProvider;
118
- exports.getI18nextProvider = __webpack_exports__.getI18nextProvider;
119
- exports.getInitReactI18next = __webpack_exports__.getInitReactI18next;
120
100
  exports.isI18nInstance = __webpack_exports__.isI18nInstance;
121
101
  exports.isI18nWrapperInstance = __webpack_exports__.isI18nWrapperInstance;
122
102
  for(var __rspack_i in __webpack_exports__)if (-1 === [
@@ -124,8 +104,6 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
124
104
  "getI18nInstance",
125
105
  "getI18nWrapperI18nextInstance",
126
106
  "getI18nextInstanceForProvider",
127
- "getI18nextProvider",
128
- "getInitReactI18next",
129
107
  "isI18nInstance",
130
108
  "isI18nWrapperInstance"
131
109
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, getters, values)=>{
5
+ var define = (defs, kind)=>{
6
+ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
7
+ enumerable: true,
8
+ [kind]: defs[key]
9
+ });
10
+ };
11
+ define(getters, "get");
12
+ define(values, "value");
13
+ };
14
+ })();
15
+ (()=>{
16
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
17
+ })();
18
+ (()=>{
19
+ __webpack_require__.r = (exports1)=>{
20
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
21
+ value: 'Module'
22
+ });
23
+ Object.defineProperty(exports1, '__esModule', {
24
+ value: true
25
+ });
26
+ };
27
+ })();
28
+ var __webpack_exports__ = {};
29
+ __webpack_require__.r(__webpack_exports__);
30
+ async function getInitReactI18next() {
31
+ try {
32
+ const { initReactI18next } = await import("react-i18next");
33
+ return initReactI18next;
34
+ } catch {
35
+ return null;
36
+ }
37
+ }
38
+ async function getI18nextProvider() {
39
+ try {
40
+ const { I18nextProvider } = await import("react-i18next");
41
+ return I18nextProvider;
42
+ } catch {
43
+ return null;
44
+ }
45
+ }
46
+ __webpack_require__.d(__webpack_exports__, {}, {
47
+ getI18nextProvider: getI18nextProvider,
48
+ getInitReactI18next: getInitReactI18next
49
+ });
50
+ exports.getI18nextProvider = __webpack_exports__.getI18nextProvider;
51
+ exports.getInitReactI18next = __webpack_exports__.getInitReactI18next;
52
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
53
+ "getI18nextProvider",
54
+ "getInitReactI18next"
55
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
56
+ Object.defineProperty(exports, '__esModule', {
57
+ value: true
58
+ });
@@ -46,10 +46,12 @@ const middleware_js_namespaceObject = require("./i18n/backend/middleware.js");
46
46
  const detection_index_js_namespaceObject = require("./i18n/detection/index.js");
47
47
  const detection_middleware_js_namespaceObject = require("./i18n/detection/middleware.js");
48
48
  const instance_js_namespaceObject = require("./i18n/instance.js");
49
+ const react_i18next_js_namespaceObject = require("./i18n/react-i18next.js");
49
50
  const utils_js_namespaceObject = require("./i18n/utils.js");
50
51
  const external_utils_js_namespaceObject = require("./utils.js");
51
52
  require("./types.js");
52
53
  const external_I18nLink_js_namespaceObject = require("./I18nLink.js");
54
+ const reactI18nextInstances = new WeakSet();
53
55
  const i18nPlugin = (options)=>({
54
56
  name: '@modern-js/plugin-i18n',
55
57
  setup: (api)=>{
@@ -62,9 +64,12 @@ const i18nPlugin = (options)=>({
62
64
  const { i18n: otherConfig } = api.getRuntimeConfig();
63
65
  const { initOptions: otherInitOptions } = otherConfig || {};
64
66
  const userInitOptions = (0, merge_namespaceObject.merge)(otherInitOptions || {}, initOptions || {});
65
- const initReactI18next = await (0, instance_js_namespaceObject.getInitReactI18next)();
66
- I18nextProvider = await (0, instance_js_namespaceObject.getI18nextProvider)();
67
- if (initReactI18next) i18nInstance.use(initReactI18next);
67
+ const initReactI18next = await (0, react_i18next_js_namespaceObject.getInitReactI18next)();
68
+ I18nextProvider = await (0, react_i18next_js_namespaceObject.getI18nextProvider)();
69
+ if (initReactI18next && !reactI18nextInstances.has(i18nInstance)) {
70
+ i18nInstance.use(initReactI18next);
71
+ reactI18nextInstances.add(i18nInstance);
72
+ }
68
73
  const pathname = (0, external_utils_js_namespaceObject.getPathname)(context);
69
74
  if (i18nextDetector) (0, detection_middleware_js_namespaceObject.useI18nextLanguageDetector)(i18nInstance);
70
75
  const mergedDetection = (0, detection_index_js_namespaceObject.mergeDetectionOptions)(i18nextDetector, detection, localePathRedirect, userInitOptions);
@@ -94,7 +94,7 @@ const useRouterHooks = ()=>{
94
94
  params: (0, router_namespaceObject.useParams)(),
95
95
  hasRouter: true
96
96
  };
97
- } catch (error) {
97
+ } catch {
98
98
  return {
99
99
  navigate: null,
100
100
  location: null,
@@ -111,7 +111,7 @@ function detectLanguageFromRequest(req, languages, detectionOptions) {
111
111
  if (languages.includes(baseLang)) return baseLang;
112
112
  }
113
113
  }
114
- } catch (error) {}
114
+ } catch {}
115
115
  return null;
116
116
  }
117
117
  exports.detectLanguageFromRequest = __webpack_exports__.detectLanguageFromRequest;
@@ -2,10 +2,20 @@ import { getPublicDirRoutePrefixes } from "@modern-js/server-core";
2
2
  import { getBackendOptions, getLocaleDetectionOptions } from "../shared/utils.mjs";
3
3
  import { applyDetectedBackendPaths, detectLocalesDirectory } from "./locales.mjs";
4
4
  import "../runtime/types.mjs";
5
+ const eagerReactI18next = (chain)=>{
6
+ chain.module.rule('plugin-i18n-react-i18next').test(/[\\/]plugin-i18n[\\/]dist[\\/][^\\/]+[\\/]runtime[\\/]i18n[\\/]react-i18next\.m?js$/).parser({
7
+ dynamicImportMode: 'eager'
8
+ });
9
+ };
5
10
  const i18nPlugin = (options = {})=>({
6
11
  name: '@modern-js/plugin-i18n',
7
12
  setup: (api)=>{
8
13
  const { localeDetection, backend, transformRuntimeConfig, customPlugin, ...restOptions } = options;
14
+ api.config(()=>({
15
+ tools: {
16
+ bundlerChain: eagerReactI18next
17
+ }
18
+ }));
9
19
  api._internalRuntimePlugins(({ entrypoint, plugins })=>{
10
20
  const localeDetectionOptions = localeDetection ? getLocaleDetectionOptions(entrypoint.entryName, localeDetection) : void 0;
11
21
  let backendOptions;
@@ -77,7 +77,7 @@ const getLanguageFromSSRData = (window1)=>{
77
77
  if (!ssrData || !ssrData.data || !ssrData.data.i18nData) return;
78
78
  const lng = ssrData.data.i18nData.lng;
79
79
  return 'string' == typeof lng && '' !== lng.trim() ? lng : void 0;
80
- } catch (error) {
80
+ } catch {
81
81
  return;
82
82
  }
83
83
  };
@@ -106,7 +106,7 @@ const detectLanguageFromSSR = (languages)=>{
106
106
  try {
107
107
  const ssrLanguage = getLanguageFromSSRData(window);
108
108
  if (ssrLanguage && isLanguageSupported(ssrLanguage, languages)) return ssrLanguage;
109
- } catch (error) {}
109
+ } catch {}
110
110
  };
111
111
  const detectLanguageFromPathPriority = (pathname, languages, localePathRedirect)=>{
112
112
  if (!localePathRedirect) return;
@@ -115,7 +115,7 @@ const detectLanguageFromPathPriority = (pathname, languages, localePathRedirect)
115
115
  try {
116
116
  const pathDetection = detectLanguageFromPath(pathname, languages, localePathRedirect);
117
117
  if (true === pathDetection.detected && pathDetection.language) return pathDetection.language;
118
- } catch (error) {}
118
+ } catch {}
119
119
  };
120
120
  const createDetectorInstance = (baseInstance, configKey)=>{
121
121
  const cached = detectorInstanceCache.get(baseInstance);
@@ -199,7 +199,7 @@ const detectLanguageFromI18nextDetector = async (i18nInstance, options)=>{
199
199
  const currentLang = detectorInstance.language;
200
200
  if (isLanguageSupported(currentLang, options.languages)) return currentLang;
201
201
  }
202
- } catch (error) {} finally{
202
+ } catch {} finally{
203
203
  if (isTemporary && detectorInstance !== i18nInstance) detectorInstanceCache.set(i18nInstance, {
204
204
  instance: detectorInstance,
205
205
  isTemporary: true,
@@ -66,7 +66,7 @@ const readLanguageFromStorage = (detectionOptions, _request, _languages)=>{
66
66
  break;
67
67
  }
68
68
  }
69
- } catch (error) {}
69
+ } catch {}
70
70
  };
71
71
  const detectLanguage = (i18nInstance, _request, detectionOptions)=>{
72
72
  try {
@@ -97,7 +97,7 @@ const detectLanguage = (i18nInstance, _request, detectionOptions)=>{
97
97
  if (Array.isArray(result) && result.length > 0) return result[0];
98
98
  }
99
99
  }
100
- } catch (error) {}
100
+ } catch {}
101
101
  };
102
102
  const cacheUserLanguage = (i18nInstance, language, detectionOptions)=>{
103
103
  if ("u" < typeof window) return;
@@ -31,6 +31,6 @@ const detectLanguage = (i18nInstance, request, detectionOptions)=>{
31
31
  if ('string' == typeof result) return result;
32
32
  if (Array.isArray(result) && result.length > 0) return result[0];
33
33
  }
34
- } catch (error) {}
34
+ } catch {}
35
35
  };
36
36
  export { cacheUserLanguage, detectLanguage, readLanguageFromStorage, useI18nextLanguageDetector };
@@ -25,7 +25,7 @@ async function tryImportI18next() {
25
25
  try {
26
26
  const i18next = await import("i18next");
27
27
  return i18next.default;
28
- } catch (error) {
28
+ } catch {
29
29
  return null;
30
30
  }
31
31
  }
@@ -36,7 +36,7 @@ async function createI18nextInstance() {
36
36
  return i18next.createInstance?.({
37
37
  initImmediate: false
38
38
  });
39
- } catch (error) {
39
+ } catch {
40
40
  return null;
41
41
  }
42
42
  }
@@ -56,20 +56,4 @@ async function getI18nInstance(userInstance) {
56
56
  if (i18nextInstance) return i18nextInstance;
57
57
  throw new Error('No i18n instance found');
58
58
  }
59
- async function getInitReactI18next() {
60
- try {
61
- const { initReactI18next } = await import("react-i18next");
62
- return initReactI18next;
63
- } catch (error) {
64
- return null;
65
- }
66
- }
67
- async function getI18nextProvider() {
68
- try {
69
- const { I18nextProvider } = await import("react-i18next");
70
- return I18nextProvider;
71
- } catch (error) {
72
- return null;
73
- }
74
- }
75
- export { getActualI18nextInstance, getI18nInstance, getI18nWrapperI18nextInstance, getI18nextInstanceForProvider, getI18nextProvider, getInitReactI18next, isI18nInstance, isI18nWrapperInstance };
59
+ export { getActualI18nextInstance, getI18nInstance, getI18nWrapperI18nextInstance, getI18nextInstanceForProvider, isI18nInstance, isI18nWrapperInstance };
@@ -0,0 +1,17 @@
1
+ async function getInitReactI18next() {
2
+ try {
3
+ const { initReactI18next } = await import("react-i18next");
4
+ return initReactI18next;
5
+ } catch {
6
+ return null;
7
+ }
8
+ }
9
+ async function getI18nextProvider() {
10
+ try {
11
+ const { I18nextProvider } = await import("react-i18next");
12
+ return I18nextProvider;
13
+ } catch {
14
+ return null;
15
+ }
16
+ }
17
+ export { getI18nextProvider, getInitReactI18next };
@@ -10,10 +10,12 @@ import { mergeBackendOptions } from "./i18n/backend/index.mjs";
10
10
  import { useI18nextBackend } from "./i18n/backend/middleware.mjs";
11
11
  import { detectLanguageWithPriority, exportServerLngToWindow, mergeDetectionOptions } from "./i18n/detection/index.mjs";
12
12
  import { useI18nextLanguageDetector } from "./i18n/detection/middleware.mjs";
13
- import { getI18nextInstanceForProvider, getI18nextProvider, getInitReactI18next } from "./i18n/instance.mjs";
13
+ import { getI18nextInstanceForProvider } from "./i18n/instance.mjs";
14
+ import { getI18nextProvider, getInitReactI18next } from "./i18n/react-i18next.mjs";
14
15
  import { changeI18nLanguage, ensureLanguageMatch, initializeI18nInstance, setupClonedInstance } from "./i18n/utils.mjs";
15
16
  import { getPathname } from "./utils.mjs";
16
17
  import "./types.mjs";
18
+ const reactI18nextInstances = new WeakSet();
17
19
  const i18nPlugin = (options)=>({
18
20
  name: '@modern-js/plugin-i18n',
19
21
  setup: (api)=>{
@@ -28,7 +30,10 @@ const i18nPlugin = (options)=>({
28
30
  const userInitOptions = merge(otherInitOptions || {}, initOptions || {});
29
31
  const initReactI18next = await getInitReactI18next();
30
32
  I18nextProvider = await getI18nextProvider();
31
- if (initReactI18next) i18nInstance.use(initReactI18next);
33
+ if (initReactI18next && !reactI18nextInstances.has(i18nInstance)) {
34
+ i18nInstance.use(initReactI18next);
35
+ reactI18nextInstances.add(i18nInstance);
36
+ }
32
37
  const pathname = getPathname(context);
33
38
  if (i18nextDetector) useI18nextLanguageDetector(i18nInstance);
34
39
  const mergedDetection = mergeDetectionOptions(i18nextDetector, detection, localePathRedirect, userInitOptions);
@@ -56,7 +56,7 @@ const useRouterHooks = ()=>{
56
56
  params: useParams(),
57
57
  hasRouter: true
58
58
  };
59
- } catch (error) {
59
+ } catch {
60
60
  return {
61
61
  navigate: null,
62
62
  location: null,
@@ -79,7 +79,7 @@ function detectLanguageFromRequest(req, languages, detectionOptions) {
79
79
  if (languages.includes(baseLang)) return baseLang;
80
80
  }
81
81
  }
82
- } catch (error) {}
82
+ } catch {}
83
83
  return null;
84
84
  }
85
85
  export { detectLanguageFromRequest };
@@ -3,10 +3,20 @@ import { getPublicDirRoutePrefixes } from "@modern-js/server-core";
3
3
  import { getBackendOptions, getLocaleDetectionOptions } from "../shared/utils.mjs";
4
4
  import { applyDetectedBackendPaths, detectLocalesDirectory } from "./locales.mjs";
5
5
  import "../runtime/types.mjs";
6
+ const eagerReactI18next = (chain)=>{
7
+ chain.module.rule('plugin-i18n-react-i18next').test(/[\\/]plugin-i18n[\\/]dist[\\/][^\\/]+[\\/]runtime[\\/]i18n[\\/]react-i18next\.m?js$/).parser({
8
+ dynamicImportMode: 'eager'
9
+ });
10
+ };
6
11
  const i18nPlugin = (options = {})=>({
7
12
  name: '@modern-js/plugin-i18n',
8
13
  setup: (api)=>{
9
14
  const { localeDetection, backend, transformRuntimeConfig, customPlugin, ...restOptions } = options;
15
+ api.config(()=>({
16
+ tools: {
17
+ bundlerChain: eagerReactI18next
18
+ }
19
+ }));
10
20
  api._internalRuntimePlugins(({ entrypoint, plugins })=>{
11
21
  const localeDetectionOptions = localeDetection ? getLocaleDetectionOptions(entrypoint.entryName, localeDetection) : void 0;
12
22
  let backendOptions;
@@ -78,7 +78,7 @@ const getLanguageFromSSRData = (window1)=>{
78
78
  if (!ssrData || !ssrData.data || !ssrData.data.i18nData) return;
79
79
  const lng = ssrData.data.i18nData.lng;
80
80
  return 'string' == typeof lng && '' !== lng.trim() ? lng : void 0;
81
- } catch (error) {
81
+ } catch {
82
82
  return;
83
83
  }
84
84
  };
@@ -107,7 +107,7 @@ const detectLanguageFromSSR = (languages)=>{
107
107
  try {
108
108
  const ssrLanguage = getLanguageFromSSRData(window);
109
109
  if (ssrLanguage && isLanguageSupported(ssrLanguage, languages)) return ssrLanguage;
110
- } catch (error) {}
110
+ } catch {}
111
111
  };
112
112
  const detectLanguageFromPathPriority = (pathname, languages, localePathRedirect)=>{
113
113
  if (!localePathRedirect) return;
@@ -116,7 +116,7 @@ const detectLanguageFromPathPriority = (pathname, languages, localePathRedirect)
116
116
  try {
117
117
  const pathDetection = detectLanguageFromPath(pathname, languages, localePathRedirect);
118
118
  if (true === pathDetection.detected && pathDetection.language) return pathDetection.language;
119
- } catch (error) {}
119
+ } catch {}
120
120
  };
121
121
  const createDetectorInstance = (baseInstance, configKey)=>{
122
122
  const cached = detectorInstanceCache.get(baseInstance);
@@ -200,7 +200,7 @@ const detectLanguageFromI18nextDetector = async (i18nInstance, options)=>{
200
200
  const currentLang = detectorInstance.language;
201
201
  if (isLanguageSupported(currentLang, options.languages)) return currentLang;
202
202
  }
203
- } catch (error) {} finally{
203
+ } catch {} finally{
204
204
  if (isTemporary && detectorInstance !== i18nInstance) detectorInstanceCache.set(i18nInstance, {
205
205
  instance: detectorInstance,
206
206
  isTemporary: true,
@@ -67,7 +67,7 @@ const readLanguageFromStorage = (detectionOptions, _request, _languages)=>{
67
67
  break;
68
68
  }
69
69
  }
70
- } catch (error) {}
70
+ } catch {}
71
71
  };
72
72
  const detectLanguage = (i18nInstance, _request, detectionOptions)=>{
73
73
  try {
@@ -98,7 +98,7 @@ const detectLanguage = (i18nInstance, _request, detectionOptions)=>{
98
98
  if (Array.isArray(result) && result.length > 0) return result[0];
99
99
  }
100
100
  }
101
- } catch (error) {}
101
+ } catch {}
102
102
  };
103
103
  const cacheUserLanguage = (i18nInstance, language, detectionOptions)=>{
104
104
  if ("u" < typeof window) return;
@@ -32,6 +32,6 @@ const detectLanguage = (i18nInstance, request, detectionOptions)=>{
32
32
  if ('string' == typeof result) return result;
33
33
  if (Array.isArray(result) && result.length > 0) return result[0];
34
34
  }
35
- } catch (error) {}
35
+ } catch {}
36
36
  };
37
37
  export { cacheUserLanguage, detectLanguage, readLanguageFromStorage, useI18nextLanguageDetector };
@@ -26,7 +26,7 @@ async function tryImportI18next() {
26
26
  try {
27
27
  const i18next = await import("i18next");
28
28
  return i18next.default;
29
- } catch (error) {
29
+ } catch {
30
30
  return null;
31
31
  }
32
32
  }
@@ -37,7 +37,7 @@ async function createI18nextInstance() {
37
37
  return i18next.createInstance?.({
38
38
  initImmediate: false
39
39
  });
40
- } catch (error) {
40
+ } catch {
41
41
  return null;
42
42
  }
43
43
  }
@@ -57,20 +57,4 @@ async function getI18nInstance(userInstance) {
57
57
  if (i18nextInstance) return i18nextInstance;
58
58
  throw new Error('No i18n instance found');
59
59
  }
60
- async function getInitReactI18next() {
61
- try {
62
- const { initReactI18next } = await import("react-i18next");
63
- return initReactI18next;
64
- } catch (error) {
65
- return null;
66
- }
67
- }
68
- async function getI18nextProvider() {
69
- try {
70
- const { I18nextProvider } = await import("react-i18next");
71
- return I18nextProvider;
72
- } catch (error) {
73
- return null;
74
- }
75
- }
76
- export { getActualI18nextInstance, getI18nInstance, getI18nWrapperI18nextInstance, getI18nextInstanceForProvider, getI18nextProvider, getInitReactI18next, isI18nInstance, isI18nWrapperInstance };
60
+ export { getActualI18nextInstance, getI18nInstance, getI18nWrapperI18nextInstance, getI18nextInstanceForProvider, isI18nInstance, isI18nWrapperInstance };
@@ -0,0 +1,18 @@
1
+ import "node:module";
2
+ async function getInitReactI18next() {
3
+ try {
4
+ const { initReactI18next } = await import("react-i18next");
5
+ return initReactI18next;
6
+ } catch {
7
+ return null;
8
+ }
9
+ }
10
+ async function getI18nextProvider() {
11
+ try {
12
+ const { I18nextProvider } = await import("react-i18next");
13
+ return I18nextProvider;
14
+ } catch {
15
+ return null;
16
+ }
17
+ }
18
+ export { getI18nextProvider, getInitReactI18next };
@@ -11,10 +11,12 @@ import { mergeBackendOptions } from "./i18n/backend/index.mjs";
11
11
  import { useI18nextBackend } from "./i18n/backend/middleware.mjs";
12
12
  import { detectLanguageWithPriority, exportServerLngToWindow, mergeDetectionOptions } from "./i18n/detection/index.mjs";
13
13
  import { useI18nextLanguageDetector } from "./i18n/detection/middleware.mjs";
14
- import { getI18nextInstanceForProvider, getI18nextProvider, getInitReactI18next } from "./i18n/instance.mjs";
14
+ import { getI18nextInstanceForProvider } from "./i18n/instance.mjs";
15
+ import { getI18nextProvider, getInitReactI18next } from "./i18n/react-i18next.mjs";
15
16
  import { changeI18nLanguage, ensureLanguageMatch, initializeI18nInstance, setupClonedInstance } from "./i18n/utils.mjs";
16
17
  import { getPathname } from "./utils.mjs";
17
18
  import "./types.mjs";
19
+ const reactI18nextInstances = new WeakSet();
18
20
  const i18nPlugin = (options)=>({
19
21
  name: '@modern-js/plugin-i18n',
20
22
  setup: (api)=>{
@@ -29,7 +31,10 @@ const i18nPlugin = (options)=>({
29
31
  const userInitOptions = merge(otherInitOptions || {}, initOptions || {});
30
32
  const initReactI18next = await getInitReactI18next();
31
33
  I18nextProvider = await getI18nextProvider();
32
- if (initReactI18next) i18nInstance.use(initReactI18next);
34
+ if (initReactI18next && !reactI18nextInstances.has(i18nInstance)) {
35
+ i18nInstance.use(initReactI18next);
36
+ reactI18nextInstances.add(i18nInstance);
37
+ }
33
38
  const pathname = getPathname(context);
34
39
  if (i18nextDetector) useI18nextLanguageDetector(i18nInstance);
35
40
  const mergedDetection = mergeDetectionOptions(i18nextDetector, detection, localePathRedirect, userInitOptions);
@@ -57,7 +57,7 @@ const useRouterHooks = ()=>{
57
57
  params: useParams(),
58
58
  hasRouter: true
59
59
  };
60
- } catch (error) {
60
+ } catch {
61
61
  return {
62
62
  navigate: null,
63
63
  location: null,
@@ -80,7 +80,7 @@ function detectLanguageFromRequest(req, languages, detectionOptions) {
80
80
  if (languages.includes(baseLang)) return baseLang;
81
81
  }
82
82
  }
83
- } catch (error) {}
83
+ } catch {}
84
84
  return null;
85
85
  }
86
86
  export { detectLanguageFromRequest };
@@ -88,6 +88,4 @@ export type I18nInitOptions = {
88
88
  export declare function isI18nInstance(obj: any): obj is I18nInstance;
89
89
  export declare function getI18nextInstanceForProvider(instance: I18nInstance | any): any;
90
90
  export declare function getI18nInstance(userInstance?: I18nInstance | any): Promise<I18nInstance>;
91
- export declare function getInitReactI18next(): Promise<import("i18next").ThirdPartyModule | null>;
92
- export declare function getI18nextProvider(): Promise<import("react").FunctionComponent<import("react-i18next").I18nextProviderProps> | null>;
93
91
  export {};
@@ -0,0 +1,2 @@
1
+ export declare function getInitReactI18next(): Promise<import("i18next").ThirdPartyModule | null>;
2
+ export declare function getI18nextProvider(): Promise<import("react").FunctionComponent<import("react-i18next").I18nextProviderProps> | null>;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "3.11.2",
18
+ "version": "3.12.0",
19
19
  "engines": {
20
20
  "node": ">=20"
21
21
  },
@@ -82,22 +82,22 @@
82
82
  "@swc/helpers": "^0.5.17",
83
83
  "i18next-browser-languagedetector": "^8.3.0",
84
84
  "i18next-chained-backend": "^4.6.3",
85
- "i18next-fs-backend": "^2.6.9",
86
- "i18next-http-backend": "^4.0.9",
85
+ "i18next-fs-backend": "^2.6.12",
86
+ "i18next-http-backend": "^4.0.10",
87
87
  "i18next-http-middleware": "^3.10.0",
88
- "@modern-js/runtime-utils": "3.11.2",
89
- "@modern-js/plugin": "3.11.2",
90
- "@modern-js/utils": "3.11.2",
91
- "@modern-js/types": "3.11.2",
92
- "@modern-js/server-runtime": "3.11.2",
93
- "@modern-js/server-core": "3.11.2"
88
+ "@modern-js/plugin": "3.12.0",
89
+ "@modern-js/server-runtime": "3.12.0",
90
+ "@modern-js/runtime-utils": "3.12.0",
91
+ "@modern-js/server-core": "3.12.0",
92
+ "@modern-js/types": "3.12.0",
93
+ "@modern-js/utils": "3.12.0"
94
94
  },
95
95
  "peerDependencies": {
96
96
  "react": ">=18.3.1",
97
97
  "react-dom": ">=18.3.1",
98
98
  "i18next": ">=25.10.10",
99
99
  "react-i18next": ">=15.7.4",
100
- "@modern-js/runtime": "^3.11.2"
100
+ "@modern-js/runtime": "^3.12.0"
101
101
  },
102
102
  "peerDependenciesMeta": {
103
103
  "i18next": {
@@ -115,9 +115,9 @@
115
115
  "react-i18next": "15.7.4",
116
116
  "ts-node": "^10.9.2",
117
117
  "typescript": "^5",
118
- "@modern-js/app-tools": "3.11.2",
119
- "@modern-js/rslib": "3.11.2",
120
- "@modern-js/runtime": "3.11.2"
118
+ "@modern-js/app-tools": "3.12.0",
119
+ "@modern-js/rslib": "3.12.0",
120
+ "@modern-js/runtime": "3.12.0"
121
121
  },
122
122
  "sideEffects": false,
123
123
  "publishConfig": {
package/src/cli/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AppTools, CliPlugin } from '@modern-js/app-tools';
1
+ import type { AppTools, AppUserConfig, CliPlugin } from '@modern-js/app-tools';
2
2
  import { getPublicDirRoutePrefixes } from '@modern-js/server-core';
3
3
  import type { Entrypoint } from '@modern-js/types';
4
4
  import type { BackendOptions, LocaleDetectionOptions } from '../shared/type';
@@ -6,6 +6,22 @@ import { getBackendOptions, getLocaleDetectionOptions } from '../shared/utils';
6
6
  import { applyDetectedBackendPaths, detectLocalesDirectory } from './locales';
7
7
  import '../runtime/types';
8
8
 
9
+ type BundlerChain = NonNullable<AppUserConfig['tools']>['bundlerChain'];
10
+
11
+ // Before the first render the runtime loads react-i18next with a dynamic import.
12
+ // Bundle that module eagerly so it doesn't become an extra async request. Only
13
+ // runtime/i18n/react-i18next is matched: the i18next fallback import in
14
+ // runtime/i18n/instance stays lazy, so apps that pass their own instance don't
15
+ // get an unused copy of i18next in the initial chunk.
16
+ const eagerReactI18next: BundlerChain = chain => {
17
+ chain.module
18
+ .rule('plugin-i18n-react-i18next')
19
+ .test(
20
+ /[\\/]plugin-i18n[\\/]dist[\\/][^\\/]+[\\/]runtime[\\/]i18n[\\/]react-i18next\.m?js$/,
21
+ )
22
+ .parser({ dynamicImportMode: 'eager' });
23
+ };
24
+
9
25
  export type TransformRuntimeConfigFn = (
10
26
  extendedConfig: Record<string, any>,
11
27
  entrypoint: Entrypoint,
@@ -40,6 +56,8 @@ export const i18nPlugin = (
40
56
  ...restOptions
41
57
  } = options;
42
58
 
59
+ api.config(() => ({ tools: { bundlerChain: eagerReactI18next } }));
60
+
43
61
  api._internalRuntimePlugins(({ entrypoint, plugins }) => {
44
62
  const localeDetectionOptions = localeDetection
45
63
  ? getLocaleDetectionOptions(entrypoint.entryName, localeDetection)
@@ -144,7 +144,7 @@ export const getLanguageFromSSRData = (window: Window): string | undefined => {
144
144
  const lng = ssrData.data.i18nData.lng;
145
145
  // Return language only if it's a non-empty string
146
146
  return typeof lng === 'string' && lng.trim() !== '' ? lng : undefined;
147
- } catch (error) {
147
+ } catch {
148
148
  // If accessing window._SSR_DATA throws an error, return undefined
149
149
  return undefined;
150
150
  }
@@ -250,7 +250,7 @@ const detectLanguageFromSSR = (languages: string[]): string | undefined => {
250
250
  if (ssrLanguage && isLanguageSupported(ssrLanguage, languages)) {
251
251
  return ssrLanguage;
252
252
  }
253
- } catch (error) {
253
+ } catch {
254
254
  // Silently ignore errors
255
255
  }
256
256
 
@@ -290,7 +290,7 @@ const detectLanguageFromPathPriority = (
290
290
  if (pathDetection.detected === true && pathDetection.language) {
291
291
  return pathDetection.language;
292
292
  }
293
- } catch (error) {
293
+ } catch {
294
294
  // Silently ignore errors, return undefined
295
295
  }
296
296
 
@@ -466,7 +466,7 @@ const detectLanguageFromI18nextDetector = async (
466
466
  return currentLang;
467
467
  }
468
468
  }
469
- } catch (error) {
469
+ } catch {
470
470
  // Silently ignore errors
471
471
  } finally {
472
472
  // Clean up temporary instance to avoid affecting subsequent formal initialization
@@ -83,7 +83,7 @@ export const detectLanguage = (
83
83
  }
84
84
  return undefined;
85
85
  }
86
- } catch (error) {
86
+ } catch {
87
87
  return undefined;
88
88
  }
89
89
 
@@ -101,7 +101,7 @@ export const readLanguageFromStorage = (
101
101
  }
102
102
  }
103
103
  }
104
- } catch (error) {
104
+ } catch {
105
105
  // Ignore errors
106
106
  }
107
107
 
@@ -174,7 +174,7 @@ export const detectLanguage = (
174
174
  }
175
175
  }
176
176
  }
177
- } catch (error) {
177
+ } catch {
178
178
  return undefined;
179
179
  }
180
180
 
@@ -148,7 +148,7 @@ async function tryImportI18next(): Promise<I18nInstance | null> {
148
148
  try {
149
149
  const i18next = await import('i18next');
150
150
  return i18next.default as unknown as I18nInstance;
151
- } catch (error) {
151
+ } catch {
152
152
  return null;
153
153
  }
154
154
  }
@@ -162,7 +162,7 @@ async function createI18nextInstance(): Promise<I18nInstance | null> {
162
162
  return i18next.createInstance?.({
163
163
  initImmediate: false,
164
164
  }) as unknown as I18nInstance;
165
- } catch (error) {
165
+ } catch {
166
166
  return null;
167
167
  }
168
168
  }
@@ -200,21 +200,3 @@ export async function getI18nInstance(
200
200
 
201
201
  throw new Error('No i18n instance found');
202
202
  }
203
-
204
- export async function getInitReactI18next() {
205
- try {
206
- const { initReactI18next } = await import('react-i18next');
207
- return initReactI18next;
208
- } catch (error) {
209
- return null;
210
- }
211
- }
212
-
213
- export async function getI18nextProvider() {
214
- try {
215
- const { I18nextProvider } = await import('react-i18next');
216
- return I18nextProvider;
217
- } catch (error) {
218
- return null;
219
- }
220
- }
@@ -0,0 +1,20 @@
1
+ // Kept in its own module so the CLI plugin can bundle only react-i18next
2
+ // eagerly. instance.ts also imports i18next dynamically as a fallback, which
3
+ // must stay lazy for apps that pass their own i18n instance.
4
+ export async function getInitReactI18next() {
5
+ try {
6
+ const { initReactI18next } = await import('react-i18next');
7
+ return initReactI18next;
8
+ } catch {
9
+ return null;
10
+ }
11
+ }
12
+
13
+ export async function getI18nextProvider() {
14
+ try {
15
+ const { I18nextProvider } = await import('react-i18next');
16
+ return I18nextProvider;
17
+ } catch {
18
+ return null;
19
+ }
20
+ }
@@ -29,11 +29,8 @@ import {
29
29
  mergeDetectionOptions,
30
30
  } from './i18n/detection';
31
31
  import { useI18nextLanguageDetector } from './i18n/detection/middleware';
32
- import {
33
- getI18nextInstanceForProvider,
34
- getI18nextProvider,
35
- getInitReactI18next,
36
- } from './i18n/instance';
32
+ import { getI18nextInstanceForProvider } from './i18n/instance';
33
+ import { getI18nextProvider, getInitReactI18next } from './i18n/react-i18next';
37
34
  import {
38
35
  changeI18nLanguage,
39
36
  ensureLanguageMatch,
@@ -62,6 +59,10 @@ interface RuntimeContextWithI18n extends TInternalRuntimeContext {
62
59
  changeLanguage?: (lang: string) => Promise<void>;
63
60
  }
64
61
 
62
+ // i18next appends third-party plugins on every `use()`, and the base instance
63
+ // is shared across server requests, so register react-i18next only once.
64
+ const reactI18nextInstances = new WeakSet<object>();
65
+
65
66
  export const i18nPlugin = (options: I18nPluginOptions): RuntimePlugin => ({
66
67
  name: '@modern-js/plugin-i18n',
67
68
  setup: api => {
@@ -91,8 +92,9 @@ export const i18nPlugin = (options: I18nPluginOptions): RuntimePlugin => ({
91
92
  const userInitOptions = merge(otherInitOptions || {}, initOptions || {});
92
93
  const initReactI18next = await getInitReactI18next();
93
94
  I18nextProvider = await getI18nextProvider();
94
- if (initReactI18next) {
95
+ if (initReactI18next && !reactI18nextInstances.has(i18nInstance)) {
95
96
  i18nInstance.use(initReactI18next);
97
+ reactI18nextInstances.add(i18nInstance);
96
98
  }
97
99
 
98
100
  const pathname = getPathname(context);
@@ -148,7 +148,7 @@ export const useRouterHooks = () => {
148
148
  params: useParams(),
149
149
  hasRouter: true,
150
150
  };
151
- } catch (error) {
151
+ } catch {
152
152
  return {
153
153
  navigate: null,
154
154
  location: null,
@@ -147,7 +147,7 @@ export function detectLanguageFromRequest(
147
147
  }
148
148
  }
149
149
  }
150
- } catch (error) {
150
+ } catch {
151
151
  // Silently ignore errors
152
152
  }
153
153