vite-intlayer 6.0.0-canary.1 → 6.0.0-canary.3

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/README.md CHANGED
@@ -88,26 +88,10 @@ yarn add vite-intlayer
88
88
  ```ts
89
89
  // vite.config.ts
90
90
  import { defineConfig } from "vite";
91
- import { intlayerPlugin } from "vite-intlayer";
91
+ import { intlayer } from "vite-intlayer";
92
92
 
93
93
  export default defineConfig({
94
- plugins: [intlayerPlugin()],
95
- });
96
- ```
97
-
98
- ### Configuration Options
99
-
100
- ```ts
101
- // vite.config.ts
102
- import { defineConfig } from "vite";
103
- import { intlayerPlugin } from "vite-intlayer";
104
-
105
- export default defineConfig({
106
- plugins: [
107
- intlayerPlugin({
108
- enableBabelTransform: true, // Enable automatic useIntlayer transformation (default: true)
109
- }),
110
- ],
94
+ plugins: [intlayer()],
111
95
  });
112
96
  ```
113
97
 
@@ -164,37 +148,6 @@ function MyComponent() {
164
148
  5. **Individual File Loading**: Only the dictionaries you use are imported, reducing bundle size
165
149
  6. **Better Caching**: Individual dictionary files can be cached separately by the browser
166
150
 
167
- ### Using the Babel Plugin Separately
168
-
169
- You can also use the Babel plugin independently:
170
-
171
- ```ts
172
- // babel.config.js
173
- import { babelPluginIntlayer } from "vite-intlayer";
174
-
175
- export default {
176
- plugins: [
177
- babelPluginIntlayer,
178
- // other plugins...
179
- ],
180
- };
181
- ```
182
-
183
- ### Disabling the Transformation
184
-
185
- If you prefer to use `useIntlayer` without transformation:
186
-
187
- ```ts
188
- // vite.config.ts
189
- export default defineConfig({
190
- plugins: [
191
- intlayerPlugin({
192
- enableBabelTransform: false,
193
- }),
194
- ],
195
- });
196
- ```
197
-
198
151
  ## How It Works
199
152
 
200
153
  1. **Dictionary Resolution**: The plugin resolves paths to generated dictionaries and configurations
@@ -202,13 +155,6 @@ export default defineConfig({
202
155
  3. **Watch Mode**: In development, watches for dictionary changes and triggers rebuilds
203
156
  4. **Babel Transformation**: Transforms `useIntlayer` calls to `useDictionary` calls for better performance
204
157
 
205
- ## Related Packages
206
-
207
- - [`@intlayer/dictionaries-entry`](../dictionaries-entry): Dictionary entry point package
208
- - [`react-intlayer`](../react-intlayer): React integration for Intlayer
209
- - [`@intlayer/config`](../config): Configuration management
210
- - [`@intlayer/chokidar`](../chokidar): File watching and dictionary building
211
-
212
158
  ## License
213
159
 
214
160
  Apache-2.0
@@ -36,7 +36,7 @@ const {
36
36
  serverSetCookie,
37
37
  basePath = ""
38
38
  } = middleware;
39
- const intlayerMiddleware = () => {
39
+ const intlayerMiddlewarePlugin = () => {
40
40
  return {
41
41
  name: "vite-intlayer-middleware-plugin",
42
42
  configureServer: (server) => {
@@ -232,7 +232,7 @@ const handleDefaultLocaleRedirect = ({
232
232
  rewriteUrl(req, res, originalPath, pathLocale);
233
233
  return next();
234
234
  };
235
- const intlayerMiddlewarePlugin = intlayerMiddleware;
235
+ const intlayerMiddleware = intlayerMiddlewarePlugin;
236
236
  // Annotate the CommonJS export names for ESM import in node:
237
237
  0 && (module.exports = {
238
238
  intlayerMiddleware,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\nimport type { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\n/* @ts-ignore - Vite types error */\nimport type { Connect, Plugin } from 'vite';\n\n// Grab all the config you need.\n// Make sure your config includes the following fields if you want to replicate Next.js logic:\n// - internationalization.locales\n// - internationalization.defaultLocale\n// - middleware.cookieName\n// - middleware.headerName\n// - middleware.prefixDefault\n// - middleware.noPrefix\n// - middleware.serverSetCookie\n// - middleware.basePath\n// - etc.\nconst intlayerConfig = getConfiguration();\nconst { internationalization, middleware } = intlayerConfig;\nconst { locales: supportedLocales, defaultLocale } = internationalization;\n\nconst {\n cookieName,\n headerName,\n prefixDefault,\n noPrefix,\n serverSetCookie,\n basePath = '',\n} = middleware;\n\n/**\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n */\nexport const intlayerMiddleware = (): Plugin => {\n return {\n name: 'vite-intlayer-middleware-plugin',\n configureServer: (server) => {\n server.middlewares.use((req, res, next) => {\n // 1. Bypass assets and special Vite endpoints\n if (\n req.url?.startsWith('/node_modules') ||\n req.url?.startsWith('/@') ||\n req.url?.split('?')[0].match(/\\.[a-z]+$/i) // checks for file extensions\n ) {\n return next();\n }\n\n // 2. Parse original URL for path and query\n const parsedUrl = parse(req.url ?? '/', true);\n const originalPath = parsedUrl.pathname ?? '/';\n\n // 3. Attempt to read the cookie locale\n const cookies = parseCookies(req.headers.cookie ?? '');\n const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);\n\n // 4. Check if there's a locale prefix in the path\n const pathLocale = getPathLocale(originalPath);\n\n // 5. If noPrefix is true, we skip prefix logic altogether\n if (noPrefix) {\n handleNoPrefix({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 6. Otherwise, handle prefix logic\n handlePrefix({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n });\n },\n };\n};\n\n/* --------------------------------------------------------------------\n * Helper & Utility Functions\n * --------------------------------------------------------------------\n */\n\n/**\n * Parses cookies from the Cookie header string into an object.\n */\nconst parseCookies = (cookieHeader: string) => {\n return cookieHeader.split(';').reduce(\n (acc, cookie) => {\n const [key, val] = cookie.trim().split('=');\n acc[key] = val;\n return acc;\n },\n {} as Record<string, string>\n );\n};\n\n/**\n * Checks if the cookie locale is valid and is included in the supported locales.\n */\nconst getValidLocaleFromCookie = (\n locale: string | undefined\n): Locales | undefined => {\n if (locale && supportedLocales.includes(locale as Locales)) {\n return locale as Locales;\n }\n return undefined;\n};\n\n/**\n * Extracts the locale from the URL pathname if present as the first segment.\n */\nconst getPathLocale = (pathname: string): Locales | undefined => {\n // e.g. if pathname is /en/some/page or /en\n // we check if \"en\" is in your supportedLocales\n const segments = pathname.split('/').filter(Boolean);\n const firstSegment = segments[0];\n if (firstSegment && supportedLocales.includes(firstSegment as Locales)) {\n return firstSegment as Locales;\n }\n return undefined;\n};\n\n/**\n * Writes a 301 redirect response with the given new URL.\n */\nconst redirectUrl = (res: ServerResponse<IncomingMessage>, newUrl: string) => {\n res.writeHead(301, { Location: newUrl });\n return res.end();\n};\n\n/**\n * \"Rewrite\" the request internally by adjusting req.url;\n * we also set the locale in the response header if needed.\n */\nconst rewriteUrl = (\n req: Connect.IncomingMessage,\n res: ServerResponse<IncomingMessage>,\n newUrl: string,\n locale?: Locales\n) => {\n req.url = newUrl;\n // If you want to mimic Next.js's behavior of setting a header for the locale:\n if (locale && headerName) {\n res.setHeader(headerName, locale);\n }\n};\n\n/**\n * Constructs a new path string, optionally including a locale prefix and basePath.\n * - basePath: (e.g., '/myapp')\n * - locale: (e.g., 'en')\n * - currentPath:(e.g., '/products/shoes')\n */\nconst constructPath = (locale: Locales, currentPath: string) => {\n // Ensure basePath always starts with '/', and remove trailing slash if needed\n const cleanBasePath = basePath.startsWith('/') ? basePath : `/${basePath}`;\n // If basePath is '/', no trailing slash is needed\n const normalizedBasePath = cleanBasePath === '/' ? '' : cleanBasePath;\n\n // Combine basePath + locale + the rest of the path\n // Example: basePath = '/myapp', locale = 'en', currentPath = '/products' => '/myapp/en/products'\n let newPath = `${normalizedBasePath}/${locale}${currentPath}`;\n\n // Special case: if prefixDefault is false and locale is defaultLocale, remove the locale prefix\n if (!prefixDefault && locale === defaultLocale) {\n newPath = `${normalizedBasePath}${currentPath}`;\n }\n\n return newPath;\n};\n\n/* --------------------------------------------------------------------\n * Handlers that mirror Next.js style logic\n * --------------------------------------------------------------------\n */\n\n/**\n * If `noPrefix` is true, we never prefix the locale in the URL.\n * We simply rewrite the request to the same path, but with the best-chosen locale\n * in a header or cookie if desired.\n */\nconst handleNoPrefix = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // Determine the best locale\n let locale = cookieLocale ?? defaultLocale;\n\n // Use fallback to localeDetector if no cookie\n if (!cookieLocale) {\n const detectedLocale = localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale as Locales;\n }\n\n // Just rewrite the URL in-place (no prefix). We do NOT redirect because we do not want to alter the URL.\n rewriteUrl(req, res, originalPath, locale);\n return next();\n};\n\n/**\n * The main prefix logic:\n * - If there's no pathLocale in the URL, we might want to detect & redirect or rewrite\n * - If there is a pathLocale, handle cookie mismatch or default locale special cases\n */\nconst handlePrefix = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale?: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If pathLocale is missing, handle\n if (!pathLocale) {\n handleMissingPathLocale({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 2. If pathLocale exists, handle possible mismatch with cookie\n handleExistingPathLocale({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n * We detect a locale from cookie / headers / default, then either redirect or rewrite.\n */\nconst handleMissingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // If navigation comes from the same origin (e.g., via an in-app language switcher),\n // treat unprefixed paths as an explicit intent to view the default locale.\n // This avoids redirecting back to the cookie locale (e.g., '/tr/') when user selects '/'.\n const referer = (req.headers.referer || req.headers.referrer) as\n | string\n | undefined;\n if (referer) {\n try {\n const refererUrl = new URL(referer);\n const host = req.headers.host;\n if (host && refererUrl.host === host) {\n const locale = defaultLocale as Locales;\n const newPath = constructPath(locale, originalPath);\n rewriteUrl(req, res, newPath, locale);\n return next();\n }\n } catch {\n // ignore invalid referer\n }\n }\n\n // 1. Choose the best locale\n let locale = (cookieLocale ??\n localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n )) as Locales;\n\n // 2. If still invalid, fallback\n if (!supportedLocales.includes(locale)) {\n locale = defaultLocale;\n }\n\n // 3. Construct new path\n const newPath = constructPath(locale, originalPath);\n\n // If we always prefix default or if this is not the default locale, do a 301 redirect\n // so that the user sees the locale in the URL.\n if (prefixDefault || locale !== defaultLocale) {\n return redirectUrl(res, newPath);\n }\n\n // If we do NOT prefix the default locale, just rewrite in place\n rewriteUrl(req, res, newPath, locale);\n return next();\n};\n\n/**\n * Handles requests where the locale prefix is present in the pathname.\n * We verify if the cookie locale differs from the path locale; if so, handle.\n */\nconst handleExistingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If the cookie locale is set and differs from the path locale,\n // and we're not forcing the cookie to always override\n if (\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n // We want to swap out the pathLocale with the cookieLocale\n const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);\n const finalPath = constructPath(cookieLocale, newPath.replace(/^\\/+/, '/'));\n return redirectUrl(res, finalPath);\n }\n\n // 2. Otherwise, handle default-locale prefix if needed\n handleDefaultLocaleRedirect({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n });\n};\n\n/**\n * If the path locale is the default locale but we don't want to prefix the default, remove it.\n */\nconst handleDefaultLocaleRedirect = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n}) => {\n // If we don't prefix default AND the path locale is the default locale -> remove it\n if (!prefixDefault && pathLocale === defaultLocale) {\n // Remove the default locale part from the path\n const newPath = originalPath.replace(`/${defaultLocale}`, '') ?? '/';\n rewriteUrl(req, res, newPath, pathLocale);\n return next();\n }\n\n // If we do prefix default or pathLocale != default, keep as is, but rewrite headers\n rewriteUrl(req, res, originalPath, pathLocale);\n return next();\n};\n\n/**\n * @deprecated Rename to intlayerMiddleware instead\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\n * ```\n */\nexport const intlayerMiddlewarePlugin = intlayerMiddleware;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,kBAA+B;AAE/B,iBAAsB;AAetB,MAAM,qBAAiB,gCAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,kBAAkB,cAAc,IAAI;AAErD,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AACb,IAAI;AAKG,MAAM,qBAAqB,MAAc;AAC9C,SAAO;AAAA,IACL,MAAM;AAAA,IACN,iBAAiB,CAAC,WAAW;AAC3B,aAAO,YAAY,IAAI,CAAC,KAAK,KAAK,SAAS;AAEzC,YACE,IAAI,KAAK,WAAW,eAAe,KACnC,IAAI,KAAK,WAAW,IAAI,KACxB,IAAI,KAAK,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,YAAY,GACzC;AACA,iBAAO,KAAK;AAAA,QACd;AAGA,cAAM,gBAAY,kBAAM,IAAI,OAAO,KAAK,IAAI;AAC5C,cAAM,eAAe,UAAU,YAAY;AAG3C,cAAM,UAAU,aAAa,IAAI,QAAQ,UAAU,EAAE;AACrD,cAAM,eAAe,yBAAyB,Q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AAAA,MACd;AAAA,IACF,QAAQ;AAAA,IAER;AAAA,EACF;AAGA,MAAI,SAAU,oBACZ;AAAA,IACE,IAAI;AAAA,IACJ;AAAA,IACA;AAAA,EACF;AAGF,MAAI,CAAC,iBAAiB,SAAS,MAAM,GAAG;AACtC,aAAS;AAAA,EACX;AAGA,QAAM,UAAU,cAAc,QAAQ,YAAY;AAIlD,MAAI,iBAAiB,WAAW,eAAe;AAC7C,WAAO,YAAY,KAAK,OAAO;AAAA,EACjC;AAGA,aAAW,KAAK,KAAK,SAAS,MAAM;AACpC,SAAO,KAAK;AACd;AAMA,MAAM,2BAA2B,CAAC;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAOM;AAGJ,MACE,gBACA,iBAAiB,cACjB,oBAAoB,UACpB;AAEA,UAAM,UAAU,aAAa,QAAQ,IAAI,UAAU,IAAI,IAAI,YAAY,EAAE;AACzE,UAAM,YAAY,cAAc,cAAc,QAAQ,QAAQ,QAAQ,GAAG,CAAC;AAC1E,WAAO,YAAY,KAAK,SAAS;AAAA,EACnC;AAGA,8BAA4B;AAAA,IAC1B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAKA,MAAM,8BAA8B,CAAC;AAAA,EACnC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAMM;AAEJ,MAAI,CAAC,iBAAiB,eAAe,eAAe;AAElD,UAAM,UAAU,aAAa,QAAQ,IAAI,aAAa,IAAI,EAAE,KAAK;AACjE,eAAW,KAAK,KAAK,SAAS,UAAU;AACxC,WAAO,KAAK;AAAA,EACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;AAYO,MAAM,2BAA2B;","names":["locale","newPath"]}
1
+ {"version":3,"sources":["../../src/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\nimport type { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\n/* @ts-ignore - Vite types error */\nimport type { Connect, Plugin } from 'vite';\n\n// Grab all the config you need.\n// Make sure your config includes the following fields if you want to replicate Next.js logic:\n// - internationalization.locales\n// - internationalization.defaultLocale\n// - middleware.cookieName\n// - middleware.headerName\n// - middleware.prefixDefault\n// - middleware.noPrefix\n// - middleware.serverSetCookie\n// - middleware.basePath\n// - etc.\nconst intlayerConfig = getConfiguration();\nconst { internationalization, middleware } = intlayerConfig;\nconst { locales: supportedLocales, defaultLocale } = internationalization;\n\nconst {\n cookieName,\n headerName,\n prefixDefault,\n noPrefix,\n serverSetCookie,\n basePath = '',\n} = middleware;\n\n/**\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n */\nexport const intlayerMiddlewarePlugin = (): Plugin => {\n return {\n name: 'vite-intlayer-middleware-plugin',\n configureServer: (server) => {\n server.middlewares.use((req, res, next) => {\n // 1. Bypass assets and special Vite endpoints\n if (\n req.url?.startsWith('/node_modules') ||\n req.url?.startsWith('/@') ||\n req.url?.split('?')[0].match(/\\.[a-z]+$/i) // checks for file extensions\n ) {\n return next();\n }\n\n // 2. Parse original URL for path and query\n const parsedUrl = parse(req.url ?? '/', true);\n const originalPath = parsedUrl.pathname ?? '/';\n\n // 3. Attempt to read the cookie locale\n const cookies = parseCookies(req.headers.cookie ?? '');\n const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);\n\n // 4. Check if there's a locale prefix in the path\n const pathLocale = getPathLocale(originalPath);\n\n // 5. If noPrefix is true, we skip prefix logic altogether\n if (noPrefix) {\n handleNoPrefix({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 6. Otherwise, handle prefix logic\n handlePrefix({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n });\n },\n };\n};\n\n/* --------------------------------------------------------------------\n * Helper & Utility Functions\n * --------------------------------------------------------------------\n */\n\n/**\n * Parses cookies from the Cookie header string into an object.\n */\nconst parseCookies = (cookieHeader: string) => {\n return cookieHeader.split(';').reduce(\n (acc, cookie) => {\n const [key, val] = cookie.trim().split('=');\n acc[key] = val;\n return acc;\n },\n {} as Record<string, string>\n );\n};\n\n/**\n * Checks if the cookie locale is valid and is included in the supported locales.\n */\nconst getValidLocaleFromCookie = (\n locale: string | undefined\n): Locales | undefined => {\n if (locale && supportedLocales.includes(locale as Locales)) {\n return locale as Locales;\n }\n return undefined;\n};\n\n/**\n * Extracts the locale from the URL pathname if present as the first segment.\n */\nconst getPathLocale = (pathname: string): Locales | undefined => {\n // e.g. if pathname is /en/some/page or /en\n // we check if \"en\" is in your supportedLocales\n const segments = pathname.split('/').filter(Boolean);\n const firstSegment = segments[0];\n if (firstSegment && supportedLocales.includes(firstSegment as Locales)) {\n return firstSegment as Locales;\n }\n return undefined;\n};\n\n/**\n * Writes a 301 redirect response with the given new URL.\n */\nconst redirectUrl = (res: ServerResponse<IncomingMessage>, newUrl: string) => {\n res.writeHead(301, { Location: newUrl });\n return res.end();\n};\n\n/**\n * \"Rewrite\" the request internally by adjusting req.url;\n * we also set the locale in the response header if needed.\n */\nconst rewriteUrl = (\n req: Connect.IncomingMessage,\n res: ServerResponse<IncomingMessage>,\n newUrl: string,\n locale?: Locales\n) => {\n req.url = newUrl;\n // If you want to mimic Next.js's behavior of setting a header for the locale:\n if (locale && headerName) {\n res.setHeader(headerName, locale);\n }\n};\n\n/**\n * Constructs a new path string, optionally including a locale prefix and basePath.\n * - basePath: (e.g., '/myapp')\n * - locale: (e.g., 'en')\n * - currentPath:(e.g., '/products/shoes')\n */\nconst constructPath = (locale: Locales, currentPath: string) => {\n // Ensure basePath always starts with '/', and remove trailing slash if needed\n const cleanBasePath = basePath.startsWith('/') ? basePath : `/${basePath}`;\n // If basePath is '/', no trailing slash is needed\n const normalizedBasePath = cleanBasePath === '/' ? '' : cleanBasePath;\n\n // Combine basePath + locale + the rest of the path\n // Example: basePath = '/myapp', locale = 'en', currentPath = '/products' => '/myapp/en/products'\n let newPath = `${normalizedBasePath}/${locale}${currentPath}`;\n\n // Special case: if prefixDefault is false and locale is defaultLocale, remove the locale prefix\n if (!prefixDefault && locale === defaultLocale) {\n newPath = `${normalizedBasePath}${currentPath}`;\n }\n\n return newPath;\n};\n\n/* --------------------------------------------------------------------\n * Handlers that mirror Next.js style logic\n * --------------------------------------------------------------------\n */\n\n/**\n * If `noPrefix` is true, we never prefix the locale in the URL.\n * We simply rewrite the request to the same path, but with the best-chosen locale\n * in a header or cookie if desired.\n */\nconst handleNoPrefix = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // Determine the best locale\n let locale = cookieLocale ?? defaultLocale;\n\n // Use fallback to localeDetector if no cookie\n if (!cookieLocale) {\n const detectedLocale = localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale as Locales;\n }\n\n // Just rewrite the URL in-place (no prefix). We do NOT redirect because we do not want to alter the URL.\n rewriteUrl(req, res, originalPath, locale);\n return next();\n};\n\n/**\n * The main prefix logic:\n * - If there's no pathLocale in the URL, we might want to detect & redirect or rewrite\n * - If there is a pathLocale, handle cookie mismatch or default locale special cases\n */\nconst handlePrefix = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale?: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If pathLocale is missing, handle\n if (!pathLocale) {\n handleMissingPathLocale({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 2. If pathLocale exists, handle possible mismatch with cookie\n handleExistingPathLocale({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n * We detect a locale from cookie / headers / default, then either redirect or rewrite.\n */\nconst handleMissingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // If navigation comes from the same origin (e.g., via an in-app language switcher),\n // treat unprefixed paths as an explicit intent to view the default locale.\n // This avoids redirecting back to the cookie locale (e.g., '/tr/') when user selects '/'.\n const referer = (req.headers.referer || req.headers.referrer) as\n | string\n | undefined;\n if (referer) {\n try {\n const refererUrl = new URL(referer);\n const host = req.headers.host;\n if (host && refererUrl.host === host) {\n const locale = defaultLocale as Locales;\n const newPath = constructPath(locale, originalPath);\n rewriteUrl(req, res, newPath, locale);\n return next();\n }\n } catch {\n // ignore invalid referer\n }\n }\n\n // 1. Choose the best locale\n let locale = (cookieLocale ??\n localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n )) as Locales;\n\n // 2. If still invalid, fallback\n if (!supportedLocales.includes(locale)) {\n locale = defaultLocale;\n }\n\n // 3. Construct new path\n const newPath = constructPath(locale, originalPath);\n\n // If we always prefix default or if this is not the default locale, do a 301 redirect\n // so that the user sees the locale in the URL.\n if (prefixDefault || locale !== defaultLocale) {\n return redirectUrl(res, newPath);\n }\n\n // If we do NOT prefix the default locale, just rewrite in place\n rewriteUrl(req, res, newPath, locale);\n return next();\n};\n\n/**\n * Handles requests where the locale prefix is present in the pathname.\n * We verify if the cookie locale differs from the path locale; if so, handle.\n */\nconst handleExistingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If the cookie locale is set and differs from the path locale,\n // and we're not forcing the cookie to always override\n if (\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n // We want to swap out the pathLocale with the cookieLocale\n const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);\n const finalPath = constructPath(cookieLocale, newPath.replace(/^\\/+/, '/'));\n return redirectUrl(res, finalPath);\n }\n\n // 2. Otherwise, handle default-locale prefix if needed\n handleDefaultLocaleRedirect({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n });\n};\n\n/**\n * If the path locale is the default locale but we don't want to prefix the default, remove it.\n */\nconst handleDefaultLocaleRedirect = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n}) => {\n // If we don't prefix default AND the path locale is the default locale -> remove it\n if (!prefixDefault && pathLocale === defaultLocale) {\n // Remove the default locale part from the path\n const newPath = originalPath.replace(`/${defaultLocale}`, '') ?? '/';\n rewriteUrl(req, res, newPath, pathLocale);\n return next();\n }\n\n // If we do prefix default or pathLocale != default, keep as is, but rewrite headers\n rewriteUrl(req, res, originalPath, pathLocale);\n return next();\n};\n\n/**\n * @deprecated Rename to intlayerMiddleware instead\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\n * ```\n */\nexport const intlayerMiddleware = intlayerMiddlewarePlugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,kBAA+B;AAE/B,iBAAsB;AAetB,MAAM,qBAAiB,gCAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,kBAAkB,cAAc,IAAI;AAErD,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AACb,IAAI;AAKG,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ACH;AAKA,MAAM,8BAA8B,CAAC;AAAA,EACnC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAMM;AAEJ,MAAI,CAAC,iBAAiB,eAAe,eAAe;AAElD,UAAM,UAAU,aAAa,QAAQ,IAAI,aAAa,IAAI,EAAE,KAAK;AACjE,eAAW,KAAK,KAAK,SAAS,UAAU;AACxC,WAAO,KAAK;AAAA,EACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;AAYO,MAAM,qBAAqB;","names":["locale","newPath"]}
@@ -37,7 +37,7 @@ var import_built = __toESM(require("@intlayer/config/built"));
37
37
  var import_path = require("path");
38
38
  var import_config = require("@intlayer/config");
39
39
  var import_intlayerPrunePlugin = require('./intlayerPrunePlugin.cjs');
40
- const intlayer = () => {
40
+ const intlayerPlugin = () => {
41
41
  const { watch: isWatchMode } = import_built.default.content;
42
42
  const { optimize } = import_built.default.build;
43
43
  const appLogger = (0, import_config.getAppLogger)(import_built.default);
@@ -92,7 +92,7 @@ const intlayer = () => {
92
92
  }
93
93
  return plugins;
94
94
  };
95
- const intlayerPlugin = intlayer;
95
+ const intlayer = intlayerPlugin;
96
96
  // Annotate the CommonJS export names for ESM import in node:
97
97
  0 && (module.exports = {
98
98
  intlayer,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/intlayerPlugin.ts"],"sourcesContent":["import { prepareIntlayer, runOnce, watch } from '@intlayer/chokidar';\nimport intlayerConfig from '@intlayer/config/built';\nimport { join, resolve } from 'path';\n// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { getAlias, getAppLogger } from '@intlayer/config';\nimport { type PluginOption } from 'vite';\nimport { intlayerPrune } from './intlayerPrunePlugin';\n\n/**\n *\n * A Vite plugin that integrates Intlayer configuration into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayer() ],\n * });\n * ```\n * */\nexport const intlayer = (): PluginOption => {\n const { watch: isWatchMode } = intlayerConfig.content;\n const { optimize } = intlayerConfig.build;\n const appLogger = getAppLogger(intlayerConfig);\n\n const plugins: PluginOption[] = [\n {\n name: 'vite-intlayer-plugin',\n\n config: (config) => {\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n ...getAlias({\n configuration: intlayerConfig,\n formatter: (value: string) => resolve(value),\n }),\n },\n };\n\n if (isWatchMode) {\n // Ajout de l'option optimizeDeps.exclude\n config.optimizeDeps = {\n ...config.optimizeDeps,\n exclude: [\n ...(config.optimizeDeps?.exclude ?? []),\n '@intlayer/dictionaries-entry',\n '@intlayer/config/built',\n ],\n };\n }\n\n return config;\n },\n\n configureServer: async (server) => {\n if (intlayerConfig.content.watch) {\n // Start watching (assuming watch is also async)\n watch({ configuration: intlayerConfig });\n }\n },\n\n buildStart: async () => {\n const sentinelPath = join(\n intlayerConfig.content.baseDir,\n '.intlayer',\n 'cache',\n 'intlayer-prepared.lock'\n );\n\n // Code to run when Vite build starts\n // Only call prepareIntlayer once per server startup\n await runOnce(\n sentinelPath,\n async () => await prepareIntlayer(intlayerConfig),\n () => appLogger('Intlayer prepared')\n );\n },\n },\n ];\n\n // Add Babel transform plugin if enabled\n if (optimize) {\n plugins.push(intlayerPrune(intlayerConfig));\n }\n\n return plugins;\n};\n\n/**\n * @deprecated Rename to intlayer instead\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayer() ],\n * });\n * ```\n */\nexport const intlayerPlugin = intlayer;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAgD;AAChD,mBAA2B;AAC3B,kBAA8B;AAE9B,oBAAuC;AAEvC,iCAA8B;AAavB,MAAM,WAAW,MAAoB;AAC1C,QAAM,EAAE,OAAO,YAAY,IAAI,aAAAA,QAAe;AAC9C,QAAM,EAAE,SAAS,IAAI,aAAAA,QAAe;AACpC,QAAM,gBAAY,4BAAa,aAAAA,OAAc;AAE7C,QAAM,UAA0B;AAAA,IAC9B;AAAA,MACE,MAAM;AAAA,MAEN,QAAQ,CAAC,WAAW;AAElB,eAAO,UAAU;AAAA,UACf,GAAG,OAAO;AAAA,UACV,OAAO;AAAA,YACL,GAAG,OAAO,SAAS;AAAA,YACnB,OAAG,wBAAS;AAAA,cACV,eAAe,aAAAA;AAAA,cACf,WAAW,CAAC,cAAkB,qBAAQ,KAAK;AAAA,YAC7C,CAAC;AAAA,UACH;AAAA,QACF;AAEA,YAAI,aAAa;AAEf,iBAAO,eAAe;AAAA,YACpB,GAAG,OAAO;AAAA,YACV,SAAS;AAAA,cACP,GAAI,OAAO,cAAc,WAAW,CAAC;AAAA,cACrC;AAAA,cACA;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAEA,eAAO;AAAA,MACT;AAAA,MAEA,iBAAiB,OAAO,WAAW;AACjC,YAAI,aAAAA,QAAe,QAAQ,OAAO;AAEhC,qCAAM,EAAE,eAAe,aAAAA,QAAe,CAAC;AAAA,QACzC;AAAA,MACF;AAAA,MAEA,YAAY,YAAY;AACtB,cAAM,mBAAe;AAAA,UACnB,aAAAA,QAAe,QAAQ;AAAA,UACvB;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAIA,kBAAM;AAAA,UACJ;AAAA,UACA,YAAY,UAAM,iCAAgB,aAAAA,OAAc;AAAA,UAChD,MAAM,UAAU,mBAAmB;AAAA,QACrC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAGA,MAAI,UAAU;AACZ,YAAQ,SAAK,0CAAc,aAAAA,OAAc,CAAC;AAAA,EAC5C;AAEA,SAAO;AACT;AAYO,MAAM,iBAAiB;","names":["intlayerConfig"]}
1
+ {"version":3,"sources":["../../src/intlayerPlugin.ts"],"sourcesContent":["import { prepareIntlayer, runOnce, watch } from '@intlayer/chokidar';\nimport intlayerConfig from '@intlayer/config/built';\nimport { join, resolve } from 'path';\n// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { getAlias, getAppLogger } from '@intlayer/config';\nimport { type PluginOption } from 'vite';\nimport { intlayerPrune } from './intlayerPrunePlugin';\n\n/**\n *\n * A Vite plugin that integrates Intlayer configuration into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayer() ],\n * });\n * ```\n * */\nexport const intlayerPlugin = (): PluginOption => {\n const { watch: isWatchMode } = intlayerConfig.content;\n const { optimize } = intlayerConfig.build;\n const appLogger = getAppLogger(intlayerConfig);\n\n const plugins: PluginOption[] = [\n {\n name: 'vite-intlayer-plugin',\n\n config: (config) => {\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n ...getAlias({\n configuration: intlayerConfig,\n formatter: (value: string) => resolve(value),\n }),\n },\n };\n\n if (isWatchMode) {\n // Ajout de l'option optimizeDeps.exclude\n config.optimizeDeps = {\n ...config.optimizeDeps,\n exclude: [\n ...(config.optimizeDeps?.exclude ?? []),\n '@intlayer/dictionaries-entry',\n '@intlayer/config/built',\n ],\n };\n }\n\n return config;\n },\n\n configureServer: async (server) => {\n if (intlayerConfig.content.watch) {\n // Start watching (assuming watch is also async)\n watch({ configuration: intlayerConfig });\n }\n },\n\n buildStart: async () => {\n const sentinelPath = join(\n intlayerConfig.content.baseDir,\n '.intlayer',\n 'cache',\n 'intlayer-prepared.lock'\n );\n\n // Code to run when Vite build starts\n // Only call prepareIntlayer once per server startup\n await runOnce(\n sentinelPath,\n async () => await prepareIntlayer(intlayerConfig),\n () => appLogger('Intlayer prepared')\n );\n },\n },\n ];\n\n // Add Babel transform plugin if enabled\n if (optimize) {\n plugins.push(intlayerPrune(intlayerConfig));\n }\n\n return plugins;\n};\n\n/**\n * @deprecated Rename to intlayer instead\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayer() ],\n * });\n * ```\n */\nexport const intlayer = intlayerPlugin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MAAI,UAAU;AACZ,YAAQ,SAAK,0CAAc,aAAAA,OAAc,CAAC;AAAA,EAC5C;AAEA,SAAO;AACT;AAYO,MAAM,WAAW;","names":["intlayerConfig"]}
@@ -12,7 +12,7 @@ const {
12
12
  serverSetCookie,
13
13
  basePath = ""
14
14
  } = middleware;
15
- const intlayerMiddleware = () => {
15
+ const intlayerMiddlewarePlugin = () => {
16
16
  return {
17
17
  name: "vite-intlayer-middleware-plugin",
18
18
  configureServer: (server) => {
@@ -208,7 +208,7 @@ const handleDefaultLocaleRedirect = ({
208
208
  rewriteUrl(req, res, originalPath, pathLocale);
209
209
  return next();
210
210
  };
211
- const intlayerMiddlewarePlugin = intlayerMiddleware;
211
+ const intlayerMiddleware = intlayerMiddlewarePlugin;
212
212
  export {
213
213
  intlayerMiddleware,
214
214
  intlayerMiddlewarePlugin
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\nimport type { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\n/* @ts-ignore - Vite types error */\nimport type { Connect, Plugin } from 'vite';\n\n// Grab all the config you need.\n// Make sure your config includes the following fields if you want to replicate Next.js logic:\n// - internationalization.locales\n// - internationalization.defaultLocale\n// - middleware.cookieName\n// - middleware.headerName\n// - middleware.prefixDefault\n// - middleware.noPrefix\n// - middleware.serverSetCookie\n// - middleware.basePath\n// - etc.\nconst intlayerConfig = getConfiguration();\nconst { internationalization, middleware } = intlayerConfig;\nconst { locales: supportedLocales, defaultLocale } = internationalization;\n\nconst {\n cookieName,\n headerName,\n prefixDefault,\n noPrefix,\n serverSetCookie,\n basePath = '',\n} = middleware;\n\n/**\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n */\nexport const intlayerMiddleware = (): Plugin => {\n return {\n name: 'vite-intlayer-middleware-plugin',\n configureServer: (server) => {\n server.middlewares.use((req, res, next) => {\n // 1. Bypass assets and special Vite endpoints\n if (\n req.url?.startsWith('/node_modules') ||\n req.url?.startsWith('/@') ||\n req.url?.split('?')[0].match(/\\.[a-z]+$/i) // checks for file extensions\n ) {\n return next();\n }\n\n // 2. Parse original URL for path and query\n const parsedUrl = parse(req.url ?? '/', true);\n const originalPath = parsedUrl.pathname ?? '/';\n\n // 3. Attempt to read the cookie locale\n const cookies = parseCookies(req.headers.cookie ?? '');\n const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);\n\n // 4. Check if there's a locale prefix in the path\n const pathLocale = getPathLocale(originalPath);\n\n // 5. If noPrefix is true, we skip prefix logic altogether\n if (noPrefix) {\n handleNoPrefix({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 6. Otherwise, handle prefix logic\n handlePrefix({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n });\n },\n };\n};\n\n/* --------------------------------------------------------------------\n * Helper & Utility Functions\n * --------------------------------------------------------------------\n */\n\n/**\n * Parses cookies from the Cookie header string into an object.\n */\nconst parseCookies = (cookieHeader: string) => {\n return cookieHeader.split(';').reduce(\n (acc, cookie) => {\n const [key, val] = cookie.trim().split('=');\n acc[key] = val;\n return acc;\n },\n {} as Record<string, string>\n );\n};\n\n/**\n * Checks if the cookie locale is valid and is included in the supported locales.\n */\nconst getValidLocaleFromCookie = (\n locale: string | undefined\n): Locales | undefined => {\n if (locale && supportedLocales.includes(locale as Locales)) {\n return locale as Locales;\n }\n return undefined;\n};\n\n/**\n * Extracts the locale from the URL pathname if present as the first segment.\n */\nconst getPathLocale = (pathname: string): Locales | undefined => {\n // e.g. if pathname is /en/some/page or /en\n // we check if \"en\" is in your supportedLocales\n const segments = pathname.split('/').filter(Boolean);\n const firstSegment = segments[0];\n if (firstSegment && supportedLocales.includes(firstSegment as Locales)) {\n return firstSegment as Locales;\n }\n return undefined;\n};\n\n/**\n * Writes a 301 redirect response with the given new URL.\n */\nconst redirectUrl = (res: ServerResponse<IncomingMessage>, newUrl: string) => {\n res.writeHead(301, { Location: newUrl });\n return res.end();\n};\n\n/**\n * \"Rewrite\" the request internally by adjusting req.url;\n * we also set the locale in the response header if needed.\n */\nconst rewriteUrl = (\n req: Connect.IncomingMessage,\n res: ServerResponse<IncomingMessage>,\n newUrl: string,\n locale?: Locales\n) => {\n req.url = newUrl;\n // If you want to mimic Next.js's behavior of setting a header for the locale:\n if (locale && headerName) {\n res.setHeader(headerName, locale);\n }\n};\n\n/**\n * Constructs a new path string, optionally including a locale prefix and basePath.\n * - basePath: (e.g., '/myapp')\n * - locale: (e.g., 'en')\n * - currentPath:(e.g., '/products/shoes')\n */\nconst constructPath = (locale: Locales, currentPath: string) => {\n // Ensure basePath always starts with '/', and remove trailing slash if needed\n const cleanBasePath = basePath.startsWith('/') ? basePath : `/${basePath}`;\n // If basePath is '/', no trailing slash is needed\n const normalizedBasePath = cleanBasePath === '/' ? '' : cleanBasePath;\n\n // Combine basePath + locale + the rest of the path\n // Example: basePath = '/myapp', locale = 'en', currentPath = '/products' => '/myapp/en/products'\n let newPath = `${normalizedBasePath}/${locale}${currentPath}`;\n\n // Special case: if prefixDefault is false and locale is defaultLocale, remove the locale prefix\n if (!prefixDefault && locale === defaultLocale) {\n newPath = `${normalizedBasePath}${currentPath}`;\n }\n\n return newPath;\n};\n\n/* --------------------------------------------------------------------\n * Handlers that mirror Next.js style logic\n * --------------------------------------------------------------------\n */\n\n/**\n * If `noPrefix` is true, we never prefix the locale in the URL.\n * We simply rewrite the request to the same path, but with the best-chosen locale\n * in a header or cookie if desired.\n */\nconst handleNoPrefix = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // Determine the best locale\n let locale = cookieLocale ?? defaultLocale;\n\n // Use fallback to localeDetector if no cookie\n if (!cookieLocale) {\n const detectedLocale = localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale as Locales;\n }\n\n // Just rewrite the URL in-place (no prefix). We do NOT redirect because we do not want to alter the URL.\n rewriteUrl(req, res, originalPath, locale);\n return next();\n};\n\n/**\n * The main prefix logic:\n * - If there's no pathLocale in the URL, we might want to detect & redirect or rewrite\n * - If there is a pathLocale, handle cookie mismatch or default locale special cases\n */\nconst handlePrefix = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale?: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If pathLocale is missing, handle\n if (!pathLocale) {\n handleMissingPathLocale({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 2. If pathLocale exists, handle possible mismatch with cookie\n handleExistingPathLocale({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n * We detect a locale from cookie / headers / default, then either redirect or rewrite.\n */\nconst handleMissingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // If navigation comes from the same origin (e.g., via an in-app language switcher),\n // treat unprefixed paths as an explicit intent to view the default locale.\n // This avoids redirecting back to the cookie locale (e.g., '/tr/') when user selects '/'.\n const referer = (req.headers.referer || req.headers.referrer) as\n | string\n | undefined;\n if (referer) {\n try {\n const refererUrl = new URL(referer);\n const host = req.headers.host;\n if (host && refererUrl.host === host) {\n const locale = defaultLocale as Locales;\n const newPath = constructPath(locale, originalPath);\n rewriteUrl(req, res, newPath, locale);\n return next();\n }\n } catch {\n // ignore invalid referer\n }\n }\n\n // 1. Choose the best locale\n let locale = (cookieLocale ??\n localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n )) as Locales;\n\n // 2. If still invalid, fallback\n if (!supportedLocales.includes(locale)) {\n locale = defaultLocale;\n }\n\n // 3. Construct new path\n const newPath = constructPath(locale, originalPath);\n\n // If we always prefix default or if this is not the default locale, do a 301 redirect\n // so that the user sees the locale in the URL.\n if (prefixDefault || locale !== defaultLocale) {\n return redirectUrl(res, newPath);\n }\n\n // If we do NOT prefix the default locale, just rewrite in place\n rewriteUrl(req, res, newPath, locale);\n return next();\n};\n\n/**\n * Handles requests where the locale prefix is present in the pathname.\n * We verify if the cookie locale differs from the path locale; if so, handle.\n */\nconst handleExistingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If the cookie locale is set and differs from the path locale,\n // and we're not forcing the cookie to always override\n if (\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n // We want to swap out the pathLocale with the cookieLocale\n const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);\n const finalPath = constructPath(cookieLocale, newPath.replace(/^\\/+/, '/'));\n return redirectUrl(res, finalPath);\n }\n\n // 2. Otherwise, handle default-locale prefix if needed\n handleDefaultLocaleRedirect({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n });\n};\n\n/**\n * If the path locale is the default locale but we don't want to prefix the default, remove it.\n */\nconst handleDefaultLocaleRedirect = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n}) => {\n // If we don't prefix default AND the path locale is the default locale -> remove it\n if (!prefixDefault && pathLocale === defaultLocale) {\n // Remove the default locale part from the path\n const newPath = originalPath.replace(`/${defaultLocale}`, '') ?? '/';\n rewriteUrl(req, res, newPath, pathLocale);\n return next();\n }\n\n // If we do prefix default or pathLocale != default, keep as is, but rewrite headers\n rewriteUrl(req, res, originalPath, pathLocale);\n return next();\n};\n\n/**\n * @deprecated Rename to intlayerMiddleware instead\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\n * ```\n */\nexport const intlayerMiddlewarePlugin = intlayerMiddleware;\n"],"mappings":"AAAA,SAAS,wBAAsC;AAC/C,SAAS,sBAAsB;AAE/B,SAAS,aAAa;AAetB,MAAM,iBAAiB,iBAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,kBAAkB,cAAc,IAAI;AAErD,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AACb,IAAI;AAKG,MAAM,qBAAqB,MAAc;AAC9C,SAAO;AAAA,IACL,MAAM;AAAA,IACN,iBAAiB,CAAC,WAAW;AAC3B,aAAO,YAAY,IAAI,CAAC,KAAK,KAAK,SAAS;AAEzC,YACE,IAAI,KAAK,WAAW,eAAe,KACnC,IAAI,KAAK,WAAW,IAAI,KACxB,IAAI,KAAK,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,YAAY,GACzC;AACA,iBAAO,KAAK;AAAA,QACd;AAGA,cAAM,YAAY,MAAM,IAAI,OAAO,KAAK,IAAI;AAC5C,cAAM,eAAe,UAAU,YAAY;AAG3C,cAAM,UAAU,aAAa,IAAI,QAAQ,UAAU,EAAE;AACrD,cAAM,eAAe,yBAAyB,QAAQ,UAAU,CAAC;AAGjE,cAAM,aAAa,cAAc,YAAY;AAG7C,YAAI,UAAU;AACZ,yBAAe;AAAA,YACb;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF,CAAC;AACD;AAAA,QACF;AAGA,qBAAa;AAAA,UACX;AAAA,U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QAAM,UAAU,cAAc,QAAQ,YAAY;AAIlD,MAAI,iBAAiB,WAAW,eAAe;AAC7C,WAAO,YAAY,KAAK,OAAO;AAAA,EACjC;AAGA,aAAW,KAAK,KAAK,SAAS,MAAM;AACpC,SAAO,KAAK;AACd;AAMA,MAAM,2BAA2B,CAAC;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAOM;AAGJ,MACE,gBACA,iBAAiB,cACjB,oBAAoB,UACpB;AAEA,UAAM,UAAU,aAAa,QAAQ,IAAI,UAAU,IAAI,IAAI,YAAY,EAAE;AACzE,UAAM,YAAY,cAAc,cAAc,QAAQ,QAAQ,QAAQ,GAAG,CAAC;AAC1E,WAAO,YAAY,KAAK,SAAS;AAAA,EACnC;AAGA,8BAA4B;AAAA,IAC1B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAKA,MAAM,8BAA8B,CAAC;AAAA,EACnC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAMM;AAEJ,MAAI,CAAC,iBAAiB,eAAe,eAAe;AAElD,UAAM,UAAU,aAAa,QAAQ,IAAI,aAAa,IAAI,EAAE,KAAK;AACjE,eAAW,KAAK,KAAK,SAAS,UAAU;AACxC,WAAO,KAAK;AAAA,EACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;AAYO,MAAM,2BAA2B;","names":["locale","newPath"]}
1
+ {"version":3,"sources":["../../src/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\nimport type { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\n/* @ts-ignore - Vite types error */\nimport type { Connect, Plugin } from 'vite';\n\n// Grab all the config you need.\n// Make sure your config includes the following fields if you want to replicate Next.js logic:\n// - internationalization.locales\n// - internationalization.defaultLocale\n// - middleware.cookieName\n// - middleware.headerName\n// - middleware.prefixDefault\n// - middleware.noPrefix\n// - middleware.serverSetCookie\n// - middleware.basePath\n// - etc.\nconst intlayerConfig = getConfiguration();\nconst { internationalization, middleware } = intlayerConfig;\nconst { locales: supportedLocales, defaultLocale } = internationalization;\n\nconst {\n cookieName,\n headerName,\n prefixDefault,\n noPrefix,\n serverSetCookie,\n basePath = '',\n} = middleware;\n\n/**\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n */\nexport const intlayerMiddlewarePlugin = (): Plugin => {\n return {\n name: 'vite-intlayer-middleware-plugin',\n configureServer: (server) => {\n server.middlewares.use((req, res, next) => {\n // 1. Bypass assets and special Vite endpoints\n if (\n req.url?.startsWith('/node_modules') ||\n req.url?.startsWith('/@') ||\n req.url?.split('?')[0].match(/\\.[a-z]+$/i) // checks for file extensions\n ) {\n return next();\n }\n\n // 2. Parse original URL for path and query\n const parsedUrl = parse(req.url ?? '/', true);\n const originalPath = parsedUrl.pathname ?? '/';\n\n // 3. Attempt to read the cookie locale\n const cookies = parseCookies(req.headers.cookie ?? '');\n const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);\n\n // 4. Check if there's a locale prefix in the path\n const pathLocale = getPathLocale(originalPath);\n\n // 5. If noPrefix is true, we skip prefix logic altogether\n if (noPrefix) {\n handleNoPrefix({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 6. Otherwise, handle prefix logic\n handlePrefix({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n });\n },\n };\n};\n\n/* --------------------------------------------------------------------\n * Helper & Utility Functions\n * --------------------------------------------------------------------\n */\n\n/**\n * Parses cookies from the Cookie header string into an object.\n */\nconst parseCookies = (cookieHeader: string) => {\n return cookieHeader.split(';').reduce(\n (acc, cookie) => {\n const [key, val] = cookie.trim().split('=');\n acc[key] = val;\n return acc;\n },\n {} as Record<string, string>\n );\n};\n\n/**\n * Checks if the cookie locale is valid and is included in the supported locales.\n */\nconst getValidLocaleFromCookie = (\n locale: string | undefined\n): Locales | undefined => {\n if (locale && supportedLocales.includes(locale as Locales)) {\n return locale as Locales;\n }\n return undefined;\n};\n\n/**\n * Extracts the locale from the URL pathname if present as the first segment.\n */\nconst getPathLocale = (pathname: string): Locales | undefined => {\n // e.g. if pathname is /en/some/page or /en\n // we check if \"en\" is in your supportedLocales\n const segments = pathname.split('/').filter(Boolean);\n const firstSegment = segments[0];\n if (firstSegment && supportedLocales.includes(firstSegment as Locales)) {\n return firstSegment as Locales;\n }\n return undefined;\n};\n\n/**\n * Writes a 301 redirect response with the given new URL.\n */\nconst redirectUrl = (res: ServerResponse<IncomingMessage>, newUrl: string) => {\n res.writeHead(301, { Location: newUrl });\n return res.end();\n};\n\n/**\n * \"Rewrite\" the request internally by adjusting req.url;\n * we also set the locale in the response header if needed.\n */\nconst rewriteUrl = (\n req: Connect.IncomingMessage,\n res: ServerResponse<IncomingMessage>,\n newUrl: string,\n locale?: Locales\n) => {\n req.url = newUrl;\n // If you want to mimic Next.js's behavior of setting a header for the locale:\n if (locale && headerName) {\n res.setHeader(headerName, locale);\n }\n};\n\n/**\n * Constructs a new path string, optionally including a locale prefix and basePath.\n * - basePath: (e.g., '/myapp')\n * - locale: (e.g., 'en')\n * - currentPath:(e.g., '/products/shoes')\n */\nconst constructPath = (locale: Locales, currentPath: string) => {\n // Ensure basePath always starts with '/', and remove trailing slash if needed\n const cleanBasePath = basePath.startsWith('/') ? basePath : `/${basePath}`;\n // If basePath is '/', no trailing slash is needed\n const normalizedBasePath = cleanBasePath === '/' ? '' : cleanBasePath;\n\n // Combine basePath + locale + the rest of the path\n // Example: basePath = '/myapp', locale = 'en', currentPath = '/products' => '/myapp/en/products'\n let newPath = `${normalizedBasePath}/${locale}${currentPath}`;\n\n // Special case: if prefixDefault is false and locale is defaultLocale, remove the locale prefix\n if (!prefixDefault && locale === defaultLocale) {\n newPath = `${normalizedBasePath}${currentPath}`;\n }\n\n return newPath;\n};\n\n/* --------------------------------------------------------------------\n * Handlers that mirror Next.js style logic\n * --------------------------------------------------------------------\n */\n\n/**\n * If `noPrefix` is true, we never prefix the locale in the URL.\n * We simply rewrite the request to the same path, but with the best-chosen locale\n * in a header or cookie if desired.\n */\nconst handleNoPrefix = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // Determine the best locale\n let locale = cookieLocale ?? defaultLocale;\n\n // Use fallback to localeDetector if no cookie\n if (!cookieLocale) {\n const detectedLocale = localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale as Locales;\n }\n\n // Just rewrite the URL in-place (no prefix). We do NOT redirect because we do not want to alter the URL.\n rewriteUrl(req, res, originalPath, locale);\n return next();\n};\n\n/**\n * The main prefix logic:\n * - If there's no pathLocale in the URL, we might want to detect & redirect or rewrite\n * - If there is a pathLocale, handle cookie mismatch or default locale special cases\n */\nconst handlePrefix = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale?: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If pathLocale is missing, handle\n if (!pathLocale) {\n handleMissingPathLocale({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 2. If pathLocale exists, handle possible mismatch with cookie\n handleExistingPathLocale({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n * We detect a locale from cookie / headers / default, then either redirect or rewrite.\n */\nconst handleMissingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // If navigation comes from the same origin (e.g., via an in-app language switcher),\n // treat unprefixed paths as an explicit intent to view the default locale.\n // This avoids redirecting back to the cookie locale (e.g., '/tr/') when user selects '/'.\n const referer = (req.headers.referer || req.headers.referrer) as\n | string\n | undefined;\n if (referer) {\n try {\n const refererUrl = new URL(referer);\n const host = req.headers.host;\n if (host && refererUrl.host === host) {\n const locale = defaultLocale as Locales;\n const newPath = constructPath(locale, originalPath);\n rewriteUrl(req, res, newPath, locale);\n return next();\n }\n } catch {\n // ignore invalid referer\n }\n }\n\n // 1. Choose the best locale\n let locale = (cookieLocale ??\n localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n )) as Locales;\n\n // 2. If still invalid, fallback\n if (!supportedLocales.includes(locale)) {\n locale = defaultLocale;\n }\n\n // 3. Construct new path\n const newPath = constructPath(locale, originalPath);\n\n // If we always prefix default or if this is not the default locale, do a 301 redirect\n // so that the user sees the locale in the URL.\n if (prefixDefault || locale !== defaultLocale) {\n return redirectUrl(res, newPath);\n }\n\n // If we do NOT prefix the default locale, just rewrite in place\n rewriteUrl(req, res, newPath, locale);\n return next();\n};\n\n/**\n * Handles requests where the locale prefix is present in the pathname.\n * We verify if the cookie locale differs from the path locale; if so, handle.\n */\nconst handleExistingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If the cookie locale is set and differs from the path locale,\n // and we're not forcing the cookie to always override\n if (\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n // We want to swap out the pathLocale with the cookieLocale\n const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);\n const finalPath = constructPath(cookieLocale, newPath.replace(/^\\/+/, '/'));\n return redirectUrl(res, finalPath);\n }\n\n // 2. Otherwise, handle default-locale prefix if needed\n handleDefaultLocaleRedirect({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n });\n};\n\n/**\n * If the path locale is the default locale but we don't want to prefix the default, remove it.\n */\nconst handleDefaultLocaleRedirect = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n}) => {\n // If we don't prefix default AND the path locale is the default locale -> remove it\n if (!prefixDefault && pathLocale === defaultLocale) {\n // Remove the default locale part from the path\n const newPath = originalPath.replace(`/${defaultLocale}`, '') ?? '/';\n rewriteUrl(req, res, newPath, pathLocale);\n return next();\n }\n\n // If we do prefix default or pathLocale != default, keep as is, but rewrite headers\n rewriteUrl(req, res, originalPath, pathLocale);\n return next();\n};\n\n/**\n * @deprecated Rename to intlayerMiddleware instead\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\n * ```\n */\nexport const intlayerMiddleware = intlayerMiddlewarePlugin;\n"],"mappings":"AAAA,SAAS,wBAAsC;AAC/C,SAAS,sBAAsB;AAE/B,SAAS,aAAa;AAetB,MAAM,iBAAiB,iBAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,kBAAkB,cAAc,IAAI;AAErD,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AACb,IAAI;AAKG,MAAM,2BAA2B,MAAc;AACpD,SAAO;AAAA,IACL,MAAM;AAAA,IACN,iBAAiB,CAAC,WAAW;AAC3B,aAAO,YAAY,IAAI,CAAC,KAAK,KAAK,SAAS;AAEzC,YACE,IAAI,KAAK,WAAW,eAAe,KACnC,IAAI,KAAK,WAAW,IAAI,KACxB,IAAI,KAAK,MAAM,GAAG,EAAE,CAAC,EA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O,KAAK;AAAA,EACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;AAYO,MAAM,qBAAqB;","names":["locale","newPath"]}
@@ -3,7 +3,7 @@ import intlayerConfig from "@intlayer/config/built";
3
3
  import { join, resolve } from "path";
4
4
  import { getAlias, getAppLogger } from "@intlayer/config";
5
5
  import { intlayerPrune } from "./intlayerPrunePlugin.mjs";
6
- const intlayer = () => {
6
+ const intlayerPlugin = () => {
7
7
  const { watch: isWatchMode } = intlayerConfig.content;
8
8
  const { optimize } = intlayerConfig.build;
9
9
  const appLogger = getAppLogger(intlayerConfig);
@@ -58,7 +58,7 @@ const intlayer = () => {
58
58
  }
59
59
  return plugins;
60
60
  };
61
- const intlayerPlugin = intlayer;
61
+ const intlayer = intlayerPlugin;
62
62
  export {
63
63
  intlayer,
64
64
  intlayerPlugin
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/intlayerPlugin.ts"],"sourcesContent":["import { prepareIntlayer, runOnce, watch } from '@intlayer/chokidar';\nimport intlayerConfig from '@intlayer/config/built';\nimport { join, resolve } from 'path';\n// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { getAlias, getAppLogger } from '@intlayer/config';\nimport { type PluginOption } from 'vite';\nimport { intlayerPrune } from './intlayerPrunePlugin';\n\n/**\n *\n * A Vite plugin that integrates Intlayer configuration into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayer() ],\n * });\n * ```\n * */\nexport const intlayer = (): PluginOption => {\n const { watch: isWatchMode } = intlayerConfig.content;\n const { optimize } = intlayerConfig.build;\n const appLogger = getAppLogger(intlayerConfig);\n\n const plugins: PluginOption[] = [\n {\n name: 'vite-intlayer-plugin',\n\n config: (config) => {\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n ...getAlias({\n configuration: intlayerConfig,\n formatter: (value: string) => resolve(value),\n }),\n },\n };\n\n if (isWatchMode) {\n // Ajout de l'option optimizeDeps.exclude\n config.optimizeDeps = {\n ...config.optimizeDeps,\n exclude: [\n ...(config.optimizeDeps?.exclude ?? []),\n '@intlayer/dictionaries-entry',\n '@intlayer/config/built',\n ],\n };\n }\n\n return config;\n },\n\n configureServer: async (server) => {\n if (intlayerConfig.content.watch) {\n // Start watching (assuming watch is also async)\n watch({ configuration: intlayerConfig });\n }\n },\n\n buildStart: async () => {\n const sentinelPath = join(\n intlayerConfig.content.baseDir,\n '.intlayer',\n 'cache',\n 'intlayer-prepared.lock'\n );\n\n // Code to run when Vite build starts\n // Only call prepareIntlayer once per server startup\n await runOnce(\n sentinelPath,\n async () => await prepareIntlayer(intlayerConfig),\n () => appLogger('Intlayer prepared')\n );\n },\n },\n ];\n\n // Add Babel transform plugin if enabled\n if (optimize) {\n plugins.push(intlayerPrune(intlayerConfig));\n }\n\n return plugins;\n};\n\n/**\n * @deprecated Rename to intlayer instead\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayer() ],\n * });\n * ```\n */\nexport const intlayerPlugin = intlayer;\n"],"mappings":"AAAA,SAAS,iBAAiB,SAAS,aAAa;AAChD,OAAO,oBAAoB;AAC3B,SAAS,MAAM,eAAe;AAE9B,SAAS,UAAU,oBAAoB;AAEvC,SAAS,qBAAqB;AAavB,MAAM,WAAW,MAAoB;AAC1C,QAAM,EAAE,OAAO,YAAY,IAAI,eAAe;AAC9C,QAAM,EAAE,SAAS,IAAI,eAAe;AACpC,QAAM,YAAY,aAAa,cAAc;AAE7C,QAAM,UAA0B;AAAA,IAC9B;AAAA,MACE,MAAM;AAAA,MAEN,QAAQ,CAAC,WAAW;AAElB,eAAO,UAAU;AAAA,UACf,GAAG,OAAO;AAAA,UACV,OAAO;AAAA,YACL,GAAG,OAAO,SAAS;AAAA,YACnB,GAAG,SAAS;AAAA,cACV,eAAe;AAAA,cACf,WAAW,CAAC,UAAkB,QAAQ,KAAK;AAAA,YAC7C,CAAC;AAAA,UACH;AAAA,QACF;AAEA,YAAI,aAAa;AAEf,iBAAO,eAAe;AAAA,YACpB,GAAG,OAAO;AAAA,YACV,SAAS;AAAA,cACP,GAAI,OAAO,cAAc,WAAW,CAAC;AAAA,cACrC;AAAA,cACA;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAEA,eAAO;AAAA,MACT;AAAA,MAEA,iBAAiB,OAAO,WAAW;AACjC,YAAI,eAAe,QAAQ,OAAO;AAEhC,gBAAM,EAAE,eAAe,eAAe,CAAC;AAAA,QACzC;AAAA,MACF;AAAA,MAEA,YAAY,YAAY;AACtB,cAAM,eAAe;AAAA,UACnB,eAAe,QAAQ;AAAA,UACvB;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAIA,cAAM;AAAA,UACJ;AAAA,UACA,YAAY,MAAM,gBAAgB,cAAc;AAAA,UAChD,MAAM,UAAU,mBAAmB;AAAA,QACrC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAGA,MAAI,UAAU;AACZ,YAAQ,KAAK,cAAc,cAAc,CAAC;AAAA,EAC5C;AAEA,SAAO;AACT;AAYO,MAAM,iBAAiB;","names":[]}
1
+ {"version":3,"sources":["../../src/intlayerPlugin.ts"],"sourcesContent":["import { prepareIntlayer, runOnce, watch } from '@intlayer/chokidar';\nimport intlayerConfig from '@intlayer/config/built';\nimport { join, resolve } from 'path';\n// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { getAlias, getAppLogger } from '@intlayer/config';\nimport { type PluginOption } from 'vite';\nimport { intlayerPrune } from './intlayerPrunePlugin';\n\n/**\n *\n * A Vite plugin that integrates Intlayer configuration into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayer() ],\n * });\n * ```\n * */\nexport const intlayerPlugin = (): PluginOption => {\n const { watch: isWatchMode } = intlayerConfig.content;\n const { optimize } = intlayerConfig.build;\n const appLogger = getAppLogger(intlayerConfig);\n\n const plugins: PluginOption[] = [\n {\n name: 'vite-intlayer-plugin',\n\n config: (config) => {\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n ...getAlias({\n configuration: intlayerConfig,\n formatter: (value: string) => resolve(value),\n }),\n },\n };\n\n if (isWatchMode) {\n // Ajout de l'option optimizeDeps.exclude\n config.optimizeDeps = {\n ...config.optimizeDeps,\n exclude: [\n ...(config.optimizeDeps?.exclude ?? []),\n '@intlayer/dictionaries-entry',\n '@intlayer/config/built',\n ],\n };\n }\n\n return config;\n },\n\n configureServer: async (server) => {\n if (intlayerConfig.content.watch) {\n // Start watching (assuming watch is also async)\n watch({ configuration: intlayerConfig });\n }\n },\n\n buildStart: async () => {\n const sentinelPath = join(\n intlayerConfig.content.baseDir,\n '.intlayer',\n 'cache',\n 'intlayer-prepared.lock'\n );\n\n // Code to run when Vite build starts\n // Only call prepareIntlayer once per server startup\n await runOnce(\n sentinelPath,\n async () => await prepareIntlayer(intlayerConfig),\n () => appLogger('Intlayer prepared')\n );\n },\n },\n ];\n\n // Add Babel transform plugin if enabled\n if (optimize) {\n plugins.push(intlayerPrune(intlayerConfig));\n }\n\n return plugins;\n};\n\n/**\n * @deprecated Rename to intlayer instead\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayer() ],\n * });\n * ```\n */\nexport const intlayer = intlayerPlugin;\n"],"mappings":"AAAA,SAAS,iBAAiB,SAAS,aAAa;AAChD,OAAO,oBAAoB;AAC3B,SAAS,MAAM,eAAe;AAE9B,SAAS,UAAU,oBAAoB;AAEvC,SAAS,qBAAqB;AAavB,MAAM,iBAAiB,MAAoB;AAChD,QAAM,EAAE,OAAO,YAAY,IAAI,eAAe;AAC9C,QAAM,EAAE,SAAS,IAAI,eAAe;AACpC,QAAM,YAAY,aAAa,cAAc;AAE7C,QAAM,UAA0B;AAAA,IAC9B;AAAA,MACE,MAAM;AAAA,MAEN,QAAQ,CAAC,WAAW;AAElB,eAAO,UAAU;AAAA,UACf,GAAG,OAAO;AAAA,UACV,OAAO;AAAA,YACL,GAAG,OAAO,SAAS;AAAA,YACnB,GAAG,SAAS;AAAA,cACV,eAAe;AAAA,cACf,WAAW,CAAC,UAAkB,QAAQ,KAAK;AAAA,YAC7C,CAAC;AAAA,UACH;AAAA,QACF;AAEA,YAAI,aAAa;AAEf,iBAAO,eAAe;AAAA,YACpB,GAAG,OAAO;AAAA,YACV,SAAS;AAAA,cACP,GAAI,OAAO,cAAc,WAAW,CAAC;AAAA,cACrC;AAAA,cACA;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAEA,eAAO;AAAA,MACT;AAAA,MAEA,iBAAiB,OAAO,WAAW;AACjC,YAAI,eAAe,QAAQ,OAAO;AAEhC,gBAAM,EAAE,eAAe,eAAe,CAAC;AAAA,QACzC;AAAA,MACF;AAAA,MAEA,YAAY,YAAY;AACtB,cAAM,eAAe;AAAA,UACnB,eAAe,QAAQ;AAAA,UACvB;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAIA,cAAM;AAAA,UACJ;AAAA,UACA,YAAY,MAAM,gBAAgB,cAAc;AAAA,UAChD,MAAM,UAAU,mBAAmB;AAAA,QACrC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAGA,MAAI,UAAU;AACZ,YAAQ,KAAK,cAAc,cAAc,CAAC;AAAA,EAC5C;AAEA,SAAO;AACT;AAYO,MAAM,WAAW;","names":[]}
@@ -2,7 +2,7 @@ import type { Plugin } from 'vite';
2
2
  /**
3
3
  * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.
4
4
  */
5
- export declare const intlayerMiddleware: () => Plugin;
5
+ export declare const intlayerMiddlewarePlugin: () => Plugin;
6
6
  /**
7
7
  * @deprecated Rename to intlayerMiddleware instead
8
8
  *
@@ -13,5 +13,5 @@ export declare const intlayerMiddleware: () => Plugin;
13
13
  * });
14
14
  * ```
15
15
  */
16
- export declare const intlayerMiddlewarePlugin: () => Plugin;
16
+ export declare const intlayerMiddleware: () => Plugin;
17
17
  //# sourceMappingURL=intlayerMiddlewarePlugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"intlayerMiddlewarePlugin.d.ts","sourceRoot":"","sources":["../../src/intlayerMiddlewarePlugin.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAW,MAAM,EAAE,MAAM,MAAM,CAAC;AA0B5C;;GAEG;AACH,eAAO,MAAM,kBAAkB,QAAO,MAiDrC,CAAC;AA4TF;;;;;;;;;GASG;AACH,eAAO,MAAM,wBAAwB,QAvXC,MAuXoB,CAAC"}
1
+ {"version":3,"file":"intlayerMiddlewarePlugin.d.ts","sourceRoot":"","sources":["../../src/intlayerMiddlewarePlugin.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAW,MAAM,EAAE,MAAM,MAAM,CAAC;AA0B5C;;GAEG;AACH,eAAO,MAAM,wBAAwB,QAAO,MAiD3C,CAAC;AA4TF;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,QAvXa,MAuXc,CAAC"}
@@ -10,7 +10,7 @@ import { type PluginOption } from 'vite';
10
10
  * });
11
11
  * ```
12
12
  * */
13
- export declare const intlayer: () => PluginOption;
13
+ export declare const intlayerPlugin: () => PluginOption;
14
14
  /**
15
15
  * @deprecated Rename to intlayer instead
16
16
  *
@@ -21,5 +21,5 @@ export declare const intlayer: () => PluginOption;
21
21
  * });
22
22
  * ```
23
23
  */
24
- export declare const intlayerPlugin: () => PluginOption;
24
+ export declare const intlayer: () => PluginOption;
25
25
  //# sourceMappingURL=intlayerPlugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"intlayerPlugin.d.ts","sourceRoot":"","sources":["../../src/intlayerPlugin.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,MAAM,CAAC;AAGzC;;;;;;;;;;MAUM;AACN,eAAO,MAAM,QAAQ,QAAO,YAqE3B,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,QAjFC,YAiFU,CAAC"}
1
+ {"version":3,"file":"intlayerPlugin.d.ts","sourceRoot":"","sources":["../../src/intlayerPlugin.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,MAAM,CAAC;AAGzC;;;;;;;;;;MAUM;AACN,eAAO,MAAM,cAAc,QAAO,YAqEjC,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,QAAQ,QAjFa,YAiFI,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-intlayer",
3
- "version": "6.0.0-canary.1",
3
+ "version": "6.0.0-canary.3",
4
4
  "private": false,
5
5
  "description": "A Vite plugin for seamless internationalization (i18n), providing locale detection, redirection, and environment-based configuration",
6
6
  "keywords": [
@@ -58,11 +58,11 @@
58
58
  "./package.json"
59
59
  ],
60
60
  "dependencies": {
61
- "@intlayer/chokidar": "6.0.0-canary.1",
62
- "@intlayer/config": "6.0.0-canary.1",
63
- "@intlayer/core": "6.0.0-canary.1",
64
- "@intlayer/dictionaries-entry": "6.0.0-canary.1",
65
- "@intlayer/babel": "6.0.0-canary.1"
61
+ "@intlayer/config": "6.0.0-canary.3",
62
+ "@intlayer/core": "6.0.0-canary.3",
63
+ "@intlayer/dictionaries-entry": "6.0.0-canary.3",
64
+ "@intlayer/chokidar": "6.0.0-canary.3",
65
+ "@intlayer/babel": "6.0.0-canary.3"
66
66
  },
67
67
  "devDependencies": {
68
68
  "@types/node": "^24.2.1",
@@ -77,15 +77,15 @@
77
77
  "vitest": "^3.2.4",
78
78
  "@utils/eslint-config": "1.0.4",
79
79
  "@utils/ts-config": "1.0.4",
80
- "@utils/tsup-config": "1.0.4",
81
- "@utils/ts-config-types": "1.0.4"
80
+ "@utils/ts-config-types": "1.0.4",
81
+ "@utils/tsup-config": "1.0.4"
82
82
  },
83
83
  "peerDependencies": {
84
84
  "vite": ">=4.0.0",
85
- "@intlayer/config": "6.0.0-canary.1",
86
- "@intlayer/chokidar": "6.0.0-canary.1",
87
- "@intlayer/core": "6.0.0-canary.1",
88
- "@intlayer/dictionaries-entry": "6.0.0-canary.1"
85
+ "@intlayer/chokidar": "6.0.0-canary.3",
86
+ "@intlayer/config": "6.0.0-canary.3",
87
+ "@intlayer/core": "6.0.0-canary.3",
88
+ "@intlayer/dictionaries-entry": "6.0.0-canary.3"
89
89
  },
90
90
  "engines": {
91
91
  "node": ">=14.18"