@bleedingdev/modern-js-i18n-integration 3.9.0-ultramodern.9 → 3.9.3-ultramodern.2

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.
@@ -1,182 +0,0 @@
1
- import { jsx } from "react/jsx-runtime";
2
- import { I18nNavigationProvider, useNativeI18nRouterAdapter } from "@modern-js/plugin-i18n/runtime/no-react-i18next";
3
- import { RuntimeContext, isBrowser } from "@modern-js/runtime";
4
- import { InternalRuntimeContext } from "@modern-js/runtime/context";
5
- import { getRouterRuntimeState } from "@modern-js/runtime-extensions/router-state";
6
- import { useCallback, useContext, useSyncExternalStore } from "react";
7
- const normalizeUrlPart = (value, prefix)=>{
8
- if ('string' != typeof value || !value) return '';
9
- return value.startsWith(prefix) ? value : `${prefix}${value}`;
10
- };
11
- const normalizeLocation = (location)=>{
12
- if (!location || 'object' != typeof location) return null;
13
- const locationValue = location;
14
- if ('string' != typeof locationValue.pathname) return null;
15
- return {
16
- pathname: locationValue.pathname,
17
- search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
18
- hash: normalizeUrlPart(locationValue.hash, '#')
19
- };
20
- };
21
- const getWindowLocation = ()=>{
22
- if (!isBrowser()) return null;
23
- return {
24
- pathname: window.location.pathname,
25
- search: window.location.search,
26
- hash: window.location.hash
27
- };
28
- };
29
- const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
30
- const framework = getRouterRuntimeState(internalContext)?.framework || getRouterRuntimeState(runtimeContext)?.framework;
31
- if (framework) return framework;
32
- if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
33
- if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
34
- if (inReactRouter) return 'react-router';
35
- };
36
- const getRouterInstance = (internalContext, contextRouter)=>{
37
- if (contextRouter) return contextRouter;
38
- const router = getRouterRuntimeState(internalContext)?.instance;
39
- if (!router || 'object' != typeof router) return null;
40
- return router;
41
- };
42
- const getRouterStateLocation = (internalContext, contextRouter)=>{
43
- const router = getRouterInstance(internalContext, contextRouter);
44
- return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
45
- };
46
- const getRouterParams = (internalContext, contextRouter)=>{
47
- const router = getRouterInstance(internalContext, contextRouter);
48
- const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
49
- if (!Array.isArray(matches)) return {};
50
- return matches.reduce((params, match)=>{
51
- if (match?.params) Object.assign(params, match.params);
52
- return params;
53
- }, {});
54
- };
55
- const getRouterSnapshot = (internalContext, contextRouter)=>{
56
- const location = getRouterStateLocation(internalContext, contextRouter);
57
- const params = getRouterParams(internalContext, contextRouter);
58
- return JSON.stringify([
59
- location?.pathname ?? '',
60
- location?.search ?? '',
61
- location?.hash ?? '',
62
- Object.entries(params).sort(([left], [right])=>left.localeCompare(right))
63
- ]);
64
- };
65
- const useIntegratedRouterAdapter = ()=>{
66
- const runtimeContext = useContext(RuntimeContext);
67
- const internalContext = useContext(InternalRuntimeContext);
68
- const routerApi = internalContext.router || runtimeContext.router;
69
- const native = useNativeI18nRouterAdapter();
70
- const inReactRouter = native.hasRouter;
71
- const reactRouterNavigate = native.navigate;
72
- const reactRouterLocation = inReactRouter ? native.location : null;
73
- const reactRouterParams = native.params;
74
- const framework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
75
- const contextUseRouter = inReactRouter || 'tanstack' !== framework ? void 0 : internalContext.router?.useRouter || runtimeContext.router?.useRouter;
76
- const contextRouter = contextUseRouter ? contextUseRouter({
77
- warn: false
78
- }) : null;
79
- const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
80
- const subscribeToRouter = useCallback((update)=>{
81
- const router = getRouterInstance(internalContext, contextRouter);
82
- if (!router) return ()=>void 0;
83
- const unsubscribers = [];
84
- if ('react-router' === framework && !inReactRouter && 'function' == typeof router.subscribe) {
85
- const subscribe = router.subscribe;
86
- const unsubscribe = subscribe.call(router, update);
87
- if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
88
- }
89
- if ('tanstack' === framework && 'function' == typeof router.stores?.location?.subscribe) {
90
- const unsubscribe = router.stores.location.subscribe(update);
91
- if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
92
- }
93
- if ('tanstack' === framework && 'function' == typeof router.subscribe) {
94
- const subscribe = router.subscribe;
95
- for (const eventType of [
96
- 'onBeforeNavigate',
97
- 'onBeforeLoad'
98
- ]){
99
- const unsubscribe = subscribe.call(router, eventType, update);
100
- if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
101
- }
102
- }
103
- return ()=>{
104
- for (const unsubscribe of unsubscribers)unsubscribe();
105
- };
106
- }, [
107
- contextRouter,
108
- framework,
109
- inReactRouter,
110
- internalContext
111
- ]);
112
- const getSnapshot = useCallback(()=>getRouterSnapshot(internalContext, contextRouter), [
113
- contextRouter,
114
- internalContext
115
- ]);
116
- useSyncExternalStore(subscribeToRouter, getSnapshot, getSnapshot);
117
- const navigate = useCallback((href, options)=>{
118
- const router = getRouterInstance(internalContext, contextRouter);
119
- const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
120
- if ('tanstack' === activeFramework) {
121
- if ('function' == typeof router?.navigate) return router.navigate({
122
- to: href,
123
- replace: options?.replace,
124
- ...options?.state === void 0 ? {} : {
125
- state: options.state
126
- }
127
- });
128
- throw new Error('TanStack router instance is not available.');
129
- }
130
- if (reactRouterNavigate) return reactRouterNavigate(href, options);
131
- if ('react-router' === activeFramework) {
132
- if ('function' == typeof router?.navigate) return router.navigate(href, options);
133
- throw new Error('React Router instance is not available.');
134
- }
135
- }, [
136
- contextRouter,
137
- internalContext,
138
- inReactRouter,
139
- reactRouterNavigate,
140
- runtimeContext
141
- ]);
142
- const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
143
- const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
144
- const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? routerApi?.Link ?? null : null;
145
- return {
146
- framework,
147
- hasRouter,
148
- location,
149
- navigate: hasRouter ? navigate : null,
150
- Link,
151
- params,
152
- ...'tanstack' === framework ? {
153
- createLinkProps: createTanstackLinkProps
154
- } : {}
155
- };
156
- };
157
- const createTanstackLinkProps = (target)=>{
158
- const preload = void 0 !== target.preload ? target.preload : 'none' === target.prefetch ? false : target.prefetch;
159
- return {
160
- to: target.pathname,
161
- ...target.search ? {
162
- search: target.search
163
- } : {},
164
- ...target.hash ? {
165
- hash: target.hash
166
- } : {},
167
- ...void 0 === target.hashScrollIntoView ? {} : {
168
- hashScrollIntoView: target.hashScrollIntoView
169
- },
170
- ...void 0 === preload ? {} : {
171
- preload
172
- }
173
- };
174
- };
175
- const I18nRouterNavigationProvider = ({ children })=>{
176
- const value = useIntegratedRouterAdapter();
177
- return /*#__PURE__*/ jsx(I18nNavigationProvider, {
178
- value: value,
179
- children: children
180
- });
181
- };
182
- export { I18nRouterNavigationProvider };
@@ -1,10 +0,0 @@
1
- import { getLocaleDetectionOptions } from "@modern-js/plugin-i18n/config";
2
- import { i18nServerPlugin } from "@modern-js/plugin-i18n/server";
3
- import { createI18nUrlStrategy } from "./urlStrategy.mjs";
4
- const server_i18nServerPlugin = (options)=>i18nServerPlugin({
5
- ...options,
6
- resolveUrlStrategy: options.resolveUrlStrategy ?? ((entryName)=>createI18nUrlStrategy(options.localeDetection ? getLocaleDetectionOptions(entryName, options.localeDetection).localisedUrls : void 0))
7
- });
8
- const server = server_i18nServerPlugin;
9
- export default server;
10
- export { server_i18nServerPlugin as i18nServerPlugin };
@@ -1,183 +0,0 @@
1
- import "node:module";
2
- import { jsx } from "react/jsx-runtime";
3
- import { I18nNavigationProvider, useNativeI18nRouterAdapter } from "@modern-js/plugin-i18n/runtime/no-react-i18next";
4
- import { RuntimeContext, isBrowser } from "@modern-js/runtime";
5
- import { InternalRuntimeContext } from "@modern-js/runtime/context";
6
- import { getRouterRuntimeState } from "@modern-js/runtime-extensions/router-state";
7
- import { useCallback, useContext, useSyncExternalStore } from "react";
8
- const normalizeUrlPart = (value, prefix)=>{
9
- if ('string' != typeof value || !value) return '';
10
- return value.startsWith(prefix) ? value : `${prefix}${value}`;
11
- };
12
- const normalizeLocation = (location)=>{
13
- if (!location || 'object' != typeof location) return null;
14
- const locationValue = location;
15
- if ('string' != typeof locationValue.pathname) return null;
16
- return {
17
- pathname: locationValue.pathname,
18
- search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
19
- hash: normalizeUrlPart(locationValue.hash, '#')
20
- };
21
- };
22
- const getWindowLocation = ()=>{
23
- if (!isBrowser()) return null;
24
- return {
25
- pathname: window.location.pathname,
26
- search: window.location.search,
27
- hash: window.location.hash
28
- };
29
- };
30
- const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
31
- const framework = getRouterRuntimeState(internalContext)?.framework || getRouterRuntimeState(runtimeContext)?.framework;
32
- if (framework) return framework;
33
- if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
34
- if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
35
- if (inReactRouter) return 'react-router';
36
- };
37
- const getRouterInstance = (internalContext, contextRouter)=>{
38
- if (contextRouter) return contextRouter;
39
- const router = getRouterRuntimeState(internalContext)?.instance;
40
- if (!router || 'object' != typeof router) return null;
41
- return router;
42
- };
43
- const getRouterStateLocation = (internalContext, contextRouter)=>{
44
- const router = getRouterInstance(internalContext, contextRouter);
45
- return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
46
- };
47
- const getRouterParams = (internalContext, contextRouter)=>{
48
- const router = getRouterInstance(internalContext, contextRouter);
49
- const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
50
- if (!Array.isArray(matches)) return {};
51
- return matches.reduce((params, match)=>{
52
- if (match?.params) Object.assign(params, match.params);
53
- return params;
54
- }, {});
55
- };
56
- const getRouterSnapshot = (internalContext, contextRouter)=>{
57
- const location = getRouterStateLocation(internalContext, contextRouter);
58
- const params = getRouterParams(internalContext, contextRouter);
59
- return JSON.stringify([
60
- location?.pathname ?? '',
61
- location?.search ?? '',
62
- location?.hash ?? '',
63
- Object.entries(params).sort(([left], [right])=>left.localeCompare(right))
64
- ]);
65
- };
66
- const useIntegratedRouterAdapter = ()=>{
67
- const runtimeContext = useContext(RuntimeContext);
68
- const internalContext = useContext(InternalRuntimeContext);
69
- const routerApi = internalContext.router || runtimeContext.router;
70
- const native = useNativeI18nRouterAdapter();
71
- const inReactRouter = native.hasRouter;
72
- const reactRouterNavigate = native.navigate;
73
- const reactRouterLocation = inReactRouter ? native.location : null;
74
- const reactRouterParams = native.params;
75
- const framework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
76
- const contextUseRouter = inReactRouter || 'tanstack' !== framework ? void 0 : internalContext.router?.useRouter || runtimeContext.router?.useRouter;
77
- const contextRouter = contextUseRouter ? contextUseRouter({
78
- warn: false
79
- }) : null;
80
- const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
81
- const subscribeToRouter = useCallback((update)=>{
82
- const router = getRouterInstance(internalContext, contextRouter);
83
- if (!router) return ()=>void 0;
84
- const unsubscribers = [];
85
- if ('react-router' === framework && !inReactRouter && 'function' == typeof router.subscribe) {
86
- const subscribe = router.subscribe;
87
- const unsubscribe = subscribe.call(router, update);
88
- if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
89
- }
90
- if ('tanstack' === framework && 'function' == typeof router.stores?.location?.subscribe) {
91
- const unsubscribe = router.stores.location.subscribe(update);
92
- if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
93
- }
94
- if ('tanstack' === framework && 'function' == typeof router.subscribe) {
95
- const subscribe = router.subscribe;
96
- for (const eventType of [
97
- 'onBeforeNavigate',
98
- 'onBeforeLoad'
99
- ]){
100
- const unsubscribe = subscribe.call(router, eventType, update);
101
- if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
102
- }
103
- }
104
- return ()=>{
105
- for (const unsubscribe of unsubscribers)unsubscribe();
106
- };
107
- }, [
108
- contextRouter,
109
- framework,
110
- inReactRouter,
111
- internalContext
112
- ]);
113
- const getSnapshot = useCallback(()=>getRouterSnapshot(internalContext, contextRouter), [
114
- contextRouter,
115
- internalContext
116
- ]);
117
- useSyncExternalStore(subscribeToRouter, getSnapshot, getSnapshot);
118
- const navigate = useCallback((href, options)=>{
119
- const router = getRouterInstance(internalContext, contextRouter);
120
- const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
121
- if ('tanstack' === activeFramework) {
122
- if ('function' == typeof router?.navigate) return router.navigate({
123
- to: href,
124
- replace: options?.replace,
125
- ...options?.state === void 0 ? {} : {
126
- state: options.state
127
- }
128
- });
129
- throw new Error('TanStack router instance is not available.');
130
- }
131
- if (reactRouterNavigate) return reactRouterNavigate(href, options);
132
- if ('react-router' === activeFramework) {
133
- if ('function' == typeof router?.navigate) return router.navigate(href, options);
134
- throw new Error('React Router instance is not available.');
135
- }
136
- }, [
137
- contextRouter,
138
- internalContext,
139
- inReactRouter,
140
- reactRouterNavigate,
141
- runtimeContext
142
- ]);
143
- const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
144
- const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
145
- const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? routerApi?.Link ?? null : null;
146
- return {
147
- framework,
148
- hasRouter,
149
- location,
150
- navigate: hasRouter ? navigate : null,
151
- Link,
152
- params,
153
- ...'tanstack' === framework ? {
154
- createLinkProps: createTanstackLinkProps
155
- } : {}
156
- };
157
- };
158
- const createTanstackLinkProps = (target)=>{
159
- const preload = void 0 !== target.preload ? target.preload : 'none' === target.prefetch ? false : target.prefetch;
160
- return {
161
- to: target.pathname,
162
- ...target.search ? {
163
- search: target.search
164
- } : {},
165
- ...target.hash ? {
166
- hash: target.hash
167
- } : {},
168
- ...void 0 === target.hashScrollIntoView ? {} : {
169
- hashScrollIntoView: target.hashScrollIntoView
170
- },
171
- ...void 0 === preload ? {} : {
172
- preload
173
- }
174
- };
175
- };
176
- const I18nRouterNavigationProvider = ({ children })=>{
177
- const value = useIntegratedRouterAdapter();
178
- return /*#__PURE__*/ jsx(I18nNavigationProvider, {
179
- value: value,
180
- children: children
181
- });
182
- };
183
- export { I18nRouterNavigationProvider };
@@ -1,11 +0,0 @@
1
- import "node:module";
2
- import { getLocaleDetectionOptions } from "@modern-js/plugin-i18n/config";
3
- import { i18nServerPlugin } from "@modern-js/plugin-i18n/server";
4
- import { createI18nUrlStrategy } from "./urlStrategy.mjs";
5
- const server_i18nServerPlugin = (options)=>i18nServerPlugin({
6
- ...options,
7
- resolveUrlStrategy: options.resolveUrlStrategy ?? ((entryName)=>createI18nUrlStrategy(options.localeDetection ? getLocaleDetectionOptions(entryName, options.localeDetection).localisedUrls : void 0))
8
- });
9
- const server = server_i18nServerPlugin;
10
- export default server;
11
- export { server_i18nServerPlugin as i18nServerPlugin };
@@ -1,3 +0,0 @@
1
- import type React from 'react';
2
- /** Supplies the actual selected router; native links still render its Link. */
3
- export declare const I18nRouterNavigationProvider: ({ children, }: React.PropsWithChildren) => React.JSX.Element;
@@ -1,3 +0,0 @@
1
- import type { I18nServerPluginOptions } from './options.js';
2
- export declare const i18nServerPlugin: (options: I18nServerPluginOptions) => import("@modern-js/server-core").ServerPlugin;
3
- export default i18nServerPlugin;