vite-intlayer 5.8.1 → 6.0.0-canary.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.
- package/dist/cjs/intlayerMiddlewarePlugin.cjs +21 -4
- package/dist/cjs/intlayerMiddlewarePlugin.cjs.map +1 -1
- package/dist/cjs/intlayerPlugin.cjs +14 -34
- package/dist/cjs/intlayerPlugin.cjs.map +1 -1
- package/dist/cjs/intlayerPrunePlugin.cjs +25 -15
- package/dist/cjs/intlayerPrunePlugin.cjs.map +1 -1
- package/dist/esm/intlayerMiddlewarePlugin.mjs +19 -3
- package/dist/esm/intlayerMiddlewarePlugin.mjs.map +1 -1
- package/dist/esm/intlayerPlugin.mjs +16 -42
- package/dist/esm/intlayerPlugin.mjs.map +1 -1
- package/dist/esm/intlayerPrunePlugin.mjs +24 -14
- package/dist/esm/intlayerPrunePlugin.mjs.map +1 -1
- package/dist/types/intlayerMiddlewarePlugin.d.ts +12 -1
- package/dist/types/intlayerMiddlewarePlugin.d.ts.map +1 -1
- package/dist/types/intlayerPlugin.d.ts +12 -1
- package/dist/types/intlayerPlugin.d.ts.map +1 -1
- package/dist/types/intlayerPrunePlugin.d.ts +1 -1
- package/dist/types/intlayerPrunePlugin.d.ts.map +1 -1
- package/package.json +14 -12
|
@@ -18,12 +18,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var intlayerMiddlewarePlugin_exports = {};
|
|
20
20
|
__export(intlayerMiddlewarePlugin_exports, {
|
|
21
|
-
|
|
21
|
+
intlayerMiddleware: () => intlayerMiddleware,
|
|
22
|
+
intlayerMiddlewarePlugin: () => intlayerMiddlewarePlugin
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(intlayerMiddlewarePlugin_exports);
|
|
24
|
-
var import_url = require("url");
|
|
25
25
|
var import_config = require("@intlayer/config");
|
|
26
26
|
var import_core = require("@intlayer/core");
|
|
27
|
+
var import_url = require("url");
|
|
27
28
|
const intlayerConfig = (0, import_config.getConfiguration)();
|
|
28
29
|
const { internationalization, middleware } = intlayerConfig;
|
|
29
30
|
const { locales: supportedLocales, defaultLocale } = internationalization;
|
|
@@ -35,7 +36,7 @@ const {
|
|
|
35
36
|
serverSetCookie,
|
|
36
37
|
basePath = ""
|
|
37
38
|
} = middleware;
|
|
38
|
-
const
|
|
39
|
+
const intlayerMiddleware = () => {
|
|
39
40
|
return {
|
|
40
41
|
name: "vite-intlayer-middleware-plugin",
|
|
41
42
|
configureServer: (server) => {
|
|
@@ -166,6 +167,20 @@ const handleMissingPathLocale = ({
|
|
|
166
167
|
originalPath,
|
|
167
168
|
cookieLocale
|
|
168
169
|
}) => {
|
|
170
|
+
const referer = req.headers.referer || req.headers.referrer;
|
|
171
|
+
if (referer) {
|
|
172
|
+
try {
|
|
173
|
+
const refererUrl = new URL(referer);
|
|
174
|
+
const host = req.headers.host;
|
|
175
|
+
if (host && refererUrl.host === host) {
|
|
176
|
+
const locale2 = defaultLocale;
|
|
177
|
+
const newPath2 = constructPath(locale2, originalPath);
|
|
178
|
+
rewriteUrl(req, res, newPath2, locale2);
|
|
179
|
+
return next();
|
|
180
|
+
}
|
|
181
|
+
} catch {
|
|
182
|
+
}
|
|
183
|
+
}
|
|
169
184
|
let locale = cookieLocale ?? (0, import_core.localeDetector)(
|
|
170
185
|
req.headers,
|
|
171
186
|
supportedLocales,
|
|
@@ -217,8 +232,10 @@ const handleDefaultLocaleRedirect = ({
|
|
|
217
232
|
rewriteUrl(req, res, originalPath, pathLocale);
|
|
218
233
|
return next();
|
|
219
234
|
};
|
|
235
|
+
const intlayerMiddlewarePlugin = intlayerMiddleware;
|
|
220
236
|
// Annotate the CommonJS export names for ESM import in node:
|
|
221
237
|
0 && (module.exports = {
|
|
222
|
-
|
|
238
|
+
intlayerMiddleware,
|
|
239
|
+
intlayerMiddlewarePlugin
|
|
223
240
|
});
|
|
224
241
|
//# sourceMappingURL=intlayerMiddlewarePlugin.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\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 // 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"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,iBAAsB;AACtB,oBAA+C;AAC/C,kBAA+B;AAe/B,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,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,EAAE,MAAM,YAAY,GACzC;AACA,iBAAO,KAAK;AAAA,QACd;AAGA,cAAM,gBAAY,kBAAM,IAAI,OAAO,KAAK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AIlD,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;","names":[]}
|
|
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,GAC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ACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;AAYO,MAAM,2BAA2B;","names":["locale","newPath"]}
|
|
@@ -28,50 +28,31 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
var intlayerPlugin_exports = {};
|
|
30
30
|
__export(intlayerPlugin_exports, {
|
|
31
|
+
intlayer: () => intlayer,
|
|
31
32
|
intlayerPlugin: () => intlayerPlugin
|
|
32
33
|
});
|
|
33
34
|
module.exports = __toCommonJS(intlayerPlugin_exports);
|
|
34
35
|
var import_chokidar = require("@intlayer/chokidar");
|
|
35
36
|
var import_built = __toESM(require("@intlayer/config/built"));
|
|
36
37
|
var import_path = require("path");
|
|
38
|
+
var import_config = require("@intlayer/config");
|
|
37
39
|
var import_intlayerPrunePlugin = require('./intlayerPrunePlugin.cjs');
|
|
38
|
-
const
|
|
39
|
-
const {
|
|
40
|
-
mainDir,
|
|
41
|
-
dictionariesDir,
|
|
42
|
-
unmergedDictionariesDir,
|
|
43
|
-
dynamicDictionariesDir,
|
|
44
|
-
configDir,
|
|
45
|
-
baseDir,
|
|
46
|
-
watch: isWatchMode
|
|
47
|
-
} = import_built.default.content;
|
|
48
|
-
const { hotReload } = import_built.default.editor;
|
|
40
|
+
const intlayer = () => {
|
|
41
|
+
const { watch: isWatchMode } = import_built.default.content;
|
|
49
42
|
const { optimize } = import_built.default.build;
|
|
43
|
+
const appLogger = (0, import_config.getAppLogger)(import_built.default);
|
|
50
44
|
const plugins = [
|
|
51
45
|
{
|
|
52
46
|
name: "vite-intlayer-plugin",
|
|
53
47
|
config: (config) => {
|
|
54
|
-
const dictionariesPath = (0, import_path.join)(mainDir, "dictionaries.mjs");
|
|
55
|
-
const relativeDictionariesPath = (0, import_path.relative)(baseDir, dictionariesPath);
|
|
56
|
-
const unmergedDictionariesPath = (0, import_path.join)(
|
|
57
|
-
mainDir,
|
|
58
|
-
"unmerged_dictionaries.mjs"
|
|
59
|
-
);
|
|
60
|
-
const relativeUnmergedDictionariesPath = (0, import_path.relative)(
|
|
61
|
-
baseDir,
|
|
62
|
-
unmergedDictionariesPath
|
|
63
|
-
);
|
|
64
|
-
const configurationPath = (0, import_path.join)(configDir, "configuration.json");
|
|
65
|
-
const relativeConfigurationPath = (0, import_path.relative)(baseDir, configurationPath);
|
|
66
48
|
config.resolve = {
|
|
67
49
|
...config.resolve,
|
|
68
50
|
alias: {
|
|
69
51
|
...config.resolve?.alias,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
)
|
|
74
|
-
"@intlayer/config/built": (0, import_path.resolve)(relativeConfigurationPath)
|
|
52
|
+
...(0, import_config.getAlias)({
|
|
53
|
+
configuration: import_built.default,
|
|
54
|
+
formatter: (value) => (0, import_path.resolve)(value)
|
|
55
|
+
})
|
|
75
56
|
}
|
|
76
57
|
};
|
|
77
58
|
if (isWatchMode) {
|
|
@@ -80,7 +61,6 @@ const intlayerPlugin = () => {
|
|
|
80
61
|
exclude: [
|
|
81
62
|
...config.optimizeDeps?.exclude ?? [],
|
|
82
63
|
"@intlayer/dictionaries-entry",
|
|
83
|
-
"@intlayer/unmerged-dictionaries-entry",
|
|
84
64
|
"@intlayer/config/built"
|
|
85
65
|
]
|
|
86
66
|
};
|
|
@@ -91,9 +71,6 @@ const intlayerPlugin = () => {
|
|
|
91
71
|
if (import_built.default.content.watch) {
|
|
92
72
|
(0, import_chokidar.watch)({ configuration: import_built.default });
|
|
93
73
|
}
|
|
94
|
-
if (hotReload) {
|
|
95
|
-
await (0, import_chokidar.checkDictionaryChanges)();
|
|
96
|
-
}
|
|
97
74
|
},
|
|
98
75
|
buildStart: async () => {
|
|
99
76
|
const sentinelPath = (0, import_path.join)(
|
|
@@ -104,18 +81,21 @@ const intlayerPlugin = () => {
|
|
|
104
81
|
);
|
|
105
82
|
await (0, import_chokidar.runOnce)(
|
|
106
83
|
sentinelPath,
|
|
107
|
-
async () => await (0, import_chokidar.prepareIntlayer)(import_built.default)
|
|
84
|
+
async () => await (0, import_chokidar.prepareIntlayer)(import_built.default),
|
|
85
|
+
() => appLogger("Intlayer prepared")
|
|
108
86
|
);
|
|
109
87
|
}
|
|
110
88
|
}
|
|
111
89
|
];
|
|
112
90
|
if (optimize) {
|
|
113
|
-
plugins.push((0, import_intlayerPrunePlugin.
|
|
91
|
+
plugins.push((0, import_intlayerPrunePlugin.intlayerPrune)(import_built.default));
|
|
114
92
|
}
|
|
115
93
|
return plugins;
|
|
116
94
|
};
|
|
95
|
+
const intlayerPlugin = intlayer;
|
|
117
96
|
// Annotate the CommonJS export names for ESM import in node:
|
|
118
97
|
0 && (module.exports = {
|
|
98
|
+
intlayer,
|
|
119
99
|
intlayerPlugin
|
|
120
100
|
});
|
|
121
101
|
//# sourceMappingURL=intlayerPlugin.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerPlugin.ts"],"sourcesContent":["import {
|
|
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"]}
|
|
@@ -28,34 +28,42 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
var intlayerPrunePlugin_exports = {};
|
|
30
30
|
__export(intlayerPrunePlugin_exports, {
|
|
31
|
-
|
|
31
|
+
intlayerPrune: () => intlayerPrune
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(intlayerPrunePlugin_exports);
|
|
34
34
|
var import_babel = require("@intlayer/babel");
|
|
35
35
|
var import_config = require("@intlayer/config");
|
|
36
|
+
var import_dictionaries_entry = __toESM(require("@intlayer/dictionaries-entry"));
|
|
36
37
|
var import_fast_glob = __toESM(require("fast-glob"));
|
|
37
38
|
var import_path = require("path");
|
|
38
|
-
const
|
|
39
|
+
const intlayerPrune = (intlayerConfig) => {
|
|
39
40
|
const { optimize, importMode, traversePattern } = intlayerConfig.build;
|
|
40
|
-
const {
|
|
41
|
+
const {
|
|
42
|
+
dictionariesDir,
|
|
43
|
+
dynamicDictionariesDir,
|
|
44
|
+
fetchDictionariesDir,
|
|
45
|
+
mainDir,
|
|
46
|
+
baseDir
|
|
47
|
+
} = intlayerConfig.content;
|
|
41
48
|
const filesListPattern = import_fast_glob.default.sync(traversePattern, {
|
|
42
49
|
cwd: baseDir
|
|
43
50
|
}).map((file) => (0, import_path.join)(baseDir, file));
|
|
51
|
+
const dictionariesEntryPath = (0, import_path.join)(mainDir, "dictionaries.mjs");
|
|
52
|
+
const dynamicDictionariesEntryPath = (0, import_path.join)(
|
|
53
|
+
mainDir,
|
|
54
|
+
"dynamic_dictionaries.mjs"
|
|
55
|
+
);
|
|
56
|
+
const filesList = [
|
|
57
|
+
...filesListPattern,
|
|
58
|
+
dictionariesEntryPath
|
|
59
|
+
// should add dictionariesEntryPath to replace it by a empty object if import made dynamic
|
|
60
|
+
];
|
|
61
|
+
const liveSyncKeys = Object.values(import_dictionaries_entry.default).filter((dictionary) => dictionary.live).map((dictionary) => dictionary.key);
|
|
44
62
|
return {
|
|
45
63
|
name: "vite-intlayer-babel-transform",
|
|
46
64
|
enforce: "post",
|
|
47
65
|
// Run after other transformations as vue
|
|
48
66
|
transform(code, id) {
|
|
49
|
-
const dictionariesEntryPath = (0, import_path.join)(mainDir, "dictionaries.mjs");
|
|
50
|
-
const dynamicDictionariesEntryPath = (0, import_path.join)(
|
|
51
|
-
mainDir,
|
|
52
|
-
"dynamic_dictionaries.mjs"
|
|
53
|
-
);
|
|
54
|
-
const filesList = [
|
|
55
|
-
...filesListPattern,
|
|
56
|
-
dictionariesEntryPath
|
|
57
|
-
// should add dictionariesEntryPath to replace it by a empty object if import made dynamic
|
|
58
|
-
];
|
|
59
67
|
const filename = id.split("?", 1)[0];
|
|
60
68
|
if (!filesList.includes(filename)) return null;
|
|
61
69
|
if (!optimize) return null;
|
|
@@ -71,9 +79,11 @@ const IntlayerPrunePlugin = (intlayerConfig) => {
|
|
|
71
79
|
dictionariesEntryPath,
|
|
72
80
|
dynamicDictionariesDir,
|
|
73
81
|
dynamicDictionariesEntryPath,
|
|
82
|
+
fetchDictionariesDir,
|
|
74
83
|
importMode,
|
|
75
84
|
filesList,
|
|
76
|
-
replaceDictionaryEntry: false
|
|
85
|
+
replaceDictionaryEntry: false,
|
|
86
|
+
liveSyncKeys
|
|
77
87
|
}
|
|
78
88
|
]
|
|
79
89
|
],
|
|
@@ -111,6 +121,6 @@ const IntlayerPrunePlugin = (intlayerConfig) => {
|
|
|
111
121
|
};
|
|
112
122
|
// Annotate the CommonJS export names for ESM import in node:
|
|
113
123
|
0 && (module.exports = {
|
|
114
|
-
|
|
124
|
+
intlayerPrune
|
|
115
125
|
});
|
|
116
126
|
//# sourceMappingURL=intlayerPrunePlugin.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerPrunePlugin.ts"],"sourcesContent":["// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { intlayerBabelPlugin } from '@intlayer/babel';\nimport { ESMxCJSRequire, IntlayerConfig } from '@intlayer/config';\nimport fg from 'fast-glob';\nimport { join } from 'path';\nimport { type PluginOption } from 'vite';\n\nexport const
|
|
1
|
+
{"version":3,"sources":["../../src/intlayerPrunePlugin.ts"],"sourcesContent":["// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { intlayerBabelPlugin } from '@intlayer/babel';\nimport { ESMxCJSRequire, IntlayerConfig } from '@intlayer/config';\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport fg from 'fast-glob';\nimport { join } from 'path';\nimport { type PluginOption } from 'vite';\n\nexport const intlayerPrune = (intlayerConfig: IntlayerConfig): PluginOption => {\n const { optimize, importMode, traversePattern } = intlayerConfig.build;\n\n const {\n dictionariesDir,\n dynamicDictionariesDir,\n fetchDictionariesDir,\n mainDir,\n baseDir,\n } = intlayerConfig.content;\n\n const filesListPattern = fg\n .sync(traversePattern, {\n cwd: baseDir,\n })\n .map((file) => join(baseDir, file));\n\n const dictionariesEntryPath = join(mainDir, 'dictionaries.mjs');\n const dynamicDictionariesEntryPath = join(\n mainDir,\n 'dynamic_dictionaries.mjs'\n );\n\n const filesList = [\n ...filesListPattern,\n dictionariesEntryPath, // should add dictionariesEntryPath to replace it by a empty object if import made dynamic\n ];\n\n const liveSyncKeys = Object.values(dictionaries)\n .filter((dictionary) => dictionary.live)\n .map((dictionary) => dictionary.key);\n\n return {\n name: 'vite-intlayer-babel-transform',\n enforce: 'post', // Run after other transformations as vue\n transform(code, id) {\n /**\n * Transform file as\n * .../HelloWorld.vue?vue&type=script&setup=true&lang.ts\n * Into\n * .../HelloWorld.vue\n *\n * Prevention for virtual file\n */\n const filename = id.split('?', 1)[0];\n if (!filesList.includes(filename)) return null;\n if (!optimize) return null;\n\n try {\n const babel = ESMxCJSRequire('@babel/core');\n\n const result = babel.transformSync(code, {\n filename,\n plugins: [\n [\n intlayerBabelPlugin,\n {\n dictionariesDir,\n dictionariesEntryPath,\n dynamicDictionariesDir,\n dynamicDictionariesEntryPath,\n fetchDictionariesDir,\n importMode,\n filesList,\n replaceDictionaryEntry: false,\n liveSyncKeys,\n },\n ],\n ],\n parserOpts: {\n sourceType: 'module',\n allowImportExportEverywhere: true,\n plugins: [\n 'typescript',\n 'jsx',\n 'decorators-legacy',\n 'classProperties',\n 'objectRestSpread',\n 'asyncGenerators',\n 'functionBind',\n 'exportDefaultFrom',\n 'exportNamespaceFrom',\n 'dynamicImport',\n 'nullishCoalescingOperator',\n 'optionalChaining',\n ],\n },\n });\n\n if (result?.code) {\n return {\n code: result.code,\n map: result.map,\n };\n }\n } catch (error) {\n console.warn('Failed to transform with Babel plugin:', error);\n }\n\n return null;\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAoC;AACpC,oBAA+C;AAC/C,gCAAyB;AACzB,uBAAe;AACf,kBAAqB;AAGd,MAAM,gBAAgB,CAAC,mBAAiD;AAC7E,QAAM,EAAE,UAAU,YAAY,gBAAgB,IAAI,eAAe;AAEjE,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,eAAe;AAEnB,QAAM,mBAAmB,iBAAAA,QACtB,KAAK,iBAAiB;AAAA,IACrB,KAAK;AAAA,EACP,CAAC,EACA,IAAI,CAAC,aAAS,kBAAK,SAAS,IAAI,CAAC;AAEpC,QAAM,4BAAwB,kBAAK,SAAS,kBAAkB;AAC9D,QAAM,mCAA+B;AAAA,IACnC;AAAA,IACA;AAAA,EACF;AAEA,QAAM,YAAY;AAAA,IAChB,GAAG;AAAA,IACH;AAAA;AAAA,EACF;AAEA,QAAM,eAAe,OAAO,OAAO,0BAAAC,OAAY,EAC5C,OAAO,CAAC,eAAe,WAAW,IAAI,EACtC,IAAI,CAAC,eAAe,WAAW,GAAG;AAErC,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA;AAAA,IACT,UAAU,MAAM,IAAI;AASlB,YAAM,WAAW,GAAG,MAAM,KAAK,CAAC,EAAE,CAAC;AACnC,UAAI,CAAC,UAAU,SAAS,QAAQ,EAAG,QAAO;AAC1C,UAAI,CAAC,SAAU,QAAO;AAEtB,UAAI;AACF,cAAM,YAAQ,8BAAe,aAAa;AAE1C,cAAM,SAAS,MAAM,cAAc,MAAM;AAAA,UACvC;AAAA,UACA,SAAS;AAAA,YACP;AAAA,cACE;AAAA,cACA;AAAA,gBACE;AAAA,gBACA;AAAA,gBACA;AAAA,gBACA;AAAA,gBACA;AAAA,gBACA;AAAA,gBACA;AAAA,gBACA,wBAAwB;AAAA,gBACxB;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,UACA,YAAY;AAAA,YACV,YAAY;AAAA,YACZ,6BAA6B;AAAA,YAC7B,SAAS;AAAA,cACP;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YACF;AAAA,UACF;AAAA,QACF,CAAC;AAED,YAAI,QAAQ,MAAM;AAChB,iBAAO;AAAA,YACL,MAAM,OAAO;AAAA,YACb,KAAK,OAAO;AAAA,UACd;AAAA,QACF;AAAA,MACF,SAAS,OAAO;AACd,gBAAQ,KAAK,0CAA0C,KAAK;AAAA,MAC9D;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":["fg","dictionaries"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { parse } from "url";
|
|
2
1
|
import { getConfiguration } from "@intlayer/config";
|
|
3
2
|
import { localeDetector } from "@intlayer/core";
|
|
3
|
+
import { parse } from "url";
|
|
4
4
|
const intlayerConfig = getConfiguration();
|
|
5
5
|
const { internationalization, middleware } = intlayerConfig;
|
|
6
6
|
const { locales: supportedLocales, defaultLocale } = internationalization;
|
|
@@ -12,7 +12,7 @@ const {
|
|
|
12
12
|
serverSetCookie,
|
|
13
13
|
basePath = ""
|
|
14
14
|
} = middleware;
|
|
15
|
-
const
|
|
15
|
+
const intlayerMiddleware = () => {
|
|
16
16
|
return {
|
|
17
17
|
name: "vite-intlayer-middleware-plugin",
|
|
18
18
|
configureServer: (server) => {
|
|
@@ -143,6 +143,20 @@ const handleMissingPathLocale = ({
|
|
|
143
143
|
originalPath,
|
|
144
144
|
cookieLocale
|
|
145
145
|
}) => {
|
|
146
|
+
const referer = req.headers.referer || req.headers.referrer;
|
|
147
|
+
if (referer) {
|
|
148
|
+
try {
|
|
149
|
+
const refererUrl = new URL(referer);
|
|
150
|
+
const host = req.headers.host;
|
|
151
|
+
if (host && refererUrl.host === host) {
|
|
152
|
+
const locale2 = defaultLocale;
|
|
153
|
+
const newPath2 = constructPath(locale2, originalPath);
|
|
154
|
+
rewriteUrl(req, res, newPath2, locale2);
|
|
155
|
+
return next();
|
|
156
|
+
}
|
|
157
|
+
} catch {
|
|
158
|
+
}
|
|
159
|
+
}
|
|
146
160
|
let locale = cookieLocale ?? localeDetector(
|
|
147
161
|
req.headers,
|
|
148
162
|
supportedLocales,
|
|
@@ -194,7 +208,9 @@ const handleDefaultLocaleRedirect = ({
|
|
|
194
208
|
rewriteUrl(req, res, originalPath, pathLocale);
|
|
195
209
|
return next();
|
|
196
210
|
};
|
|
211
|
+
const intlayerMiddlewarePlugin = intlayerMiddleware;
|
|
197
212
|
export {
|
|
198
|
-
|
|
213
|
+
intlayerMiddleware,
|
|
214
|
+
intlayerMiddlewarePlugin
|
|
199
215
|
};
|
|
200
216
|
//# sourceMappingURL=intlayerMiddlewarePlugin.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\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 // 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);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AA,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;","names":[]}
|
|
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 * ```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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,49 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
checkDictionaryChanges,
|
|
3
|
-
prepareIntlayer,
|
|
4
|
-
runOnce,
|
|
5
|
-
watch
|
|
6
|
-
} from "@intlayer/chokidar";
|
|
1
|
+
import { prepareIntlayer, runOnce, watch } from "@intlayer/chokidar";
|
|
7
2
|
import intlayerConfig from "@intlayer/config/built";
|
|
8
|
-
import { join,
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
dictionariesDir,
|
|
14
|
-
unmergedDictionariesDir,
|
|
15
|
-
dynamicDictionariesDir,
|
|
16
|
-
configDir,
|
|
17
|
-
baseDir,
|
|
18
|
-
watch: isWatchMode
|
|
19
|
-
} = intlayerConfig.content;
|
|
20
|
-
const { hotReload } = intlayerConfig.editor;
|
|
3
|
+
import { join, resolve } from "path";
|
|
4
|
+
import { getAlias, getAppLogger } from "@intlayer/config";
|
|
5
|
+
import { intlayerPrune } from "./intlayerPrunePlugin.mjs";
|
|
6
|
+
const intlayer = () => {
|
|
7
|
+
const { watch: isWatchMode } = intlayerConfig.content;
|
|
21
8
|
const { optimize } = intlayerConfig.build;
|
|
9
|
+
const appLogger = getAppLogger(intlayerConfig);
|
|
22
10
|
const plugins = [
|
|
23
11
|
{
|
|
24
12
|
name: "vite-intlayer-plugin",
|
|
25
13
|
config: (config) => {
|
|
26
|
-
const dictionariesPath = join(mainDir, "dictionaries.mjs");
|
|
27
|
-
const relativeDictionariesPath = relative(baseDir, dictionariesPath);
|
|
28
|
-
const unmergedDictionariesPath = join(
|
|
29
|
-
mainDir,
|
|
30
|
-
"unmerged_dictionaries.mjs"
|
|
31
|
-
);
|
|
32
|
-
const relativeUnmergedDictionariesPath = relative(
|
|
33
|
-
baseDir,
|
|
34
|
-
unmergedDictionariesPath
|
|
35
|
-
);
|
|
36
|
-
const configurationPath = join(configDir, "configuration.json");
|
|
37
|
-
const relativeConfigurationPath = relative(baseDir, configurationPath);
|
|
38
14
|
config.resolve = {
|
|
39
15
|
...config.resolve,
|
|
40
16
|
alias: {
|
|
41
17
|
...config.resolve?.alias,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
)
|
|
46
|
-
"@intlayer/config/built": resolve(relativeConfigurationPath)
|
|
18
|
+
...getAlias({
|
|
19
|
+
configuration: intlayerConfig,
|
|
20
|
+
formatter: (value) => resolve(value)
|
|
21
|
+
})
|
|
47
22
|
}
|
|
48
23
|
};
|
|
49
24
|
if (isWatchMode) {
|
|
@@ -52,7 +27,6 @@ const intlayerPlugin = () => {
|
|
|
52
27
|
exclude: [
|
|
53
28
|
...config.optimizeDeps?.exclude ?? [],
|
|
54
29
|
"@intlayer/dictionaries-entry",
|
|
55
|
-
"@intlayer/unmerged-dictionaries-entry",
|
|
56
30
|
"@intlayer/config/built"
|
|
57
31
|
]
|
|
58
32
|
};
|
|
@@ -63,9 +37,6 @@ const intlayerPlugin = () => {
|
|
|
63
37
|
if (intlayerConfig.content.watch) {
|
|
64
38
|
watch({ configuration: intlayerConfig });
|
|
65
39
|
}
|
|
66
|
-
if (hotReload) {
|
|
67
|
-
await checkDictionaryChanges();
|
|
68
|
-
}
|
|
69
40
|
},
|
|
70
41
|
buildStart: async () => {
|
|
71
42
|
const sentinelPath = join(
|
|
@@ -76,17 +47,20 @@ const intlayerPlugin = () => {
|
|
|
76
47
|
);
|
|
77
48
|
await runOnce(
|
|
78
49
|
sentinelPath,
|
|
79
|
-
async () => await prepareIntlayer(intlayerConfig)
|
|
50
|
+
async () => await prepareIntlayer(intlayerConfig),
|
|
51
|
+
() => appLogger("Intlayer prepared")
|
|
80
52
|
);
|
|
81
53
|
}
|
|
82
54
|
}
|
|
83
55
|
];
|
|
84
56
|
if (optimize) {
|
|
85
|
-
plugins.push(
|
|
57
|
+
plugins.push(intlayerPrune(intlayerConfig));
|
|
86
58
|
}
|
|
87
59
|
return plugins;
|
|
88
60
|
};
|
|
61
|
+
const intlayerPlugin = intlayer;
|
|
89
62
|
export {
|
|
63
|
+
intlayer,
|
|
90
64
|
intlayerPlugin
|
|
91
65
|
};
|
|
92
66
|
//# sourceMappingURL=intlayerPlugin.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerPlugin.ts"],"sourcesContent":["import {
|
|
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,28 +1,36 @@
|
|
|
1
1
|
import { intlayerBabelPlugin } from "@intlayer/babel";
|
|
2
2
|
import { ESMxCJSRequire } from "@intlayer/config";
|
|
3
|
+
import dictionaries from "@intlayer/dictionaries-entry";
|
|
3
4
|
import fg from "fast-glob";
|
|
4
5
|
import { join } from "path";
|
|
5
|
-
const
|
|
6
|
+
const intlayerPrune = (intlayerConfig) => {
|
|
6
7
|
const { optimize, importMode, traversePattern } = intlayerConfig.build;
|
|
7
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
dictionariesDir,
|
|
10
|
+
dynamicDictionariesDir,
|
|
11
|
+
fetchDictionariesDir,
|
|
12
|
+
mainDir,
|
|
13
|
+
baseDir
|
|
14
|
+
} = intlayerConfig.content;
|
|
8
15
|
const filesListPattern = fg.sync(traversePattern, {
|
|
9
16
|
cwd: baseDir
|
|
10
17
|
}).map((file) => join(baseDir, file));
|
|
18
|
+
const dictionariesEntryPath = join(mainDir, "dictionaries.mjs");
|
|
19
|
+
const dynamicDictionariesEntryPath = join(
|
|
20
|
+
mainDir,
|
|
21
|
+
"dynamic_dictionaries.mjs"
|
|
22
|
+
);
|
|
23
|
+
const filesList = [
|
|
24
|
+
...filesListPattern,
|
|
25
|
+
dictionariesEntryPath
|
|
26
|
+
// should add dictionariesEntryPath to replace it by a empty object if import made dynamic
|
|
27
|
+
];
|
|
28
|
+
const liveSyncKeys = Object.values(dictionaries).filter((dictionary) => dictionary.live).map((dictionary) => dictionary.key);
|
|
11
29
|
return {
|
|
12
30
|
name: "vite-intlayer-babel-transform",
|
|
13
31
|
enforce: "post",
|
|
14
32
|
// Run after other transformations as vue
|
|
15
33
|
transform(code, id) {
|
|
16
|
-
const dictionariesEntryPath = join(mainDir, "dictionaries.mjs");
|
|
17
|
-
const dynamicDictionariesEntryPath = join(
|
|
18
|
-
mainDir,
|
|
19
|
-
"dynamic_dictionaries.mjs"
|
|
20
|
-
);
|
|
21
|
-
const filesList = [
|
|
22
|
-
...filesListPattern,
|
|
23
|
-
dictionariesEntryPath
|
|
24
|
-
// should add dictionariesEntryPath to replace it by a empty object if import made dynamic
|
|
25
|
-
];
|
|
26
34
|
const filename = id.split("?", 1)[0];
|
|
27
35
|
if (!filesList.includes(filename)) return null;
|
|
28
36
|
if (!optimize) return null;
|
|
@@ -38,9 +46,11 @@ const IntlayerPrunePlugin = (intlayerConfig) => {
|
|
|
38
46
|
dictionariesEntryPath,
|
|
39
47
|
dynamicDictionariesDir,
|
|
40
48
|
dynamicDictionariesEntryPath,
|
|
49
|
+
fetchDictionariesDir,
|
|
41
50
|
importMode,
|
|
42
51
|
filesList,
|
|
43
|
-
replaceDictionaryEntry: false
|
|
52
|
+
replaceDictionaryEntry: false,
|
|
53
|
+
liveSyncKeys
|
|
44
54
|
}
|
|
45
55
|
]
|
|
46
56
|
],
|
|
@@ -77,6 +87,6 @@ const IntlayerPrunePlugin = (intlayerConfig) => {
|
|
|
77
87
|
};
|
|
78
88
|
};
|
|
79
89
|
export {
|
|
80
|
-
|
|
90
|
+
intlayerPrune
|
|
81
91
|
};
|
|
82
92
|
//# sourceMappingURL=intlayerPrunePlugin.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerPrunePlugin.ts"],"sourcesContent":["// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { intlayerBabelPlugin } from '@intlayer/babel';\nimport { ESMxCJSRequire, IntlayerConfig } from '@intlayer/config';\nimport fg from 'fast-glob';\nimport { join } from 'path';\nimport { type PluginOption } from 'vite';\n\nexport const
|
|
1
|
+
{"version":3,"sources":["../../src/intlayerPrunePlugin.ts"],"sourcesContent":["// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { intlayerBabelPlugin } from '@intlayer/babel';\nimport { ESMxCJSRequire, IntlayerConfig } from '@intlayer/config';\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport fg from 'fast-glob';\nimport { join } from 'path';\nimport { type PluginOption } from 'vite';\n\nexport const intlayerPrune = (intlayerConfig: IntlayerConfig): PluginOption => {\n const { optimize, importMode, traversePattern } = intlayerConfig.build;\n\n const {\n dictionariesDir,\n dynamicDictionariesDir,\n fetchDictionariesDir,\n mainDir,\n baseDir,\n } = intlayerConfig.content;\n\n const filesListPattern = fg\n .sync(traversePattern, {\n cwd: baseDir,\n })\n .map((file) => join(baseDir, file));\n\n const dictionariesEntryPath = join(mainDir, 'dictionaries.mjs');\n const dynamicDictionariesEntryPath = join(\n mainDir,\n 'dynamic_dictionaries.mjs'\n );\n\n const filesList = [\n ...filesListPattern,\n dictionariesEntryPath, // should add dictionariesEntryPath to replace it by a empty object if import made dynamic\n ];\n\n const liveSyncKeys = Object.values(dictionaries)\n .filter((dictionary) => dictionary.live)\n .map((dictionary) => dictionary.key);\n\n return {\n name: 'vite-intlayer-babel-transform',\n enforce: 'post', // Run after other transformations as vue\n transform(code, id) {\n /**\n * Transform file as\n * .../HelloWorld.vue?vue&type=script&setup=true&lang.ts\n * Into\n * .../HelloWorld.vue\n *\n * Prevention for virtual file\n */\n const filename = id.split('?', 1)[0];\n if (!filesList.includes(filename)) return null;\n if (!optimize) return null;\n\n try {\n const babel = ESMxCJSRequire('@babel/core');\n\n const result = babel.transformSync(code, {\n filename,\n plugins: [\n [\n intlayerBabelPlugin,\n {\n dictionariesDir,\n dictionariesEntryPath,\n dynamicDictionariesDir,\n dynamicDictionariesEntryPath,\n fetchDictionariesDir,\n importMode,\n filesList,\n replaceDictionaryEntry: false,\n liveSyncKeys,\n },\n ],\n ],\n parserOpts: {\n sourceType: 'module',\n allowImportExportEverywhere: true,\n plugins: [\n 'typescript',\n 'jsx',\n 'decorators-legacy',\n 'classProperties',\n 'objectRestSpread',\n 'asyncGenerators',\n 'functionBind',\n 'exportDefaultFrom',\n 'exportNamespaceFrom',\n 'dynamicImport',\n 'nullishCoalescingOperator',\n 'optionalChaining',\n ],\n },\n });\n\n if (result?.code) {\n return {\n code: result.code,\n map: result.map,\n };\n }\n } catch (error) {\n console.warn('Failed to transform with Babel plugin:', error);\n }\n\n return null;\n },\n };\n};\n"],"mappings":"AACA,SAAS,2BAA2B;AACpC,SAAS,sBAAsC;AAC/C,OAAO,kBAAkB;AACzB,OAAO,QAAQ;AACf,SAAS,YAAY;AAGd,MAAM,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A,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YACF;AAAA,UACF;AAAA,QACF,CAAC;AAED,YAAI,QAAQ,MAAM;AAChB,iBAAO;AAAA,YACL,MAAM,OAAO;AAAA,YACb,KAAK,OAAO;AAAA,UACd;AAAA,QACF;AAAA,MACF,SAAS,OAAO;AACd,gBAAQ,KAAK,0CAA0C,KAAK;AAAA,MAC9D;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
|
|
@@ -2,5 +2,16 @@ 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
|
|
5
|
+
export declare const intlayerMiddleware: () => Plugin;
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated Rename to intlayerMiddleware instead
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* // Example usage of the plugin in a Vite configuration
|
|
11
|
+
* export default defineConfig({
|
|
12
|
+
* plugins: [ intlayerMiddleware() ],
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const intlayerMiddlewarePlugin: () => Plugin;
|
|
6
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,
|
|
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"}
|
|
@@ -6,9 +6,20 @@ import { type PluginOption } from 'vite';
|
|
|
6
6
|
* ```ts
|
|
7
7
|
* // Example usage of the plugin in a Vite configuration
|
|
8
8
|
* export default defineConfig({
|
|
9
|
-
* plugins: [
|
|
9
|
+
* plugins: [ intlayer() ],
|
|
10
10
|
* });
|
|
11
11
|
* ```
|
|
12
12
|
* */
|
|
13
|
+
export declare const intlayer: () => PluginOption;
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated Rename to intlayer instead
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* // Example usage of the plugin in a Vite configuration
|
|
19
|
+
* export default defineConfig({
|
|
20
|
+
* plugins: [ intlayer() ],
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
13
24
|
export declare const intlayerPlugin: () => PluginOption;
|
|
14
25
|
//# sourceMappingURL=intlayerPlugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intlayerPlugin.d.ts","sourceRoot":"","sources":["../../src/intlayerPlugin.ts"],"names":[],"mappings":"
|
|
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,4 +1,4 @@
|
|
|
1
1
|
import { IntlayerConfig } from '@intlayer/config';
|
|
2
2
|
import { type PluginOption } from 'vite';
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const intlayerPrune: (intlayerConfig: IntlayerConfig) => PluginOption;
|
|
4
4
|
//# sourceMappingURL=intlayerPrunePlugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intlayerPrunePlugin.d.ts","sourceRoot":"","sources":["../../src/intlayerPrunePlugin.ts"],"names":[],"mappings":"AAEA,OAAO,EAAkB,cAAc,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"intlayerPrunePlugin.d.ts","sourceRoot":"","sources":["../../src/intlayerPrunePlugin.ts"],"names":[],"mappings":"AAEA,OAAO,EAAkB,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAIlE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,MAAM,CAAC;AAEzC,eAAO,MAAM,aAAa,GAAI,gBAAgB,cAAc,KAAG,YAsG9D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-intlayer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0-canary.0",
|
|
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,32 +58,34 @@
|
|
|
58
58
|
"./package.json"
|
|
59
59
|
],
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@intlayer/
|
|
62
|
-
"@intlayer/config": "
|
|
63
|
-
"@intlayer/
|
|
64
|
-
"@intlayer/
|
|
61
|
+
"@intlayer/chokidar": "6.0.0-canary.0",
|
|
62
|
+
"@intlayer/config": "6.0.0-canary.0",
|
|
63
|
+
"@intlayer/core": "6.0.0-canary.0",
|
|
64
|
+
"@intlayer/dictionaries-entry": "6.0.0-canary.0",
|
|
65
|
+
"@intlayer/babel": "6.0.0-canary.0"
|
|
65
66
|
},
|
|
66
67
|
"devDependencies": {
|
|
67
68
|
"@types/node": "^24.2.1",
|
|
68
69
|
"@typescript-eslint/parser": "^8.33.1",
|
|
69
70
|
"concurrently": "^9.1.2",
|
|
70
|
-
"eslint": "^9.
|
|
71
|
-
"prettier": "^3.
|
|
71
|
+
"eslint": "^9.34.0",
|
|
72
|
+
"prettier": "^3.6.2",
|
|
72
73
|
"rimraf": "^6.0.1",
|
|
73
74
|
"tsc-alias": "^1.8.16",
|
|
74
75
|
"tsup": "^8.5.0",
|
|
75
76
|
"typescript": "^5.9.2",
|
|
76
|
-
"vitest": "^3.2.
|
|
77
|
+
"vitest": "^3.2.4",
|
|
77
78
|
"@utils/ts-config": "1.0.4",
|
|
78
|
-
"@utils/ts-config-types": "1.0.4",
|
|
79
79
|
"@utils/eslint-config": "1.0.4",
|
|
80
|
+
"@utils/ts-config-types": "1.0.4",
|
|
80
81
|
"@utils/tsup-config": "1.0.4"
|
|
81
82
|
},
|
|
82
83
|
"peerDependencies": {
|
|
83
84
|
"vite": ">=4.0.0",
|
|
84
|
-
"@intlayer/chokidar": "
|
|
85
|
-
"@intlayer/config": "
|
|
86
|
-
"@intlayer/
|
|
85
|
+
"@intlayer/chokidar": "6.0.0-canary.0",
|
|
86
|
+
"@intlayer/config": "6.0.0-canary.0",
|
|
87
|
+
"@intlayer/dictionaries-entry": "6.0.0-canary.0",
|
|
88
|
+
"@intlayer/core": "6.0.0-canary.0"
|
|
87
89
|
},
|
|
88
90
|
"engines": {
|
|
89
91
|
"node": ">=14.18"
|