preact-intlayer 5.5.4 → 5.5.6
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/IntlayerNode.cjs +47 -0
- package/dist/cjs/IntlayerNode.cjs.map +1 -0
- package/dist/cjs/UI/ContentSelector.cjs +127 -0
- package/dist/cjs/UI/ContentSelector.cjs.map +1 -0
- package/dist/cjs/client/IntlayerProvider.cjs +98 -0
- package/dist/cjs/client/IntlayerProvider.cjs.map +1 -0
- package/dist/cjs/client/getBrowserLocale.cjs +173 -0
- package/dist/cjs/client/getBrowserLocale.cjs.map +1 -0
- package/dist/cjs/client/index.cjs +69 -0
- package/dist/cjs/client/index.cjs.map +1 -0
- package/dist/cjs/client/t.cjs +37 -0
- package/dist/cjs/client/t.cjs.map +1 -0
- package/dist/cjs/client/useContent.cjs +39 -0
- package/dist/cjs/client/useContent.cjs.map +1 -0
- package/dist/cjs/client/useDictionary.cjs +42 -0
- package/dist/cjs/client/useDictionary.cjs.map +1 -0
- package/dist/cjs/client/useDictionaryDynamic.cjs +42 -0
- package/dist/cjs/client/useDictionaryDynamic.cjs.map +1 -0
- package/dist/cjs/client/useIntlayer.cjs +43 -0
- package/dist/cjs/client/useIntlayer.cjs.map +1 -0
- package/dist/cjs/client/useIntlayerAsync.cjs +54 -0
- package/dist/cjs/client/useIntlayerAsync.cjs.map +1 -0
- package/dist/cjs/client/useLoadDynamic.cjs +56 -0
- package/dist/cjs/client/useLoadDynamic.cjs.map +1 -0
- package/dist/cjs/client/useLocale.cjs +69 -0
- package/dist/cjs/client/useLocale.cjs.map +1 -0
- package/dist/cjs/client/useLocaleBase.cjs +56 -0
- package/dist/cjs/client/useLocaleBase.cjs.map +1 -0
- package/dist/cjs/client/useLocaleCookie.cjs +60 -0
- package/dist/cjs/client/useLocaleCookie.cjs.map +1 -0
- package/dist/cjs/client/useTraduction.cjs +35 -0
- package/dist/cjs/client/useTraduction.cjs.map +1 -0
- package/dist/cjs/editor/ChangedContentContext.cjs +77 -0
- package/dist/cjs/editor/ChangedContentContext.cjs.map +1 -0
- package/dist/cjs/editor/CommunicatorContext.cjs +66 -0
- package/dist/cjs/editor/CommunicatorContext.cjs.map +1 -0
- package/dist/cjs/editor/ConfigurationContext.cjs +50 -0
- package/dist/cjs/editor/ConfigurationContext.cjs.map +1 -0
- package/dist/cjs/editor/ContentSelectorWrapper.cjs +66 -0
- package/dist/cjs/editor/ContentSelectorWrapper.cjs.map +1 -0
- package/dist/cjs/editor/DictionariesRecordContext.cjs +77 -0
- package/dist/cjs/editor/DictionariesRecordContext.cjs.map +1 -0
- package/dist/cjs/editor/EditedContentContext.cjs +217 -0
- package/dist/cjs/editor/EditedContentContext.cjs.map +1 -0
- package/dist/cjs/editor/EditorEnabledContext.cjs +62 -0
- package/dist/cjs/editor/EditorEnabledContext.cjs.map +1 -0
- package/dist/cjs/editor/EditorProvider.cjs +72 -0
- package/dist/cjs/editor/EditorProvider.cjs.map +1 -0
- package/dist/cjs/editor/FocusDictionaryContext.cjs +81 -0
- package/dist/cjs/editor/FocusDictionaryContext.cjs.map +1 -0
- package/dist/cjs/editor/IntlayerEditorProvider.cjs +122 -0
- package/dist/cjs/editor/IntlayerEditorProvider.cjs.map +1 -0
- package/dist/cjs/editor/index.cjs +23 -0
- package/dist/cjs/editor/index.cjs.map +1 -0
- package/dist/cjs/editor/useCrossFrameMessageListener.cjs +53 -0
- package/dist/cjs/editor/useCrossFrameMessageListener.cjs.map +1 -0
- package/dist/cjs/editor/useCrossFrameState.cjs +77 -0
- package/dist/cjs/editor/useCrossFrameState.cjs.map +1 -0
- package/dist/cjs/editor/useCrossURLPathState.cjs +69 -0
- package/dist/cjs/editor/useCrossURLPathState.cjs.map +1 -0
- package/dist/cjs/editor/useEditedContentRenderer.cjs +63 -0
- package/dist/cjs/editor/useEditedContentRenderer.cjs.map +1 -0
- package/dist/cjs/editor/useIframeClickInterceptor.cjs +50 -0
- package/dist/cjs/editor/useIframeClickInterceptor.cjs.map +1 -0
- package/dist/cjs/getDictionary.cjs +43 -0
- package/dist/cjs/getDictionary.cjs.map +1 -0
- package/dist/cjs/getIntlayer.cjs +39 -0
- package/dist/cjs/getIntlayer.cjs.map +1 -0
- package/dist/cjs/index.cjs +42 -5
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/markdown/MarkdownProvider.cjs +41 -0
- package/dist/cjs/markdown/MarkdownProvider.cjs.map +1 -0
- package/dist/cjs/markdown/MarkdownRenderer.cjs +83 -0
- package/dist/cjs/markdown/MarkdownRenderer.cjs.map +1 -0
- package/dist/cjs/markdown/index.cjs +25 -0
- package/dist/cjs/markdown/index.cjs.map +1 -0
- package/dist/cjs/plugins.cjs +128 -0
- package/dist/cjs/plugins.cjs.map +1 -0
- package/dist/cjs/preactElement/renderPreactElement.cjs +62 -0
- package/dist/cjs/preactElement/renderPreactElement.cjs.map +1 -0
- package/dist/esm/IntlayerNode.mjs +27 -0
- package/dist/esm/IntlayerNode.mjs.map +1 -0
- package/dist/esm/UI/ContentSelector.mjs +103 -0
- package/dist/esm/UI/ContentSelector.mjs.map +1 -0
- package/dist/esm/client/IntlayerProvider.mjs +63 -0
- package/dist/esm/client/IntlayerProvider.mjs.map +1 -0
- package/dist/esm/client/getBrowserLocale.mjs +138 -0
- package/dist/esm/client/getBrowserLocale.mjs.map +1 -0
- package/dist/esm/client/index.mjs +39 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/client/t.mjs +13 -0
- package/dist/esm/client/t.mjs.map +1 -0
- package/dist/esm/client/useContent.mjs +15 -0
- package/dist/esm/client/useContent.mjs.map +1 -0
- package/dist/esm/client/useDictionary.mjs +18 -0
- package/dist/esm/client/useDictionary.mjs.map +1 -0
- package/dist/esm/client/useDictionaryDynamic.mjs +18 -0
- package/dist/esm/client/useDictionaryDynamic.mjs.map +1 -0
- package/dist/esm/client/useIntlayer.mjs +19 -0
- package/dist/esm/client/useIntlayer.mjs.map +1 -0
- package/dist/esm/client/useIntlayerAsync.mjs +33 -0
- package/dist/esm/client/useIntlayerAsync.mjs.map +1 -0
- package/dist/esm/client/useLoadDynamic.mjs +32 -0
- package/dist/esm/client/useLoadDynamic.mjs.map +1 -0
- package/dist/esm/client/useLocale.mjs +35 -0
- package/dist/esm/client/useLocale.mjs.map +1 -0
- package/dist/esm/client/useLocaleBase.mjs +22 -0
- package/dist/esm/client/useLocaleBase.mjs.map +1 -0
- package/dist/esm/client/useLocaleCookie.mjs +24 -0
- package/dist/esm/client/useLocaleCookie.mjs.map +1 -0
- package/dist/esm/client/useTraduction.mjs +11 -0
- package/dist/esm/client/useTraduction.mjs.map +1 -0
- package/dist/esm/editor/ChangedContentContext.mjs +55 -0
- package/dist/esm/editor/ChangedContentContext.mjs.map +1 -0
- package/dist/esm/editor/CommunicatorContext.mjs +33 -0
- package/dist/esm/editor/CommunicatorContext.mjs.map +1 -0
- package/dist/esm/editor/ConfigurationContext.mjs +27 -0
- package/dist/esm/editor/ConfigurationContext.mjs.map +1 -0
- package/dist/esm/editor/ContentSelectorWrapper.mjs +45 -0
- package/dist/esm/editor/ContentSelectorWrapper.mjs.map +1 -0
- package/dist/esm/editor/DictionariesRecordContext.mjs +55 -0
- package/dist/esm/editor/DictionariesRecordContext.mjs.map +1 -0
- package/dist/esm/editor/EditedContentContext.mjs +198 -0
- package/dist/esm/editor/EditedContentContext.mjs.map +1 -0
- package/dist/esm/editor/EditorEnabledContext.mjs +39 -0
- package/dist/esm/editor/EditorEnabledContext.mjs.map +1 -0
- package/dist/esm/editor/EditorProvider.mjs +62 -0
- package/dist/esm/editor/EditorProvider.mjs.map +1 -0
- package/dist/esm/editor/FocusDictionaryContext.mjs +58 -0
- package/dist/esm/editor/FocusDictionaryContext.mjs.map +1 -0
- package/dist/esm/editor/IntlayerEditorProvider.mjs +88 -0
- package/dist/esm/editor/IntlayerEditorProvider.mjs.map +1 -0
- package/dist/esm/editor/index.mjs +2 -0
- package/dist/esm/editor/index.mjs.map +1 -0
- package/dist/esm/editor/useCrossFrameMessageListener.mjs +29 -0
- package/dist/esm/editor/useCrossFrameMessageListener.mjs.map +1 -0
- package/dist/esm/editor/useCrossFrameState.mjs +53 -0
- package/dist/esm/editor/useCrossFrameState.mjs.map +1 -0
- package/dist/esm/editor/useCrossURLPathState.mjs +46 -0
- package/dist/esm/editor/useCrossURLPathState.mjs.map +1 -0
- package/dist/esm/editor/useEditedContentRenderer.mjs +38 -0
- package/dist/esm/editor/useEditedContentRenderer.mjs.map +1 -0
- package/dist/esm/editor/useIframeClickInterceptor.mjs +25 -0
- package/dist/esm/editor/useIframeClickInterceptor.mjs.map +1 -0
- package/dist/esm/getDictionary.mjs +25 -0
- package/dist/esm/getDictionary.mjs.map +1 -0
- package/dist/esm/getIntlayer.mjs +21 -0
- package/dist/esm/getIntlayer.mjs.map +1 -0
- package/dist/esm/index.mjs +38 -2
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/markdown/MarkdownProvider.mjs +19 -0
- package/dist/esm/markdown/MarkdownProvider.mjs.map +1 -0
- package/dist/esm/markdown/MarkdownRenderer.mjs +62 -0
- package/dist/esm/markdown/MarkdownRenderer.mjs.map +1 -0
- package/dist/esm/markdown/index.mjs +3 -0
- package/dist/esm/markdown/index.mjs.map +1 -0
- package/dist/esm/plugins.mjs +104 -0
- package/dist/esm/plugins.mjs.map +1 -0
- package/dist/esm/preactElement/renderPreactElement.mjs +38 -0
- package/dist/esm/preactElement/renderPreactElement.mjs.map +1 -0
- package/dist/types/IntlayerNode.d.ts +15 -0
- package/dist/types/IntlayerNode.d.ts.map +1 -0
- package/dist/types/UI/ContentSelector.d.ts +10 -0
- package/dist/types/UI/ContentSelector.d.ts.map +1 -0
- package/dist/types/client/IntlayerProvider.d.ts +29 -0
- package/dist/types/client/IntlayerProvider.d.ts.map +1 -0
- package/dist/types/client/getBrowserLocale.d.ts +25 -0
- package/dist/types/client/getBrowserLocale.d.ts.map +1 -0
- package/dist/types/client/index.d.ts +12 -0
- package/dist/types/client/index.d.ts.map +1 -0
- package/dist/types/client/t.d.ts +9 -0
- package/dist/types/client/t.d.ts.map +1 -0
- package/dist/types/client/useContent.d.ts +10 -0
- package/dist/types/client/useContent.d.ts.map +1 -0
- package/dist/types/client/useDictionary.d.ts +9 -0
- package/dist/types/client/useDictionary.d.ts.map +1 -0
- package/dist/types/client/useDictionaryDynamic.d.ts +9 -0
- package/dist/types/client/useDictionaryDynamic.d.ts.map +1 -0
- package/dist/types/client/useIntlayer.d.ts +11 -0
- package/dist/types/client/useIntlayer.d.ts.map +1 -0
- package/dist/types/client/useIntlayerAsync.d.ts +14 -0
- package/dist/types/client/useIntlayerAsync.d.ts.map +1 -0
- package/dist/types/client/useLoadDynamic.d.ts +2 -0
- package/dist/types/client/useLoadDynamic.d.ts.map +1 -0
- package/dist/types/client/useLocale.d.ts +15 -0
- package/dist/types/client/useLocale.d.ts.map +1 -0
- package/dist/types/client/useLocaleBase.d.ts +10 -0
- package/dist/types/client/useLocaleBase.d.ts.map +1 -0
- package/dist/types/client/useLocaleCookie.d.ts +17 -0
- package/dist/types/client/useLocaleCookie.d.ts.map +1 -0
- package/dist/types/client/useTraduction.d.ts +24 -0
- package/dist/types/client/useTraduction.d.ts.map +1 -0
- package/dist/types/editor/ChangedContentContext.d.ts +13 -0
- package/dist/types/editor/ChangedContentContext.d.ts.map +1 -0
- package/dist/types/editor/CommunicatorContext.d.ts +14 -0
- package/dist/types/editor/CommunicatorContext.d.ts.map +1 -0
- package/dist/types/editor/ConfigurationContext.d.ts +9 -0
- package/dist/types/editor/ConfigurationContext.d.ts.map +1 -0
- package/dist/types/editor/ContentSelectorWrapper.d.ts +5 -0
- package/dist/types/editor/ContentSelectorWrapper.d.ts.map +1 -0
- package/dist/types/editor/DictionariesRecordContext.d.ts +16 -0
- package/dist/types/editor/DictionariesRecordContext.d.ts.map +1 -0
- package/dist/types/editor/EditedContentContext.d.ts +33 -0
- package/dist/types/editor/EditedContentContext.d.ts.map +1 -0
- package/dist/types/editor/EditorEnabledContext.d.ts +11 -0
- package/dist/types/editor/EditorEnabledContext.d.ts.map +1 -0
- package/dist/types/editor/EditorProvider.d.ts +6 -0
- package/dist/types/editor/EditorProvider.d.ts.map +1 -0
- package/dist/types/editor/FocusDictionaryContext.d.ts +21 -0
- package/dist/types/editor/FocusDictionaryContext.d.ts.map +1 -0
- package/dist/types/editor/IntlayerEditorProvider.d.ts +5 -0
- package/dist/types/editor/IntlayerEditorProvider.d.ts.map +1 -0
- package/dist/types/editor/index.d.ts +2 -0
- package/dist/types/editor/index.d.ts.map +1 -0
- package/dist/types/editor/useCrossFrameMessageListener.d.ts +19 -0
- package/dist/types/editor/useCrossFrameMessageListener.d.ts.map +1 -0
- package/dist/types/editor/useCrossFrameState.d.ts +30 -0
- package/dist/types/editor/useCrossFrameState.d.ts.map +1 -0
- package/dist/types/editor/useCrossURLPathState.d.ts +4 -0
- package/dist/types/editor/useCrossURLPathState.d.ts.map +1 -0
- package/dist/types/editor/useEditedContentRenderer.d.ts +13 -0
- package/dist/types/editor/useEditedContentRenderer.d.ts.map +1 -0
- package/dist/types/editor/useIframeClickInterceptor.d.ts +3 -0
- package/dist/types/editor/useIframeClickInterceptor.d.ts.map +1 -0
- package/dist/types/getDictionary.d.ts +5 -0
- package/dist/types/getDictionary.d.ts.map +1 -0
- package/dist/types/getIntlayer.d.ts +5 -0
- package/dist/types/getIntlayer.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/markdown/MarkdownProvider.d.ts +9 -0
- package/dist/types/markdown/MarkdownProvider.d.ts.map +1 -0
- package/dist/types/markdown/MarkdownRenderer.d.ts +16 -0
- package/dist/types/markdown/MarkdownRenderer.d.ts.map +1 -0
- package/dist/types/markdown/index.d.ts +3 -0
- package/dist/types/markdown/index.d.ts.map +1 -0
- package/dist/types/plugins.d.ts +54 -0
- package/dist/types/plugins.d.ts.map +1 -0
- package/dist/types/preactElement/renderPreactElement.d.ts +5 -0
- package/dist/types/preactElement/renderPreactElement.d.ts.map +1 -0
- package/package.json +14 -7
- package/dist/cjs/intlayerMiddlewarePlugin.cjs +0 -224
- package/dist/cjs/intlayerMiddlewarePlugin.cjs.map +0 -1
- package/dist/cjs/intlayerPlugin.cjs +0 -76
- package/dist/cjs/intlayerPlugin.cjs.map +0 -1
- package/dist/esm/intlayerMiddlewarePlugin.mjs +0 -200
- package/dist/esm/intlayerMiddlewarePlugin.mjs.map +0 -1
- package/dist/esm/intlayerPlugin.mjs +0 -52
- package/dist/esm/intlayerPlugin.mjs.map +0 -1
- package/dist/types/intlayerMiddlewarePlugin.d.ts +0 -6
- package/dist/types/intlayerMiddlewarePlugin.d.ts.map +0 -1
- package/dist/types/intlayerPlugin.d.ts +0 -16
- package/dist/types/intlayerPlugin.d.ts.map +0 -1
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var intlayerMiddlewarePlugin_exports = {};
|
|
20
|
-
__export(intlayerMiddlewarePlugin_exports, {
|
|
21
|
-
intLayerMiddlewarePlugin: () => intLayerMiddlewarePlugin
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(intlayerMiddlewarePlugin_exports);
|
|
24
|
-
var import_url = require("url");
|
|
25
|
-
var import_config = require("@intlayer/config");
|
|
26
|
-
var import_core = require("@intlayer/core");
|
|
27
|
-
const intlayerConfig = (0, import_config.getConfiguration)();
|
|
28
|
-
const { internationalization, middleware } = intlayerConfig;
|
|
29
|
-
const { locales: supportedLocales, defaultLocale } = internationalization;
|
|
30
|
-
const {
|
|
31
|
-
cookieName,
|
|
32
|
-
headerName,
|
|
33
|
-
prefixDefault,
|
|
34
|
-
noPrefix,
|
|
35
|
-
serverSetCookie,
|
|
36
|
-
basePath = ""
|
|
37
|
-
} = middleware;
|
|
38
|
-
const intLayerMiddlewarePlugin = () => {
|
|
39
|
-
return {
|
|
40
|
-
name: "vite-intlayer-middleware-plugin",
|
|
41
|
-
configureServer: (server) => {
|
|
42
|
-
server.middlewares.use((req, res, next) => {
|
|
43
|
-
if (req.url?.startsWith("/node_modules") || req.url?.startsWith("/@") || req.url?.split("?")[0].match(/\.[a-z]+$/i)) {
|
|
44
|
-
return next();
|
|
45
|
-
}
|
|
46
|
-
const parsedUrl = (0, import_url.parse)(req.url ?? "/", true);
|
|
47
|
-
const originalPath = parsedUrl.pathname ?? "/";
|
|
48
|
-
const cookies = parseCookies(req.headers.cookie ?? "");
|
|
49
|
-
const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);
|
|
50
|
-
const pathLocale = getPathLocale(originalPath);
|
|
51
|
-
if (noPrefix) {
|
|
52
|
-
handleNoPrefix({
|
|
53
|
-
req,
|
|
54
|
-
res,
|
|
55
|
-
next,
|
|
56
|
-
originalPath,
|
|
57
|
-
cookieLocale
|
|
58
|
-
});
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
handlePrefix({
|
|
62
|
-
req,
|
|
63
|
-
res,
|
|
64
|
-
next,
|
|
65
|
-
originalPath,
|
|
66
|
-
pathLocale,
|
|
67
|
-
cookieLocale
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
const parseCookies = (cookieHeader) => {
|
|
74
|
-
return cookieHeader.split(";").reduce(
|
|
75
|
-
(acc, cookie) => {
|
|
76
|
-
const [key, val] = cookie.trim().split("=");
|
|
77
|
-
acc[key] = val;
|
|
78
|
-
return acc;
|
|
79
|
-
},
|
|
80
|
-
{}
|
|
81
|
-
);
|
|
82
|
-
};
|
|
83
|
-
const getValidLocaleFromCookie = (locale) => {
|
|
84
|
-
if (locale && supportedLocales.includes(locale)) {
|
|
85
|
-
return locale;
|
|
86
|
-
}
|
|
87
|
-
return void 0;
|
|
88
|
-
};
|
|
89
|
-
const getPathLocale = (pathname) => {
|
|
90
|
-
const segments = pathname.split("/").filter(Boolean);
|
|
91
|
-
const firstSegment = segments[0];
|
|
92
|
-
if (firstSegment && supportedLocales.includes(firstSegment)) {
|
|
93
|
-
return firstSegment;
|
|
94
|
-
}
|
|
95
|
-
return void 0;
|
|
96
|
-
};
|
|
97
|
-
const redirectUrl = (res, newUrl) => {
|
|
98
|
-
res.writeHead(301, { Location: newUrl });
|
|
99
|
-
return res.end();
|
|
100
|
-
};
|
|
101
|
-
const rewriteUrl = (req, res, newUrl, locale) => {
|
|
102
|
-
req.url = newUrl;
|
|
103
|
-
if (locale && headerName) {
|
|
104
|
-
res.setHeader(headerName, locale);
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
const constructPath = (locale, currentPath) => {
|
|
108
|
-
const cleanBasePath = basePath.startsWith("/") ? basePath : `/${basePath}`;
|
|
109
|
-
const normalizedBasePath = cleanBasePath === "/" ? "" : cleanBasePath;
|
|
110
|
-
let newPath = `${normalizedBasePath}/${locale}${currentPath}`;
|
|
111
|
-
if (!prefixDefault && locale === defaultLocale) {
|
|
112
|
-
newPath = `${normalizedBasePath}${currentPath}`;
|
|
113
|
-
}
|
|
114
|
-
return newPath;
|
|
115
|
-
};
|
|
116
|
-
const handleNoPrefix = ({
|
|
117
|
-
req,
|
|
118
|
-
res,
|
|
119
|
-
next,
|
|
120
|
-
originalPath,
|
|
121
|
-
cookieLocale
|
|
122
|
-
}) => {
|
|
123
|
-
let locale = cookieLocale ?? defaultLocale;
|
|
124
|
-
if (!cookieLocale) {
|
|
125
|
-
const detectedLocale = (0, import_core.localeDetector)(
|
|
126
|
-
req.headers,
|
|
127
|
-
supportedLocales,
|
|
128
|
-
defaultLocale
|
|
129
|
-
);
|
|
130
|
-
locale = detectedLocale;
|
|
131
|
-
}
|
|
132
|
-
rewriteUrl(req, res, originalPath, locale);
|
|
133
|
-
return next();
|
|
134
|
-
};
|
|
135
|
-
const handlePrefix = ({
|
|
136
|
-
req,
|
|
137
|
-
res,
|
|
138
|
-
next,
|
|
139
|
-
originalPath,
|
|
140
|
-
pathLocale,
|
|
141
|
-
cookieLocale
|
|
142
|
-
}) => {
|
|
143
|
-
if (!pathLocale) {
|
|
144
|
-
handleMissingPathLocale({
|
|
145
|
-
req,
|
|
146
|
-
res,
|
|
147
|
-
next,
|
|
148
|
-
originalPath,
|
|
149
|
-
cookieLocale
|
|
150
|
-
});
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
handleExistingPathLocale({
|
|
154
|
-
req,
|
|
155
|
-
res,
|
|
156
|
-
next,
|
|
157
|
-
originalPath,
|
|
158
|
-
pathLocale,
|
|
159
|
-
cookieLocale
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
const handleMissingPathLocale = ({
|
|
163
|
-
req,
|
|
164
|
-
res,
|
|
165
|
-
next,
|
|
166
|
-
originalPath,
|
|
167
|
-
cookieLocale
|
|
168
|
-
}) => {
|
|
169
|
-
let locale = cookieLocale ?? (0, import_core.localeDetector)(
|
|
170
|
-
req.headers,
|
|
171
|
-
supportedLocales,
|
|
172
|
-
defaultLocale
|
|
173
|
-
);
|
|
174
|
-
if (!supportedLocales.includes(locale)) {
|
|
175
|
-
locale = defaultLocale;
|
|
176
|
-
}
|
|
177
|
-
const newPath = constructPath(locale, originalPath);
|
|
178
|
-
if (prefixDefault || locale !== defaultLocale) {
|
|
179
|
-
return redirectUrl(res, newPath);
|
|
180
|
-
}
|
|
181
|
-
rewriteUrl(req, res, newPath, locale);
|
|
182
|
-
return next();
|
|
183
|
-
};
|
|
184
|
-
const handleExistingPathLocale = ({
|
|
185
|
-
req,
|
|
186
|
-
res,
|
|
187
|
-
next,
|
|
188
|
-
originalPath,
|
|
189
|
-
pathLocale,
|
|
190
|
-
cookieLocale
|
|
191
|
-
}) => {
|
|
192
|
-
if (cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always") {
|
|
193
|
-
const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);
|
|
194
|
-
const finalPath = constructPath(cookieLocale, newPath.replace(/^\/+/, "/"));
|
|
195
|
-
return redirectUrl(res, finalPath);
|
|
196
|
-
}
|
|
197
|
-
handleDefaultLocaleRedirect({
|
|
198
|
-
req,
|
|
199
|
-
res,
|
|
200
|
-
next,
|
|
201
|
-
originalPath,
|
|
202
|
-
pathLocale
|
|
203
|
-
});
|
|
204
|
-
};
|
|
205
|
-
const handleDefaultLocaleRedirect = ({
|
|
206
|
-
req,
|
|
207
|
-
res,
|
|
208
|
-
next,
|
|
209
|
-
originalPath,
|
|
210
|
-
pathLocale
|
|
211
|
-
}) => {
|
|
212
|
-
if (!prefixDefault && pathLocale === defaultLocale) {
|
|
213
|
-
const newPath = originalPath.replace(`/${defaultLocale}`, "") ?? "/";
|
|
214
|
-
rewriteUrl(req, res, newPath, pathLocale);
|
|
215
|
-
return next();
|
|
216
|
-
}
|
|
217
|
-
rewriteUrl(req, res, originalPath, pathLocale);
|
|
218
|
-
return next();
|
|
219
|
-
};
|
|
220
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
221
|
-
0 && (module.exports = {
|
|
222
|
-
intLayerMiddlewarePlugin
|
|
223
|
-
});
|
|
224
|
-
//# sourceMappingURL=intlayerMiddlewarePlugin.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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,OA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,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;","names":[]}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var intlayerPlugin_exports = {};
|
|
20
|
-
__export(intlayerPlugin_exports, {
|
|
21
|
-
intlayerPlugin: () => intlayerPlugin
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(intlayerPlugin_exports);
|
|
24
|
-
var import_path = require("path");
|
|
25
|
-
var import_chokidar = require("@intlayer/chokidar");
|
|
26
|
-
var import_config = require("@intlayer/config");
|
|
27
|
-
const intlayerPlugin = (_pluginOptions = {}) => ({
|
|
28
|
-
name: "vite-intlayer-plugin",
|
|
29
|
-
config: (config) => {
|
|
30
|
-
const intlayerConfig = (0, import_config.getConfiguration)();
|
|
31
|
-
const {
|
|
32
|
-
mainDir,
|
|
33
|
-
configDir,
|
|
34
|
-
baseDir,
|
|
35
|
-
watch: isWatchMode
|
|
36
|
-
} = intlayerConfig.content;
|
|
37
|
-
const dictionariesPath = (0, import_path.join)(mainDir, "dictionaries.mjs");
|
|
38
|
-
const relativeDictionariesPath = (0, import_path.relative)(baseDir, dictionariesPath);
|
|
39
|
-
const configurationPath = (0, import_path.join)(configDir, "configuration.json");
|
|
40
|
-
const relativeConfigurationPath = (0, import_path.relative)(baseDir, configurationPath);
|
|
41
|
-
config.resolve = {
|
|
42
|
-
...config.resolve,
|
|
43
|
-
alias: {
|
|
44
|
-
...config.resolve?.alias,
|
|
45
|
-
"@intlayer/dictionaries-entry": (0, import_path.resolve)(relativeDictionariesPath),
|
|
46
|
-
"@intlayer/config/built": (0, import_path.resolve)(relativeConfigurationPath)
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
if (isWatchMode) {
|
|
50
|
-
config.optimizeDeps = {
|
|
51
|
-
...config.optimizeDeps,
|
|
52
|
-
exclude: [
|
|
53
|
-
...config.optimizeDeps?.exclude ?? [],
|
|
54
|
-
"@intlayer/dictionaries-entry",
|
|
55
|
-
"@intlayer/config/built"
|
|
56
|
-
]
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
return config;
|
|
60
|
-
},
|
|
61
|
-
configureServer: async () => {
|
|
62
|
-
const intlayerConfig = (0, import_config.getConfiguration)();
|
|
63
|
-
if (intlayerConfig.content.watch) {
|
|
64
|
-
(0, import_chokidar.watch)({ configuration: intlayerConfig });
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
buildStart: async () => {
|
|
68
|
-
const intlayerConfig = (0, import_config.getConfiguration)();
|
|
69
|
-
await (0, import_chokidar.prepareIntlayer)(intlayerConfig);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
-
0 && (module.exports = {
|
|
74
|
-
intlayerPlugin
|
|
75
|
-
});
|
|
76
|
-
//# sourceMappingURL=intlayerPlugin.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerPlugin.ts"],"sourcesContent":["import { join, relative, resolve } from 'path';\nimport { prepareIntlayer, watch } from '@intlayer/chokidar';\nimport { getConfiguration } from '@intlayer/config';\n// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { type PluginOption } from 'vite';\n\n// Plugin options type definition\ntype PluginOptions = {\n // Custom options for your plugin, if any\n};\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: [ intlayerPlugin() ],\n * });\n * ```\n * */\nexport const intlayerPlugin = (\n _pluginOptions: PluginOptions = {}\n): PluginOption => ({\n name: 'vite-intlayer-plugin',\n\n config: (config) => {\n const intlayerConfig = getConfiguration();\n const {\n mainDir,\n configDir,\n baseDir,\n watch: isWatchMode,\n } = intlayerConfig.content;\n\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n const configurationPath = join(configDir, 'configuration.json');\n const relativeConfigurationPath = relative(baseDir, configurationPath);\n\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n '@intlayer/config/built': resolve(relativeConfigurationPath),\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 () => {\n // Runs when the dev server starts\n const intlayerConfig = getConfiguration();\n\n if (intlayerConfig.content.watch) {\n // Start watching (assuming watch is also async)\n watch({ configuration: intlayerConfig });\n }\n },\n\n buildStart: async () => {\n // Code to run when Vite build starts\n const intlayerConfig = getConfiguration();\n await prepareIntlayer(intlayerConfig);\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwC;AACxC,sBAAuC;AACvC,oBAAiC;AAoB1B,MAAM,iBAAiB,CAC5B,iBAAgC,CAAC,OACf;AAAA,EAClB,MAAM;AAAA,EAEN,QAAQ,CAAC,WAAW;AAClB,UAAM,qBAAiB,gCAAiB;AACxC,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA;AAAA,MACA,OAAO;AAAA,IACT,IAAI,eAAe;AAEnB,UAAM,uBAAmB,kBAAK,SAAS,kBAAkB;AACzD,UAAM,+BAA2B,sBAAS,SAAS,gBAAgB;AAEnE,UAAM,wBAAoB,kBAAK,WAAW,oBAAoB;AAC9D,UAAM,gCAA4B,sBAAS,SAAS,iBAAiB;AAGrE,WAAO,UAAU;AAAA,MACf,GAAG,OAAO;AAAA,MACV,OAAO;AAAA,QACL,GAAG,OAAO,SAAS;AAAA,QACnB,oCAAgC,qBAAQ,wBAAwB;AAAA,QAChE,8BAA0B,qBAAQ,yBAAyB;AAAA,MAC7D;AAAA,IACF;AAEA,QAAI,aAAa;AAEf,aAAO,eAAe;AAAA,QACpB,GAAG,OAAO;AAAA,QACV,SAAS;AAAA,UACP,GAAI,OAAO,cAAc,WAAW,CAAC;AAAA,UACrC;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,iBAAiB,YAAY;AAE3B,UAAM,qBAAiB,gCAAiB;AAExC,QAAI,eAAe,QAAQ,OAAO;AAEhC,iCAAM,EAAE,eAAe,eAAe,CAAC;AAAA,IACzC;AAAA,EACF;AAAA,EAEA,YAAY,YAAY;AAEtB,UAAM,qBAAiB,gCAAiB;AACxC,cAAM,iCAAgB,cAAc;AAAA,EACtC;AACF;","names":[]}
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { parse } from "url";
|
|
2
|
-
import { getConfiguration } from "@intlayer/config";
|
|
3
|
-
import { localeDetector } from "@intlayer/core";
|
|
4
|
-
const intlayerConfig = getConfiguration();
|
|
5
|
-
const { internationalization, middleware } = intlayerConfig;
|
|
6
|
-
const { locales: supportedLocales, defaultLocale } = internationalization;
|
|
7
|
-
const {
|
|
8
|
-
cookieName,
|
|
9
|
-
headerName,
|
|
10
|
-
prefixDefault,
|
|
11
|
-
noPrefix,
|
|
12
|
-
serverSetCookie,
|
|
13
|
-
basePath = ""
|
|
14
|
-
} = middleware;
|
|
15
|
-
const intLayerMiddlewarePlugin = () => {
|
|
16
|
-
return {
|
|
17
|
-
name: "vite-intlayer-middleware-plugin",
|
|
18
|
-
configureServer: (server) => {
|
|
19
|
-
server.middlewares.use((req, res, next) => {
|
|
20
|
-
if (req.url?.startsWith("/node_modules") || req.url?.startsWith("/@") || req.url?.split("?")[0].match(/\.[a-z]+$/i)) {
|
|
21
|
-
return next();
|
|
22
|
-
}
|
|
23
|
-
const parsedUrl = parse(req.url ?? "/", true);
|
|
24
|
-
const originalPath = parsedUrl.pathname ?? "/";
|
|
25
|
-
const cookies = parseCookies(req.headers.cookie ?? "");
|
|
26
|
-
const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);
|
|
27
|
-
const pathLocale = getPathLocale(originalPath);
|
|
28
|
-
if (noPrefix) {
|
|
29
|
-
handleNoPrefix({
|
|
30
|
-
req,
|
|
31
|
-
res,
|
|
32
|
-
next,
|
|
33
|
-
originalPath,
|
|
34
|
-
cookieLocale
|
|
35
|
-
});
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
handlePrefix({
|
|
39
|
-
req,
|
|
40
|
-
res,
|
|
41
|
-
next,
|
|
42
|
-
originalPath,
|
|
43
|
-
pathLocale,
|
|
44
|
-
cookieLocale
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
const parseCookies = (cookieHeader) => {
|
|
51
|
-
return cookieHeader.split(";").reduce(
|
|
52
|
-
(acc, cookie) => {
|
|
53
|
-
const [key, val] = cookie.trim().split("=");
|
|
54
|
-
acc[key] = val;
|
|
55
|
-
return acc;
|
|
56
|
-
},
|
|
57
|
-
{}
|
|
58
|
-
);
|
|
59
|
-
};
|
|
60
|
-
const getValidLocaleFromCookie = (locale) => {
|
|
61
|
-
if (locale && supportedLocales.includes(locale)) {
|
|
62
|
-
return locale;
|
|
63
|
-
}
|
|
64
|
-
return void 0;
|
|
65
|
-
};
|
|
66
|
-
const getPathLocale = (pathname) => {
|
|
67
|
-
const segments = pathname.split("/").filter(Boolean);
|
|
68
|
-
const firstSegment = segments[0];
|
|
69
|
-
if (firstSegment && supportedLocales.includes(firstSegment)) {
|
|
70
|
-
return firstSegment;
|
|
71
|
-
}
|
|
72
|
-
return void 0;
|
|
73
|
-
};
|
|
74
|
-
const redirectUrl = (res, newUrl) => {
|
|
75
|
-
res.writeHead(301, { Location: newUrl });
|
|
76
|
-
return res.end();
|
|
77
|
-
};
|
|
78
|
-
const rewriteUrl = (req, res, newUrl, locale) => {
|
|
79
|
-
req.url = newUrl;
|
|
80
|
-
if (locale && headerName) {
|
|
81
|
-
res.setHeader(headerName, locale);
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
const constructPath = (locale, currentPath) => {
|
|
85
|
-
const cleanBasePath = basePath.startsWith("/") ? basePath : `/${basePath}`;
|
|
86
|
-
const normalizedBasePath = cleanBasePath === "/" ? "" : cleanBasePath;
|
|
87
|
-
let newPath = `${normalizedBasePath}/${locale}${currentPath}`;
|
|
88
|
-
if (!prefixDefault && locale === defaultLocale) {
|
|
89
|
-
newPath = `${normalizedBasePath}${currentPath}`;
|
|
90
|
-
}
|
|
91
|
-
return newPath;
|
|
92
|
-
};
|
|
93
|
-
const handleNoPrefix = ({
|
|
94
|
-
req,
|
|
95
|
-
res,
|
|
96
|
-
next,
|
|
97
|
-
originalPath,
|
|
98
|
-
cookieLocale
|
|
99
|
-
}) => {
|
|
100
|
-
let locale = cookieLocale ?? defaultLocale;
|
|
101
|
-
if (!cookieLocale) {
|
|
102
|
-
const detectedLocale = localeDetector(
|
|
103
|
-
req.headers,
|
|
104
|
-
supportedLocales,
|
|
105
|
-
defaultLocale
|
|
106
|
-
);
|
|
107
|
-
locale = detectedLocale;
|
|
108
|
-
}
|
|
109
|
-
rewriteUrl(req, res, originalPath, locale);
|
|
110
|
-
return next();
|
|
111
|
-
};
|
|
112
|
-
const handlePrefix = ({
|
|
113
|
-
req,
|
|
114
|
-
res,
|
|
115
|
-
next,
|
|
116
|
-
originalPath,
|
|
117
|
-
pathLocale,
|
|
118
|
-
cookieLocale
|
|
119
|
-
}) => {
|
|
120
|
-
if (!pathLocale) {
|
|
121
|
-
handleMissingPathLocale({
|
|
122
|
-
req,
|
|
123
|
-
res,
|
|
124
|
-
next,
|
|
125
|
-
originalPath,
|
|
126
|
-
cookieLocale
|
|
127
|
-
});
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
handleExistingPathLocale({
|
|
131
|
-
req,
|
|
132
|
-
res,
|
|
133
|
-
next,
|
|
134
|
-
originalPath,
|
|
135
|
-
pathLocale,
|
|
136
|
-
cookieLocale
|
|
137
|
-
});
|
|
138
|
-
};
|
|
139
|
-
const handleMissingPathLocale = ({
|
|
140
|
-
req,
|
|
141
|
-
res,
|
|
142
|
-
next,
|
|
143
|
-
originalPath,
|
|
144
|
-
cookieLocale
|
|
145
|
-
}) => {
|
|
146
|
-
let locale = cookieLocale ?? localeDetector(
|
|
147
|
-
req.headers,
|
|
148
|
-
supportedLocales,
|
|
149
|
-
defaultLocale
|
|
150
|
-
);
|
|
151
|
-
if (!supportedLocales.includes(locale)) {
|
|
152
|
-
locale = defaultLocale;
|
|
153
|
-
}
|
|
154
|
-
const newPath = constructPath(locale, originalPath);
|
|
155
|
-
if (prefixDefault || locale !== defaultLocale) {
|
|
156
|
-
return redirectUrl(res, newPath);
|
|
157
|
-
}
|
|
158
|
-
rewriteUrl(req, res, newPath, locale);
|
|
159
|
-
return next();
|
|
160
|
-
};
|
|
161
|
-
const handleExistingPathLocale = ({
|
|
162
|
-
req,
|
|
163
|
-
res,
|
|
164
|
-
next,
|
|
165
|
-
originalPath,
|
|
166
|
-
pathLocale,
|
|
167
|
-
cookieLocale
|
|
168
|
-
}) => {
|
|
169
|
-
if (cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always") {
|
|
170
|
-
const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);
|
|
171
|
-
const finalPath = constructPath(cookieLocale, newPath.replace(/^\/+/, "/"));
|
|
172
|
-
return redirectUrl(res, finalPath);
|
|
173
|
-
}
|
|
174
|
-
handleDefaultLocaleRedirect({
|
|
175
|
-
req,
|
|
176
|
-
res,
|
|
177
|
-
next,
|
|
178
|
-
originalPath,
|
|
179
|
-
pathLocale
|
|
180
|
-
});
|
|
181
|
-
};
|
|
182
|
-
const handleDefaultLocaleRedirect = ({
|
|
183
|
-
req,
|
|
184
|
-
res,
|
|
185
|
-
next,
|
|
186
|
-
originalPath,
|
|
187
|
-
pathLocale
|
|
188
|
-
}) => {
|
|
189
|
-
if (!prefixDefault && pathLocale === defaultLocale) {
|
|
190
|
-
const newPath = originalPath.replace(`/${defaultLocale}`, "") ?? "/";
|
|
191
|
-
rewriteUrl(req, res, newPath, pathLocale);
|
|
192
|
-
return next();
|
|
193
|
-
}
|
|
194
|
-
rewriteUrl(req, res, originalPath, pathLocale);
|
|
195
|
-
return next();
|
|
196
|
-
};
|
|
197
|
-
export {
|
|
198
|
-
intLayerMiddlewarePlugin
|
|
199
|
-
};
|
|
200
|
-
//# sourceMappingURL=intlayerMiddlewarePlugin.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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":"AACA,SAAS,aAAa;AACtB,SAAS,wBAAsC;AAC/C,SAAS,sBAAsB;AAe/B,MAAM,iBAAiB,iBAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,IAAI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eAAW,KAAK,KAAK,SAAS,UAAU;AACxC,WAAO,KAAK;AAAA,EACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;","names":[]}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { join, relative, resolve } from "path";
|
|
2
|
-
import { prepareIntlayer, watch } from "@intlayer/chokidar";
|
|
3
|
-
import { getConfiguration } from "@intlayer/config";
|
|
4
|
-
const intlayerPlugin = (_pluginOptions = {}) => ({
|
|
5
|
-
name: "vite-intlayer-plugin",
|
|
6
|
-
config: (config) => {
|
|
7
|
-
const intlayerConfig = getConfiguration();
|
|
8
|
-
const {
|
|
9
|
-
mainDir,
|
|
10
|
-
configDir,
|
|
11
|
-
baseDir,
|
|
12
|
-
watch: isWatchMode
|
|
13
|
-
} = intlayerConfig.content;
|
|
14
|
-
const dictionariesPath = join(mainDir, "dictionaries.mjs");
|
|
15
|
-
const relativeDictionariesPath = relative(baseDir, dictionariesPath);
|
|
16
|
-
const configurationPath = join(configDir, "configuration.json");
|
|
17
|
-
const relativeConfigurationPath = relative(baseDir, configurationPath);
|
|
18
|
-
config.resolve = {
|
|
19
|
-
...config.resolve,
|
|
20
|
-
alias: {
|
|
21
|
-
...config.resolve?.alias,
|
|
22
|
-
"@intlayer/dictionaries-entry": resolve(relativeDictionariesPath),
|
|
23
|
-
"@intlayer/config/built": resolve(relativeConfigurationPath)
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
if (isWatchMode) {
|
|
27
|
-
config.optimizeDeps = {
|
|
28
|
-
...config.optimizeDeps,
|
|
29
|
-
exclude: [
|
|
30
|
-
...config.optimizeDeps?.exclude ?? [],
|
|
31
|
-
"@intlayer/dictionaries-entry",
|
|
32
|
-
"@intlayer/config/built"
|
|
33
|
-
]
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
return config;
|
|
37
|
-
},
|
|
38
|
-
configureServer: async () => {
|
|
39
|
-
const intlayerConfig = getConfiguration();
|
|
40
|
-
if (intlayerConfig.content.watch) {
|
|
41
|
-
watch({ configuration: intlayerConfig });
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
buildStart: async () => {
|
|
45
|
-
const intlayerConfig = getConfiguration();
|
|
46
|
-
await prepareIntlayer(intlayerConfig);
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
export {
|
|
50
|
-
intlayerPlugin
|
|
51
|
-
};
|
|
52
|
-
//# sourceMappingURL=intlayerPlugin.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerPlugin.ts"],"sourcesContent":["import { join, relative, resolve } from 'path';\nimport { prepareIntlayer, watch } from '@intlayer/chokidar';\nimport { getConfiguration } from '@intlayer/config';\n// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { type PluginOption } from 'vite';\n\n// Plugin options type definition\ntype PluginOptions = {\n // Custom options for your plugin, if any\n};\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: [ intlayerPlugin() ],\n * });\n * ```\n * */\nexport const intlayerPlugin = (\n _pluginOptions: PluginOptions = {}\n): PluginOption => ({\n name: 'vite-intlayer-plugin',\n\n config: (config) => {\n const intlayerConfig = getConfiguration();\n const {\n mainDir,\n configDir,\n baseDir,\n watch: isWatchMode,\n } = intlayerConfig.content;\n\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n const configurationPath = join(configDir, 'configuration.json');\n const relativeConfigurationPath = relative(baseDir, configurationPath);\n\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n '@intlayer/config/built': resolve(relativeConfigurationPath),\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 () => {\n // Runs when the dev server starts\n const intlayerConfig = getConfiguration();\n\n if (intlayerConfig.content.watch) {\n // Start watching (assuming watch is also async)\n watch({ configuration: intlayerConfig });\n }\n },\n\n buildStart: async () => {\n // Code to run when Vite build starts\n const intlayerConfig = getConfiguration();\n await prepareIntlayer(intlayerConfig);\n },\n});\n"],"mappings":"AAAA,SAAS,MAAM,UAAU,eAAe;AACxC,SAAS,iBAAiB,aAAa;AACvC,SAAS,wBAAwB;AAoB1B,MAAM,iBAAiB,CAC5B,iBAAgC,CAAC,OACf;AAAA,EAClB,MAAM;AAAA,EAEN,QAAQ,CAAC,WAAW;AAClB,UAAM,iBAAiB,iBAAiB;AACxC,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA;AAAA,MACA,OAAO;AAAA,IACT,IAAI,eAAe;AAEnB,UAAM,mBAAmB,KAAK,SAAS,kBAAkB;AACzD,UAAM,2BAA2B,SAAS,SAAS,gBAAgB;AAEnE,UAAM,oBAAoB,KAAK,WAAW,oBAAoB;AAC9D,UAAM,4BAA4B,SAAS,SAAS,iBAAiB;AAGrE,WAAO,UAAU;AAAA,MACf,GAAG,OAAO;AAAA,MACV,OAAO;AAAA,QACL,GAAG,OAAO,SAAS;AAAA,QACnB,gCAAgC,QAAQ,wBAAwB;AAAA,QAChE,0BAA0B,QAAQ,yBAAyB;AAAA,MAC7D;AAAA,IACF;AAEA,QAAI,aAAa;AAEf,aAAO,eAAe;AAAA,QACpB,GAAG,OAAO;AAAA,QACV,SAAS;AAAA,UACP,GAAI,OAAO,cAAc,WAAW,CAAC;AAAA,UACrC;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,iBAAiB,YAAY;AAE3B,UAAM,iBAAiB,iBAAiB;AAExC,QAAI,eAAe,QAAQ,OAAO;AAEhC,YAAM,EAAE,eAAe,eAAe,CAAC;AAAA,IACzC;AAAA,EACF;AAAA,EAEA,YAAY,YAAY;AAEtB,UAAM,iBAAiB,iBAAiB;AACxC,UAAM,gBAAgB,cAAc;AAAA,EACtC;AACF;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { type PluginOption } from 'vite';
|
|
2
|
-
type PluginOptions = {};
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* A Vite plugin that integrates Intlayer configuration into the build process
|
|
6
|
-
*
|
|
7
|
-
* ```ts
|
|
8
|
-
* // Example usage of the plugin in a Vite configuration
|
|
9
|
-
* export default defineConfig({
|
|
10
|
-
* plugins: [ intlayerPlugin() ],
|
|
11
|
-
* });
|
|
12
|
-
* ```
|
|
13
|
-
* */
|
|
14
|
-
export declare const intlayerPlugin: (_pluginOptions?: PluginOptions) => PluginOption;
|
|
15
|
-
export {};
|
|
16
|
-
//# sourceMappingURL=intlayerPlugin.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"intlayerPlugin.d.ts","sourceRoot":"","sources":["../../src/intlayerPlugin.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,MAAM,CAAC;AAGzC,KAAK,aAAa,GAAG,EAEpB,CAAC;AAEF;;;;;;;;;;MAUM;AACN,eAAO,MAAM,cAAc,GACzB,iBAAgB,aAAkB,KACjC,YA0DD,CAAC"}
|