next-intlayer 1.0.0 → 1.2.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 (67) hide show
  1. package/README.md +199 -0
  2. package/dist/cjs/client/index.cjs +17 -23
  3. package/dist/cjs/client/index.cjs.map +1 -1
  4. package/dist/cjs/client/index.d.ts +3 -10
  5. package/dist/cjs/client/useLocale.cjs +13 -20
  6. package/dist/cjs/client/useLocale.cjs.map +1 -1
  7. package/dist/cjs/client/useLocale.d.ts +7 -7
  8. package/dist/cjs/generateStaticParams.cjs +8 -13
  9. package/dist/cjs/generateStaticParams.d.ts +2 -2
  10. package/dist/cjs/index.cjs +19 -25
  11. package/dist/cjs/index.cjs.map +1 -1
  12. package/dist/cjs/index.d.ts +8 -15
  13. package/dist/cjs/middleware/index.cjs +9 -21
  14. package/dist/cjs/middleware/index.d.ts +2 -2
  15. package/dist/cjs/middleware/intlayerMiddleware.cjs +28 -86
  16. package/dist/cjs/middleware/intlayerMiddleware.cjs.map +1 -1
  17. package/dist/cjs/middleware/intlayerMiddleware.d.ts +16 -1
  18. package/dist/cjs/middleware/localeDetector.cjs +20 -37
  19. package/dist/cjs/middleware/localeDetector.cjs.map +1 -1
  20. package/dist/cjs/middleware/localeDetector.d.ts +7 -2
  21. package/dist/cjs/server/index.cjs +17 -22
  22. package/dist/cjs/server/index.cjs.map +1 -1
  23. package/dist/cjs/server/index.d.ts +3 -10
  24. package/dist/cjs/server/withIntlayer.cjs +38 -53
  25. package/dist/cjs/server/withIntlayer.cjs.map +1 -1
  26. package/dist/cjs/server/withIntlayer.d.ts +13 -5
  27. package/dist/cjs/types/NextPage.cjs +4 -8
  28. package/dist/cjs/types/NextPage.d.ts +6 -6
  29. package/dist/cjs/types/index.cjs +4 -8
  30. package/dist/cjs/types/index.d.ts +4 -4
  31. package/dist/esm/client/index.d.mts +3 -10
  32. package/dist/esm/client/index.mjs +8 -8
  33. package/dist/esm/client/index.mjs.map +1 -1
  34. package/dist/esm/client/useLocale.d.mts +7 -7
  35. package/dist/esm/client/useLocale.mjs +12 -10
  36. package/dist/esm/client/useLocale.mjs.map +1 -1
  37. package/dist/esm/generateStaticParams.d.mts +2 -2
  38. package/dist/esm/generateStaticParams.mjs +4 -2
  39. package/dist/esm/index.d.mts +8 -15
  40. package/dist/esm/index.mjs +9 -9
  41. package/dist/esm/index.mjs.map +1 -1
  42. package/dist/esm/middleware/index.d.mts +2 -2
  43. package/dist/esm/middleware/index.mjs +2 -2
  44. package/dist/esm/middleware/intlayerMiddleware.d.mts +16 -1
  45. package/dist/esm/middleware/intlayerMiddleware.mjs +21 -66
  46. package/dist/esm/middleware/intlayerMiddleware.mjs.map +1 -1
  47. package/dist/esm/middleware/localeDetector.d.mts +7 -2
  48. package/dist/esm/middleware/localeDetector.mjs +5 -3
  49. package/dist/esm/middleware/localeDetector.mjs.map +1 -1
  50. package/dist/esm/server/index.d.mts +3 -10
  51. package/dist/esm/server/index.mjs +8 -8
  52. package/dist/esm/server/index.mjs.map +1 -1
  53. package/dist/esm/server/withIntlayer.d.mts +13 -5
  54. package/dist/esm/server/withIntlayer.mjs +34 -37
  55. package/dist/esm/server/withIntlayer.mjs.map +1 -1
  56. package/dist/esm/types/NextPage.d.mts +6 -6
  57. package/dist/esm/types/NextPage.mjs +1 -1
  58. package/dist/esm/types/index.d.mts +4 -4
  59. package/dist/esm/types/index.mjs +1 -1
  60. package/package.json +14 -12
  61. package/src/client/index.ts +2 -2
  62. package/src/client/useLocale.ts +6 -4
  63. package/src/index.ts +2 -2
  64. package/src/middleware/intlayerMiddleware.ts +15 -0
  65. package/src/middleware/localeDetector.ts +5 -0
  66. package/src/server/index.ts +2 -2
  67. package/src/server/withIntlayer.ts +33 -27
@@ -8,28 +8,23 @@ var __export = (target, all) => {
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
10
  var __copyProps = (to, from, except, desc) => {
11
- if ((from && typeof from === "object") || typeof from === "function") {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
12
  for (let key of __getOwnPropNames(from))
13
13
  if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, {
15
- get: () => from[key],
16
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
17
- });
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
15
  }
19
16
  return to;
20
17
  };
21
- var __toCommonJS = (mod) =>
22
- __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
19
  var intlayerMiddleware_exports = {};
24
20
  __export(intlayerMiddleware_exports, {
25
- intlayerMiddleware: () => intlayerMiddleware,
21
+ intlayerMiddleware: () => intlayerMiddleware
26
22
  });
27
23
  module.exports = __toCommonJS(intlayerMiddleware_exports);
28
24
  var import_client = require("@intlayer/config/client");
29
25
  var import_server = require("next/server");
30
- var import_localeDetector = require("./localeDetector.cjs");
31
- const { internationalization, middleware } =
32
- import_client.intlayerConfiguration;
26
+ var import_localeDetector = require('./localeDetector.cjs');
27
+ const { internationalization, middleware } = import_client.intlayerConfiguration;
33
28
  const { locales, defaultLocale } = internationalization;
34
29
  const {
35
30
  headerName,
@@ -37,7 +32,7 @@ const {
37
32
  prefixDefault,
38
33
  basePath,
39
34
  serverSetCookie,
40
- noPrefix,
35
+ noPrefix
41
36
  } = middleware;
42
37
  const intlayerMiddleware = (request) => {
43
38
  const pathname = request.nextUrl.pathname;
@@ -61,18 +56,14 @@ const intlayerMiddleware = (request) => {
61
56
  );
62
57
  };
63
58
  const getCookieLocale = (request) => {
64
- if (!cookieName) return void 0;
59
+ if (!cookieName)
60
+ return void 0;
65
61
  const cookieValue = request.cookies.get(cookieName)?.value;
66
62
  if (cookieValue && locales.includes(cookieValue)) {
67
63
  return cookieValue;
68
64
  }
69
65
  };
70
- const handleNoPrefix = (
71
- request,
72
- cookieLocale,
73
- pathname,
74
- basePathTrailingSlash
75
- ) => {
66
+ const handleNoPrefix = (request, cookieLocale, pathname, basePathTrailingSlash) => {
76
67
  const locale = cookieLocale ?? defaultLocale;
77
68
  const newPath = constructPath(
78
69
  locale,
@@ -82,17 +73,10 @@ const handleNoPrefix = (
82
73
  );
83
74
  return rewriteUrl(request, newPath, locale);
84
75
  };
85
- const getPathLocale = (pathname) =>
86
- locales.find(
87
- (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
88
- );
89
- const handlePrefix = (
90
- request,
91
- cookieLocale,
92
- pathLocale,
93
- pathname,
94
- basePathTrailingSlash
95
- ) => {
76
+ const getPathLocale = (pathname) => locales.find(
77
+ (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
78
+ );
79
+ const handlePrefix = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
96
80
  if (!pathLocale) {
97
81
  return handleMissingPathLocale(
98
82
  request,
@@ -109,16 +93,8 @@ const handlePrefix = (
109
93
  basePathTrailingSlash
110
94
  );
111
95
  };
112
- const handleMissingPathLocale = (
113
- request,
114
- cookieLocale,
115
- pathname,
116
- basePathTrailingSlash
117
- ) => {
118
- let locale =
119
- cookieLocale ??
120
- (0, import_localeDetector.localeDetector)?.(request) ??
121
- defaultLocale;
96
+ const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => {
97
+ let locale = cookieLocale ?? (0, import_localeDetector.localeDetector)?.(request) ?? defaultLocale;
122
98
  if (!locales.includes(locale)) {
123
99
  console.warn(
124
100
  "The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale."
@@ -131,22 +107,10 @@ const handleMissingPathLocale = (
131
107
  basePath,
132
108
  basePathTrailingSlash
133
109
  );
134
- return prefixDefault || locale !== defaultLocale
135
- ? redirectUrl(request, newPath)
136
- : rewriteUrl(request, newPath, locale);
110
+ return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
137
111
  };
138
- const handleExistingPathLocale = (
139
- request,
140
- cookieLocale,
141
- pathLocale,
142
- pathname,
143
- basePathTrailingSlash
144
- ) => {
145
- if (
146
- cookieLocale &&
147
- cookieLocale !== pathLocale &&
148
- serverSetCookie !== "always"
149
- ) {
112
+ const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
113
+ if (cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always") {
150
114
  const newPath = handleCookieLocaleMismatch(
151
115
  request,
152
116
  pathname,
@@ -164,14 +128,7 @@ const handleExistingPathLocale = (
164
128
  basePathTrailingSlash
165
129
  );
166
130
  };
167
- const handleCookieLocaleMismatch = (
168
- request,
169
- pathname,
170
- pathLocale,
171
- cookieLocale,
172
- basePath2,
173
- basePathTrailingSlash
174
- ) => {
131
+ const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath2, basePathTrailingSlash) => {
175
132
  const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);
176
133
  return constructPath(
177
134
  cookieLocale,
@@ -181,12 +138,7 @@ const handleCookieLocaleMismatch = (
181
138
  request.nextUrl.search
182
139
  );
183
140
  };
184
- const handleDefaultLocaleRedirect = (
185
- request,
186
- pathLocale,
187
- pathname,
188
- basePathTrailingSlash
189
- ) => {
141
+ const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrailingSlash) => {
190
142
  if (!prefixDefault && pathLocale === defaultLocale) {
191
143
  let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || "/";
192
144
  if (basePathTrailingSlash) {
@@ -199,13 +151,7 @@ const handleDefaultLocaleRedirect = (
199
151
  }
200
152
  return rewriteUrl(request, pathname, pathLocale);
201
153
  };
202
- const constructPath = (
203
- locale,
204
- path,
205
- basePath2,
206
- basePathTrailingSlash,
207
- search
208
- ) => {
154
+ const constructPath = (locale, path, basePath2, basePathTrailingSlash, search) => {
209
155
  let newPath = `${locale}${path}`;
210
156
  newPath = `${basePath2}${basePathTrailingSlash ? "" : "/"}${newPath}`;
211
157
  if (search) {
@@ -214,17 +160,13 @@ const constructPath = (
214
160
  return newPath;
215
161
  };
216
162
  const rewriteUrl = (request, newPath, locale) => {
217
- const response = import_server.NextResponse.rewrite(
218
- new URL(newPath, request.url)
219
- );
163
+ const response = import_server.NextResponse.rewrite(new URL(newPath, request.url));
220
164
  response.headers.set(headerName, locale);
221
165
  return response;
222
166
  };
223
- const redirectUrl = (request, newPath) =>
224
- import_server.NextResponse.redirect(new URL(newPath, request.url));
167
+ const redirectUrl = (request, newPath) => import_server.NextResponse.redirect(new URL(newPath, request.url));
225
168
  // Annotate the CommonJS export names for ESM import in node:
226
- 0 &&
227
- (module.exports = {
228
- intlayerMiddleware,
229
- });
230
- //# sourceMappingURL=intlayerMiddleware.cjs.map
169
+ 0 && (module.exports = {
170
+ intlayerMiddleware
171
+ });
172
+ //# sourceMappingURL=intlayerMiddleware.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import { type Locales, intlayerConfiguration } from '@intlayer/config/client';\nimport { type NextRequest, NextResponse } from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = intlayerConfiguration;\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\n\nexport const intlayerMiddleware = (request: NextRequest): NextResponse => {\n const pathname = request.nextUrl.pathname;\n const cookieLocale = getCookieLocale(request);\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (noPrefix) {\n return handleNoPrefix(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n const pathLocale = getPathLocale(pathname);\n return handlePrefix(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\nconst getCookieLocale = (request: NextRequest): Locales | undefined => {\n if (!cookieName) return undefined;\n const cookieValue = request.cookies.get(cookieName)?.value as Locales;\n if (cookieValue && locales.includes(cookieValue)) {\n return cookieValue;\n }\n};\n\nconst handleNoPrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n const locale = cookieLocale ?? defaultLocale;\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash\n );\n return rewriteUrl(request, newPath, locale);\n};\n\nconst getPathLocale = (pathname: string): Locales | undefined =>\n locales.find(\n (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`\n );\n\nconst handlePrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (!pathLocale) {\n return handleMissingPathLocale(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n return handleExistingPathLocale(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\nconst handleMissingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n let locale = cookieLocale ?? localeDetector?.(request) ?? defaultLocale;\n if (!locales.includes(locale)) {\n console.warn(\n 'The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale.'\n );\n locale = defaultLocale;\n }\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash\n );\n return prefixDefault || locale !== defaultLocale\n ? redirectUrl(request, newPath)\n : rewriteUrl(request, newPath, locale);\n};\n\nconst handleExistingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n cookieLocale,\n basePath,\n basePathTrailingSlash\n );\n return redirectUrl(request, newPath);\n }\n\n return handleDefaultLocaleRedirect(\n request,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n\n pathname: string,\n pathLocale: Locales,\n cookieLocale: Locales,\n basePath: string,\n basePathTrailingSlash: boolean\n): string => {\n const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);\n return constructPath(\n cookieLocale,\n newPath,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\n );\n};\n\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (!prefixDefault && pathLocale === defaultLocale) {\n let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n\n if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);\n }\n return rewriteUrl(request, pathname, pathLocale);\n};\n\nconst constructPath = (\n locale: Locales,\n path: string,\n basePath: string,\n basePathTrailingSlash: boolean,\n search?: string\n): string => {\n let newPath = `${locale}${path}`;\n newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;\n if (search) {\n newPath += search;\n }\n return newPath;\n};\n\nconst rewriteUrl = (\n request: NextRequest,\n newPath: string,\n locale: Locales\n): NextResponse => {\n const response = NextResponse.rewrite(new URL(newPath, request.url));\n response.headers.set(headerName, locale);\n return response;\n};\n\nconst redirectUrl = (request: NextRequest, newPath: string): NextResponse =>\n NextResponse.redirect(new URL(newPath, request.url));\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoD;AACpD,oBAA+C;AAC/C,4BAA+B;AAE/B,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,cAAc,IAAI;AACnC,MAAM;AAAA,EACJ;AAAA,EACA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MACAA,WACA,uBACA,WACW;AACX,MAAI,UAAU,GAAG,MAAM,GAAG,IAAI;AAC9B,YAAU,GAAGA,SAAQ,GAAG,wBAAwB,KAAK,GAAG,GAAG,OAAO;AAClE,MAAI,QAAQ;AACV,eAAW;AAAA,EACb;AACA,SAAO;AACT;AAEA,MAAM,aAAa,CACjB,SACA,SACA,WACiB;AACjB,QAAM,WAAW,2BAAa,QAAQ,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AACnE,WAAS,QAAQ,IAAI,YAAY,MAAM;AACvC,SAAO;AACT;AAEA,MAAM,cAAc,CAAC,SAAsB,YACzC,2BAAa,SAAS,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;","names":["basePath"]}
1
+ {"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import { type Locales, intlayerConfiguration } from '@intlayer/config/client';\nimport { type NextRequest, NextResponse } from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = intlayerConfiguration;\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\n\n/**\n * Middleware that handles the internationalization layer\n *\n * Usage:\n *\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 */\nexport const intlayerMiddleware = (request: NextRequest): NextResponse => {\n const pathname = request.nextUrl.pathname;\n const cookieLocale = getCookieLocale(request);\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (noPrefix) {\n return handleNoPrefix(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n const pathLocale = getPathLocale(pathname);\n return handlePrefix(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\nconst getCookieLocale = (request: NextRequest): Locales | undefined => {\n if (!cookieName) return undefined;\n const cookieValue = request.cookies.get(cookieName)?.value as Locales;\n if (cookieValue && locales.includes(cookieValue)) {\n return cookieValue;\n }\n};\n\nconst handleNoPrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n const locale = cookieLocale ?? defaultLocale;\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash\n );\n return rewriteUrl(request, newPath, locale);\n};\n\nconst getPathLocale = (pathname: string): Locales | undefined =>\n locales.find(\n (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`\n );\n\nconst handlePrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (!pathLocale) {\n return handleMissingPathLocale(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n return handleExistingPathLocale(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\nconst handleMissingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n let locale = cookieLocale ?? localeDetector?.(request) ?? defaultLocale;\n if (!locales.includes(locale)) {\n console.warn(\n 'The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale.'\n );\n locale = defaultLocale;\n }\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash\n );\n return prefixDefault || locale !== defaultLocale\n ? redirectUrl(request, newPath)\n : rewriteUrl(request, newPath, locale);\n};\n\nconst handleExistingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n cookieLocale,\n basePath,\n basePathTrailingSlash\n );\n return redirectUrl(request, newPath);\n }\n\n return handleDefaultLocaleRedirect(\n request,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n\n pathname: string,\n pathLocale: Locales,\n cookieLocale: Locales,\n basePath: string,\n basePathTrailingSlash: boolean\n): string => {\n const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);\n return constructPath(\n cookieLocale,\n newPath,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\n );\n};\n\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (!prefixDefault && pathLocale === defaultLocale) {\n let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n\n if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);\n }\n return rewriteUrl(request, pathname, pathLocale);\n};\n\nconst constructPath = (\n locale: Locales,\n path: string,\n basePath: string,\n basePathTrailingSlash: boolean,\n search?: string\n): string => {\n let newPath = `${locale}${path}`;\n newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;\n if (search) {\n newPath += search;\n }\n return newPath;\n};\n\nconst rewriteUrl = (\n request: NextRequest,\n newPath: string,\n locale: Locales\n): NextResponse => {\n const response = NextResponse.rewrite(new URL(newPath, request.url));\n response.headers.set(headerName, locale);\n return response;\n};\n\nconst redirectUrl = (request: NextRequest, newPath: string): NextResponse =>\n NextResponse.redirect(new URL(newPath, request.url));\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoD;AACpD,oBAA+C;AAC/C,4BAA+B;AAE/B,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC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,SAAO,WAAW,SAAS,UAAU,UAAU;AACjD;AAEA,MAAM,gBAAgB,CACpB,QACA,MACAA,WACA,uBACA,WACW;AACX,MAAI,UAAU,GAAG,MAAM,GAAG,IAAI;AAC9B,YAAU,GAAGA,SAAQ,GAAG,wBAAwB,KAAK,GAAG,GAAG,OAAO;AAClE,MAAI,QAAQ;AACV,eAAW;AAAA,EACb;AACA,SAAO;AACT;AAEA,MAAM,aAAa,CACjB,SACA,SACA,WACiB;AACjB,QAAM,WAAW,2BAAa,QAAQ,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AACnE,WAAS,QAAQ,IAAI,YAAY,MAAM;AACvC,SAAO;AACT;AAEA,MAAM,cAAc,CAAC,SAAsB,YACzC,2BAAa,SAAS,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;","names":["basePath"]}
@@ -1,5 +1,20 @@
1
- import { NextRequest, NextResponse } from "next/server";
1
+ import { NextRequest, NextResponse } from 'next/server';
2
2
 
3
+ /**
4
+ * Middleware that handles the internationalization layer
5
+ *
6
+ * Usage:
7
+ *
8
+ * // ./src/middleware.ts
9
+ *
10
+ * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';
11
+ *
12
+ * // applies this middleware only to files in the app directory
13
+ * export const config = {
14
+ * matcher: '/((?!api|static|.*\\..*|_next).*)',
15
+ * };
16
+ *
17
+ */
3
18
  declare const intlayerMiddleware: (request: NextRequest) => NextResponse;
4
19
 
5
20
  export { intlayerMiddleware };
@@ -10,53 +10,37 @@ var __export = (target, all) => {
10
10
  __defProp(target, name, { get: all[name], enumerable: true });
11
11
  };
12
12
  var __copyProps = (to, from, except, desc) => {
13
- if ((from && typeof from === "object") || typeof from === "function") {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
14
  for (let key of __getOwnPropNames(from))
15
15
  if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, {
17
- get: () => from[key],
18
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
19
- });
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
17
  }
21
18
  return to;
22
19
  };
23
- var __toESM = (mod, isNodeMode, target) => (
24
- (target = mod != null ? __create(__getProtoOf(mod)) : {}),
25
- __copyProps(
26
- // If the importer is in node compatibility mode or this is not an ESM
27
- // file that has been converted to a CommonJS file using a Babel-
28
- // compatible transform (i.e. "__esModule" has not been set), then set
29
- // "default" to the CommonJS "module.exports" for node compatibility.
30
- isNodeMode || !mod || !mod.__esModule
31
- ? __defProp(target, "default", { value: mod, enumerable: true })
32
- : target,
33
- mod
34
- )
35
- );
36
- var __toCommonJS = (mod) =>
37
- __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
29
  var localeDetector_exports = {};
39
30
  __export(localeDetector_exports, {
40
- localeDetector: () => localeDetector,
31
+ localeDetector: () => localeDetector
41
32
  });
42
33
  module.exports = __toCommonJS(localeDetector_exports);
43
34
  var import_intl_localematcher = require("@formatjs/intl-localematcher");
44
35
  var import_client = require("@intlayer/config/client");
45
36
  var import_negotiator = __toESM(require("negotiator"));
46
- const { locales, defaultLocale } = (0, import_client.getConfiguration)()
47
- .internationalization;
37
+ const { locales, defaultLocale } = (0, import_client.getConfiguration)().internationalization;
48
38
  const localeDetector = (request) => {
49
39
  const negotiatorHeaders = {};
50
- request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));
51
- const languages = new import_negotiator.default({
52
- headers: negotiatorHeaders,
53
- }).languages();
40
+ request.headers.forEach((value, key) => negotiatorHeaders[key] = value);
41
+ const languages = new import_negotiator.default({ headers: negotiatorHeaders }).languages();
54
42
  try {
55
- return (0, import_intl_localematcher.match)(
56
- languages,
57
- locales,
58
- defaultLocale
59
- );
43
+ return (0, import_intl_localematcher.match)(languages, locales, defaultLocale);
60
44
  } catch (e) {
61
45
  console.warn(
62
46
  `No valid locales in accept-language header: ${languages.join(", ")}`
@@ -66,8 +50,7 @@ const localeDetector = (request) => {
66
50
  }
67
51
  };
68
52
  // Annotate the CommonJS export names for ESM import in node:
69
- 0 &&
70
- (module.exports = {
71
- localeDetector,
72
- });
73
- //# sourceMappingURL=localeDetector.cjs.map
53
+ 0 && (module.exports = {
54
+ localeDetector
55
+ });
56
+ //# sourceMappingURL=localeDetector.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/middleware/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport type { Locales } from '@intlayer/config';\nimport { getConfiguration } from '@intlayer/config/client';\nimport Negotiator from 'negotiator';\nimport type { NextRequest } from 'next/server.js';\n\nconst { locales, defaultLocale } = getConfiguration().internationalization;\n\nexport const localeDetector = (request: NextRequest): Locales => {\n const negotiatorHeaders: Record<string, string> = {};\n\n request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));\n\n const languages = new Negotiator({ headers: negotiatorHeaders }).languages();\n\n // match can only use specifically formatted locales\n // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided\n try {\n return match(languages, locales, defaultLocale) as Locales;\n } catch (e) {\n console.warn(\n `No valid locales in accept-language header: ${languages.join(', ')}`\n );\n console.warn(`Reverting to using defaultLocale: ${defaultLocale}`);\n\n return defaultLocale;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gCAAsB;AAEtB,oBAAiC;AACjC,wBAAuB;AAGvB,MAAM,EAAE,SAAS,cAAc,QAAI,gCAAiB,EAAE;AAE/C,MAAM,iBAAiB,CAAC,YAAkC;AAC/D,QAAM,oBAA4C,CAAC;AAEnD,UAAQ,QAAQ,QAAQ,CAAC,OAAO,QAAS,kBAAkB,GAAG,IAAI,KAAM;AAExE,QAAM,YAAY,IAAI,kBAAAA,QAAW,EAAE,SAAS,kBAAkB,CAAC,EAAE,UAAU;AAI3E,MAAI;AACF,eAAO,iCAAM,WAAW,SAAS,aAAa;AAAA,EAChD,SAAS,GAAG;AACV,YAAQ;AAAA,MACN,+CAA+C,UAAU,KAAK,IAAI,CAAC;AAAA,IACrE;AACA,YAAQ,KAAK,qCAAqC,aAAa,EAAE;AAEjE,WAAO;AAAA,EACT;AACF;","names":["Negotiator"]}
1
+ {"version":3,"sources":["../../../src/middleware/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport type { Locales } from '@intlayer/config';\nimport { getConfiguration } from '@intlayer/config/client';\nimport Negotiator from 'negotiator';\nimport type { NextRequest } from 'next/server.js';\n\nconst { locales, defaultLocale } = getConfiguration().internationalization;\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): Locales => {\n const negotiatorHeaders: Record<string, string> = {};\n\n request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));\n\n const languages = new Negotiator({ headers: negotiatorHeaders }).languages();\n\n // match can only use specifically formatted locales\n // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided\n try {\n return match(languages, locales, defaultLocale) as Locales;\n } catch (e) {\n console.warn(\n `No valid locales in accept-language header: ${languages.join(', ')}`\n );\n console.warn(`Reverting to using defaultLocale: ${defaultLocale}`);\n\n return defaultLocale;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gCAAsB;AAEtB,oBAAiC;AACjC,wBAAuB;AAGvB,MAAM,EAAE,SAAS,cAAc,QAAI,gCAAiB,EAAE;AAO/C,MAAM,iBAAiB,CAAC,YAAkC;AAC/D,QAAM,oBAA4C,CAAC;AAEnD,UAAQ,QAAQ,QAAQ,CAAC,OAAO,QAAS,kBAAkB,GAAG,IAAI,KAAM;AAExE,QAAM,YAAY,IAAI,kBAAAA,QAAW,EAAE,SAAS,kBAAkB,CAAC,EAAE,UAAU;AAI3E,MAAI;AACF,eAAO,iCAAM,WAAW,SAAS,aAAa;AAAA,EAChD,SAAS,GAAG;AACV,YAAQ;AAAA,MACN,+CAA+C,UAAU,KAAK,IAAI,CAAC;AAAA,IACrE;AACA,YAAQ,KAAK,qCAAqC,aAAa,EAAE;AAEjE,WAAO;AAAA,EACT;AACF;","names":["Negotiator"]}
@@ -1,6 +1,11 @@
1
- import { Locales } from "@intlayer/config";
2
- import { NextRequest } from "next/server.js";
1
+ import { Locales } from '@intlayer/config';
2
+ import { NextRequest } from 'next/server.js';
3
3
 
4
+ /**
5
+ * Detects the locale from the request headers
6
+ *
7
+ * Headers are provided by the browser and can be used to determine the user's preferred language
8
+ */
4
9
  declare const localeDetector: (request: NextRequest) => Locales;
5
10
 
6
11
  export { localeDetector };
@@ -8,40 +8,35 @@ var __export = (target, all) => {
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
10
  var __copyProps = (to, from, except, desc) => {
11
- if ((from && typeof from === "object") || typeof from === "function") {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
12
  for (let key of __getOwnPropNames(from))
13
13
  if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, {
15
- get: () => from[key],
16
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
17
- });
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
15
  }
19
16
  return to;
20
17
  };
21
- var __toCommonJS = (mod) =>
22
- __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
19
  var server_exports = {};
24
20
  __export(server_exports, {
25
- LocaleServerContext: () => import_server.LocaleServerContext,
26
- LocaleServerContextProvider: () => import_server.LocaleServerContextProvider,
21
+ IntlayerServer: () => import_server.IntlayerServer,
22
+ IntlayerServerProvider: () => import_server.IntlayerServerProvider,
27
23
  getLocaleContent: () => import_server.getLocaleContent,
28
24
  locale: () => import_server.locale,
29
25
  useIntlayer: () => import_server.useIntlayer,
30
26
  useTraduction: () => import_server.useTraduction,
31
- withIntlayer: () => import_withIntlayer.withIntlayer,
27
+ withIntlayer: () => import_withIntlayer.withIntlayer
32
28
  });
33
29
  module.exports = __toCommonJS(server_exports);
34
30
  var import_server = require("react-intlayer/server");
35
- var import_withIntlayer = require("./withIntlayer.cjs");
31
+ var import_withIntlayer = require('./withIntlayer.cjs');
36
32
  // Annotate the CommonJS export names for ESM import in node:
37
- 0 &&
38
- (module.exports = {
39
- LocaleServerContext,
40
- LocaleServerContextProvider,
41
- getLocaleContent,
42
- locale,
43
- useIntlayer,
44
- useTraduction,
45
- withIntlayer,
46
- });
47
- //# sourceMappingURL=index.cjs.map
33
+ 0 && (module.exports = {
34
+ IntlayerServer,
35
+ IntlayerServerProvider,
36
+ getLocaleContent,
37
+ locale,
38
+ useIntlayer,
39
+ useTraduction,
40
+ withIntlayer
41
+ });
42
+ //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export {\n getLocaleContent,\n useTraduction,\n LocaleServerContext,\n LocaleServerContextProvider,\n locale,\n useIntlayer,\n} from 'react-intlayer/server';\nexport { withIntlayer } from './withIntlayer';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAOO;AACP,0BAA6B;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export {\n getLocaleContent,\n useTraduction,\n IntlayerServer,\n IntlayerServerProvider,\n locale,\n useIntlayer,\n} from 'react-intlayer/server';\nexport { withIntlayer } from './withIntlayer';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAOO;AACP,0BAA6B;","names":[]}
@@ -1,10 +1,3 @@
1
- export {
2
- LocaleServerContext,
3
- LocaleServerContextProvider,
4
- getLocaleContent,
5
- locale,
6
- useIntlayer,
7
- useTraduction,
8
- } from "react-intlayer/server";
9
- export { withIntlayer } from "./withIntlayer.js";
10
- import "next";
1
+ export { IntlayerServer, IntlayerServerProvider, getLocaleContent, locale, useIntlayer, useTraduction } from 'react-intlayer/server';
2
+ export { withIntlayer } from './withIntlayer.js';
3
+ import 'next';
@@ -8,69 +8,54 @@ var __export = (target, all) => {
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
10
  var __copyProps = (to, from, except, desc) => {
11
- if ((from && typeof from === "object") || typeof from === "function") {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
12
  for (let key of __getOwnPropNames(from))
13
13
  if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, {
15
- get: () => from[key],
16
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
17
- });
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
15
  }
19
16
  return to;
20
17
  };
21
- var __toCommonJS = (mod) =>
22
- __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
19
  var withIntlayer_exports = {};
24
20
  __export(withIntlayer_exports, {
25
- withIntlayer: () => withIntlayer,
21
+ withIntlayer: () => withIntlayer
26
22
  });
27
23
  module.exports = __toCommonJS(withIntlayer_exports);
28
24
  var import_path = require("path");
29
25
  var import_config = require("@intlayer/config");
30
- const intlayerConfig = (0, import_config.getConfiguration)({
31
- verbose: true,
32
- });
33
- const env = (0, import_config.formatEnvVariable)("NEXT_PUBLIC_INTLAYER_");
34
- const { mainDir, baseDir } = intlayerConfig.content;
35
- const mergeEnvVariable = (nextEnv = {}) => Object.assign({}, nextEnv, env);
36
- const mergeStats = (nextStats = {}) =>
37
- Object.assign({}, nextStats, {
38
- warnings: false,
26
+ var import_webpack = require("@intlayer/webpack");
27
+ const withIntlayer = (_pluginOptions = {}) => (nextConfig = {}) => {
28
+ if (typeof nextConfig !== "object")
29
+ nextConfig = {};
30
+ const intlayerConfig = (0, import_config.getConfiguration)();
31
+ const env = (0, import_config.formatEnvVariable)("next");
32
+ const { mainDir, baseDir } = intlayerConfig.content;
33
+ return Object.assign({}, nextConfig, {
34
+ env: { ...nextConfig.env, ...env },
35
+ webpack: (config, { isServer, nextRuntime }) => {
36
+ const dictionariesPath = (0, import_path.join)(mainDir, "dictionaries.cjs");
37
+ const relativeDictionariesPath = (0, import_path.relative)(baseDir, dictionariesPath);
38
+ config.resolve.alias["@intlayer/dictionaries-entry"] = (0, import_path.resolve)(
39
+ relativeDictionariesPath
40
+ );
41
+ config.externals.push({
42
+ esbuild: "esbuild",
43
+ module: "module",
44
+ fs: "fs"
45
+ });
46
+ config.module.rules.push({
47
+ test: /\.node$/,
48
+ loader: "node-loader"
49
+ });
50
+ if (isServer && nextRuntime === "nodejs") {
51
+ config.plugins.push(new import_webpack.IntLayerPlugin());
52
+ }
53
+ return config;
54
+ }
39
55
  });
40
- const withIntlayer =
41
- (_pluginOptions = {}) =>
42
- (nextConfig = {}) => {
43
- if (typeof nextConfig !== "object") nextConfig = {};
44
- return Object.assign({}, nextConfig, {
45
- env: mergeEnvVariable(nextConfig.env),
46
- stats: mergeStats(nextConfig.stats),
47
- webpack: (config) => {
48
- const dictionariesPath = (0, import_path.join)(
49
- mainDir,
50
- "dictionaries.cjs"
51
- );
52
- const relativeDictionariesPath = (0, import_path.relative)(
53
- baseDir,
54
- dictionariesPath
55
- );
56
- config.externals.push({
57
- esbuild: "esbuild",
58
- module: "module",
59
- fs: "fs",
60
- });
61
- config.resolve.alias["@intlayer/dictionaries-entry"] = (0,
62
- import_path.resolve)(relativeDictionariesPath);
63
- config.module.rules.push({
64
- test: /\.node$/,
65
- loader: "node-loader",
66
- });
67
- return config;
68
- },
69
- });
70
- };
56
+ };
71
57
  // Annotate the CommonJS export names for ESM import in node:
72
- 0 &&
73
- (module.exports = {
74
- withIntlayer,
75
- });
76
- //# sourceMappingURL=withIntlayer.cjs.map
58
+ 0 && (module.exports = {
59
+ withIntlayer
60
+ });
61
+ //# sourceMappingURL=withIntlayer.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { resolve, relative, join } from 'path';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\n\ntype PluginOptions = {\n // TODO: add options\n};\n\nconst intlayerConfig = getConfiguration({\n verbose: true,\n});\n\n// Set all configuration values as environment variables\nconst env = formatEnvVariable('NEXT_PUBLIC_INTLAYER_');\nconst { mainDir, baseDir } = intlayerConfig.content;\n\nconst mergeEnvVariable = (\n nextEnv: Record<string, unknown> | undefined = {}\n): Record<string, string> => Object.assign({}, nextEnv, env);\n\nconst mergeStats = (\n nextStats: Record<string, unknown> | undefined = {}\n): Record<string, unknown> =>\n Object.assign({}, nextStats, {\n warnings: false,\n });\n\nexport const withIntlayer =\n (_pluginOptions: PluginOptions = {}) =>\n (nextConfig: Partial<NextConfig> = {}): Partial<NextConfig> => {\n if (typeof nextConfig !== 'object') nextConfig = {};\n\n return Object.assign({}, nextConfig, {\n env: mergeEnvVariable(nextConfig.env),\n\n stats: mergeStats(nextConfig.stats),\n\n webpack: (config: Parameters<NextJsWebpackConfig>['0']) => {\n const dictionariesPath = join(mainDir, 'dictionaries.cjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n config.externals.push({\n esbuild: 'esbuild',\n module: 'module',\n fs: 'fs',\n });\n\n config.resolve.alias['@intlayer/dictionaries-entry'] = resolve(\n relativeDictionariesPath\n );\n\n config.module.rules.push({\n test: /\\.node$/,\n loader: 'node-loader',\n });\n\n return config;\n },\n });\n };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwC;AACxC,oBAAoD;AAQpD,MAAM,qBAAiB,gCAAiB;AAAA,EACtC,SAAS;AACX,CAAC;AAGD,MAAM,UAAM,iCAAkB,uBAAuB;AACrD,MAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAE5C,MAAM,mBAAmB,CACvB,UAA+C,CAAC,MACrB,OAAO,OAAO,CAAC,GAAG,SAAS,GAAG;AAE3D,MAAM,aAAa,CACjB,YAAiD,CAAC,MAElD,OAAO,OAAO,CAAC,GAAG,WAAW;AAAA,EAC3B,UAAU;AACZ,CAAC;AAEI,MAAM,eACX,CAAC,iBAAgC,CAAC,MAClC,CAAC,aAAkC,CAAC,MAA2B;AAC7D,MAAI,OAAO,eAAe;AAAU,iBAAa,CAAC;AAElD,SAAO,OAAO,OAAO,CAAC,GAAG,YAAY;AAAA,IACnC,KAAK,iBAAiB,WAAW,GAAG;AAAA,IAEpC,OAAO,WAAW,WAAW,KAAK;AAAA,IAElC,SAAS,CAAC,WAAiD;AACzD,YAAM,uBAAmB,kBAAK,SAAS,kBAAkB;AACzD,YAAM,+BAA2B,sBAAS,SAAS,gBAAgB;AAEnE,aAAO,UAAU,KAAK;AAAA,QACpB,SAAS;AAAA,QACT,QAAQ;AAAA,QACR,IAAI;AAAA,MACN,CAAC;AAED,aAAO,QAAQ,MAAM,8BAA8B,QAAI;AAAA,QACrD;AAAA,MACF;AAEA,aAAO,OAAO,MAAM,KAAK;AAAA,QACvB,MAAM;AAAA,QACN,QAAQ;AAAA,MACV,CAAC;AAED,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { resolve, relative, join } from 'path';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport { IntLayerPlugin } from '@intlayer/webpack';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\n\ntype PluginOptions = {\n // TODO: add options\n};\n\ntype WebpackParams = Parameters<NextJsWebpackConfig>;\n\n/**\n * A Next.js plugin that adds the intlayer configuration to the webpack configuration\n * and sets the environment variables\n *\n * Usage:\n *\n * // next.config.js\n * export default withIntlayer(nextConfig)\n *\n */\nexport const withIntlayer =\n (_pluginOptions: PluginOptions = {}) =>\n (nextConfig: Partial<NextConfig> = {}): Partial<NextConfig> => {\n if (typeof nextConfig !== 'object') nextConfig = {};\n\n const intlayerConfig = getConfiguration();\n\n // Set all configuration values as environment variables\n const env = formatEnvVariable('next');\n\n const { mainDir, baseDir } = intlayerConfig.content;\n\n return Object.assign({}, nextConfig, {\n env: { ...nextConfig.env, ...env },\n\n webpack: (\n config: WebpackParams['0'],\n { isServer, nextRuntime }: WebpackParams[1]\n ) => {\n const dictionariesPath = join(mainDir, 'dictionaries.cjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n config.resolve.alias['@intlayer/dictionaries-entry'] = resolve(\n relativeDictionariesPath\n );\n\n config.externals.push({\n esbuild: 'esbuild',\n module: 'module',\n fs: 'fs',\n });\n config.module.rules.push({\n test: /\\.node$/,\n loader: 'node-loader',\n });\n\n // Apply IntLayerPlugin only on the server-side\n if (isServer && nextRuntime === 'nodejs') {\n config.plugins.push(new IntLayerPlugin());\n }\n\n return config;\n },\n });\n };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwC;AACxC,oBAAoD;AACpD,qBAA+B;AAoBxB,MAAM,eACX,CAAC,iBAAgC,CAAC,MAClC,CAAC,aAAkC,CAAC,MAA2B;AAC7D,MAAI,OAAO,eAAe;AAAU,iBAAa,CAAC;AAElD,QAAM,qBAAiB,gCAAiB;AAGxC,QAAM,UAAM,iCAAkB,MAAM;AAEpC,QAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAE5C,SAAO,OAAO,OAAO,CAAC,GAAG,YAAY;AAAA,IACnC,KAAK,EAAE,GAAG,WAAW,KAAK,GAAG,IAAI;AAAA,IAEjC,SAAS,CACP,QACA,EAAE,UAAU,YAAY,MACrB;AACH,YAAM,uBAAmB,kBAAK,SAAS,kBAAkB;AACzD,YAAM,+BAA2B,sBAAS,SAAS,gBAAgB;AAEnE,aAAO,QAAQ,MAAM,8BAA8B,QAAI;AAAA,QACrD;AAAA,MACF;AAEA,aAAO,UAAU,KAAK;AAAA,QACpB,SAAS;AAAA,QACT,QAAQ;AAAA,QACR,IAAI;AAAA,MACN,CAAC;AACD,aAAO,OAAO,MAAM,KAAK;AAAA,QACvB,MAAM;AAAA,QACN,QAAQ;AAAA,MACV,CAAC;AAGD,UAAI,YAAY,gBAAgB,UAAU;AACxC,eAAO,QAAQ,KAAK,IAAI,8BAAe,CAAC;AAAA,MAC1C;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -1,9 +1,17 @@
1
- import "react-intlayer/server";
2
- import { NextConfig } from "next";
1
+ import 'react-intlayer/server';
2
+ import { NextConfig } from 'next';
3
3
 
4
4
  type PluginOptions = {};
5
- declare const withIntlayer: (
6
- _pluginOptions?: PluginOptions
7
- ) => (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
5
+ /**
6
+ * A Next.js plugin that adds the intlayer configuration to the webpack configuration
7
+ * and sets the environment variables
8
+ *
9
+ * Usage:
10
+ *
11
+ * // next.config.js
12
+ * export default withIntlayer(nextConfig)
13
+ *
14
+ */
15
+ declare const withIntlayer: (_pluginOptions?: PluginOptions) => (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
8
16
 
9
17
  export { withIntlayer };
@@ -4,18 +4,14 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
6
  var __copyProps = (to, from, except, desc) => {
7
- if ((from && typeof from === "object") || typeof from === "function") {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
8
  for (let key of __getOwnPropNames(from))
9
9
  if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, {
11
- get: () => from[key],
12
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
13
- });
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
11
  }
15
12
  return to;
16
13
  };
17
- var __toCommonJS = (mod) =>
18
- __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
15
  var NextPage_exports = {};
20
16
  module.exports = __toCommonJS(NextPage_exports);
21
- //# sourceMappingURL=NextPage.cjs.map
17
+ //# sourceMappingURL=NextPage.cjs.map
@@ -1,11 +1,11 @@
1
- import { Locales } from "intlayer";
2
- import { NextPage } from "next";
3
- import { PropsWithChildren } from "react";
1
+ import { Locales } from 'intlayer';
2
+ import { NextPage } from 'next';
3
+ import { PropsWithChildren } from 'react';
4
4
 
5
5
  type LocalParams = {
6
- params: {
7
- locale: Locales;
8
- };
6
+ params: {
7
+ locale: Locales;
8
+ };
9
9
  };
10
10
  type NextPageIntlayer = NextPage<LocalParams>;
11
11
  type NextLayoutIntlayer = NextPage<PropsWithChildren<LocalParams>>;