next-intlayer 8.4.3 → 8.4.5
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/dist/cjs/_virtual/_rolldown/runtime.cjs +29 -1
- package/dist/cjs/client/IntlayerClientProvider.cjs +36 -1
- package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -1
- package/dist/cjs/client/format/index.cjs +9 -1
- package/dist/cjs/client/index.cjs +16 -1
- package/dist/cjs/client/useLocale.cjs +71 -1
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/client/useLocalePageRouter.cjs +45 -1
- package/dist/cjs/client/useLocalePageRouter.cjs.map +1 -1
- package/dist/cjs/client/useRewriteURL.cjs +12 -1
- package/dist/cjs/generateStaticParams.cjs +29 -1
- package/dist/cjs/generateStaticParams.cjs.map +1 -1
- package/dist/cjs/index.cjs +132 -1
- package/dist/cjs/proxy/index.cjs +8 -1
- package/dist/cjs/proxy/intlayerProxy.cjs +249 -1
- package/dist/cjs/proxy/intlayerProxy.cjs.map +1 -1
- package/dist/cjs/proxy/localeDetector.cjs +20 -1
- package/dist/cjs/proxy/localeDetector.cjs.map +1 -1
- package/dist/cjs/proxy/middleware.cjs +37 -1
- package/dist/cjs/proxy/middleware.cjs.map +1 -1
- package/dist/cjs/proxy/multipleProxies.cjs +57 -1
- package/dist/cjs/proxy/multipleProxies.cjs.map +1 -1
- package/dist/cjs/server/format/index.cjs +9 -1
- package/dist/cjs/server/getLocale.cjs +48 -1
- package/dist/cjs/server/getLocale.cjs.map +1 -1
- package/dist/cjs/server/index.cjs +75 -1
- package/dist/cjs/server/useDictionary.cjs +17 -1
- package/dist/cjs/server/useDictionary.cjs.map +1 -1
- package/dist/cjs/server/useDictionaryAsync.cjs +17 -1
- package/dist/cjs/server/useDictionaryAsync.cjs.map +1 -1
- package/dist/cjs/server/useDictionaryDynamic.cjs +17 -1
- package/dist/cjs/server/useDictionaryDynamic.cjs.map +1 -1
- package/dist/cjs/server/useIntlayer.cjs +29 -1
- package/dist/cjs/server/useIntlayer.cjs.map +1 -1
- package/dist/cjs/server/withIntlayer.cjs +258 -1
- package/dist/cjs/server/withIntlayer.cjs.map +1 -1
- package/dist/esm/client/IntlayerClientProvider.mjs +34 -1
- package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -1
- package/dist/esm/client/format/index.mjs +1 -1
- package/dist/esm/client/index.mjs +6 -1
- package/dist/esm/client/useLocale.mjs +69 -1
- package/dist/esm/client/useLocale.mjs.map +1 -1
- package/dist/esm/client/useLocalePageRouter.mjs +43 -1
- package/dist/esm/client/useLocalePageRouter.mjs.map +1 -1
- package/dist/esm/client/useRewriteURL.mjs +5 -1
- package/dist/esm/generateStaticParams.mjs +26 -1
- package/dist/esm/generateStaticParams.mjs.map +1 -1
- package/dist/esm/index.mjs +8 -1
- package/dist/esm/proxy/index.mjs +5 -1
- package/dist/esm/proxy/intlayerProxy.mjs +246 -1
- package/dist/esm/proxy/intlayerProxy.mjs.map +1 -1
- package/dist/esm/proxy/localeDetector.mjs +18 -1
- package/dist/esm/proxy/localeDetector.mjs.map +1 -1
- package/dist/esm/proxy/middleware.mjs +35 -1
- package/dist/esm/proxy/middleware.mjs.map +1 -1
- package/dist/esm/proxy/multipleProxies.mjs +55 -1
- package/dist/esm/proxy/multipleProxies.mjs.map +1 -1
- package/dist/esm/server/format/index.mjs +1 -1
- package/dist/esm/server/getLocale.mjs +45 -1
- package/dist/esm/server/getLocale.mjs.map +1 -1
- package/dist/esm/server/index.mjs +5 -1
- package/dist/esm/server/useDictionary.mjs +15 -1
- package/dist/esm/server/useDictionary.mjs.map +1 -1
- package/dist/esm/server/useDictionaryAsync.mjs +15 -1
- package/dist/esm/server/useDictionaryAsync.mjs.map +1 -1
- package/dist/esm/server/useDictionaryDynamic.mjs +15 -1
- package/dist/esm/server/useDictionaryDynamic.mjs.map +1 -1
- package/dist/esm/server/useIntlayer.mjs +25 -1
- package/dist/esm/server/useIntlayer.mjs.map +1 -1
- package/dist/esm/server/withIntlayer.mjs +253 -1
- package/dist/esm/server/withIntlayer.mjs.map +1 -1
- package/dist/types/types/NextPage.d.ts +0 -3
- package/dist/types/types/NextPage.d.ts.map +1 -1
- package/package.json +8 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localeDetector.mjs","names":["localeDetectorCore"],"sources":["../../../src/proxy/localeDetector.ts"],"sourcesContent":["import { localeDetector as localeDetectorCore } from '@intlayer/core/localization';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport type { NextRequest } from 'next/server.js';\n\n/**\n * Detects the locale from the request headers\n *\n * Headers are provided by the browser and can be used to determine the user's preferred language\n */\nexport const localeDetector = (request: NextRequest): LocalesValues => {\n const negotiatorHeaders: Record<string, string> = {};\n\n request.headers.forEach((value, key) => {\n negotiatorHeaders[key] = value;\n });\n\n const locale = localeDetectorCore(negotiatorHeaders);\n\n return locale;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"localeDetector.mjs","names":["localeDetectorCore"],"sources":["../../../src/proxy/localeDetector.ts"],"sourcesContent":["import { localeDetector as localeDetectorCore } from '@intlayer/core/localization';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport type { NextRequest } from 'next/server.js';\n\n/**\n * Detects the locale from the request headers\n *\n * Headers are provided by the browser and can be used to determine the user's preferred language\n */\nexport const localeDetector = (request: NextRequest): LocalesValues => {\n const negotiatorHeaders: Record<string, string> = {};\n\n request.headers.forEach((value, key) => {\n negotiatorHeaders[key] = value;\n });\n\n const locale = localeDetectorCore(negotiatorHeaders);\n\n return locale;\n};\n"],"mappings":";;;;;;;;AASA,MAAa,kBAAkB,YAAwC;CACrE,MAAM,oBAA4C,EAAE;AAEpD,SAAQ,QAAQ,SAAS,OAAO,QAAQ;AACtC,oBAAkB,OAAO;GACzB;AAIF,QAFeA,iBAAmB,kBAAkB"}
|
|
@@ -1,2 +1,36 @@
|
|
|
1
|
-
import{intlayerProxy
|
|
1
|
+
import { intlayerProxy } from "./intlayerProxy.mjs";
|
|
2
|
+
import { multipleProxies } from "./multipleProxies.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/proxy/middleware.ts
|
|
5
|
+
/**
|
|
6
|
+
* Reexport for nextjs <=15
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Middleware that handles the internationalization layer
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* // ./src/middleware.ts
|
|
15
|
+
*
|
|
16
|
+
* export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';
|
|
17
|
+
*
|
|
18
|
+
* // applies this middleware only to files in the app directory
|
|
19
|
+
* export const config = {
|
|
20
|
+
* matcher: '/((?!api|static|.*\\..*|_next).*)',
|
|
21
|
+
* };
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* Main middleware function for handling internationalization.
|
|
25
|
+
*
|
|
26
|
+
* @param request - The incoming Next.js request object.
|
|
27
|
+
* @param event - The Next.js fetch event (optional).
|
|
28
|
+
* @param response - The Next.js response object (optional).
|
|
29
|
+
* @returns - The response to be returned to the client.
|
|
30
|
+
*/
|
|
31
|
+
const intlayerMiddleware = intlayerProxy;
|
|
32
|
+
const multipleMiddlewares = multipleProxies;
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { intlayerMiddleware, multipleMiddlewares };
|
|
2
36
|
//# sourceMappingURL=middleware.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.mjs","names":[],"sources":["../../../src/proxy/middleware.ts"],"sourcesContent":["import { intlayerProxy } from './intlayerProxy';\nimport { multipleProxies } from './multipleProxies';\n\n/**\n * Reexport for nextjs <=15\n */\n\n/**\n * Middleware that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/middleware.ts\n *\n * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';\n *\n * // applies this middleware only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main middleware function for handling internationalization.\n *\n * @param request - The incoming Next.js request object.\n * @param event - The Next.js fetch event (optional).\n * @param response - The Next.js response object (optional).\n * @returns - The response to be returned to the client.\n */\nexport const intlayerMiddleware = intlayerProxy;\n\nexport const multipleMiddlewares = multipleProxies;\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"middleware.mjs","names":[],"sources":["../../../src/proxy/middleware.ts"],"sourcesContent":["import { intlayerProxy } from './intlayerProxy';\nimport { multipleProxies } from './multipleProxies';\n\n/**\n * Reexport for nextjs <=15\n */\n\n/**\n * Middleware that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/middleware.ts\n *\n * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';\n *\n * // applies this middleware only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main middleware function for handling internationalization.\n *\n * @param request - The incoming Next.js request object.\n * @param event - The Next.js fetch event (optional).\n * @param response - The Next.js response object (optional).\n * @returns - The response to be returned to the client.\n */\nexport const intlayerMiddleware = intlayerProxy;\n\nexport const multipleMiddlewares = multipleProxies;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,MAAa,qBAAqB;AAElC,MAAa,sBAAsB"}
|
|
@@ -1,2 +1,56 @@
|
|
|
1
|
-
import{NextResponse
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
//#region src/proxy/multipleProxies.ts
|
|
4
|
+
/**
|
|
5
|
+
* Utility to combine multiple Next.js proxies into one.
|
|
6
|
+
*
|
|
7
|
+
* It executes proxies in order, merges headers, and correctly handles
|
|
8
|
+
* redirects and rewrites.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import { multipleProxies, intlayerProxy } from "next-intlayer/proxy";
|
|
12
|
+
* import { NextResponse } from "next/server";
|
|
13
|
+
*
|
|
14
|
+
* const authMiddleware = (req: NextRequest) => {
|
|
15
|
+
* if (!req.cookies.get("token")) {
|
|
16
|
+
* return NextResponse.redirect(new URL("/login", req.url));
|
|
17
|
+
* }
|
|
18
|
+
* return NextResponse.next();
|
|
19
|
+
* };
|
|
20
|
+
*
|
|
21
|
+
* export default multipleProxies([
|
|
22
|
+
* intlayerProxy,
|
|
23
|
+
* authMiddleware,
|
|
24
|
+
* ]);
|
|
25
|
+
*
|
|
26
|
+
* @param proxies - An array of proxy functions to execute in order.
|
|
27
|
+
* @returns A single proxy function that runs all provided proxies.
|
|
28
|
+
*/
|
|
29
|
+
const multipleProxies = (proxies) => async (req, event, response) => {
|
|
30
|
+
const proxyHeader = [];
|
|
31
|
+
for (const proxy of proxies) {
|
|
32
|
+
const result = await proxy(req, event, response);
|
|
33
|
+
if (!result.ok) return result;
|
|
34
|
+
proxyHeader.push(result.headers);
|
|
35
|
+
}
|
|
36
|
+
const mergedHeaders = new Headers();
|
|
37
|
+
const transmittedHeaders = new Headers(req.headers);
|
|
38
|
+
proxyHeader.forEach((header) => {
|
|
39
|
+
for (const [key, value] of header.entries()) {
|
|
40
|
+
mergedHeaders.append(key, value);
|
|
41
|
+
if (key.startsWith("x-middleware-request-")) {
|
|
42
|
+
const fixedKey = key.replace("x-middleware-request-", "");
|
|
43
|
+
transmittedHeaders.append(fixedKey, value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
const redirect = mergedHeaders.get("x-middleware-request-redirect");
|
|
48
|
+
if (redirect) return NextResponse.redirect(new URL(redirect, req.url), { status: 307 });
|
|
49
|
+
const rewrite = mergedHeaders.get("x-middleware-rewrite");
|
|
50
|
+
if (rewrite) return NextResponse.rewrite(new URL(rewrite, req.url), { request: { headers: transmittedHeaders } });
|
|
51
|
+
return NextResponse.next({ request: { headers: transmittedHeaders } });
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
export { multipleProxies };
|
|
2
56
|
//# sourceMappingURL=multipleProxies.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multipleProxies.mjs","names":[],"sources":["../../../src/proxy/multipleProxies.ts"],"sourcesContent":["import {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\n\n/**\n * Utility to combine multiple Next.js proxies into one.\n *\n * It executes proxies in order, merges headers, and correctly handles\n * redirects and rewrites.\n *\n * @example\n * import { multipleProxies, intlayerProxy } from \"next-intlayer/proxy\";\n * import { NextResponse } from \"next/server\";\n *\n * const authMiddleware = (req: NextRequest) => {\n * if (!req.cookies.get(\"token\")) {\n * return NextResponse.redirect(new URL(\"/login\", req.url));\n * }\n * return NextResponse.next();\n * };\n *\n * export default multipleProxies([\n * intlayerProxy,\n * authMiddleware,\n * ]);\n *\n * @param proxies - An array of proxy functions to execute in order.\n * @returns A single proxy function that runs all provided proxies.\n */\nexport const multipleProxies =\n (\n proxies: ((\n req: NextRequest,\n event?: NextFetchEvent,\n response?: NextResponse\n ) => NextResponse | Promise<NextResponse>)[]\n ) =>\n async (req: NextRequest, event?: NextFetchEvent, response?: NextResponse) => {\n // Array to store proxy headers\n const proxyHeader: Headers[] = [];\n\n // Loop through proxy functions\n for (const proxy of proxies) {\n // Execute proxy function and await the result\n const result = await proxy(req, event, response);\n\n // Check if the result is not okay and return it\n if (!result.ok) {\n return result;\n }\n\n // Push proxy headers to the array\n proxyHeader.push(result.headers);\n }\n\n // Merge all the headers to check if there is a redirection or rewrite\n const mergedHeaders = new Headers();\n\n // Merge all the custom headers added by the proxies\n const transmittedHeaders = new Headers(req.headers);\n\n // Merge headers\n proxyHeader.forEach((header) => {\n for (const [key, value] of header.entries()) {\n mergedHeaders.append(key, value);\n\n // check if it's a custom header added by one of the proxies\n if (key.startsWith('x-middleware-request-')) {\n // remove the prefix to get the original key\n const fixedKey = key.replace('x-middleware-request-', '');\n\n // add the original key to the transmitted headers\n transmittedHeaders.append(fixedKey, value);\n }\n }\n });\n\n // Look for the 'x-middleware-request-redirect' header\n const redirect = mergedHeaders.get('x-middleware-request-redirect');\n\n // If a redirection is required based on the proxy headers\n if (redirect) {\n // Perform the redirection\n return NextResponse.redirect(new URL(redirect, req.url), {\n status: 307, // Temporary redirect\n });\n }\n\n // Look for the 'x-middleware-rewrite' header\n const rewrite = mergedHeaders.get('x-middleware-rewrite');\n if (rewrite) {\n // Perform the rewrite\n return NextResponse.rewrite(new URL(rewrite, req.url), {\n request: {\n headers: transmittedHeaders,\n },\n });\n }\n\n // Default: continue to next proxy\n return NextResponse.next({\n request: {\n headers: transmittedHeaders,\n },\n });\n };\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"multipleProxies.mjs","names":[],"sources":["../../../src/proxy/multipleProxies.ts"],"sourcesContent":["import {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\n\n/**\n * Utility to combine multiple Next.js proxies into one.\n *\n * It executes proxies in order, merges headers, and correctly handles\n * redirects and rewrites.\n *\n * @example\n * import { multipleProxies, intlayerProxy } from \"next-intlayer/proxy\";\n * import { NextResponse } from \"next/server\";\n *\n * const authMiddleware = (req: NextRequest) => {\n * if (!req.cookies.get(\"token\")) {\n * return NextResponse.redirect(new URL(\"/login\", req.url));\n * }\n * return NextResponse.next();\n * };\n *\n * export default multipleProxies([\n * intlayerProxy,\n * authMiddleware,\n * ]);\n *\n * @param proxies - An array of proxy functions to execute in order.\n * @returns A single proxy function that runs all provided proxies.\n */\nexport const multipleProxies =\n (\n proxies: ((\n req: NextRequest,\n event?: NextFetchEvent,\n response?: NextResponse\n ) => NextResponse | Promise<NextResponse>)[]\n ) =>\n async (req: NextRequest, event?: NextFetchEvent, response?: NextResponse) => {\n // Array to store proxy headers\n const proxyHeader: Headers[] = [];\n\n // Loop through proxy functions\n for (const proxy of proxies) {\n // Execute proxy function and await the result\n const result = await proxy(req, event, response);\n\n // Check if the result is not okay and return it\n if (!result.ok) {\n return result;\n }\n\n // Push proxy headers to the array\n proxyHeader.push(result.headers);\n }\n\n // Merge all the headers to check if there is a redirection or rewrite\n const mergedHeaders = new Headers();\n\n // Merge all the custom headers added by the proxies\n const transmittedHeaders = new Headers(req.headers);\n\n // Merge headers\n proxyHeader.forEach((header) => {\n for (const [key, value] of header.entries()) {\n mergedHeaders.append(key, value);\n\n // check if it's a custom header added by one of the proxies\n if (key.startsWith('x-middleware-request-')) {\n // remove the prefix to get the original key\n const fixedKey = key.replace('x-middleware-request-', '');\n\n // add the original key to the transmitted headers\n transmittedHeaders.append(fixedKey, value);\n }\n }\n });\n\n // Look for the 'x-middleware-request-redirect' header\n const redirect = mergedHeaders.get('x-middleware-request-redirect');\n\n // If a redirection is required based on the proxy headers\n if (redirect) {\n // Perform the redirection\n return NextResponse.redirect(new URL(redirect, req.url), {\n status: 307, // Temporary redirect\n });\n }\n\n // Look for the 'x-middleware-rewrite' header\n const rewrite = mergedHeaders.get('x-middleware-rewrite');\n if (rewrite) {\n // Perform the rewrite\n return NextResponse.rewrite(new URL(rewrite, req.url), {\n request: {\n headers: transmittedHeaders,\n },\n });\n }\n\n // Default: continue to next proxy\n return NextResponse.next({\n request: {\n headers: transmittedHeaders,\n },\n });\n };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAa,mBAET,YAMF,OAAO,KAAkB,OAAwB,aAA4B;CAE3E,MAAM,cAAyB,EAAE;AAGjC,MAAK,MAAM,SAAS,SAAS;EAE3B,MAAM,SAAS,MAAM,MAAM,KAAK,OAAO,SAAS;AAGhD,MAAI,CAAC,OAAO,GACV,QAAO;AAIT,cAAY,KAAK,OAAO,QAAQ;;CAIlC,MAAM,gBAAgB,IAAI,SAAS;CAGnC,MAAM,qBAAqB,IAAI,QAAQ,IAAI,QAAQ;AAGnD,aAAY,SAAS,WAAW;AAC9B,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,SAAS,EAAE;AAC3C,iBAAc,OAAO,KAAK,MAAM;AAGhC,OAAI,IAAI,WAAW,wBAAwB,EAAE;IAE3C,MAAM,WAAW,IAAI,QAAQ,yBAAyB,GAAG;AAGzD,uBAAmB,OAAO,UAAU,MAAM;;;GAG9C;CAGF,MAAM,WAAW,cAAc,IAAI,gCAAgC;AAGnE,KAAI,SAEF,QAAO,aAAa,SAAS,IAAI,IAAI,UAAU,IAAI,IAAI,EAAE,EACvD,QAAQ,KACT,CAAC;CAIJ,MAAM,UAAU,cAAc,IAAI,uBAAuB;AACzD,KAAI,QAEF,QAAO,aAAa,QAAQ,IAAI,IAAI,SAAS,IAAI,IAAI,EAAE,EACrD,SAAS,EACP,SAAS,oBACV,EACF,CAAC;AAIJ,QAAO,aAAa,KAAK,EACvB,SAAS,EACP,SAAS,oBACV,EACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"react-intlayer/server/format"
|
|
1
|
+
export * from "react-intlayer/server/format"
|
|
@@ -1,2 +1,46 @@
|
|
|
1
|
-
import{localeDetector
|
|
1
|
+
import { localeDetector } from "@intlayer/core/localization";
|
|
2
|
+
import configuration from "@intlayer/config/built";
|
|
3
|
+
import { DEFAULT_LOCALE } from "@intlayer/config/defaultValues";
|
|
4
|
+
import { getLocaleFromStorage } from "@intlayer/core/utils";
|
|
5
|
+
import { cookies, headers } from "next/headers.js";
|
|
6
|
+
|
|
7
|
+
//#region src/server/getLocale.ts
|
|
8
|
+
/**
|
|
9
|
+
* Helper function to extract the current locale from Next.js headers and cookies.
|
|
10
|
+
*
|
|
11
|
+
* This function is designed to be used in Server Components, Server Actions, or Route Handlers
|
|
12
|
+
* to determine the locale preferred by the user or stored in their preferences.
|
|
13
|
+
*
|
|
14
|
+
* @returns A promise that resolves to the detected `Locale`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* import { getLocale } from 'next-intlayer/server';
|
|
19
|
+
*
|
|
20
|
+
* export default async function MyServerComponent() {
|
|
21
|
+
* const locale = await getLocale();
|
|
22
|
+
* // ...
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
const getLocale = async () => {
|
|
27
|
+
const defaultLocale = configuration?.internationalization?.defaultLocale ?? DEFAULT_LOCALE;
|
|
28
|
+
const headersList = await headers();
|
|
29
|
+
const cookiesList = await cookies();
|
|
30
|
+
const storedLocale = getLocaleFromStorage({
|
|
31
|
+
getCookie: (name) => cookiesList.get(name)?.value ?? null,
|
|
32
|
+
getHeader: (name) => headersList.get(name) ?? null
|
|
33
|
+
});
|
|
34
|
+
if (storedLocale) return storedLocale;
|
|
35
|
+
const negotiatorHeaders = {};
|
|
36
|
+
headersList.forEach((value, key) => {
|
|
37
|
+
negotiatorHeaders[key] = value;
|
|
38
|
+
});
|
|
39
|
+
const userFallbackLocale = localeDetector(negotiatorHeaders);
|
|
40
|
+
if (userFallbackLocale) return userFallbackLocale;
|
|
41
|
+
return defaultLocale;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
export { getLocale };
|
|
2
46
|
//# sourceMappingURL=getLocale.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getLocale.mjs","names":[],"sources":["../../../src/server/getLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { DEFAULT_LOCALE } from '@intlayer/config/defaultValues';\nimport { localeDetector } from '@intlayer/core/localization';\nimport { getLocaleFromStorage } from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { cookies, headers } from 'next/headers.js';\n\n// Helper function to extract locale from headers/cookies\n/**\n * Helper function to extract the current locale from Next.js headers and cookies.\n *\n * This function is designed to be used in Server Components, Server Actions, or Route Handlers\n * to determine the locale preferred by the user or stored in their preferences.\n *\n * @returns A promise that resolves to the detected `Locale`.\n *\n * @example\n * ```tsx\n * import { getLocale } from 'next-intlayer/server';\n *\n * export default async function MyServerComponent() {\n * const locale = await getLocale();\n * // ...\n * }\n * ```\n */\nexport const getLocale = async (): Promise<Locale> => {\n const defaultLocale =\n configuration?.internationalization?.defaultLocale ?? DEFAULT_LOCALE;\n\n // Try locale from header\n const headersList = await headers();\n const cookiesList = await cookies();\n\n const storedLocale = getLocaleFromStorage({\n getCookie: (name: string) => cookiesList.get(name)?.value ?? null,\n getHeader: (name: string) => headersList.get(name) ?? null,\n });\n\n if (storedLocale) return storedLocale as Locale;\n\n // Fallback to Accept-Language negotiation\n const negotiatorHeaders: Record<string, string> = {};\n headersList.forEach((value, key) => {\n negotiatorHeaders[key] = value;\n });\n\n const userFallbackLocale = localeDetector(negotiatorHeaders);\n if (userFallbackLocale) return userFallbackLocale as Locale;\n\n // Default locale\n return defaultLocale;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"getLocale.mjs","names":[],"sources":["../../../src/server/getLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { DEFAULT_LOCALE } from '@intlayer/config/defaultValues';\nimport { localeDetector } from '@intlayer/core/localization';\nimport { getLocaleFromStorage } from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { cookies, headers } from 'next/headers.js';\n\n// Helper function to extract locale from headers/cookies\n/**\n * Helper function to extract the current locale from Next.js headers and cookies.\n *\n * This function is designed to be used in Server Components, Server Actions, or Route Handlers\n * to determine the locale preferred by the user or stored in their preferences.\n *\n * @returns A promise that resolves to the detected `Locale`.\n *\n * @example\n * ```tsx\n * import { getLocale } from 'next-intlayer/server';\n *\n * export default async function MyServerComponent() {\n * const locale = await getLocale();\n * // ...\n * }\n * ```\n */\nexport const getLocale = async (): Promise<Locale> => {\n const defaultLocale =\n configuration?.internationalization?.defaultLocale ?? DEFAULT_LOCALE;\n\n // Try locale from header\n const headersList = await headers();\n const cookiesList = await cookies();\n\n const storedLocale = getLocaleFromStorage({\n getCookie: (name: string) => cookiesList.get(name)?.value ?? null,\n getHeader: (name: string) => headersList.get(name) ?? null,\n });\n\n if (storedLocale) return storedLocale as Locale;\n\n // Fallback to Accept-Language negotiation\n const negotiatorHeaders: Record<string, string> = {};\n headersList.forEach((value, key) => {\n negotiatorHeaders[key] = value;\n });\n\n const userFallbackLocale = localeDetector(negotiatorHeaders);\n if (userFallbackLocale) return userFallbackLocale as Locale;\n\n // Default locale\n return defaultLocale;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,MAAa,YAAY,YAA6B;CACpD,MAAM,gBACJ,eAAe,sBAAsB,iBAAiB;CAGxD,MAAM,cAAc,MAAM,SAAS;CACnC,MAAM,cAAc,MAAM,SAAS;CAEnC,MAAM,eAAe,qBAAqB;EACxC,YAAY,SAAiB,YAAY,IAAI,KAAK,EAAE,SAAS;EAC7D,YAAY,SAAiB,YAAY,IAAI,KAAK,IAAI;EACvD,CAAC;AAEF,KAAI,aAAc,QAAO;CAGzB,MAAM,oBAA4C,EAAE;AACpD,aAAY,SAAS,OAAO,QAAQ;AAClC,oBAAkB,OAAO;GACzB;CAEF,MAAM,qBAAqB,eAAe,kBAAkB;AAC5D,KAAI,mBAAoB,QAAO;AAG/B,QAAO"}
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
import{getLocale
|
|
1
|
+
import { getLocale } from "./getLocale.mjs";
|
|
2
|
+
import { withIntlayer, withIntlayerSync } from "./withIntlayer.mjs";
|
|
3
|
+
import { IntlayerServerProvider, locale, t, useDictionary, useDictionaryAsync, useDictionaryDynamic, useI18n, useIntl, useIntlayer, useLoadDynamic, useLocale } from "react-intlayer/server";
|
|
4
|
+
|
|
5
|
+
export { IntlayerServerProvider, getLocale, locale, t, useDictionary, useDictionaryAsync, useDictionaryDynamic, useI18n, useIntl, useIntlayer, useLoadDynamic, useLocale, withIntlayer, withIntlayerSync };
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
import{safeUseLocale
|
|
1
|
+
import { safeUseLocale } from "./useIntlayer.mjs";
|
|
2
|
+
import { useDictionary as useDictionary$1 } from "react-intlayer/server";
|
|
3
|
+
|
|
4
|
+
//#region src/server/useDictionary.ts
|
|
5
|
+
/**
|
|
6
|
+
* On the server side, Hook that transform a dictionary and return the content
|
|
7
|
+
*
|
|
8
|
+
* If the locale is not provided, it will use the locale from the server context
|
|
9
|
+
*/
|
|
10
|
+
const useDictionary = (dictionary, locale) => {
|
|
11
|
+
return useDictionary$1(dictionary, locale, safeUseLocale());
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { useDictionary };
|
|
2
16
|
//# sourceMappingURL=useDictionary.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDictionary.mjs","names":["useDictionaryBase"],"sources":["../../../src/server/useDictionary.ts"],"sourcesContent":["import type { DeclaredLocales } from '@intlayer/types/module_augmentation';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport { useDictionary as useDictionaryBase } from 'react-intlayer/server';\nimport { safeUseLocale } from './useIntlayer';\n\n/**\n * On the server side, Hook that transform a dictionary and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useDictionary = <\n T extends Dictionary,\n L extends DeclaredLocales = DeclaredLocales,\n>(\n dictionary: T,\n locale?: L\n): ReturnType<typeof useDictionaryBase<T, L>> => {\n const storedLocale = safeUseLocale();\n\n return useDictionaryBase<T, L>(dictionary, locale, storedLocale);\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDictionary.mjs","names":["useDictionaryBase"],"sources":["../../../src/server/useDictionary.ts"],"sourcesContent":["import type { DeclaredLocales } from '@intlayer/types/module_augmentation';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport { useDictionary as useDictionaryBase } from 'react-intlayer/server';\nimport { safeUseLocale } from './useIntlayer';\n\n/**\n * On the server side, Hook that transform a dictionary and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useDictionary = <\n T extends Dictionary,\n L extends DeclaredLocales = DeclaredLocales,\n>(\n dictionary: T,\n locale?: L\n): ReturnType<typeof useDictionaryBase<T, L>> => {\n const storedLocale = safeUseLocale();\n\n return useDictionaryBase<T, L>(dictionary, locale, storedLocale);\n};\n"],"mappings":";;;;;;;;;AAUA,MAAa,iBAIX,YACA,WAC+C;AAG/C,QAAOA,gBAAwB,YAAY,QAFtB,eAAe,CAE4B"}
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
import{getLocale
|
|
1
|
+
import { getLocale } from "./getLocale.mjs";
|
|
2
|
+
import { useDictionaryAsync as useDictionaryAsync$1 } from "react-intlayer/server";
|
|
3
|
+
|
|
4
|
+
//#region src/server/useDictionaryAsync.ts
|
|
5
|
+
/**
|
|
6
|
+
* On the server side, Hook that transform a dictionary and return the content
|
|
7
|
+
*
|
|
8
|
+
* If the locale is not provided, it will use the locale from the server context
|
|
9
|
+
*/
|
|
10
|
+
const useDictionaryAsync = async (dictionaryPromise, locale) => {
|
|
11
|
+
return useDictionaryAsync$1(dictionaryPromise, locale, await getLocale());
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { useDictionaryAsync };
|
|
2
16
|
//# sourceMappingURL=useDictionaryAsync.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDictionaryAsync.mjs","names":["useDictionaryAsyncBase"],"sources":["../../../src/server/useDictionaryAsync.ts"],"sourcesContent":["import type { DeclaredLocales, StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport { useDictionaryAsync as useDictionaryAsyncBase } from 'react-intlayer/server';\nimport { getLocale } from './getLocale';\n\n/**\n * On the server side, Hook that transform a dictionary and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useDictionaryAsync = async <\n T extends Dictionary,\n L extends DeclaredLocales = DeclaredLocales,\n>(\n dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>,\n locale?: L\n): ReturnType<typeof useDictionaryAsyncBase<T, L>> => {\n const storedLocale = await getLocale();\n\n return useDictionaryAsyncBase<T, L>(dictionaryPromise, locale, storedLocale);\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDictionaryAsync.mjs","names":["useDictionaryAsyncBase"],"sources":["../../../src/server/useDictionaryAsync.ts"],"sourcesContent":["import type { DeclaredLocales, StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport { useDictionaryAsync as useDictionaryAsyncBase } from 'react-intlayer/server';\nimport { getLocale } from './getLocale';\n\n/**\n * On the server side, Hook that transform a dictionary and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useDictionaryAsync = async <\n T extends Dictionary,\n L extends DeclaredLocales = DeclaredLocales,\n>(\n dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>,\n locale?: L\n): ReturnType<typeof useDictionaryAsyncBase<T, L>> => {\n const storedLocale = await getLocale();\n\n return useDictionaryAsyncBase<T, L>(dictionaryPromise, locale, storedLocale);\n};\n"],"mappings":";;;;;;;;;AAUA,MAAa,qBAAqB,OAIhC,mBACA,WACoD;AAGpD,QAAOA,qBAA6B,mBAAmB,QAFlC,MAAM,WAAW,CAEsC"}
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
import{safeUseLocale
|
|
1
|
+
import { safeUseLocale } from "./useIntlayer.mjs";
|
|
2
|
+
import { useDictionaryDynamic as useDictionaryDynamic$1 } from "react-intlayer/server";
|
|
3
|
+
|
|
4
|
+
//#region src/server/useDictionaryDynamic.ts
|
|
5
|
+
/**
|
|
6
|
+
* On the server side, Hook that transform a dictionary and return the content
|
|
7
|
+
*
|
|
8
|
+
* If the locale is not provided, it will use the locale from the server context
|
|
9
|
+
*/
|
|
10
|
+
const useDictionaryDynamic = (dictionaryPromise, key, locale) => {
|
|
11
|
+
return useDictionaryDynamic$1(dictionaryPromise, key, locale, safeUseLocale());
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { useDictionaryDynamic };
|
|
2
16
|
//# sourceMappingURL=useDictionaryDynamic.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDictionaryDynamic.mjs","names":["useDictionaryDynamicBase"],"sources":["../../../src/server/useDictionaryDynamic.ts"],"sourcesContent":["import type { DeclaredLocales, StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport { useDictionaryDynamic as useDictionaryDynamicBase } from 'react-intlayer/server';\nimport { safeUseLocale } from './useIntlayer';\n\n/**\n * On the server side, Hook that transform a dictionary and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useDictionaryDynamic = <\n T extends Dictionary,\n L extends DeclaredLocales = DeclaredLocales,\n>(\n dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>,\n key: string,\n locale?: L\n): ReturnType<typeof useDictionaryDynamicBase<T, L>> => {\n const storedLocale = safeUseLocale();\n\n return useDictionaryDynamicBase<T, L>(\n dictionaryPromise,\n key,\n locale,\n storedLocale\n );\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDictionaryDynamic.mjs","names":["useDictionaryDynamicBase"],"sources":["../../../src/server/useDictionaryDynamic.ts"],"sourcesContent":["import type { DeclaredLocales, StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport { useDictionaryDynamic as useDictionaryDynamicBase } from 'react-intlayer/server';\nimport { safeUseLocale } from './useIntlayer';\n\n/**\n * On the server side, Hook that transform a dictionary and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useDictionaryDynamic = <\n T extends Dictionary,\n L extends DeclaredLocales = DeclaredLocales,\n>(\n dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>,\n key: string,\n locale?: L\n): ReturnType<typeof useDictionaryDynamicBase<T, L>> => {\n const storedLocale = safeUseLocale();\n\n return useDictionaryDynamicBase<T, L>(\n dictionaryPromise,\n key,\n locale,\n storedLocale\n );\n};\n"],"mappings":";;;;;;;;;AAUA,MAAa,wBAIX,mBACA,KACA,WACsD;AAGtD,QAAOA,uBACL,mBACA,KACA,QALmB,eAAe,CAOnC"}
|
|
@@ -1,2 +1,26 @@
|
|
|
1
|
-
import{getLocale
|
|
1
|
+
import { getLocale } from "./getLocale.mjs";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { useIntlayer as useIntlayer$1 } from "react-intlayer/server";
|
|
4
|
+
|
|
5
|
+
//#region src/server/useIntlayer.ts
|
|
6
|
+
const getCachedLocale = typeof React.cache === "function" ? React.cache(getLocale) : getLocale;
|
|
7
|
+
const safeUseLocale = () => {
|
|
8
|
+
const localeData = getCachedLocale();
|
|
9
|
+
if (localeData instanceof Promise) {
|
|
10
|
+
if (typeof React.use === "function") return React.use(localeData);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
return localeData;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* On the server side, Hook that picking one dictionary by its key and return the content
|
|
17
|
+
*
|
|
18
|
+
* If the locale is not provided, it will use the locale from the server context
|
|
19
|
+
*/
|
|
20
|
+
const useIntlayer = (key, locale) => {
|
|
21
|
+
return useIntlayer$1(key, locale, safeUseLocale());
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { safeUseLocale, useIntlayer };
|
|
2
26
|
//# sourceMappingURL=useIntlayer.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIntlayer.mjs","names":["useIntlayerBase"],"sources":["../../../src/server/useIntlayer.ts"],"sourcesContent":["import type { DeclaredLocales, DictionaryKeys } from '@intlayer/types/module_augmentation';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport React from 'react';\nimport { useIntlayer as useIntlayerBase } from 'react-intlayer/server';\nimport { getLocale } from './getLocale';\n\nconst getCachedLocale =\n typeof React.cache === 'function' ? React.cache(getLocale) : getLocale;\n\nexport const safeUseLocale = (): Locale | undefined => {\n // getLocale returns a Promise based on your TS error\n const localeData = getCachedLocale() as Promise<Locale> | Locale;\n\n if (localeData instanceof Promise) {\n if (typeof React.use === 'function') {\n return React.use(localeData); // Safely unwraps in React 19+\n }\n\n // React < 19 cannot synchronously unwrap Promises in hooks.\n // Return undefined to trigger the localeTarget fallback.\n return undefined;\n }\n\n return localeData;\n};\n\n/**\n * On the server side, Hook that picking one dictionary by its key and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useIntlayer = <\n T extends DictionaryKeys,\n L extends DeclaredLocales = DeclaredLocales,\n>(\n key: T,\n locale?: L\n): ReturnType<typeof useIntlayerBase<T, L>> => {\n const storedLocale = safeUseLocale();\n\n return useIntlayerBase<T, L>(key, locale, storedLocale);\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useIntlayer.mjs","names":["useIntlayerBase"],"sources":["../../../src/server/useIntlayer.ts"],"sourcesContent":["import type { DeclaredLocales, DictionaryKeys } from '@intlayer/types/module_augmentation';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport React from 'react';\nimport { useIntlayer as useIntlayerBase } from 'react-intlayer/server';\nimport { getLocale } from './getLocale';\n\nconst getCachedLocale =\n typeof React.cache === 'function' ? React.cache(getLocale) : getLocale;\n\nexport const safeUseLocale = (): Locale | undefined => {\n // getLocale returns a Promise based on your TS error\n const localeData = getCachedLocale() as Promise<Locale> | Locale;\n\n if (localeData instanceof Promise) {\n if (typeof React.use === 'function') {\n return React.use(localeData); // Safely unwraps in React 19+\n }\n\n // React < 19 cannot synchronously unwrap Promises in hooks.\n // Return undefined to trigger the localeTarget fallback.\n return undefined;\n }\n\n return localeData;\n};\n\n/**\n * On the server side, Hook that picking one dictionary by its key and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useIntlayer = <\n T extends DictionaryKeys,\n L extends DeclaredLocales = DeclaredLocales,\n>(\n key: T,\n locale?: L\n): ReturnType<typeof useIntlayerBase<T, L>> => {\n const storedLocale = safeUseLocale();\n\n return useIntlayerBase<T, L>(key, locale, storedLocale);\n};\n"],"mappings":";;;;;AAMA,MAAM,kBACJ,OAAO,MAAM,UAAU,aAAa,MAAM,MAAM,UAAU,GAAG;AAE/D,MAAa,sBAA0C;CAErD,MAAM,aAAa,iBAAiB;AAEpC,KAAI,sBAAsB,SAAS;AACjC,MAAI,OAAO,MAAM,QAAQ,WACvB,QAAO,MAAM,IAAI,WAAW;AAK9B;;AAGF,QAAO;;;;;;;AAQT,MAAa,eAIX,KACA,WAC6C;AAG7C,QAAOA,cAAsB,KAAK,QAFb,eAAe,CAEmB"}
|
|
@@ -1,2 +1,254 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { IMPORT_MODE } from "@intlayer/config/defaultValues";
|
|
2
|
+
import { join, relative, resolve } from "node:path";
|
|
3
|
+
import { prepareIntlayer } from "@intlayer/chokidar/build";
|
|
4
|
+
import { logConfigDetails } from "@intlayer/chokidar/cli";
|
|
5
|
+
import { buildComponentFilesList, runOnce } from "@intlayer/chokidar/utils";
|
|
6
|
+
import * as ANSIColors from "@intlayer/config/colors";
|
|
7
|
+
import { colorize, getAppLogger } from "@intlayer/config/logger";
|
|
8
|
+
import { getConfiguration } from "@intlayer/config/node";
|
|
9
|
+
import { compareVersions, getAlias, getProjectRequire, normalizePath } from "@intlayer/config/utils";
|
|
10
|
+
import { getDictionaries } from "@intlayer/dictionaries-entry";
|
|
11
|
+
import { IntlayerPlugin } from "@intlayer/webpack";
|
|
12
|
+
import { defu } from "defu";
|
|
13
|
+
import nextPackageJSON from "next/package.json" with { type: "json" };
|
|
14
|
+
|
|
15
|
+
//#region src/server/withIntlayer.ts
|
|
16
|
+
/**
|
|
17
|
+
* Resolve the Next.js version from the *user's* project at runtime.
|
|
18
|
+
* A static `import from 'next/package.json'` would resolve relative to
|
|
19
|
+
* next-intlayer's own node_modules, which may differ in a monorepo.
|
|
20
|
+
*/
|
|
21
|
+
const getNextVersionFlags = (intlayerConfig) => {
|
|
22
|
+
let nextVersion = nextPackageJSON.version;
|
|
23
|
+
try {
|
|
24
|
+
nextVersion = (intlayerConfig.build?.require ?? getProjectRequire())("next/package.json").version;
|
|
25
|
+
} catch {}
|
|
26
|
+
return {
|
|
27
|
+
isGteNext13: compareVersions(nextVersion, "≥", "13.0.0"),
|
|
28
|
+
isGteNext15: compareVersions(nextVersion, "≥", "15.0.0"),
|
|
29
|
+
isGteNext16: compareVersions(nextVersion, "≥", "16.0.0"),
|
|
30
|
+
isTurbopackStable: compareVersions(nextVersion, "≥", "15.3.0")
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
const getIsSwcPluginAvailable = (intlayerConfig) => {
|
|
34
|
+
try {
|
|
35
|
+
(intlayerConfig.build?.require ?? getProjectRequire()).resolve("@intlayer/swc");
|
|
36
|
+
return true;
|
|
37
|
+
} catch (_e) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const getIsBabelExtractPluginAvailable = (intlayerConfig) => {
|
|
42
|
+
try {
|
|
43
|
+
(intlayerConfig.build?.require ?? getProjectRequire()).resolve("@intlayer/babel");
|
|
44
|
+
return true;
|
|
45
|
+
} catch (_e) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const resolvePluginPath = (pluginPath, intlayerConfig, isTurbopackEnabled) => {
|
|
50
|
+
const pluginPathResolved = (intlayerConfig.build?.require ?? getProjectRequire())?.resolve(pluginPath);
|
|
51
|
+
if (isTurbopackEnabled) return normalizePath(`./${relative(process.cwd(), pluginPathResolved)}`);
|
|
52
|
+
return pluginPathResolved;
|
|
53
|
+
};
|
|
54
|
+
const getPruneConfig = (intlayerConfig, isBuildCommand, isTurbopackEnabled, isDevCommand, isGteNext13) => {
|
|
55
|
+
const { optimize } = intlayerConfig.build;
|
|
56
|
+
const importMode = intlayerConfig.build.importMode ?? intlayerConfig.dictionary?.importMode;
|
|
57
|
+
const { dictionariesDir, unmergedDictionariesDir, dynamicDictionariesDir, fetchDictionariesDir, mainDir } = intlayerConfig.system;
|
|
58
|
+
const { baseDir } = intlayerConfig.system;
|
|
59
|
+
const logger = getAppLogger(intlayerConfig);
|
|
60
|
+
if (optimize === false) return {};
|
|
61
|
+
if (optimize === void 0 && !isBuildCommand) return {};
|
|
62
|
+
if (!isGteNext13) return {};
|
|
63
|
+
const isSwcPluginAvailable = getIsSwcPluginAvailable(intlayerConfig);
|
|
64
|
+
runOnce(join(baseDir, ".intlayer", "cache", "intlayer-prune-plugin-enabled.lock"), () => {
|
|
65
|
+
if (isSwcPluginAvailable) logger("Build optimization enabled");
|
|
66
|
+
else logger([
|
|
67
|
+
colorize("Recommended: Install", ANSIColors.GREY),
|
|
68
|
+
colorize("@intlayer/swc", ANSIColors.GREY_LIGHT),
|
|
69
|
+
colorize("package to enable build optimization. See documentation:", ANSIColors.GREY),
|
|
70
|
+
colorize("https://intlayer.org/docs/bundle-optimization", ANSIColors.GREY_LIGHT)
|
|
71
|
+
]);
|
|
72
|
+
}, { cacheTimeoutMs: 1e3 * 30 });
|
|
73
|
+
runOnce(join(baseDir, ".intlayer", "cache", "intlayer-compiler-plugin-enabled.lock"), () => {
|
|
74
|
+
if (getIsBabelExtractPluginAvailable(intlayerConfig)) {
|
|
75
|
+
let isEnabled = intlayerConfig.compiler?.enabled ?? true;
|
|
76
|
+
if (isEnabled === "build-only") isEnabled = !isDevCommand;
|
|
77
|
+
if (isEnabled) logger("Intlayer compiler enabled");
|
|
78
|
+
else logger("Intlayer compiler disabled");
|
|
79
|
+
}
|
|
80
|
+
}, { cacheTimeoutMs: 1e3 * 30 });
|
|
81
|
+
if (!isSwcPluginAvailable) return {};
|
|
82
|
+
const dictionariesEntryPath = join(mainDir, "dictionaries.mjs");
|
|
83
|
+
const dynamicDictionariesEntryPath = join(mainDir, "dynamic_dictionaries.mjs");
|
|
84
|
+
const unmergedDictionariesEntryPath = join(mainDir, "unmerged_dictionaries.mjs");
|
|
85
|
+
const fetchDictionariesEntryPath = join(mainDir, "fetch_dictionaries.mjs");
|
|
86
|
+
const filesList = [
|
|
87
|
+
...buildComponentFilesList(intlayerConfig),
|
|
88
|
+
dictionariesEntryPath,
|
|
89
|
+
unmergedDictionariesEntryPath
|
|
90
|
+
];
|
|
91
|
+
const dictionaries = getDictionaries(intlayerConfig);
|
|
92
|
+
const dictionaryModeMap = {};
|
|
93
|
+
Object.values(dictionaries).forEach((dictionary) => {
|
|
94
|
+
dictionaryModeMap[dictionary.key] = dictionary.importMode ?? importMode ?? IMPORT_MODE;
|
|
95
|
+
});
|
|
96
|
+
return { experimental: { swcPlugins: [[resolvePluginPath("@intlayer/swc", intlayerConfig, isTurbopackEnabled), {
|
|
97
|
+
dictionariesDir,
|
|
98
|
+
dictionariesEntryPath,
|
|
99
|
+
unmergedDictionariesEntryPath,
|
|
100
|
+
unmergedDictionariesDir,
|
|
101
|
+
dynamicDictionariesDir,
|
|
102
|
+
dynamicDictionariesEntryPath,
|
|
103
|
+
fetchDictionariesDir,
|
|
104
|
+
fetchDictionariesEntryPath,
|
|
105
|
+
importMode,
|
|
106
|
+
filesList,
|
|
107
|
+
replaceDictionaryEntry: true,
|
|
108
|
+
dictionaryModeMap
|
|
109
|
+
}]] } };
|
|
110
|
+
};
|
|
111
|
+
const getCommandsEvent = () => {
|
|
112
|
+
const lifecycleEvent = process.env.npm_lifecycle_event;
|
|
113
|
+
const lifecycleScript = process.env.npm_lifecycle_script ?? "";
|
|
114
|
+
return {
|
|
115
|
+
isDevCommand: lifecycleEvent === "dev" || process.argv.some((arg) => arg === "dev") || /(^|\s)(next\s+)?dev(\s|$)/.test(lifecycleScript),
|
|
116
|
+
isBuildCommand: lifecycleEvent === "build" || process.argv.some((arg) => arg === "build") || /(^|\s)(next\s+)?build(\s|$)/.test(lifecycleScript),
|
|
117
|
+
isStartCommand: lifecycleEvent === "start" || process.argv.some((arg) => arg === "start") || /(^|\s)(next\s+)?start(\s|$)/.test(lifecycleScript)
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* A Next.js plugin that adds the intlayer configuration to the webpack configuration
|
|
122
|
+
* and sets the environment variables
|
|
123
|
+
*
|
|
124
|
+
* Usage:
|
|
125
|
+
*
|
|
126
|
+
* ```ts
|
|
127
|
+
* // next.config.js
|
|
128
|
+
* export default withIntlayerSync(nextConfig)
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
const withIntlayerSync = (nextConfig = {}, configOptions) => {
|
|
132
|
+
if (typeof nextConfig !== "object") nextConfig = {};
|
|
133
|
+
const intlayerConfig = getConfiguration(configOptions);
|
|
134
|
+
logConfigDetails(configOptions);
|
|
135
|
+
const logger = getAppLogger(intlayerConfig);
|
|
136
|
+
const { isGteNext13, isGteNext15, isGteNext16, isTurbopackStable } = getNextVersionFlags(intlayerConfig);
|
|
137
|
+
const isTurbopackEnabledFromCommand = isGteNext16 ? !process.env.npm_lifecycle_script?.includes("--webpack") : process.env.npm_lifecycle_script?.includes("--turbo");
|
|
138
|
+
const isTurbopackEnabled = configOptions?.enableTurbopack ?? isTurbopackEnabledFromCommand;
|
|
139
|
+
if (isTurbopackEnabled && typeof nextConfig.webpack !== "undefined") logger("Turbopack is enabled but a custom webpack config is present. It will be ignored.");
|
|
140
|
+
const { isBuildCommand, isDevCommand } = getCommandsEvent();
|
|
141
|
+
const turboConfig = {
|
|
142
|
+
resolveAlias: getAlias({
|
|
143
|
+
configuration: intlayerConfig,
|
|
144
|
+
formatter: (value) => `./${value}`
|
|
145
|
+
}),
|
|
146
|
+
rules: { "*.node": {
|
|
147
|
+
as: "*.node",
|
|
148
|
+
loaders: ["node-loader"]
|
|
149
|
+
} }
|
|
150
|
+
};
|
|
151
|
+
const serverExternalPackages = [
|
|
152
|
+
"esbuild",
|
|
153
|
+
"module",
|
|
154
|
+
"fs",
|
|
155
|
+
"chokidar",
|
|
156
|
+
"fsevents",
|
|
157
|
+
"recast",
|
|
158
|
+
"@intlayer/chokidar",
|
|
159
|
+
"@intlayer/webpack"
|
|
160
|
+
];
|
|
161
|
+
const getNewConfig = () => {
|
|
162
|
+
let config = { env: { INTLAYER_EDITOR_ENABLED: intlayerConfig.editor?.enabled === false ? "false" : "true" } };
|
|
163
|
+
if (isGteNext15) config = {
|
|
164
|
+
...config,
|
|
165
|
+
serverExternalPackages
|
|
166
|
+
};
|
|
167
|
+
if (isGteNext13 && !isGteNext15) config = {
|
|
168
|
+
...config,
|
|
169
|
+
experimental: {
|
|
170
|
+
...config?.experimental ?? {},
|
|
171
|
+
serverComponentsExternalPackages: serverExternalPackages
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
if (isTurbopackEnabled) if (isGteNext15 && isTurbopackStable) config = {
|
|
175
|
+
...config,
|
|
176
|
+
turbopack: turboConfig
|
|
177
|
+
};
|
|
178
|
+
else config = {
|
|
179
|
+
...config,
|
|
180
|
+
experimental: {
|
|
181
|
+
...config?.experimental ?? {},
|
|
182
|
+
turbo: turboConfig
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
else config = {
|
|
186
|
+
...config,
|
|
187
|
+
webpack: (config, options) => {
|
|
188
|
+
const { isServer, nextRuntime } = options;
|
|
189
|
+
if (typeof nextConfig.webpack === "function") config = nextConfig.webpack(config, options);
|
|
190
|
+
if (config.externals === false) config.externals = [];
|
|
191
|
+
const externalExact = new Set([
|
|
192
|
+
"esbuild",
|
|
193
|
+
"module",
|
|
194
|
+
"fs",
|
|
195
|
+
"chokidar",
|
|
196
|
+
"fsevents",
|
|
197
|
+
"recast"
|
|
198
|
+
]);
|
|
199
|
+
const externalPrefixes = ["@intlayer/chokidar", "@intlayer/webpack"];
|
|
200
|
+
config.externals.push(({ request }, callback) => {
|
|
201
|
+
if (request && (externalExact.has(request) || externalPrefixes.some((p) => request === p || request.startsWith(`${p}/`)))) return callback(null, `commonjs ${request}`);
|
|
202
|
+
callback(null);
|
|
203
|
+
});
|
|
204
|
+
config.module.rules.push({
|
|
205
|
+
test: /\.node$/,
|
|
206
|
+
loader: "node-loader"
|
|
207
|
+
});
|
|
208
|
+
config.resolve.alias = {
|
|
209
|
+
...config.resolve.alias,
|
|
210
|
+
...getAlias({
|
|
211
|
+
configuration: intlayerConfig,
|
|
212
|
+
formatter: (value) => resolve(value)
|
|
213
|
+
})
|
|
214
|
+
};
|
|
215
|
+
if (isDevCommand && isServer && nextRuntime === "nodejs") config.plugins.push(new IntlayerPlugin(intlayerConfig));
|
|
216
|
+
return config;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
return config;
|
|
220
|
+
};
|
|
221
|
+
const pruneConfig = getPruneConfig(intlayerConfig, isBuildCommand, isTurbopackEnabled ?? false, isDevCommand, isGteNext13);
|
|
222
|
+
return defu(defu(getNewConfig(), pruneConfig), nextConfig);
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* A Next.js plugin that adds the intlayer configuration to the webpack configuration
|
|
226
|
+
* and sets the environment variables
|
|
227
|
+
*
|
|
228
|
+
* Usage:
|
|
229
|
+
*
|
|
230
|
+
* ```ts
|
|
231
|
+
* // next.config.js
|
|
232
|
+
* export default withIntlayer(nextConfig)
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* > Node withIntlayer is a promise function. Use withIntlayerSync instead if you want to use it synchronously.
|
|
236
|
+
* > Using the promise allows to prepare the intlayer dictionaries before the build starts.
|
|
237
|
+
*
|
|
238
|
+
*/
|
|
239
|
+
const withIntlayer = async (nextConfig = {}, configOptions) => {
|
|
240
|
+
const { isBuildCommand, isDevCommand, isStartCommand } = getCommandsEvent();
|
|
241
|
+
process.env.INTLAYER_IS_DEV_COMMAND = isDevCommand ? "true" : "false";
|
|
242
|
+
const intlayerConfig = getConfiguration(configOptions);
|
|
243
|
+
const { mode } = intlayerConfig.build;
|
|
244
|
+
if (!isStartCommand && (isDevCommand || isBuildCommand || mode === "auto")) await prepareIntlayer(intlayerConfig, {
|
|
245
|
+
clean: isBuildCommand,
|
|
246
|
+
cacheTimeoutMs: isBuildCommand ? 1e3 * 30 : 1e3 * 60 * 60,
|
|
247
|
+
env: isBuildCommand ? "prod" : "dev"
|
|
248
|
+
});
|
|
249
|
+
return withIntlayerSync(await nextConfig, configOptions);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
//#endregion
|
|
253
|
+
export { withIntlayer, withIntlayerSync };
|
|
2
254
|
//# sourceMappingURL=withIntlayer.mjs.map
|